From 9b07a957e351959966989323577751306a581f89 Mon Sep 17 00:00:00 2001 From: James Steinbach Date: Tue, 28 Sep 2021 15:36:07 -0600 Subject: [PATCH] lint & document options --- README.md | 11 +++++++ package.json | 3 +- src/BuildList.js | 78 ++++++++++++++++++++++++------------------------ 3 files changed, 52 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 5905723..ffce90f 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,17 @@ Pass a stringified JSON object (must be `JSON.parse()`-able) as an option for in ``` +## 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 diff --git a/package.json b/package.json index f717416..09633ff 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/BuildList.js b/src/BuildList.js index 2223f2d..929797c 100644 --- a/src/BuildList.js +++ b/src/BuildList.js @@ -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, '&') - .replace(//g, '>') - .replace(/"/g, '"') -} - -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 `
  • ${_escText(text)}
  • ${( - nestedList || [] - ).join('')}` - } else if (id && text) { - return `
  • ${_escText(text)}${nestedList}
  • ` - } 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('')}` : '' -} - -module.exports = BuildList +// Replace list copied from https://css-tricks.com/snippets/javascript/htmlentities-for-javascript/ +const _escText = text => { + return String(text) + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') +} + +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 `
  • ${_escText(text)}
  • ${( + nestedList || [] + ).join('')}` + } else if (id && text) { + return `
  • ${_escText(text)}${nestedList}
  • ` + } 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('')}` : '' +} + +module.exports = BuildList