Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(plugin-md-power): optimize icon rule #407

Merged
merged 1 commit into from
Dec 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions plugins/plugin-md-power/src/node/inline/icons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import type { PluginWithOptions } from 'markdown-it'
import type { RuleInline } from 'markdown-it/lib/parser_inline.mjs'
import type { IconsOptions } from '../../shared/index.js'

const [openTag, endTag] = [':[', ']:']

export const iconsPlugin: PluginWithOptions<IconsOptions> = (md, options = {}) =>
md.inline.ruler.before('emphasis', 'iconify', createTokenizer(options))

Expand All @@ -19,7 +17,15 @@ function createTokenizer(options: IconsOptions): RuleInline {
const max = state.posMax
const start = state.pos

if (state.src.slice(start, start + 2) !== openTag)
// :[
if (state.src.charCodeAt(start) !== 0x3A
|| state.src.charCodeAt(start + 1) !== 0x5B) {
return false
}

// :[ xxx
// ^
if (state.src.charCodeAt(start + 2) === 0x20)
return false

if (silent)
Expand All @@ -32,45 +38,45 @@ function createTokenizer(options: IconsOptions): RuleInline {
state.pos = start + 2

while (state.pos < max) {
if (state.src.slice(state.pos, state.pos + 2) === endTag) {
// ]:
if (state.src.charCodeAt(state.pos) === 0x5D
&& state.src.charCodeAt(state.pos + 1) === 0x3A) {
found = true
break
}

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)

// 不允许前后带有空格
if (/^\s|\s$/.test(content)) {
state.pos = start
return false
}

// found!
state.posMax = state.pos
state.pos = start + 2

const [name, opt = ''] = content.split(/\s+/)
const [size, color = options.color] = opt.split('/')
const [name, opt = ''] = content.split(' ')
const [size, color = options.color] = opt.trim().split('/')

const icon = state.push('vp_iconify_open', 'VPIcon', 1)
icon.markup = openTag
const icon = state.push('vp_icon_open', 'VPIcon', 1)
icon.markup = ':['
icon.attrs = [['name', name]]

icon.attrSet('name', name)
if (size || options.size)
icon.attrSet('size', String(size || options.size))
icon.attrs.push(['size', String(size || options.size)])
if (color)
icon.attrSet('color', color)
icon.attrs.push(['color', color])

const close = state.push('vp_iconify_close', 'VPIcon', -1)
close.markup = endTag
const close = state.push('vp_icon_close', 'VPIcon', -1)
close.markup = ']:'

state.pos = state.posMax + 2
state.posMax = max
Expand Down
Loading