// @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 🍍'
leftDelimiter: '%{',
rightDelimiter: '}%',
},
+ anchor: {
+ permalink: renderPermaLink,
+ },
+ config: (md) => {
+ md.use(MarkDownItCustomAnchor)
+ }
},
locales: locales.vitepressConfig,
--- /dev/null
+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
--- /dev/null
+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