Created
July 31, 2020 03:49
-
-
Save kaishuu0123/31e59a8400cd23db2435e97a8df1d55c to your computer and use it in GitHub Desktop.
markdown-it-drawio-viewer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var index = function (md, name, options) { | |
function validateDefault(params) { | |
return params.trim().split(' ', 2)[0] === name | |
} | |
function renderDefault(tokens, idx, _options, env, self) { | |
if (tokens[idx].nesting === 1) { | |
tokens[idx].attrPush(['class', name]); | |
} | |
return self.renderToken(tokens, idx, _options, env, self) | |
} | |
options = options || {} | |
var min_markers = 3, | |
marker_str = options.marker || '`', | |
marker_char = marker_str.charCodeAt(0), | |
marker_len = marker_str.length, | |
validate = options.validate || validateDefault, | |
render = options.render || renderDefault; | |
function fence(state, startLine, endLine, silent) { | |
var marker, len, params, nextLine, mem, token, markup, | |
haveEndMarker = false, | |
pos = state.bMarks[startLine] + state.tShift[startLine], | |
max = state.eMarks[startLine]; | |
// if it's indented more than 3 spaces, it should be a code block | |
if (state.sCount[startLine] - state.blkIndent >= 4) { return false; } | |
if (pos + 3 > max) { return false; } | |
marker = state.src.charCodeAt(pos); | |
if (marker !== marker_char) { | |
return false; | |
} | |
// scan marker length | |
mem = pos; | |
pos = state.skipChars(pos, marker); | |
len = pos - mem; | |
if (len < 3) { return false; } | |
markup = state.src.slice(mem, pos); | |
params = state.src.slice(pos, max); | |
if (marker === marker_char) { | |
if (params.indexOf(String.fromCharCode(marker)) >= 0) { | |
return false; | |
} | |
} | |
// Since start is found, we can report success here in validation mode | |
if (silent) { return true; } | |
// search end of block | |
nextLine = startLine; | |
for (;;) { | |
nextLine++; | |
if (nextLine >= endLine) { | |
// unclosed block should be autoclosed by end of document. | |
// also block seems to be autoclosed by end of parent | |
break; | |
} | |
pos = mem = state.bMarks[nextLine] + state.tShift[nextLine]; | |
max = state.eMarks[nextLine]; | |
if (pos < max && state.sCount[nextLine] < state.blkIndent) { | |
// non-empty line with negative indent should stop the list: | |
// - ``` | |
// test | |
break; | |
} | |
if (state.src.charCodeAt(pos) !== marker_char) { continue; } | |
if (state.sCount[nextLine] - state.blkIndent >= 4) { | |
// closing fence should be indented less than 4 spaces | |
continue; | |
} | |
pos = state.skipChars(pos, marker); | |
// closing code fence must be at least as long as the opening one | |
if (pos - mem < len) { continue; } | |
// make sure tail has spaces only | |
pos = state.skipSpaces(pos); | |
if (pos < max) { continue; } | |
haveEndMarker = true; | |
// found! | |
break; | |
} | |
// If a fence has heading spaces, they should be removed from its inner block | |
len = state.sCount[startLine] | |
state.line = nextLine + (haveEndMarker ? 1 : 0); | |
token = state.push(name, 'div', 0); | |
token.info = params; | |
token.content = state.getLines(startLine + 1, nextLine, len, true); | |
token.markup = markup; | |
token.map = [startLine + 1, state.line]; | |
return true | |
} | |
md.block.ruler.before('fence', name, fence, { | |
alt: ['paragraph', 'reference', 'blockquote']}); | |
md.renderer.rules[name] = options.render; | |
}; | |
module.exports = index; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment