From 4c042b61e7beb70d0a0b77cc9a00d725c7863089 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 7 Oct 2021 18:55:36 -0400 Subject: [PATCH] fix: fix code line hightlighting close #408 --- src/node/markdown/markdown.ts | 8 +------- src/node/markdown/plugins/highlightLines.ts | 18 ++++++++++-------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/node/markdown/markdown.ts b/src/node/markdown/markdown.ts index 35081b47..3338b240 100644 --- a/src/node/markdown/markdown.ts +++ b/src/node/markdown/markdown.ts @@ -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({}), diff --git a/src/node/markdown/plugins/highlightLines.ts b/src/node/markdown/plugins/highlightLines.ts index a99c4b0d..28b18393 100644 --- a/src/node/markdown/plugins/highlightLines.ts +++ b/src/node/markdown/plugins/highlightLines.ts @@ -1,7 +1,6 @@ // Modified from https://github.com/egoist/markdown-it-highlight-lines import MarkdownIt from 'markdown-it' -const RE = /{([\d,-]+)}/ const wrapperRE = /^
/
 
 export const highlightLinePlugin = (md: MarkdownIt) => {
@@ -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, '')