handle custom slugify

fix/4605
Divyansh Singh 6 months ago
parent 0cabe664c6
commit 8078a9b370

@ -242,7 +242,8 @@ export async function createMarkdownRenderer(
.use( .use(
linkPlugin, linkPlugin,
{ target: '_blank', rel: 'noreferrer', ...options.externalLinks }, { target: '_blank', rel: 'noreferrer', ...options.externalLinks },
base base,
options.anchor?.slugify ?? slugify
) )
.use(lineNumberPlugin, options.lineNumbers) .use(lineNumberPlugin, options.lineNumbers)

@ -16,7 +16,8 @@ const indexRE = /(^|.*\/)index.md(#?.*)$/i
export const linkPlugin = ( export const linkPlugin = (
md: MarkdownItAsync, md: MarkdownItAsync,
externalAttrs: Record<string, string>, externalAttrs: Record<string, string>,
base: string base: string,
slugify: (str: string) => string
) => { ) => {
md.renderer.rules.link_open = ( md.renderer.rules.link_open = (
tokens, tokens,
@ -27,9 +28,12 @@ export const linkPlugin = (
) => { ) => {
const token = tokens[idx] const token = tokens[idx]
const hrefIndex = token.attrIndex('href') const hrefIndex = token.attrIndex('href')
const targetIndex = token.attrIndex('target') if (
const downloadIndex = token.attrIndex('download') hrefIndex >= 0 &&
if (hrefIndex >= 0 && targetIndex < 0 && downloadIndex < 0) { token.attrIndex('target') < 0 &&
token.attrIndex('download') < 0 &&
token.attrGet('class') !== 'header-anchor' // header anchors are already normalized
) {
const hrefAttr = token.attrs![hrefIndex] const hrefAttr = token.attrs![hrefIndex]
const url = hrefAttr[1] const url = hrefAttr[1]
if (isExternal(url)) { if (isExternal(url)) {
@ -54,9 +58,7 @@ export const linkPlugin = (
) { ) {
normalizeHref(hrefAttr, env) normalizeHref(hrefAttr, env)
} else if (url.startsWith('#')) { } else if (url.startsWith('#')) {
hrefAttr[1] = decodeURI(hrefAttr[1]) hrefAttr[1] = decodeURI(normalizeHash(hrefAttr[1]))
.normalize('NFKD')
.replace(/[\u0300-\u036F]/g, '')
} }
// append base to internal (non-relative) urls // append base to internal (non-relative) urls
@ -74,7 +76,7 @@ export const linkPlugin = (
const indexMatch = url.match(indexRE) const indexMatch = url.match(indexRE)
if (indexMatch) { if (indexMatch) {
const [, path, hash] = indexMatch const [, path, hash] = indexMatch
url = path + hash.normalize('NFKD').replace(/[\u0300-\u036F]/g, '') url = path + normalizeHash(hash)
} else { } else {
let cleanUrl = url.replace(/[?#].*$/, '') let cleanUrl = url.replace(/[?#].*$/, '')
// transform foo.md -> foo[.html] // transform foo.md -> foo[.html]
@ -90,10 +92,7 @@ export const linkPlugin = (
cleanUrl += '.html' cleanUrl += '.html'
} }
const parsed = new URL(url, 'http://a.com') const parsed = new URL(url, 'http://a.com')
url = url = cleanUrl + parsed.search + normalizeHash(parsed.hash)
cleanUrl +
parsed.search +
parsed.hash.normalize('NFKD').replace(/[\u0300-\u036F]/g, '')
} }
// ensure leading . for relative paths // ensure leading . for relative paths
@ -108,6 +107,10 @@ export const linkPlugin = (
hrefAttr[1] = decodeURI(url) hrefAttr[1] = decodeURI(url)
} }
function normalizeHash(str: string) {
return str ? encodeURI('#' + slugify(decodeURI(str).slice(1))) : ''
}
function pushLink(link: string, env: MarkdownEnv) { function pushLink(link: string, env: MarkdownEnv) {
const links = env.links || (env.links = []) const links = env.links || (env.links = [])
links.push(link) links.push(link)

Loading…
Cancel
Save