diff --git a/core/htmlblocks.js b/core/htmlblocks.js new file mode 100644 index 0000000..5da76f6 --- /dev/null +++ b/core/htmlblocks.js @@ -0,0 +1,125 @@ +'use strict'; + +const { decodeEntities } = require('./util'); + +/** + * Parse sanitized description HTML (see core/sanitize.js) into a flat list + * of blocks with inline formatting runs, for renderers (PDF) that need to + * preserve bold/italic/links/lists rather than flattening to plain text. + * + * Each block: { type, runs, indent, n? } + * type: 'p' | 'h1'..'h4' | 'blockquote' | 'li' | 'oli' | 'hr' + * runs: [{ text, bold, italic, code, href }] (absent for 'hr') + * indent: list/quote nesting depth (0 = top level) + * n: list item number, only for 'oli' + */ + +const TAG_RE = /<\s*(\/?)\s*([a-zA-Z][a-zA-Z0-9]*)((?:"[^"]*"|'[^']*'|[^>"'])*)>/g; +const HREF_RE = /href\s*=\s*"([^"]*)"|href\s*=\s*'([^']*)'/i; + +function htmlToBlocks(html) { + const blocks = []; + let current = null; + const styleStack = [{}]; + const context = []; // nesting of