]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
docs: remove anchor tag by markdown plugins. (#1734)
author沈青川 <46062972+ShenQingchuan@users.noreply.github.com>
Thu, 20 Oct 2022 12:19:52 +0000 (20:19 +0800)
committerGitHub <noreply@github.com>
Thu, 20 Oct 2022 12:19:52 +0000 (14:19 +0200)
packages/docs/.vitepress/config.js
packages/docs/.vitepress/markdown-it-custom-anchor/index.js [new file with mode: 0644]
packages/docs/.vitepress/render-perma-link/index.js [new file with mode: 0644]

index b5ba63d09b4dc7a7f2a0a558253aa45207ab2165..462ca7dd56170d557c27efb05e50646a04d98212 100644 (file)
@@ -1,5 +1,7 @@
 // @ts-check
 import locales from './locales'
+import renderPermaLink from './render-perma-link'
+import MarkDownItCustomAnchor from './markdown-it-custom-anchor'
 
 const META_URL = 'https://pinia.vuejs.org'
 const META_TITLE = 'Pinia 🍍'
@@ -32,6 +34,12 @@ module.exports = {
       leftDelimiter: '%{',
       rightDelimiter: '}%',
     },
+    anchor: {
+      permalink: renderPermaLink,
+    },
+    config: (md) => {
+      md.use(MarkDownItCustomAnchor)
+    }
   },
   locales: locales.vitepressConfig,
 
diff --git a/packages/docs/.vitepress/markdown-it-custom-anchor/index.js b/packages/docs/.vitepress/markdown-it-custom-anchor/index.js
new file mode 100644 (file)
index 0000000..b18b39c
--- /dev/null
@@ -0,0 +1,25 @@
+const anchorMatch = /^.+(\s*\{#([a-z0-9\-_]+?)\}\s*)$/;
+
+const removeAnchorFromTitle = (oldTitle) => {
+  const match = anchorMatch.exec(oldTitle);
+  return match ? oldTitle.replace(match[1], '').trim() : oldTitle;
+}
+
+export default function(md) {
+  const oldTitle = md.renderer.rules.text;
+  md.renderer.rules.text = (tokens, idx, options, env, slf) => {
+    const titleAndId = oldTitle(tokens, idx, options, env, slf);
+    return removeAnchorFromTitle(titleAndId);
+  };
+
+  const oldHeading = md.renderer.rules.heading_open;
+  md.renderer.rules.heading_open = (tokens, idx, options, env, slf) => {
+    const head = oldHeading(tokens, idx, options, env, slf);
+    const data = md.__data;
+    const headers = data.headers || (data.headers = []);
+    headers.forEach(element => {
+      element.title = removeAnchorFromTitle(element.title);
+    });
+    return head;
+  }
+};
\ No newline at end of file
diff --git a/packages/docs/.vitepress/render-perma-link/index.js b/packages/docs/.vitepress/render-perma-link/index.js
new file mode 100644 (file)
index 0000000..a130180
--- /dev/null
@@ -0,0 +1,37 @@
+const position = {
+  false: 'push',
+  true: 'unshift'
+}
+
+const renderPermalink = (slug, opts, state, permalink) => {
+  try {
+    const tokens = state.tokens
+    const token = tokens[permalink]
+    const title = tokens[permalink + 1]
+      .children
+      .filter(token => token.type === 'text' || token.type === 'code_inline')
+      .reduce((acc, t) => acc + t.content, '')
+    const match = /^.+(\s*\{#([a-z0-9\-_]+?)\}\s*)$/.exec(title);
+    slug = match ? match[2] : slug;
+    token.attrSet('id', slug)
+    const space = () => Object.assign(new state.Token('text', '', 0), { content: ' ' })
+
+    const linkTokens = [
+      Object.assign(new state.Token('link_open', 'a', 1), {
+        attrs: [
+          ...(opts.permalinkClass ? [['class', opts.permalinkClass]] : []),
+          ['href', opts.permalinkHref(slug, state)],
+          ...Object.entries(opts.permalinkAttrs(slug, state))
+        ]
+      }),
+      Object.assign(new state.Token('html_block', '', 0), { content: opts.permalinkSymbol }),
+      new state.Token('link_close', 'a', -1)
+    ]
+    if (opts.permalinkSpace) {
+      linkTokens[position[!opts.permalinkBefore]](space())
+    }
+    state.tokens[permalink + 1].children[position[opts.permalinkBefore]](...linkTokens)
+  } catch(e) {}
+}
+
+export default renderPermalink