Skip to content

Commit

Permalink
lint & document options
Browse files Browse the repository at this point in the history
  • Loading branch information
jdsteinbach committed Sep 28, 2021
1 parent 030accc commit 9b07a95
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 40 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ Pass a stringified JSON object (must be `JSON.parse()`-able) as an option for in
</aside>
```

## Options

| Name | Default Value | Type | Purpose |
| --- | --- | --- | --- |
| tags | `['h2', 'h3', 'h4']` | array of strings | which heading tags are used to generate the table of contents |
| wrapper | `'nav'` | string | tag of element wrapping toc lists; `''` removes wrapper element |
| wrapperClass | `'toc'` | string | `class` on element wrapping toc lists |
| 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 |

## Roadmap

- [ ] Some tests would be nice
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": ".eleventy.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint src/**"
"lint": "eslint src/**",
"lint:fix": "eslint src/** --fix"
},
"repository": {
"type": "git",
Expand Down
78 changes: 39 additions & 39 deletions src/BuildList.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
// Replace list copied from https://css-tricks.com/snippets/javascript/htmlentities-for-javascript/
const _escText = text => {
return String(text)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
}

const _buildLink = ({id, text, children}, ul, flat) => {
let nestedList = ''

if (children.length > 0 && flat) {
nestedList = children.map(c => _buildLink(c, ul, flat))
} else if (children.length > 0) {
nestedList = BuildList(children, ul, flat)
}

if (id && text && flat) {
return `<li><a href="#${id}">${_escText(text)}</a></li>${(
nestedList || []
).join('')}`
} else if (id && text) {
return `<li><a href="#${id}">${_escText(text)}</a>${nestedList}</li>`
} else {
return nestedList
}
}

const BuildList = (listItems, ul, flat) => {
const listType = ul ? 'ul' : 'ol'
const list = listItems
.sort((a, b) => a.order - b.order)
.map(li => _buildLink(li, ul, flat))

return list.length > 0 ? `<${listType}>${list.join('')}</${listType}>` : ''
}

module.exports = BuildList
// Replace list copied from https://css-tricks.com/snippets/javascript/htmlentities-for-javascript/
const _escText = text => {
return String(text)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
}

const _buildLink = ({id, text, children}, ul, flat) => {
let nestedList = ''

if (children.length > 0 && flat) {
nestedList = children.map(c => _buildLink(c, ul, flat))
} else if (children.length > 0) {
nestedList = BuildList(children, ul, flat)
}

if (id && text && flat) {
return `<li><a href="#${id}">${_escText(text)}</a></li>${(
nestedList || []
).join('')}`
} else if (id && text) {
return `<li><a href="#${id}">${_escText(text)}</a>${nestedList}</li>`
} else {
return nestedList
}
}

const BuildList = (listItems, ul, flat) => {
const listType = ul ? 'ul' : 'ol'
const list = listItems
.sort((a, b) => a.order - b.order)
.map(li => _buildLink(li, ul, flat))

return list.length > 0 ? `<${listType}>${list.join('')}</${listType}>` : ''
}

module.exports = BuildList

0 comments on commit 9b07a95

Please sign in to comment.