mirror of
https://github.com/pengzhanbo/vuepress-theme-plume.git
synced 2026-04-23 10:58:13 +08:00
perf(plugin-md-power): optimize plot rule (#408)
This commit is contained in:
parent
463bbd3fed
commit
5b85cc91a8
@ -4,8 +4,6 @@
|
||||
import type { PluginWithOptions } from 'markdown-it'
|
||||
import type { RuleInline } from 'markdown-it/lib/parser_inline.mjs'
|
||||
|
||||
const [openTag, endTag] = ['!!', '!!']
|
||||
|
||||
export const plotPlugin: PluginWithOptions<never> = md =>
|
||||
md.inline.ruler.before('emphasis', 'plot', createTokenizer())
|
||||
|
||||
@ -15,9 +13,21 @@ function createTokenizer(): RuleInline {
|
||||
const max = state.posMax
|
||||
const start = state.pos
|
||||
|
||||
if (state.src.slice(start, start + 2) !== openTag)
|
||||
if (
|
||||
state.src.charCodeAt(start) !== 0x21
|
||||
|| state.src.charCodeAt(start + 1) !== 0x21
|
||||
) {
|
||||
return false
|
||||
}
|
||||
|
||||
const next = state.src.charCodeAt(start + 2)
|
||||
|
||||
// - !! xxx | !!!xxx
|
||||
// ^ | ^
|
||||
if (next === 0x20 || next === 0x21)
|
||||
return false
|
||||
|
||||
/* istanbul ignore if -- @preserve */
|
||||
if (silent)
|
||||
return false
|
||||
|
||||
@ -28,7 +38,8 @@ function createTokenizer(): RuleInline {
|
||||
state.pos = start + 2
|
||||
|
||||
while (state.pos < max) {
|
||||
if (state.src.slice(state.pos - 1, state.pos + 1) === endTag) {
|
||||
if (state.src.charCodeAt(state.pos) === 0x21
|
||||
&& state.src.charCodeAt(state.pos + 1) === 0x21) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
@ -36,31 +47,31 @@ function createTokenizer(): RuleInline {
|
||||
state.md.inline.skipToken(state)
|
||||
}
|
||||
|
||||
if (!found || start + 2 === state.pos) {
|
||||
if (
|
||||
!found
|
||||
|| start + 2 === state.pos
|
||||
// - !!xxx !!
|
||||
// ^
|
||||
|| state.src.charCodeAt(state.pos - 1) === 0x20
|
||||
) {
|
||||
state.pos = start
|
||||
|
||||
return false
|
||||
}
|
||||
const content = state.src.slice(start + 2, state.pos - 1)
|
||||
|
||||
// 不允许前后带有空格
|
||||
if (/^\s|\s$/.test(content)) {
|
||||
state.pos = start
|
||||
return false
|
||||
}
|
||||
const content = state.src.slice(start + 2, state.pos)
|
||||
|
||||
// found!
|
||||
state.posMax = state.pos - 1
|
||||
state.posMax = state.pos
|
||||
state.pos = start + 2
|
||||
|
||||
const open = state.push('plot_open', 'Plot', 1)
|
||||
open.markup = openTag
|
||||
open.markup = '!!'
|
||||
|
||||
const text = state.push('text', '', 0)
|
||||
text.content = content
|
||||
|
||||
const close = state.push('plot_close', 'Plot', -1)
|
||||
close.markup = endTag
|
||||
close.markup = '!!'
|
||||
|
||||
state.pos = state.posMax + 2
|
||||
state.posMax = max
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user