Fork of Chiri for Astro for my blog
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: toc slug

the3ash 0bba6d97 4b138434

+35 -1
+35 -1
src/plugins/remark-toc.mjs
··· 4 4 return function (tree, file) { 5 5 const headings = [] 6 6 let headingIndex = 0 7 + const usedSlugs = new Set() 7 8 8 9 // Extract headings from AST 9 10 visit(tree, 'heading', (node) => { ··· 21 22 const text = extractTextContent(node) 22 23 if (!text) return 23 24 24 - const id = `heading-${headingIndex}` 25 + // Generate unique slug from text 26 + const slug = generateUniqueSlug(text, usedSlugs) 27 + const id = slug 25 28 26 29 if (!node.data) node.data = {} 27 30 if (!node.data.hProperties) node.data.hProperties = {} ··· 53 56 54 57 return text.trim() 55 58 } 59 + 60 + // Generate a slug from text 61 + function generateSlug(text) { 62 + return ( 63 + text 64 + .toLowerCase() 65 + // Keep Chinese characters, English letters, numbers, spaces and hyphens 66 + .replace(/[^\u4e00-\u9fa5a-z0-9\s-]/g, '') 67 + // Replace spaces with hyphens 68 + .replace(/\s+/g, '-') 69 + // Replace multiple hyphens with single hyphen 70 + .replace(/-+/g, '-') 71 + // Remove leading and trailing hyphens 72 + .replace(/^-|-$/g, '') 73 + ) 74 + } 75 + 76 + // Generate a unique slug from text 77 + function generateUniqueSlug(text, usedSlugs) { 78 + let slug = generateSlug(text) 79 + let counter = 1 80 + let uniqueSlug = slug 81 + 82 + while (usedSlugs.has(uniqueSlug)) { 83 + uniqueSlug = `${slug}-${counter}` 84 + counter++ 85 + } 86 + 87 + usedSlugs.add(uniqueSlug) 88 + return uniqueSlug 89 + }