keep it easier to revert to older behavior and add heading level to wrapper

fix/2980
Divyansh Singh 5 months ago
parent 277688a104
commit 26d6255ed2

@ -122,7 +122,12 @@ export interface MarkdownOptions extends Options {
* Options for `markdown-it-anchor` * Options for `markdown-it-anchor`
* @see https://github.com/valeriangalliat/markdown-it-anchor * @see https://github.com/valeriangalliat/markdown-it-anchor
*/ */
anchor?: anchorPlugin.AnchorOptions anchor?: Omit<anchorPlugin.AnchorOptions, 'permalink'> & {
permalink?:
| anchorPlugin.PermalinkGenerator
| anchorPlugin.LinkAfterHeaderPermalinkOptions
| 'legacy'
}
/** /**
* Options for `markdown-it-attrs` * Options for `markdown-it-attrs`
* @see https://github.com/arve0/markdown-it-attrs * @see https://github.com/arve0/markdown-it-attrs
@ -267,21 +272,90 @@ export async function createMarkdownRenderer(
} }
md.use(emojiPlugin, { ...options.emoji }) md.use(emojiPlugin, { ...options.emoji })
const { permalink, ...anchorOptions } = options.anchor ?? {}
const linkAfterHeader = anchorPlugin.permalink.linkAfterHeader({
assistiveText: (title) => `Permalink to “${title.trim()}`,
visuallyHiddenClass: 'visually-hidden',
...(typeof permalink === 'object' ? permalink : {})
})
const linkInsideHeader = anchorPlugin.permalink.linkInsideHeader({
symbol: '&#8203;'
})
const permalinkV2: anchorPlugin.PermalinkGenerator = (
slug,
opts,
state,
idx
) => {
state.tokens.splice(
idx,
0,
Object.assign(new state.Token('div_open', 'div', 1), {
attrs: [['class', `header-wrapper ${state.tokens[idx].tag}`]],
block: true
})
)
state.tokens.splice(
idx + 4,
0,
Object.assign(new state.Token('div_close', 'div', -1), {
block: true
})
)
linkAfterHeader(slug, opts, state, idx + 1)
}
const permalinkV1: anchorPlugin.PermalinkGenerator = (
slug,
opts,
state,
idx
) => {
const title =
state.tokens[idx + 1]?.children
?.reduce(
(acc, t) =>
t.type === 'text' || t.type === 'code_inline'
? acc + t.content
: acc,
''
)
.trim() || ''
linkInsideHeader(slug, opts, state, idx)
state.tokens[idx + 1].children
?.find(
(t) =>
t.type === 'link_open' &&
t
.attrGet('class')
?.split(' ')
.includes(opts.class || 'header-anchor')
)
?.attrPush(['aria-label', `Permalink to “${title}`])
}
// mdit-vue plugins // mdit-vue plugins
md.use(anchorPlugin, { md.use(anchorPlugin, {
slugify, slugify,
getTokensText: (tokens) => { getTokensText: (tokens) =>
return tokens tokens
.filter((t) => !['html_inline', 'emoji'].includes(t.type)) .filter((t) => !['html_inline', 'emoji'].includes(t.type))
.map((t) => t.content) .map((t) => t.content)
.join('') .join(''),
}, permalink:
permalink: anchorPlugin.permalink.linkAfterHeader({ typeof permalink === 'function'
assistiveText: (title) => `Permalink to “${title.trim()}`, ? permalink
visuallyHiddenClass: 'visually-hidden', : permalink === 'legacy'
wrapper: ['<div class="header-wrapper">', '</div>'] ? permalinkV1
}), : permalinkV2,
...options.anchor ...anchorOptions
} as anchorPlugin.AnchorOptions).use(frontmatterPlugin, { } as anchorPlugin.AnchorOptions).use(frontmatterPlugin, {
...options.frontmatter ...options.frontmatter
} as FrontmatterPluginOptions) } as FrontmatterPluginOptions)

Loading…
Cancel
Save