diff --git a/src/utils/markdown-it-plugins/inlineFence.js b/src/utils/markdown-it-plugins/inlineFence.js new file mode 100644 index 0000000..0a5e500 --- /dev/null +++ b/src/utils/markdown-it-plugins/inlineFence.js @@ -0,0 +1,44 @@ +function parseInlineFence(state, silent) { + let pos = state.pos + let max = state.posMax + let src = state.src.slice(pos, max) + + if(pos + 6 > max || state.src.charCodeAt(pos) !== 0x60) { + return false + } + + + if(!src.startsWith('```') || !src.endsWith('```')) { + return false + } + + pos += 3 + + const marker = state.src.slice(state.pos, pos); + + let matchEnd = pos + let matchStart = pos + while ((matchStart = state.src.indexOf('```', matchEnd)) !== -1) { + console.log(matchStart, state) + matchEnd = matchStart + 1 + + if(!silent) { + const token = state.push('fence', 'code', 0) + token.markup = marker + token.content = state.src.slice(pos, matchStart) + .replace(/\n/g, ' ') + .replace(/^ (.+) $/, '$1'); + } + state.pos = matchEnd + 2 + return true + } + + if(!silent) { + state.pending += marker + } + state.pos += 3 + return true +} +export default function inlineFence(md, opts) { + md.inline.ruler.before('backticks', 'inline_fence', parseInlineFence) +} \ No newline at end of file diff --git a/src/utils/markdown-it-plugins/normalizeFence.js b/src/utils/markdown-it-plugins/normalizeFence.js index d031985..69dc346 100644 --- a/src/utils/markdown-it-plugins/normalizeFence.js +++ b/src/utils/markdown-it-plugins/normalizeFence.js @@ -7,7 +7,7 @@ function fence(state, startLine, endLine, silent) { // 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; } + if (pos + 6 > max) { return false; } marker = state.src.charCodeAt(pos); @@ -16,10 +16,10 @@ function fence(state, startLine, endLine, silent) { } // scan marker length - mem = pos; - pos = state.skipChars(pos, marker); + let count = state.skipChars(pos, marker); - len = pos - mem; + len = count - pos; + pos += 3 if (len < 3) { return false; } @@ -52,7 +52,7 @@ function fence(state, startLine, endLine, silent) { pos = state.skipChars(pos, marker); // closing code fence must be at least as long as the opening one - if (pos - mem < len) { continue; } + if (pos - mem < 3) { continue; } // make sure tail has spaces only pos = state.skipSpaces(pos); @@ -79,5 +79,5 @@ function fence(state, startLine, endLine, silent) { } export default function normalizeFence(md, opts) { - md.block.ruler.at('fence', fence) + md.block.ruler.at('fence', fence, {alt: [ 'paragraph', 'reference', 'blockquote', 'list' ]}) } \ No newline at end of file diff --git a/src/utils/messageFormatter.js b/src/utils/messageFormatter.js index 4edcd69..66f2cc3 100644 --- a/src/utils/messageFormatter.js +++ b/src/utils/messageFormatter.js @@ -6,6 +6,7 @@ import customEmoji from './markdown-it-plugins/customEmoji' import formatLink from './markdown-it-plugins/formatLink' import formatCode from './markdown-it-plugins/formatCode' import normalizeFence from './markdown-it-plugins/normalizeFence' +import inlineFence from './markdown-it-plugins/inlineFence' import hljs from 'highlight.js' @@ -31,7 +32,8 @@ const markdown = new MarkdownIt({ .use(chatPlugin) .use(customEmoji) .use(formatLink) - .use(formatCode); + .use(formatCode) + .use(inlineFence) export default (message) => {