nested-toc-json-generator
v0.1.0
Published
This is currently tested against Hygraph data, but should work for most data if you follow the following structure:
Downloads
3
Readme
Gets a nested ToC data structure from heading data
This is currently tested against Hygraph data, but should work for most data if you follow the following structure:
[
{
"type": "heading-one", // or h1
"id": "some-id-here" // ID for jumplink
// ...additionalData
},
{
"type": "heading-two", // or h2
"id": "some-other-id-here" // ID for jumplink
// ...additionalData
}
]
Given that structure, it will return a nested structure for frontend loops and nested lists.
[
{
"type": "heading-one",
"id": "some-id-here"
// ...additionalData,
"tocChildren": [
{
"type": "heading-two",
"id": "some-other-id-here",
"children": [ [Object] ],
"tocChildren": [],
}
]
}
]
Installation
npm i nested-toc-json-generator
Usage
const generateJSON = require("nested-toc-json-generator")
const data = [
{
"type": "heading-one", // or h1
"id": "some-id-here" // ID for jumplink
// ...additionalData
},
{
"type": "heading-two", // or h2
"id": "some-other-id-here" // ID for jumplink
// ...additionalData
}
]
const headerJson = generateJSON(headerJson)