import { renderNest } from '../../render'; import { ContextTag, NoMeta, UnknownToken } from '../../model'; import { BlockTag, BlockCode, BlockMath, BlockHeading, BlockHr, BlockQuote, BlockList, BlockOrdList, BlockParagraph, BlockText, BlockTable, BlockListItem, BlockTableCell, BlockAlign, BlockTableRow, BlockFootnotes, MetaHeadings, MetaFootnotes } from '../../block/model'; import { BlockRenderRuleStr, BlockRenderHandleStr, escapeAttr, escapeCode, textAlign, simpleId } from '../str'; export const CodeBlockHtml: BlockRenderRuleStr = [ ContextTag.Block, BlockTag.Code, ({ }, { _ }) => '
' + escapeCode(_) + '
\n' ]; export const CodeBlockWithClassHtml: BlockRenderRuleStr = [ ContextTag.Block, BlockTag.Code, ({ }, { _, l }) => '
' + escapeCode(_) + '
\n' ]; export const MathBlockHtml: BlockRenderRuleStr = [ ContextTag.Block, BlockTag.Math, ({ }, { _ }) => '' + escapeCode(_) + '\n' ]; export const MathBlockWithClassHtml: BlockRenderRuleStr = [ ContextTag.Block, BlockTag.Math, ({ }, { _, s }) => '' + escapeCode(_) + '\n' ]; export const FootnotesBlockHtml: BlockRenderRuleStr, MetaFootnotes> = [ ContextTag.Block, BlockTag.Footnotes, ($, { _ }) => '
    \n' + _.map(({ l, _ }) => { const id = simpleId(l); return '
  1. \n' + renderNest($, _) + '↩\n
  2. \n'; }).join('') + '
\n' ]; export const HeadingHtml: BlockRenderRuleStr, MetaHeadings> = [ ContextTag.Block, BlockTag.Heading, ($, { n, i, _ }) => `` + renderNest($, _, ContextTag.Inline) + `\n` ]; export const HeadingWithIdHtml: BlockRenderRuleStr, MetaHeadings> = [ ContextTag.Block, BlockTag.Heading, ($, { n, i, _ }) => `` + renderNest($, _, ContextTag.Inline) + `\n` ]; export const HrHtml: BlockRenderRuleStr = [ ContextTag.Block, BlockTag.Hr, ({ }, { }) => '
\n' ]; export const HrXHtml: BlockRenderRuleStr = [ ContextTag.Block, BlockTag.Hr, ({ }, { }) => '
\n' ]; export const QuoteHtml: BlockRenderRuleStr, NoMeta> = [ ContextTag.Block, BlockTag.Quote, ($, { _ }) => '
\n' + renderNest($, _) + '
\n' ]; export const ListHtml: BlockRenderRuleStr, NoMeta> = [ ContextTag.Block, BlockTag.List, ($, { _ }) => '
    \n' + renderListItems($, _) + '
\n' ]; export const OrdListHtml: BlockRenderRuleStr, NoMeta> = [ ContextTag.Block, BlockTag.OrdList, ($, { s, _ }) => '\n' + renderListItems($, _) + '\n' ]; function renderListItems($: BlockRenderHandleStr | BlockOrdList, NoMeta>, items: BlockListItem[]): string { let out = ''; for (const item of items) { out += '
  • ' + (item.t ? ' ' : '') + renderNest($, item._) + '
  • \n'; } return out; } export const ParagraphHtml: BlockRenderRuleStr, NoMeta> = [ ContextTag.Block, BlockTag.Paragraph, ($, { _ }) => '

    ' + renderNest($, _, ContextTag.Inline) + '

    \n' ]; export const TextBlockHtml: BlockRenderRuleStr, NoMeta> = [ ContextTag.Block, BlockTag.Text, ($, { _ }) => renderNest($, _, ContextTag.Inline) ]; export const TableHtml: BlockRenderRuleStr, NoMeta> = [ ContextTag.Block, BlockTag.Table, ($, { h, a, _ }) => '\n\n\n' + renderTableCells($, h, 'th', a) + '\n\n' + (_.length ? '\n' + renderTableRows($, _, a) + '\n' : '') + '
    ' ]; function renderTableRows($: BlockRenderHandleStr, NoMeta>, rows: BlockTableRow[], align: BlockAlign[]): string { let out = ''; for (const row of rows) { out += '\n' + renderTableCells($, row, 'td', align) + '\n'; } return out; } function renderTableCells($: BlockRenderHandleStr, NoMeta>, cells: BlockTableCell[], tag: string, align: BlockAlign[]): string { let out = ''; for (let i = 0; i < cells.length; i++) { const a = textAlign[align[i]]; out += `<${tag}` + (a ? ` align="${a}"` : '') + '>' + renderNest($, cells[i]) + `\n`; } return out; } export const BlockHtml = [CodeBlockWithClassHtml, HeadingWithIdHtml, HrHtml, QuoteHtml, ListHtml, OrdListHtml, ParagraphHtml, TextBlockHtml]; export const BlockXHtml = [CodeBlockWithClassHtml, HeadingWithIdHtml, HrXHtml, QuoteHtml, ListHtml, OrdListHtml, ParagraphHtml, TextBlockHtml]; export const BlockTablesHtml = [CodeBlockWithClassHtml, HeadingWithIdHtml, HrHtml, QuoteHtml, ListHtml, OrdListHtml, ParagraphHtml, TextBlockHtml, TableHtml]; export const BlockTablesXHtml = [CodeBlockWithClassHtml, HeadingWithIdHtml, HrXHtml, QuoteHtml, ListHtml, OrdListHtml, ParagraphHtml, TextBlockHtml, TableHtml];