-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from nojronatron/staging
Pub Pre-release v0.4.0 rc1
- Loading branch information
Showing
15 changed files
with
613 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"editor.fontFamily": "Consolas, 'Courier New', monospace", | ||
"editor.fontSize": 18, | ||
"editor.lineHeight": 1.4 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,40 @@ | ||
const { getTitleOnly, | ||
getLoweredKebabCase, | ||
getLinkFragment } = require('./process-headings'); | ||
|
||
/** | ||
* Generates a table of contents (TOC) based on the captured level 2 headings. | ||
* | ||
* @param {Array<{line, text, isHash}>} capturedL2Headings - An array of captured level 2 headings. | ||
* @returns {string} The generated table of contents. | ||
*/ | ||
module.exports = function createTOC(capturedL2Headings) { | ||
let tableOfContentsHeading = 'Table of Contents\n'; | ||
let tableOfContentsString = ''; | ||
let tableOfContentsHeading = 'Table of Contents'; | ||
let tocString = ''; | ||
let tocPrefix = ''; | ||
let tocSuffix = ''; | ||
|
||
if (capturedL2Headings[0].isHash) { | ||
tableOfContentsString = `## ${tableOfContentsHeading}\n`; | ||
tocPrefix = `## `; | ||
|
||
if (capturedL2Headings[0].isClosedAtx) { | ||
tocSuffix = ` ##`; | ||
} | ||
} else { | ||
tableOfContentsString = `${tableOfContentsHeading}-----------------\n\n`; | ||
tocSuffix = `\n-----------------`; | ||
} | ||
|
||
|
||
tocString = `${tocPrefix}${tableOfContentsHeading}${tocSuffix}\n\n`; | ||
|
||
capturedL2Headings.forEach((item) => { | ||
// Extract the level2 heading text, removing | ||
// characters not allowed in link fragments | ||
const titleOnly = item.text.replaceAll(/(?:[!@$%^&*\(\)\[\]\{\}\:';\.,~`+=\\"\|\/?])/g, '') | ||
.trim(); | ||
// Convert the heading text to kebab case | ||
const loweredKebabCase = item.text.toLowerCase() | ||
.replace(/\s/g, '-'); | ||
// compose the link fragement title and anchor element | ||
const linkFragment = `- [${titleOnly}](#${loweredKebabCase})\n`; | ||
// append the link fragment to the table of contents string | ||
tableOfContentsString += linkFragment; | ||
// call external modules to do this work | ||
const titleOnly = getTitleOnly(item.text); | ||
const loweredKebabCase = getLoweredKebabCase(titleOnly); | ||
const linkFragment = getLinkFragment(titleOnly, loweredKebabCase); | ||
tocString += linkFragment; | ||
}); | ||
|
||
// be kind, leave a blank line after the table of contents | ||
tableOfContentsString += '\n'; | ||
return tableOfContentsString; | ||
tocString += '\n'; | ||
return tocString; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.