Skip to content

Commit

Permalink
fix: fix code line hightlighting
Browse files Browse the repository at this point in the history
close #408
  • Loading branch information
yyx990803 committed Oct 7, 2021
1 parent 383d8ff commit 4c042b6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
8 changes: 1 addition & 7 deletions src/node/markdown/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,8 @@ export const createMarkdownRenderer = (
rel: 'noopener noreferrer',
...options.externalLinks
})

.use(attrs, {
leftDelimiter: '{',
rightDelimiter: '}',
allowedAttributes: [],
...options.attrs
})
// 3rd party plugins
.use(attrs, options.attrs)
.use(anchor, {
slugify,
permalink: anchor.permalink.ariaHidden({}),
Expand Down
18 changes: 10 additions & 8 deletions src/node/markdown/plugins/highlightLines.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Modified from https://github.com/egoist/markdown-it-highlight-lines
import MarkdownIt from 'markdown-it'

const RE = /{([\d,-]+)}/
const wrapperRE = /^<pre .*?><code>/

export const highlightLinePlugin = (md: MarkdownIt) => {
Expand All @@ -10,21 +9,24 @@ export const highlightLinePlugin = (md: MarkdownIt) => {
const [tokens, idx, options] = args
const token = tokens[idx]

const rawInfo = token.info
if (!rawInfo || !RE.test(rawInfo)) {
// due to use of markdown-it-attrs, the {0} syntax would have been converted
// to attrs on the token
const attr = token.attrs && token.attrs[0]
if (!attr) {
return fence(...args)
}

const langName = rawInfo.replace(RE, '').trim()
// ensure the next plugin get the correct lang.
token.info = langName
const lines = attr[0]
if (!lines || !/[\d,-]+/.test(lines)) {
return fence(...args)
}

const lineNumbers = RE.exec(rawInfo)![1]
const lineNumbers = lines
.split(',')
.map((v) => v.split('-').map((v) => parseInt(v, 10)))

const code = options.highlight
? options.highlight(token.content, langName, '')
? options.highlight(token.content, token.info, '')
: token.content

const rawCode = code.replace(wrapperRE, '')
Expand Down

0 comments on commit 4c042b6

Please sign in to comment.