|
| 1 | + |
| 2 | +// Taken & modified from markdown-it source |
| 3 | +const attr_name = '[a-zA-Z_:][a-zA-Z0-9:._-]*' |
| 4 | + |
| 5 | +const unquoted = '[^"\'=<>`\\x00-\\x20]+' |
| 6 | +const single_quoted = "'[^']*'" |
| 7 | +const double_quoted = '"[^"]*"' |
| 8 | + |
| 9 | +const attr_value = '(?:' + unquoted + '|' + single_quoted + '|' + double_quoted + ')' |
| 10 | + |
| 11 | +const attribute = '(?:\\s+' + attr_name + '(?:\\s*=\\s*' + attr_value + ')?)' |
| 12 | + |
| 13 | +const open_tag = '<([A-Za-z][A-Za-z0-9\\-]*)(' + attribute + '*)\\s*>' |
| 14 | + |
| 15 | +export const HTML_OPEN_TAG_RE = new RegExp('^' + open_tag) |
| 16 | + |
| 17 | +/** |
| 18 | + * Plugin for markdown-it to support HTML tags that span over multiple lines |
| 19 | + */ |
| 20 | +export function HtmlBlockPlugin() { |
| 21 | + return md => { |
| 22 | + md.core.ruler.push('html_block_plugin', function(state) { |
| 23 | + const tokens = [] |
| 24 | + // console.log("TOKENS", state.tokens.map((t, i) => [i, t.type, t.content])) |
| 25 | + for (let i = 0; i < state.tokens.length; i++) { |
| 26 | + if (state.tokens[i].type !== 'html_block') { |
| 27 | + tokens.push(state.tokens[i]) |
| 28 | + continue |
| 29 | + } |
| 30 | + // const tag = state.tokens[i].content.match(/^<([a-z-]+)>\n?(.*)/mi)?.[1] |
| 31 | + const match = state.tokens[i].content.match(HTML_OPEN_TAG_RE) |
| 32 | + |
| 33 | + if (!match) { |
| 34 | + tokens.push(state.tokens[i]) |
| 35 | + continue |
| 36 | + } |
| 37 | + const [, tag, attrs] = match |
| 38 | + console.log("MATCH", tag, attrs) |
| 39 | + |
| 40 | + const endTag = `</${tag}>\n` |
| 41 | + |
| 42 | + let endIdx = i + 1 |
| 43 | + while (endIdx < state.tokens.length && state.tokens[endIdx].content !== endTag) { |
| 44 | + endIdx++ |
| 45 | + } |
| 46 | + if (endIdx < state.tokens.length && state.tokens[endIdx].content === endTag) { |
| 47 | + // console.log("END", i, endIdx, state.tokens[endIdx]) |
| 48 | + const body = collectContent(state, i, endIdx) |
| 49 | + // console.log("->>", body) |
| 50 | + const token = new state.Token('html_block', '', 0) |
| 51 | + token.block = true |
| 52 | + token.level = state.level |
| 53 | + token.content = `<${tag}${attrs}>${body}</${tag}>` |
| 54 | + tokens.push(token) |
| 55 | + i = endIdx |
| 56 | + continue |
| 57 | + } |
| 58 | + else { |
| 59 | + console.warn('No closing tag found for <chapter>') |
| 60 | + } |
| 61 | + } |
| 62 | + // console.log("TOKENS", tokens.map((t, i) => [i, t.type, t.content])) |
| 63 | + state.tokens = tokens |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +function collectContent(state, start, end) { |
| 69 | + let content = []; |
| 70 | + for (let i = start + 1; i < end; i++) { |
| 71 | + const token = state.tokens[i]; |
| 72 | + if (token.type === 'inline') { |
| 73 | + content.push(token.content); |
| 74 | + } else if (token.type === 'html_block') { |
| 75 | + content.push(token.content) |
| 76 | + } |
| 77 | + } |
| 78 | + return content.join('\n\n').trim() |
| 79 | +} |
0 commit comments