Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Pass a stringified JSON object (must be `JSON.parse()`-able) as an option for in
| wrapperLabel | `undefined` | string | `aria-label` on element wrapping toc lists |
| ul | `false` | boolean | lists are `ul` if true, `ol` if `false` |
| flat | `false` | boolean | use flat list if `true`; use nested lists if false |
| extractText | `undefined` | function | A function that can be used to extract the text from the heading (if custom processing is needed). It receives the cheerio header element as argument |

## Roadmap

Expand Down
25 changes: 13 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/BuildTOC.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const defaults = {
wrapperLabel: undefined,
ul: false,
flat: false,
extractText: undefined,
}

const BuildTOC = (text, opts) => {
Expand All @@ -21,7 +22,7 @@ const BuildTOC = (text, opts) => {

const $ = cheerio.load(text)

const headings = NestHeadings(tags, $)
const headings = NestHeadings(tags, opts.extractText, $)

if (headings.length === 0) {
return undefined
Expand Down
4 changes: 2 additions & 2 deletions src/NestHeadings.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const SimplifyResults = require('./SimplifyResults')

const NestHeadings = (tags, $) => {
const NestHeadings = (tags, extractText, $) => {
const temp = {}

tags.forEach(t => {
temp[t] = SimplifyResults(t, tags, $)
temp[t] = SimplifyResults(t, tags, extractText, $)
})

const headings = []
Expand Down
8 changes: 6 additions & 2 deletions src/SimplifyResults.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
const SimplifyResults = (tag, tags, $) => {
const SimplifyResults = (tag, tags, extractText, $) => {
const results = []

$(`${tag}[id]`).each((i, el) => {
const tag = el.name
const id = $(el).attr('id')
const text = $(el).text().replace(' #', '')
let baseText = $(el).text()
if (typeof extractText === 'function') {
baseText = extractText($(el))
}
let text = baseText.replace(' #', '')
const hierarchy = tags.indexOf(tag)
const parent =
hierarchy > 0 &&
Expand Down