Skip to content

Commit

Permalink
fix: remove duplicated content in search results
Browse files Browse the repository at this point in the history
  • Loading branch information
abernier committed Aug 24, 2024
1 parent 3e82c95 commit 3d25f6e
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/components/mdx/Toc/rehypeToc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ const slugify = (title: string) => title.toLowerCase().replace(/\s+|-+/g, '-')
/**
* Generates a table of contents from page headings.
*/

const isHeading = (node: Node) => /^h[1-6]$/.test(node.tagName)

export const rehypeToc = (target: DocToC[] = [], url: string, page: string) => {
return () => (root: Node) => {
const previous: Record<number, DocToC> = {}

for (let i = 0; i < root.children.length; i++) {
const node = root.children[i]

if (node.type === 'element' && /^h[1-4]$/.test(node.tagName)) {
if (isHeading(node)) {
const level = parseInt(node.tagName[1])

const title = toString(node)
Expand All @@ -45,9 +48,9 @@ export const rehypeToc = (target: DocToC[] = [], url: string, page: string) => {

let siblingIndex = i + 1
const content: string[] = []
let sibling: Node | undefined = root.children[siblingIndex]
let sibling: Node = root.children[siblingIndex]
while (sibling) {
if (RegExp(`^h${level}$`).test(sibling.tagName)) break // stop at the next (same-level) heading
if (isHeading(sibling)) break // stop at the next heading

content.push(toString(sibling))
sibling = root.children[siblingIndex++]
Expand Down

0 comments on commit 3d25f6e

Please sign in to comment.