|
| 1 | +// Replacing the default htmlBlock rule to allow using custom components at |
| 2 | +// root level |
| 3 | + |
| 4 | +const blockNames = require('markdown-it/lib/common/htmlBlocks') |
| 5 | +const HTML_OPEN_CLOSE_TAG_RE = require('markdown-it/lib/common/html_re').HTML_OPEN_CLOSE_TAG_RE |
| 6 | + |
| 7 | +// An array of opening and corresponding closing sequences for html tags, |
| 8 | +// last argument defines whether it can terminate a paragraph or not |
| 9 | +const HTML_SEQUENCES = [ |
| 10 | + [/^<(script|pre|style)(?=(\s|>|$))/i, /<\/(script|pre|style)>/i, true], |
| 11 | + [/^<!--/, /-->/, true], |
| 12 | + [/^<\?/, /\?>/, true], |
| 13 | + [/^<![A-Z]/, />/, true], |
| 14 | + [/^<!\[CDATA\[/, /\]\]>/, true], |
| 15 | + // PascalCase Components |
| 16 | + [/^<[A-Z]/, />/, true], |
| 17 | + // custom elements with hyphens |
| 18 | + [/^<\w+\-/, />/, true], |
| 19 | + [new RegExp('^</?(' + blockNames.join('|') + ')(?=(\\s|/?>|$))', 'i'), /^$/, true], |
| 20 | + [new RegExp(HTML_OPEN_CLOSE_TAG_RE.source + '\\s*$'), /^$/, false] |
| 21 | +] |
| 22 | + |
| 23 | +module.exports = md => { |
| 24 | + md.block.ruler.at('htmlBlock', htmlBlock) |
| 25 | +} |
| 26 | + |
| 27 | +function htmlBlock (state, startLine, endLine, silent) { |
| 28 | + let i, nextLine, lineText |
| 29 | + let pos = state.bMarks[startLine] + state.tShift[startLine] |
| 30 | + let max = state.eMarks[startLine] |
| 31 | + |
| 32 | + // if it's indented more than 3 spaces, it should be a code block |
| 33 | + if (state.sCount[startLine] - state.blkIndent >= 4) { return false } |
| 34 | + |
| 35 | + if (!state.md.options.html) { return false } |
| 36 | + |
| 37 | + if (state.src.charCodeAt(pos) !== 0x3C/* < */) { return false } |
| 38 | + |
| 39 | + lineText = state.src.slice(pos, max) |
| 40 | + |
| 41 | + for (i = 0; i < HTML_SEQUENCES.length; i++) { |
| 42 | + if (HTML_SEQUENCES[i][0].test(lineText)) { break } |
| 43 | + } |
| 44 | + |
| 45 | + if (i === HTML_SEQUENCES.length) { |
| 46 | + console.log(lineText) |
| 47 | + return false |
| 48 | + } |
| 49 | + |
| 50 | + if (silent) { |
| 51 | + // true if this sequence can be a terminator, false otherwise |
| 52 | + return HTML_SEQUENCES[i][2] |
| 53 | + } |
| 54 | + |
| 55 | + nextLine = startLine + 1 |
| 56 | + |
| 57 | + // If we are here - we detected HTML block. |
| 58 | + // Let's roll down till block end. |
| 59 | + if (!HTML_SEQUENCES[i][1].test(lineText)) { |
| 60 | + for (; nextLine < endLine; nextLine++) { |
| 61 | + if (state.sCount[nextLine] < state.blkIndent) { break } |
| 62 | + |
| 63 | + pos = state.bMarks[nextLine] + state.tShift[nextLine] |
| 64 | + max = state.eMarks[nextLine] |
| 65 | + lineText = state.src.slice(pos, max) |
| 66 | + |
| 67 | + if (HTML_SEQUENCES[i][1].test(lineText)) { |
| 68 | + if (lineText.length !== 0) { nextLine++ } |
| 69 | + break |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + state.line = nextLine |
| 75 | + |
| 76 | + const token = state.push('htmlBlock', '', 0) |
| 77 | + token.map = [startLine, nextLine] |
| 78 | + token.content = state.getLines(startLine, nextLine, state.blkIndent, true) |
| 79 | + |
| 80 | + return true |
| 81 | +} |
0 commit comments