From 3897718bfce55e1394c44890eed83db9acb5eb85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Hennequin?= Date: Tue, 24 Sep 2024 15:11:59 +0200 Subject: [PATCH] refactor: create parent link before children --- src/toc.ts | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/toc.ts b/src/toc.ts index d9564b6..4e0f478 100644 --- a/src/toc.ts +++ b/src/toc.ts @@ -61,17 +61,17 @@ function slugify(heading: string): string * @param map - The map containing the elements. * @returns An array of numbers representing the children elements. */ -function getChildrens(parent: number, map: TMap): Array +function getChildren(parent: number, map: TMap): Array { - const childrens: Array = []; + const children: Array = []; for (const element of map) { if (element[1].parent != parent) { continue; } - childrens.push(element[0]); + children.push(element[0]); } - return childrens; + return children; } /** @@ -110,21 +110,20 @@ function getItem( elementsIgnored: Array ): string { - const childrensItems: Array = []; - const childrens: Array = getChildrens(element, map); + const elementLink: string = link(map.get(element)!, slugs); + const childrenItems: Array = []; + const children: Array = getChildren(element, map); - for (let index: number = 0; index < childrens.length; ++index) { - childrensItems.push( - getItem(childrens[index]!, map, slugs, elementsIgnored) + for (let index: number = 0; index < children.length; ++index) { + childrenItems.push( + getItem(children[index]!, map, slugs, elementsIgnored) ); } - elementsIgnored.push(...childrens); - if (childrensItems.length) { - return `
  • ${link(map.get(element)!, slugs)}
      ${ - childrensItems.join('') - }
  • `; + elementsIgnored.push(...children); + if (childrenItems.length) { + return `
  • ${elementLink}
      ${childrenItems.join('')}
  • `; } - return `
  • ${link(map.get(element)!, slugs)}
  • `; + return `
  • ${elementLink}
  • `; } /**