|
| 1 | +// Source copied and then modified from |
| 2 | +// https://github.com/remarkjs/remark/blob/master/packages/remark-parse/lib/tokenize/html-block.js |
| 3 | +// |
| 4 | +// MIT License https://github.com/remarkjs/remark/blob/master/license |
| 5 | + |
| 6 | +const {openCloseTag} = require('./tag') |
| 7 | + |
| 8 | +module.exports = blockHtml |
| 9 | + |
| 10 | +const tab = '\t' |
| 11 | +const space = ' ' |
| 12 | +const lineFeed = '\n' |
| 13 | +const lessThan = '<' |
| 14 | + |
| 15 | +const rawOpenExpression = /^<(script|pre|style)(?=(\s|>|$))/i |
| 16 | +const rawCloseExpression = /<\/(script|pre|style)>/i |
| 17 | +const commentOpenExpression = /^<!--/ |
| 18 | +const commentCloseExpression = /-->/ |
| 19 | +const instructionOpenExpression = /^<\?/ |
| 20 | +const instructionCloseExpression = /\?>/ |
| 21 | +const directiveOpenExpression = /^<![A-Za-z]/ |
| 22 | +const directiveCloseExpression = />/ |
| 23 | +const cdataOpenExpression = /^<!\[CDATA\[/ |
| 24 | +const cdataCloseExpression = /\]\]>/ |
| 25 | +const elementCloseExpression = /^$/ |
| 26 | +const otherElementOpenExpression = new RegExp(openCloseTag.source + '\\s*$') |
| 27 | + |
| 28 | +function blockHtml(eat, value, silent) { |
| 29 | + const blocks = '[a-z\\.]+(\\.){0,1}[a-z\\.]' |
| 30 | + const elementOpenExpression = new RegExp( |
| 31 | + '^</?(' + blocks + ')(?=(\\s|/?>|$))', |
| 32 | + 'i' |
| 33 | + ) |
| 34 | + const length = value.length |
| 35 | + let index = 0 |
| 36 | + let next |
| 37 | + let line |
| 38 | + let offset |
| 39 | + let character |
| 40 | + let count |
| 41 | + let sequence |
| 42 | + let subvalue |
| 43 | + |
| 44 | + const sequences = [ |
| 45 | + [rawOpenExpression, rawCloseExpression, true], |
| 46 | + [commentOpenExpression, commentCloseExpression, true], |
| 47 | + [instructionOpenExpression, instructionCloseExpression, true], |
| 48 | + [directiveOpenExpression, directiveCloseExpression, true], |
| 49 | + [cdataOpenExpression, cdataCloseExpression, true], |
| 50 | + [elementOpenExpression, elementCloseExpression, true], |
| 51 | + [otherElementOpenExpression, elementCloseExpression, false] |
| 52 | + ] |
| 53 | + |
| 54 | + // Eat initial spacing. |
| 55 | + while (index < length) { |
| 56 | + character = value.charAt(index) |
| 57 | + |
| 58 | + if (character !== tab && character !== space) { |
| 59 | + break |
| 60 | + } |
| 61 | + |
| 62 | + index++ |
| 63 | + } |
| 64 | + |
| 65 | + if (value.charAt(index) !== lessThan) { |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + next = value.indexOf(lineFeed, index + 1) |
| 70 | + next = next === -1 ? length : next |
| 71 | + line = value.slice(index, next) |
| 72 | + offset = -1 |
| 73 | + count = sequences.length |
| 74 | + |
| 75 | + while (++offset < count) { |
| 76 | + if (sequences[offset][0].test(line)) { |
| 77 | + sequence = sequences[offset] |
| 78 | + break |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (!sequence) { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + if (silent) { |
| 87 | + return sequence[2] |
| 88 | + } |
| 89 | + |
| 90 | + index = next |
| 91 | + |
| 92 | + if (!sequence[1].test(line)) { |
| 93 | + while (index < length) { |
| 94 | + next = value.indexOf(lineFeed, index + 1) |
| 95 | + next = next === -1 ? length : next |
| 96 | + line = value.slice(index + 1, next) |
| 97 | + |
| 98 | + if (sequence[1].test(line)) { |
| 99 | + if (line) { |
| 100 | + index = next |
| 101 | + } |
| 102 | + |
| 103 | + break |
| 104 | + } |
| 105 | + |
| 106 | + index = next |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + subvalue = value.slice(0, index) |
| 111 | + |
| 112 | + return eat(subvalue)({type: 'html', value: subvalue}) |
| 113 | +} |
0 commit comments