From 4311c749ba884b3dea3976bf7f9c8b4ebe8ef5fc Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Thu, 26 May 2022 17:17:39 +0530 Subject: [PATCH] fix: allow disabling `markdown-it-attrs` --- docs/config/app-configs.md | 1 + src/node/markdown/markdown.ts | 19 ++++++++++------- src/node/markdown/plugins/highlightLines.ts | 23 +++++++++++++++++---- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/docs/config/app-configs.md b/docs/config/app-configs.md index b1cf0d8f..bbffbb76 100644 --- a/docs/config/app-configs.md +++ b/docs/config/app-configs.md @@ -119,6 +119,7 @@ interface MarkdownOptions extends MarkdownIt.Options { leftDelimiter?: string rightDelimiter?: string allowedAttributes?: string[] + disable?: boolean } // markdown-it-table-of-contents cplugin options diff --git a/src/node/markdown/markdown.ts b/src/node/markdown/markdown.ts index b4f2d226..e343ed0c 100644 --- a/src/node/markdown/markdown.ts +++ b/src/node/markdown/markdown.ts @@ -29,6 +29,7 @@ export interface MarkdownOptions extends MarkdownIt.Options { leftDelimiter?: string rightDelimiter?: string allowedAttributes?: string[] + disable?: boolean } theme?: Theme // https://github.com/Oktavilla/markdown-it-table-of-contents @@ -80,13 +81,17 @@ export const createMarkdownRenderer = async ( }, base ) - // 3rd party plugins - .use(attrs, options.attrs) - .use(anchor, { - slugify, - permalink: anchor.permalink.ariaHidden({}), - ...options.anchor - }) + + // 3rd party plugins + if (!options.attrs?.disable) { + md.use(attrs, options.attrs) + } + + md.use(anchor, { + slugify, + permalink: anchor.permalink.ariaHidden({}), + ...options.anchor + }) .use(toc, { slugify, includeLevel: [2, 3], diff --git a/src/node/markdown/plugins/highlightLines.ts b/src/node/markdown/plugins/highlightLines.ts index 28b18393..5938f8b5 100644 --- a/src/node/markdown/plugins/highlightLines.ts +++ b/src/node/markdown/plugins/highlightLines.ts @@ -1,6 +1,7 @@ // 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) => {
@@ -12,13 +13,27 @@ export const highlightLinePlugin = (md: MarkdownIt) => {
     // 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]
+    let lines = null
     if (!attr) {
-      return fence(...args)
+      // markdown-it-attrs maybe disabled
+
+      const rawInfo = token.info
+      if (!rawInfo || !RE.test(rawInfo)) {
+        return fence(...args)
+      }
+
+      const langName = rawInfo.replace(RE, '').trim()
+      // ensure the next plugin get the correct lang.
+      token.info = langName
+
+      lines = RE.exec(rawInfo)![1]
     }
 
-    const lines = attr[0]
-    if (!lines || !/[\d,-]+/.test(lines)) {
-      return fence(...args)
+    if (!lines) {
+      lines = attr![0]
+      if (!lines || !/[\d,-]+/.test(lines)) {
+        return fence(...args)
+      }
     }
 
     const lineNumbers = lines