mirror of
https://github.com/danbulant/Nertivia-Client
synced 2026-06-24 17:11:43 +00:00
formatter fixes (by bree)
This commit is contained in:
parent
2e95fab9df
commit
099f280f01
3 changed files with 53 additions and 7 deletions
44
src/utils/markdown-it-plugins/inlineFence.js
Normal file
44
src/utils/markdown-it-plugins/inlineFence.js
Normal file
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -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 it's indented more than 3 spaces, it should be a code block
|
||||||
// if (state.sCount[startLine] - state.blkIndent >= 4) { return false; }
|
// 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);
|
marker = state.src.charCodeAt(pos);
|
||||||
|
|
||||||
|
|
@ -16,10 +16,10 @@ function fence(state, startLine, endLine, silent) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// scan marker length
|
// scan marker length
|
||||||
mem = pos;
|
let count = state.skipChars(pos, marker);
|
||||||
pos = state.skipChars(pos, marker);
|
|
||||||
|
|
||||||
len = pos - mem;
|
len = count - pos;
|
||||||
|
pos += 3
|
||||||
|
|
||||||
if (len < 3) { return false; }
|
if (len < 3) { return false; }
|
||||||
|
|
||||||
|
|
@ -52,7 +52,7 @@ function fence(state, startLine, endLine, silent) {
|
||||||
pos = state.skipChars(pos, marker);
|
pos = state.skipChars(pos, marker);
|
||||||
|
|
||||||
// closing code fence must be at least as long as the opening one
|
// 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
|
// make sure tail has spaces only
|
||||||
pos = state.skipSpaces(pos);
|
pos = state.skipSpaces(pos);
|
||||||
|
|
@ -79,5 +79,5 @@ function fence(state, startLine, endLine, silent) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function normalizeFence(md, opts) {
|
export default function normalizeFence(md, opts) {
|
||||||
md.block.ruler.at('fence', fence)
|
md.block.ruler.at('fence', fence, {alt: [ 'paragraph', 'reference', 'blockquote', 'list' ]})
|
||||||
}
|
}
|
||||||
|
|
@ -6,6 +6,7 @@ import customEmoji from './markdown-it-plugins/customEmoji'
|
||||||
import formatLink from './markdown-it-plugins/formatLink'
|
import formatLink from './markdown-it-plugins/formatLink'
|
||||||
import formatCode from './markdown-it-plugins/formatCode'
|
import formatCode from './markdown-it-plugins/formatCode'
|
||||||
import normalizeFence from './markdown-it-plugins/normalizeFence'
|
import normalizeFence from './markdown-it-plugins/normalizeFence'
|
||||||
|
import inlineFence from './markdown-it-plugins/inlineFence'
|
||||||
|
|
||||||
import hljs from 'highlight.js'
|
import hljs from 'highlight.js'
|
||||||
|
|
||||||
|
|
@ -31,7 +32,8 @@ const markdown = new MarkdownIt({
|
||||||
.use(chatPlugin)
|
.use(chatPlugin)
|
||||||
.use(customEmoji)
|
.use(customEmoji)
|
||||||
.use(formatLink)
|
.use(formatLink)
|
||||||
.use(formatCode);
|
.use(formatCode)
|
||||||
|
.use(inlineFence)
|
||||||
|
|
||||||
export default (message) => {
|
export default (message) => {
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue