import Token from "markdown-it/lib/token" ; import { TagType } from "../structureTree/structureModel" ; import { Attributes } from "./parser" ; import { getCompiler } from "./compiler" ; import { Style } from "../../editor/render" ; import paramCase from "param-case" ; /**
 * Type guard for `TagItem`.
 */ export function isTagItem ( val ) { if ( ( val as TagItem ) . tag !== undefined && ( val as TagItem ) . tagType !== undefined ) { return true ; } return false ; } /**
 * Definition of tags inside the tag items inside the `elements` array.
 */ export interface TagItem { tag : keyof HTMLElementTagNameMap ; tagType : TagType ; style ? : Style ; } /**
 * Definition of items elements inside the `elements` array.
 */ export type CompilerElement = TagItem | string ; /**
 * When `getJSX` is set to true, then `elements` is populated with items for
 * use in `HtmlEditorComponent`.
 */ let elements = [ ] ; /**
 * Set to true when `getElements` is called, so that `elements` is populated
 * with items for use in `HtmlEditorComponent`.
 */ let getJSX = false ; param tokens param index param type function renderer ( tokens , index , type ) { const token = tokens [ index ] ; if ( getJSX ) { const tag = tokens [ index ] . tag as keyof HTMLElementTagNameMap ; if ( token . nesting === 1 ) { elements . push ( { style : { [ type ] : token . attrGet ( type ) || "" , } , tag , tagType : TagType . StartTag , } ) ; return "" ; } else if ( token . nesting === - 1 ) { elements . push ( { tag , tagType : TagType . EndTag , } ) ; return "" ; } else { elements . push ( { style : { [ type ] : token . attrGet ( type ) || "" , } , tag , tagType : TagType . EndTag , } ) ; return "" ; } } else { if ( token . nesting === 1 ) { return `<span style="${ paramCase ( type ) }:${ token . attrGet ( type ) }">` ; } else if ( token . nesting === - 1 ) { return "</span>" ; } else { return "" ; } } } export function colorRenderer ( tokens , index ) { return renderer ( tokens , index , Attributes . color ) ; } export function textDecorationRenderer ( tokens , index ) { return renderer ( tokens , index , Attributes . textDecoration ) ; } export function backgroundColorRenderer ( tokens , index ) { return renderer ( tokens , index , Attributes . backgroundColor ) ; } function inlineTagsRenderer ( tokens , index ) { const token = tokens [ index ] ; const tagType = token . nesting === 1 ? TagType . StartTag : TagType . EndTag ; if ( getJSX ) { elements . push ( { tag : token . tag as keyof HTMLElementTagNameMap , tagType , } ) ; return "" ; } else { if ( tagType === TagType . StartTag ) { return `<${ token . tag }>` ; } else { return `</${ token . tag }>` ; } } } export function unfinishedEnd ( tokens , index ) { return inlineTagsRenderer ( tokens , index ) ; } export function supRenderer ( tokens , index ) { return inlineTagsRenderer ( tokens , index ) ; } export function subRenderer ( tokens , index ) { return inlineTagsRenderer ( tokens , index ) ; } param attribute param value function builtInRendererOverrides ( attribute , value ) { if ( getJSX ) { elements . push ( { style : { [ attribute ] : value , } , tag : "span" , tagType : TagType . StartTag , } ) ; return "" ; } else { return `<span style="${ paramCase ( attribute ) }:${ value }">` ; } } export function strong_open ( ) { return builtInRendererOverrides ( "fontWeight" , "bold" ) ; } export function strong_close ( ) { if ( getJSX ) { elements . push ( { tag : "span" , tagType : TagType . EndTag , } ) ; return "" ; } return "</span>" ; } export function em_open ( ) { return builtInRendererOverrides ( "fontStyle" , "italic" ) ; } export function paragraph_open ( ) { return "" ; } export const paragraph_close = paragraph_open ; export const heading_open = paragraph_open ; export const heading_close = paragraph_close ; export const em_close = strong_close ; export function text ( tokens , index ) { const content = tokens [ index ] . content ; if ( getJSX ) { if ( content ) { elements . push ( content ) ; } return "" ; } else { return getCompiler ( ) . utils . escapeHtml ( content ) ; } } /**
 * Gets all the elements from the OneNote page given content.
 */ export function getElements ( content ) { getJSX = true ; getCompiler ( ) . render ( content ) ; const outElements = elements ; elements = [ ] ; getJSX = false ; return outElements ; }