1
0
mirror of synced 2025-12-23 03:44:00 -05:00

minimize content in miniToc prop (#28522)

* make miniToc pure data and no html strings

* fixups

* minimize content in miniToc prop

* minimize content in miniToc prop

* some types refactoring

* fix tests
This commit is contained in:
Peter Bengtsson
2022-06-15 17:04:54 -04:00
committed by GitHub
parent 8f83f60766
commit 2f37efd633
3 changed files with 16 additions and 9 deletions

View File

@@ -1,12 +1,7 @@
import cheerio from 'cheerio'
import { range } from 'lodash-es'
export default function getMiniTocItems(
html,
maxHeadingLevel = 2,
headingScope = '',
isRestPage = false
) {
export default function getMiniTocItems(html, maxHeadingLevel = 2, headingScope = '') {
const $ = cheerio.load(html, { xmlMode: true })
// eg `h2, h3` or `h2, h3, h4` depending on maxHeadingLevel
@@ -67,7 +62,7 @@ export default function getMiniTocItems(
// convert the flatToc to a nested structure to simplify semantic rendering on the client
const nestedToc = buildNestedToc(flatToc)
return nestedToc
return minimalMiniToc(nestedToc)
}
// Recursively build a tree from the list of allItems
@@ -116,3 +111,15 @@ function buildNestedToc(allItems, startIndex = 0) {
return currentLevel
}
// Strip the bits and pieces from each object in the array that are
// not needed in the React component rendering.
function minimalMiniToc(toc) {
return toc.map(({ platform, contents, items }) => {
const minimal = { contents }
const subItems = minimalMiniToc(items)
if (subItems.length) minimal.items = subItems
if (platform) minimal.platform = platform
return minimal
})
}