From: 沈青川 <46062972+ShenQingchuan@users.noreply.github.com> Date: Thu, 20 Oct 2022 12:19:52 +0000 (+0800) Subject: docs: remove anchor tag by markdown plugins. (#1734) X-Git-Tag: @pinia/nuxt@0.4.4~18 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=111e7547ac2194437c6bdcb5dcc6d5dd680e5049;p=thirdparty%2Fvuejs%2Fpinia.git docs: remove anchor tag by markdown plugins. (#1734) --- diff --git a/packages/docs/.vitepress/config.js b/packages/docs/.vitepress/config.js index b5ba63d0..462ca7dd 100644 --- a/packages/docs/.vitepress/config.js +++ b/packages/docs/.vitepress/config.js @@ -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 index 00000000..b18b39c6 --- /dev/null +++ b/packages/docs/.vitepress/markdown-it-custom-anchor/index.js @@ -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 index 00000000..a1301803 --- /dev/null +++ b/packages/docs/.vitepress/render-perma-link/index.js @@ -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