import MathMl2LaTeX from 'mathml2latex'; import { Token } from '../types.js'; import type { ContextStash, TokenHandler, } from '../MarkdownSerializer.js'; import { getInlineTokensHandlers } from './inline_token_handlers.js'; export function getHtmlBasicTokensHandlers(): Record< string, Array > { return { 'frontmatter': [], 'heading_open': [ (token: Token, ctx: ContextStash) => { ctx.current.log(`<${token.tag}>`, token); }, ], 'heading_close': [ (token: Token, ctx: ContextStash) => { ctx.current.log(`\n`, token); }, ], 'paragraph_open': [ (token: Token, ctx: ContextStash) => { ctx.current.log('

', token); }, ], 'paragraph_close': [ (token: Token, ctx: ContextStash) => { ctx.current.log('

\n', token); }, ], 'fence': [ (token: Token, ctx: ContextStash) => { ctx.current.log('
' + token.content + '
\n', token); // ctx.current.log('```' + token.info + '\n' + token.content + '```\n\n', token); }, ], 'code_block': [ (token: Token, ctx: ContextStash) => { ctx.current.log('
' + token.content + '
\n', token); // ctx.current.log( // token.content // .split('\n') // .map((t) => t ? (' ' + t) : '') // .join('\n') + '\n', token // ); }, ], 'hr': [ (token: Token, ctx: ContextStash) => { ctx.current.log('
\n', token); }, ], 'html_block': [ (token: Token, ctx: ContextStash) => { ctx.current.log(token.content, token); }, ], }; } function getHeaderTokensHandlers(): Record> { const entriesBasic = Object.entries(getBasicTokensHandlers()) .filter((entry) => ['heading_close'].includes(entry[0])); const entriesInline = Object.entries(getInlineTokensHandlers()) .filter((entry) => ['text', 'link_open', 'link_close'].includes(entry[0])); return { ...Object.fromEntries(entriesInline), ...Object.fromEntries(entriesBasic), 'default': [], }; } export function getBasicTokensHandlers(): Record> { return { 'frontmatter': [ (token: Token, ctx: ContextStash) => { ctx.current.log( token.content, token, ); }, ], 'heading_open': [ (token: Token, ctx: ContextStash) => { ctx.stash('getBasicTokensHandlers.heading_open'); ctx.current.handlers = getHeaderTokensHandlers(); if (token.markup === '---') { return; } ctx.current.log('#'.repeat(+token.tag.substring(1)) + ' ', token); }, ], 'heading_close': [ (token: Token, ctx: ContextStash) => { if (token.markup === '---') { ctx.current.log('\n' + token.markup + '\n', token); } ctx.current.log('\n', token); ctx.unstash('getBasicTokensHandlers.heading_close'); }, ], 'paragraph_open': [ (token: Token, ctx: ContextStash) => { ctx.current.log('', token); // Skip, as paragraphs are implied by newlines }, ], 'paragraph_close': [ (token: Token, ctx: ContextStash) => { if (ctx.output.colPos !== 0) { ctx.current.log('\n', token); } }, ], 'fence': [ (token: Token, ctx: ContextStash) => { let content = token.content; let lang = token.attrGet('lang') || ''; if (lang === 'mathml') { lang = 'math'; const cleaned = content.replace( //g, '', ); content = MathMl2LaTeX.convert(cleaned); } if (!content.endsWith('\n')) { content += '\n'; } if (lang === 'latex') { ctx.current.log( '$$' + '\n' + content + '$$\n\n', token, ); } else { ctx.current.log( '```' + lang + '\n' + content + '```\n\n', token, ); } }, ], 'code_block': [ (token: Token, ctx: ContextStash) => { const indent = +(token.attrGet('indent') || 0); let content = token.content; let lang = token.attrGet('lang') || ''; if (indent === 0) { if (lang === 'mathml') { lang = 'math'; const cleaned = content.replace( //g, '', ); content = MathMl2LaTeX.convert(cleaned); } if (!content.endsWith('\n')) { content += '\n'; } if (lang === 'rawmd') { ctx.current.log(content, token); } else if (lang === 'latex') { ctx.current.log( '$$' + '\n' + content + '$$\n\n', token, ); } else { ctx.current.log( '```' + lang + '\n' + content + '```\n', token, ); } return; } ctx.current.log( token.content .split('\n') .map((t) => t ? (' '.repeat(indent) + t) : '') .join('\n'), token, ); }, ], 'hr': [ (token: Token, ctx: ContextStash) => { ctx.current.log((token.markup || '___') + '\n', token); }, ], 'html_block': [ (token: Token, ctx: ContextStash) => { ctx.current.log(token.content, token); }, ], }; }