mirror of https://github.com/requarks/wiki
parent
d2a18eca3c
commit
50054dfa4f
@ -0,0 +1,99 @@
|
|||||||
|
import MarkdownIt from 'markdown-it'
|
||||||
|
import mdAttrs from 'markdown-it-attrs'
|
||||||
|
import mdDecorate from 'markdown-it-decorate'
|
||||||
|
import mdEmoji from 'markdown-it-emoji'
|
||||||
|
import mdTaskLists from 'markdown-it-task-lists'
|
||||||
|
import mdExpandTabs from 'markdown-it-expand-tabs'
|
||||||
|
import mdAbbr from 'markdown-it-abbr'
|
||||||
|
import mdSup from 'markdown-it-sup'
|
||||||
|
import mdSub from 'markdown-it-sub'
|
||||||
|
import mdMark from 'markdown-it-mark'
|
||||||
|
import mdMultiTable from 'markdown-it-multimd-table'
|
||||||
|
import mdFootnote from 'markdown-it-footnote'
|
||||||
|
// import mdImsize from 'markdown-it-imsize'
|
||||||
|
import katex from 'katex'
|
||||||
|
import underline from './modules/markdown-it-underline'
|
||||||
|
import 'katex/dist/contrib/mhchem'
|
||||||
|
import twemoji from 'twemoji'
|
||||||
|
import plantuml from './modules/plantuml'
|
||||||
|
import katexHelper from './modules/katex'
|
||||||
|
|
||||||
|
import { escape } from 'lodash-es'
|
||||||
|
|
||||||
|
export class MarkdownRenderer {
|
||||||
|
constructor (conf = {}) {
|
||||||
|
this.md = new MarkdownIt({
|
||||||
|
html: true,
|
||||||
|
breaks: true,
|
||||||
|
linkify: true,
|
||||||
|
typography: true,
|
||||||
|
highlight (str, lang) {
|
||||||
|
if (lang === 'diagram') {
|
||||||
|
return `<pre class="diagram">${Buffer.from(str, 'base64').toString()}</pre>`
|
||||||
|
} else if (['mermaid', 'plantuml'].includes(lang)) {
|
||||||
|
return `<pre class="codeblock-${lang}"><code>${escape(str)}</code></pre>`
|
||||||
|
} else {
|
||||||
|
return `<pre class="line-numbers"><code class="language-${lang}">${escape(str)}</code></pre>`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.use(mdAttrs, {
|
||||||
|
allowedAttributes: ['id', 'class', 'target']
|
||||||
|
})
|
||||||
|
.use(mdDecorate)
|
||||||
|
.use(underline)
|
||||||
|
.use(mdEmoji)
|
||||||
|
.use(mdTaskLists, { label: false, labelAfter: false })
|
||||||
|
.use(mdExpandTabs)
|
||||||
|
.use(mdAbbr)
|
||||||
|
.use(mdSup)
|
||||||
|
.use(mdSub)
|
||||||
|
.use(mdMultiTable, { multiline: true, rowspan: true, headerless: true })
|
||||||
|
.use(mdMark)
|
||||||
|
.use(mdFootnote)
|
||||||
|
// .use(mdImsize)
|
||||||
|
|
||||||
|
// -> PLANTUML
|
||||||
|
plantuml.init(this.md, {})
|
||||||
|
|
||||||
|
// -> KATEX
|
||||||
|
const macros = {}
|
||||||
|
this.md.inline.ruler.after('escape', 'katex_inline', katexHelper.katexInline)
|
||||||
|
this.md.renderer.rules.katex_inline = (tokens, idx) => {
|
||||||
|
try {
|
||||||
|
return katex.renderToString(tokens[idx].content, {
|
||||||
|
displayMode: false, macros
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
console.warn(err)
|
||||||
|
return tokens[idx].content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.md.block.ruler.after('blockquote', 'katex_block', katexHelper.katexBlock, {
|
||||||
|
alt: ['paragraph', 'reference', 'blockquote', 'list']
|
||||||
|
})
|
||||||
|
this.md.renderer.rules.katex_block = (tokens, idx) => {
|
||||||
|
try {
|
||||||
|
return '<p>' + katex.renderToString(tokens[idx].content, {
|
||||||
|
displayMode: true, macros
|
||||||
|
}) + '</p>'
|
||||||
|
} catch (err) {
|
||||||
|
console.warn(err)
|
||||||
|
return tokens[idx].content
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// -> TWEMOJI
|
||||||
|
this.md.renderer.rules.emoji = (token, idx) => {
|
||||||
|
return twemoji.parse(token[idx].content, {
|
||||||
|
callback (icon, opts) {
|
||||||
|
return `/_assets/svg/twemoji/${icon}.svg`
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
render (src) {
|
||||||
|
return this.md.render(src)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,145 @@
|
|||||||
|
// Test if potential opening or closing delimieter
|
||||||
|
// Assumes that there is a "$" at state.src[pos]
|
||||||
|
function isValidDelim (state, pos) {
|
||||||
|
const max = state.posMax
|
||||||
|
let canOpen = true
|
||||||
|
let canClose = true
|
||||||
|
|
||||||
|
const prevChar = pos > 0 ? state.src.charCodeAt(pos - 1) : -1
|
||||||
|
const nextChar = pos + 1 <= max ? state.src.charCodeAt(pos + 1) : -1
|
||||||
|
|
||||||
|
// Check non-whitespace conditions for opening and closing, and
|
||||||
|
// check that closing delimeter isn't followed by a number
|
||||||
|
if (prevChar === 0x20/* " " */ || prevChar === 0x09/* \t */ ||
|
||||||
|
(nextChar >= 0x30/* "0" */ && nextChar <= 0x39/* "9" */)) {
|
||||||
|
canClose = false
|
||||||
|
}
|
||||||
|
if (nextChar === 0x20/* " " */ || nextChar === 0x09/* \t */) {
|
||||||
|
canOpen = false
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
canOpen,
|
||||||
|
canClose
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default {
|
||||||
|
katexInline (state, silent) {
|
||||||
|
let match, token, res, pos
|
||||||
|
|
||||||
|
if (state.src[state.pos] !== '$') { return false }
|
||||||
|
|
||||||
|
res = isValidDelim(state, state.pos)
|
||||||
|
if (!res.canOpen) {
|
||||||
|
if (!silent) { state.pending += '$' }
|
||||||
|
state.pos += 1
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// First check for and bypass all properly escaped delimieters
|
||||||
|
// This loop will assume that the first leading backtick can not
|
||||||
|
// be the first character in state.src, which is known since
|
||||||
|
// we have found an opening delimieter already.
|
||||||
|
const start = state.pos + 1
|
||||||
|
match = start
|
||||||
|
while ((match = state.src.indexOf('$', match)) !== -1) {
|
||||||
|
// Found potential $, look for escapes, pos will point to
|
||||||
|
// first non escape when complete
|
||||||
|
pos = match - 1
|
||||||
|
while (state.src[pos] === '\\') { pos -= 1 }
|
||||||
|
|
||||||
|
// Even number of escapes, potential closing delimiter found
|
||||||
|
if (((match - pos) % 2) === 1) { break }
|
||||||
|
match += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// No closing delimter found. Consume $ and continue.
|
||||||
|
if (match === -1) {
|
||||||
|
if (!silent) { state.pending += '$' }
|
||||||
|
state.pos = start
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if we have empty content, ie: $$. Do not parse.
|
||||||
|
if (match - start === 0) {
|
||||||
|
if (!silent) { state.pending += '$$' }
|
||||||
|
state.pos = start + 1
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for valid closing delimiter
|
||||||
|
res = isValidDelim(state, match)
|
||||||
|
if (!res.canClose) {
|
||||||
|
if (!silent) { state.pending += '$' }
|
||||||
|
state.pos = start
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!silent) {
|
||||||
|
token = state.push('katex_inline', 'math', 0)
|
||||||
|
token.markup = '$'
|
||||||
|
token.content = state.src
|
||||||
|
// Extract the math part without the $
|
||||||
|
.slice(start, match)
|
||||||
|
// Escape the curly braces since they will be interpreted as
|
||||||
|
// attributes by markdown-it-attrs (the "curly_attributes"
|
||||||
|
// core rule)
|
||||||
|
.replaceAll('{', '{{')
|
||||||
|
.replaceAll('}', '}}')
|
||||||
|
}
|
||||||
|
|
||||||
|
state.pos = match + 1
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
|
||||||
|
katexBlock (state, start, end, silent) {
|
||||||
|
let firstLine; let lastLine; let next; let lastPos; let found = false
|
||||||
|
let pos = state.bMarks[start] + state.tShift[start]
|
||||||
|
let max = state.eMarks[start]
|
||||||
|
|
||||||
|
if (pos + 2 > max) { return false }
|
||||||
|
if (state.src.slice(pos, pos + 2) !== '$$') { return false }
|
||||||
|
|
||||||
|
pos += 2
|
||||||
|
firstLine = state.src.slice(pos, max)
|
||||||
|
|
||||||
|
if (silent) { return true }
|
||||||
|
if (firstLine.trim().slice(-2) === '$$') {
|
||||||
|
// Single line expression
|
||||||
|
firstLine = firstLine.trim().slice(0, -2)
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
|
||||||
|
for (next = start; !found;) {
|
||||||
|
next++
|
||||||
|
|
||||||
|
if (next >= end) { break }
|
||||||
|
|
||||||
|
pos = state.bMarks[next] + state.tShift[next]
|
||||||
|
max = state.eMarks[next]
|
||||||
|
|
||||||
|
if (pos < max && state.tShift[next] < state.blkIndent) {
|
||||||
|
// non-empty line with negative indent should stop the list:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.src.slice(pos, max).trim().slice(-2) === '$$') {
|
||||||
|
lastPos = state.src.slice(0, max).lastIndexOf('$$')
|
||||||
|
lastLine = state.src.slice(pos, lastPos)
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
state.line = next + 1
|
||||||
|
|
||||||
|
const token = state.push('katex_block', 'math', 0)
|
||||||
|
token.block = true
|
||||||
|
token.content = (firstLine && firstLine.trim() ? firstLine + '\n' : '') +
|
||||||
|
state.getLines(start + 1, next, state.tShift[start], true) +
|
||||||
|
(lastLine && lastLine.trim() ? lastLine : '')
|
||||||
|
token.map = [start, state.line]
|
||||||
|
token.markup = '$$'
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
function renderEm (tokens, idx, opts, env, slf) {
|
||||||
|
const token = tokens[idx]
|
||||||
|
if (token.markup === '_') {
|
||||||
|
token.tag = 'u'
|
||||||
|
}
|
||||||
|
return slf.renderToken(tokens, idx, opts)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default (md) => {
|
||||||
|
md.renderer.rules.em_open = renderEm
|
||||||
|
md.renderer.rules.em_close = renderEm
|
||||||
|
}
|
@ -0,0 +1,187 @@
|
|||||||
|
import pako from 'pako'
|
||||||
|
|
||||||
|
// ------------------------------------
|
||||||
|
// Markdown - PlantUML Preprocessor
|
||||||
|
// ------------------------------------
|
||||||
|
|
||||||
|
export default {
|
||||||
|
init (mdinst, conf) {
|
||||||
|
mdinst.use((md, opts) => {
|
||||||
|
const openMarker = opts.openMarker || '```plantuml'
|
||||||
|
const openChar = openMarker.charCodeAt(0)
|
||||||
|
const closeMarker = opts.closeMarker || '```'
|
||||||
|
const closeChar = closeMarker.charCodeAt(0)
|
||||||
|
const imageFormat = opts.imageFormat || 'svg'
|
||||||
|
const server = opts.server || 'https://plantuml.requarks.io'
|
||||||
|
|
||||||
|
md.block.ruler.before('fence', 'uml_diagram', (state, startLine, endLine, silent) => {
|
||||||
|
let nextLine
|
||||||
|
let i
|
||||||
|
let autoClosed = false
|
||||||
|
let start = state.bMarks[startLine] + state.tShift[startLine]
|
||||||
|
let max = state.eMarks[startLine]
|
||||||
|
|
||||||
|
// Check out the first character quickly,
|
||||||
|
// this should filter out most of non-uml blocks
|
||||||
|
//
|
||||||
|
if (openChar !== state.src.charCodeAt(start)) { return false }
|
||||||
|
|
||||||
|
// Check out the rest of the marker string
|
||||||
|
//
|
||||||
|
for (i = 0; i < openMarker.length; ++i) {
|
||||||
|
if (openMarker[i] !== state.src[start + i]) { return false }
|
||||||
|
}
|
||||||
|
|
||||||
|
const markup = state.src.slice(start, start + i)
|
||||||
|
const params = state.src.slice(start + i, max)
|
||||||
|
|
||||||
|
// Since start is found, we can report success here in validation mode
|
||||||
|
//
|
||||||
|
if (silent) { return true }
|
||||||
|
|
||||||
|
// Search for the end of the 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
|
||||||
|
}
|
||||||
|
|
||||||
|
start = state.bMarks[nextLine] + state.tShift[nextLine]
|
||||||
|
max = state.eMarks[nextLine]
|
||||||
|
|
||||||
|
if (start < max && state.sCount[nextLine] < state.blkIndent) {
|
||||||
|
// non-empty line with negative indent should stop the list:
|
||||||
|
// - ```
|
||||||
|
// test
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if (closeChar !== state.src.charCodeAt(start)) {
|
||||||
|
// didn't find the closing fence
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (state.sCount[nextLine] > state.sCount[startLine]) {
|
||||||
|
// closing fence should not be indented with respect of opening fence
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
let closeMarkerMatched = true
|
||||||
|
for (i = 0; i < closeMarker.length; ++i) {
|
||||||
|
if (closeMarker[i] !== state.src[start + i]) {
|
||||||
|
closeMarkerMatched = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!closeMarkerMatched) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure tail has spaces only
|
||||||
|
if (state.skipSpaces(start + i) < max) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// found!
|
||||||
|
autoClosed = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
const contents = state.src
|
||||||
|
.split('\n')
|
||||||
|
.slice(startLine + 1, nextLine)
|
||||||
|
.join('\n')
|
||||||
|
|
||||||
|
// We generate a token list for the alt property, to mimic what the image parser does.
|
||||||
|
const altToken = []
|
||||||
|
// Remove leading space if any.
|
||||||
|
const alt = params ? params.slice(1) : 'uml diagram'
|
||||||
|
state.md.inline.parse(
|
||||||
|
alt,
|
||||||
|
state.md,
|
||||||
|
state.env,
|
||||||
|
altToken
|
||||||
|
)
|
||||||
|
|
||||||
|
const zippedCode = encode64(pako.deflate('@startuml\n' + contents + '\n@enduml', { to: 'string' }))
|
||||||
|
|
||||||
|
const token = state.push('uml_diagram', 'img', 0)
|
||||||
|
// alt is constructed from children. No point in populating it here.
|
||||||
|
token.attrs = [['src', `${server}/${imageFormat}/${zippedCode}`], ['alt', ''], ['class', 'uml-diagram']]
|
||||||
|
token.block = true
|
||||||
|
token.children = altToken
|
||||||
|
token.info = params
|
||||||
|
token.map = [startLine, nextLine]
|
||||||
|
token.markup = markup
|
||||||
|
|
||||||
|
state.line = nextLine + (autoClosed ? 1 : 0)
|
||||||
|
|
||||||
|
return true
|
||||||
|
}, {
|
||||||
|
alt: ['paragraph', 'reference', 'blockquote', 'list']
|
||||||
|
})
|
||||||
|
md.renderer.rules.uml_diagram = md.renderer.rules.image
|
||||||
|
}, {
|
||||||
|
openMarker: conf.openMarker,
|
||||||
|
closeMarker: conf.closeMarker,
|
||||||
|
imageFormat: conf.imageFormat,
|
||||||
|
server: conf.server
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function encode64 (data) {
|
||||||
|
let r = ''
|
||||||
|
for (let i = 0; i < data.length; i += 3) {
|
||||||
|
if (i + 2 === data.length) {
|
||||||
|
r += append3bytes(data.charCodeAt(i), data.charCodeAt(i + 1), 0)
|
||||||
|
} else if (i + 1 === data.length) {
|
||||||
|
r += append3bytes(data.charCodeAt(i), 0, 0)
|
||||||
|
} else {
|
||||||
|
r += append3bytes(data.charCodeAt(i), data.charCodeAt(i + 1), data.charCodeAt(i + 2))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
function append3bytes (b1, b2, b3) {
|
||||||
|
const c1 = b1 >> 2
|
||||||
|
const c2 = ((b1 & 0x3) << 4) | (b2 >> 4)
|
||||||
|
const c3 = ((b2 & 0xF) << 2) | (b3 >> 6)
|
||||||
|
const c4 = b3 & 0x3F
|
||||||
|
let r = ''
|
||||||
|
r += encode6bit(c1 & 0x3F)
|
||||||
|
r += encode6bit(c2 & 0x3F)
|
||||||
|
r += encode6bit(c3 & 0x3F)
|
||||||
|
r += encode6bit(c4 & 0x3F)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
function encode6bit (raw) {
|
||||||
|
let b = raw
|
||||||
|
if (b < 10) {
|
||||||
|
return String.fromCharCode(48 + b)
|
||||||
|
}
|
||||||
|
b -= 10
|
||||||
|
if (b < 26) {
|
||||||
|
return String.fromCharCode(65 + b)
|
||||||
|
}
|
||||||
|
b -= 26
|
||||||
|
if (b < 26) {
|
||||||
|
return String.fromCharCode(97 + b)
|
||||||
|
}
|
||||||
|
b -= 26
|
||||||
|
if (b === 0) {
|
||||||
|
return '-'
|
||||||
|
}
|
||||||
|
if (b === 1) {
|
||||||
|
return '_'
|
||||||
|
}
|
||||||
|
return '?'
|
||||||
|
}
|
Loading…
Reference in new issue