import { PageContent } from "../pageModel" ; import { inorderTreeTraversal , NodePosition } from "../tree/tree" ; import { getContentBetweenOffsets } from "../contentTree/tree" ; import { StructureNode } from "../structureTree/structureModel" ; import MarkdownIt from "markdown-it" ; import { CompilerElement , getElements , colorRenderer , textDecorationRenderer , backgroundColorRenderer , supRenderer , subRenderer , strong_open , strong_close , em_open , em_close , paragraph_open , paragraph_close , heading_open , heading_close , text , unfinishedEnd , } from "./renderers" ; import { autoCloseRule , nonAutoCloseRule } from "./parser" ; /**
 * markdown-it extension for the custom syntax.
 */ function customSyntaxPlugin ( md ) { md . renderer . rules . unfinishedEnd = unfinishedEnd ; md . renderer . rules . color = colorRenderer ; md . renderer . rules . textDecoration = textDecorationRenderer ; md . renderer . rules . backgroundColor = backgroundColorRenderer ; md . renderer . rules . sup = supRenderer ; md . renderer . rules . sub = subRenderer ; md . renderer . rules . strong_open = strong_open ; md . renderer . rules . strong_close = strong_close ; md . renderer . rules . em_open = em_open ; md . renderer . rules . em_close = em_close ; md . renderer . rules . paragraph_open = paragraph_open ; md . renderer . rules . paragraph_close = paragraph_close ; md . renderer . rules . heading_open = heading_open ; md . renderer . rules . heading_close = heading_close ; md . renderer . rules . text = text ; md . core . ruler . push ( "customSyntaxRuleAutoClose" , autoCloseRule ) ; md . core . ruler . push ( "customSyntaxRuleNonAutoClose" , nonAutoCloseRule ) ; } const _markdownCompiler = new MarkdownIt ( "commonmark" ) . use ( customSyntaxPlugin ) ; param closeOpenTags export function getCompiler ( closeOpenTags = false ) { if ( closeOpenTags === true ) { _markdownCompiler . enable ( "customSyntaxRuleAutoClose" ) ; _markdownCompiler . disable ( "customSyntaxRuleNonAutoClose" ) ; } else if ( closeOpenTags === false ) { _markdownCompiler . disable ( "customSyntaxRuleAutoClose" ) ; _markdownCompiler . enable ( "customSyntaxRuleNonAutoClose" ) ; } return _markdownCompiler ; } param content param closeOpenTags export function compile ( content , closeOpenTags = false ) { return getCompiler ( closeOpenTags ) . render ( content ) . trim ( ) ; } param page export function * getHtmlContentFromPage ( page ) { let startOffset = 0 ; for ( const { node } of inorderTreeTraversal ( page . structure ) ) { yield { content : compile ( getContentBetweenOffsets ( page , startOffset , startOffset + node . length ) . content ) , node , } ; startOffset += node . length ; } } param page export function * getHtmlContentElementsFromPage ( page ) { let startOffset = 0 ; for ( const { node , index } of inorderTreeTraversal ( page . structure ) ) { const content = getContentBetweenOffsets ( page , startOffset , startOffset + node . length ) . content ; const children = getElements ( content ) ; yield { children , index , node } ; startOffset += node . length ; } }