UNPKG

shelving

Version:

Toolkit for using data in JavaScript.

31 lines (30 loc) 1.1 kB
import { mergeElements } from "../util/element.js"; /** * Base class for an extractor that converts input into a tree element. * - Extractors are composable: outer extractors delegate to inner extractors. * - The output type is always a `TreeElement` (or a more specific subtype). */ export class Extractor { } export function mergeTreeElements(primary, secondary) { return { ...primary, type: primary.type, key: primary.key, props: { ...primary.props, title: primary.props.title ?? secondary.props.title, description: primary.props.description ?? secondary.props.description, content: _mergeContent(primary.props.content, secondary.props.content), children: mergeElements(primary.props.children, secondary.props.children), }, }; } /** Merge two markup content strings — primary first, secondary appended after a blank line. Returns `undefined` if both are empty. */ function _mergeContent(a, b) { if (!a) return b; if (!b) return a; return `${a}\n\n${b}`; }