import { BlockAttributes } from "block-attributes" ; import { load } from "cheerio" ; import { getCompiledHandlebarsTemplate } from "./getCompiledHandlebarsTemplate" ; import { LabelType } from "./types" ; const fakeChildrenContent = "__FAKE_CHILDREN__" ; export const renderHtmlTemplate = ( htmlTemplate , labelName , labelType , labelAttributes ) => { const rawRenderedTemplate = getCompiledHandlebarsTemplate ( htmlTemplate ) ( { ... labelAttributes , children : fakeChildrenContent , } ) ; const $ = load ( rawRenderedTemplate ) ; $ ( "*" ) . attr ( "ns-role" , "label-child" ) ; $ ( ":root" ) . attr ( "ns-role" , "label" ) . attr ( "ns-label-name" , labelName ) . attr ( "ns-label-kind" , labelType === "single" ? "single" : "paired" ) ; const rawHtml = $ . html ( ) ; const positionOfFakeChildren = rawHtml . indexOf ( fakeChildrenContent ) ; let html ; if ( labelType === "single" ) { if ( positionOfFakeChildren !== - 1 ) { throw new Error ( "Single label cannot have children" ) ; } html = rawHtml ; } else { if ( positionOfFakeChildren === - 1 ) { throw new Error ( "Paired label must have children" ) ; } if ( labelType === "paired_opening" ) { html = rawHtml . substr ( 0 , positionOfFakeChildren ) ; } else { html = rawHtml . substr ( positionOfFakeChildren + fakeChildrenContent . length ) ; } } return html ; } ;