|
| 1 | +import type MarkdownIt from "markdown-it/lib" |
| 2 | +import type Renderer from "markdown-it/lib/renderer" |
| 3 | +import type StateBlock from "markdown-it/lib/rules_block/state_block" |
| 4 | +import { escapeHtml, unescapeAll } from "markdown-it/lib/common/utils" |
| 5 | + |
| 6 | +// Ported from: https://github.com/executablebooks/mdit-py-plugins/blob/master/mdit_py_plugins/colon_fence.py |
| 7 | + |
| 8 | +function _rule(state: StateBlock, startLine: number, endLine: number, silent: boolean) { |
| 9 | + let haveEndMarker = false |
| 10 | + let pos = state.bMarks[startLine] + state.tShift[startLine] |
| 11 | + let maximum = state.eMarks[startLine] |
| 12 | + |
| 13 | + // if it's indented more than 3 spaces, it should be a code block |
| 14 | + if (state.sCount[startLine] - state.blkIndent >= 4) { |
| 15 | + return false |
| 16 | + } |
| 17 | + |
| 18 | + if (pos + 3 > maximum) { |
| 19 | + return false |
| 20 | + } |
| 21 | + |
| 22 | + const marker = state.src.charCodeAt(pos) |
| 23 | + |
| 24 | + // /* : */ |
| 25 | + if (marker !== 0x3a) { |
| 26 | + return false |
| 27 | + } |
| 28 | + |
| 29 | + // scan marker length |
| 30 | + let mem = pos |
| 31 | + pos = state.skipChars(pos, marker) |
| 32 | + |
| 33 | + let length = pos - mem |
| 34 | + |
| 35 | + if (length < 3) { |
| 36 | + return false |
| 37 | + } |
| 38 | + |
| 39 | + const markup = state.src.slice(mem, pos) |
| 40 | + const params = state.src.slice(pos, maximum) |
| 41 | + |
| 42 | + // Since start is found, we can report success here in validation mode |
| 43 | + if (silent) { |
| 44 | + return true |
| 45 | + } |
| 46 | + |
| 47 | + // search end of block |
| 48 | + let nextLine = startLine |
| 49 | + |
| 50 | + // eslint-disable-next-line no-constant-condition |
| 51 | + while (true) { |
| 52 | + nextLine += 1 |
| 53 | + if (nextLine >= endLine) { |
| 54 | + // unclosed block should be autoclosed by end of document. |
| 55 | + // also block seems to be autoclosed by end of parent |
| 56 | + break |
| 57 | + } |
| 58 | + |
| 59 | + pos = mem = state.bMarks[nextLine] + state.tShift[nextLine] |
| 60 | + maximum = state.eMarks[nextLine] |
| 61 | + |
| 62 | + if (pos < maximum && state.sCount[nextLine] < state.blkIndent) { |
| 63 | + // non-empty line with negative indent should stop the list: |
| 64 | + // - ``` |
| 65 | + // test |
| 66 | + break |
| 67 | + } |
| 68 | + |
| 69 | + if (state.src.charCodeAt(pos) != marker) { |
| 70 | + continue |
| 71 | + } |
| 72 | + |
| 73 | + if (state.sCount[nextLine] - state.blkIndent >= 4) { |
| 74 | + // closing fence should be indented less than 4 spaces |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + pos = state.skipChars(pos, marker) |
| 79 | + |
| 80 | + // closing code fence must be at least as long as the opening one |
| 81 | + if (pos - mem < length) { |
| 82 | + continue |
| 83 | + } |
| 84 | + |
| 85 | + // make sure tail has spaces only |
| 86 | + pos = state.skipSpaces(pos) |
| 87 | + |
| 88 | + if (pos < maximum) { |
| 89 | + continue |
| 90 | + } |
| 91 | + |
| 92 | + haveEndMarker = true |
| 93 | + // found! |
| 94 | + break |
| 95 | + } |
| 96 | + // If a fence has heading spaces, they should be removed from its inner block |
| 97 | + length = state.sCount[startLine] |
| 98 | + |
| 99 | + state.line = nextLine + (haveEndMarker ? 1 : 0) |
| 100 | + |
| 101 | + const token = state.push("colon_fence", "code", 0) |
| 102 | + token.info = params |
| 103 | + token.content = state.getLines(startLine + 1, nextLine, length, true) |
| 104 | + token.markup = markup |
| 105 | + token.map = [startLine, state.line] |
| 106 | + |
| 107 | + return true |
| 108 | +} |
| 109 | + |
| 110 | +const colonFenceRender: Renderer.RenderRule = function colonFenceRender(tokens, idx) { |
| 111 | + const token = tokens[idx] |
| 112 | + const info = token.info ? unescapeAll(token.info).trim() : "" |
| 113 | + const content = escapeHtml(token.content) |
| 114 | + let block_name = "" |
| 115 | + |
| 116 | + if (info) { |
| 117 | + block_name = info.split(" ")[0] |
| 118 | + } |
| 119 | + const codeClass = block_name ? ` class="block-${block_name}"` : "" |
| 120 | + return `<pre><code${codeClass}>${content}</code></pre>\n` |
| 121 | +} |
| 122 | + |
| 123 | +/** |
| 124 | + * This plugin directly mimics regular fences, but with `:` colons. |
| 125 | + * Example: |
| 126 | + * ```md |
| 127 | + * :::name |
| 128 | + * contained text |
| 129 | + * ::: |
| 130 | + * ``` |
| 131 | + */ |
| 132 | +export function colonFencePlugin(md: MarkdownIt) { |
| 133 | + md.block.ruler.before("fence", "colon_fence", _rule, { |
| 134 | + alt: ["paragraph", "reference", "blockquote", "list", "footnote_def"] |
| 135 | + }) |
| 136 | + md.renderer.rules["colon_fence"] = colonFenceRender |
| 137 | +} |
0 commit comments