mirror of
https://github.com/Redocly/redoc.git
synced 2025-02-12 16:00:33 +03:00
26 lines
748 B
TypeScript
26 lines
748 B
TypeScript
|
export const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
|
||
|
|
||
|
export function querySelector(selector: string): Element | null {
|
||
|
if (typeof document !== 'undefined') {
|
||
|
return document.querySelector(selector);
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Drop everything inside <...> (i.e., tags/elements), and keep the text.
|
||
|
* Unlike browser innerText, this removes newlines; it also doesn't handle
|
||
|
* un-encoded `<` or `>` characters very well, so don't feed it malformed HTML
|
||
|
*/
|
||
|
export function html2Str(html: string): string {
|
||
|
return html
|
||
|
.split(/<[^>]+>/)
|
||
|
.map(function(chunk) {
|
||
|
return chunk.trim();
|
||
|
})
|
||
|
.filter(function(trimmedChunk) {
|
||
|
return trimmedChunk.length > 0;
|
||
|
})
|
||
|
.join(' ');
|
||
|
}
|