diff options
Diffstat (limited to 'services/nuldoc/nuldoc-src/dom.ts')
| -rw-r--r-- | services/nuldoc/nuldoc-src/dom.ts | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/services/nuldoc/nuldoc-src/dom.ts b/services/nuldoc/nuldoc-src/dom.ts index abe7ff8..be503a3 100644 --- a/services/nuldoc/nuldoc-src/dom.ts +++ b/services/nuldoc/nuldoc-src/dom.ts @@ -17,6 +17,25 @@ export type Element = { export type Node = Element | Text | RawHTML; +export type NodeLike = Node | string | null | undefined | false | NodeLike[]; + +function flattenChildren(children: NodeLike[]): Node[] { + const result: Node[] = []; + for (const child of children) { + if (child === null || child === undefined || child === false) { + continue; + } + if (typeof child === "string") { + result.push(text(child)); + } else if (Array.isArray(child)) { + result.push(...flattenChildren(child)); + } else { + result.push(child); + } + } + return result; +} + export function text(content: string): Text { return { kind: "text", @@ -34,13 +53,13 @@ export function rawHTML(html: string): RawHTML { export function elem( name: string, attributes?: Record<string, string>, - ...children: Node[] + ...children: NodeLike[] ): Element { return { kind: "element", name, attributes: attributes || {}, - children, + children: flattenChildren(children), }; } |
