-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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 #2959 from sass/auto-tocs
Add a tool for automatically generating/updating TOCs
- Loading branch information
Showing
8 changed files
with
602 additions
and
393 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
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,30 @@ | ||
import colors from 'colors/safe.js' | ||
import diff from 'diff' | ||
import * as fs from 'fs' | ||
|
||
import * as toc from '../tool/src/toc.mjs' | ||
|
||
toc.files.forEach(function (file) { | ||
var markdown = fs.readFileSync(file).toString() | ||
console.log('Reading: ' + file) | ||
|
||
var currentToc = toc.getCurrent(markdown) | ||
if (currentToc == null) { | ||
console.log(colors.yellow("File doesn't have a table of contents")) | ||
return | ||
} | ||
|
||
var generatedToc = toc.generate(markdown) | ||
if (currentToc === generatedToc) return | ||
|
||
var tocDiff = diff.diffLines(currentToc, generatedToc) | ||
tocDiff.forEach(function (part) { | ||
var color = part.added | ||
? colors.green | ||
: (part.removed ? colors.red : colors.grey) | ||
process.stderr.write(color(part.value)) | ||
}) | ||
process.stderr.write('\n') | ||
|
||
process.exitCode = 1 | ||
}) |
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,36 @@ | ||
import glob from 'glob' | ||
import markdownToc from 'markdown-toc' | ||
|
||
/// Files that may contain tables of contents. | ||
export const files = glob.sync('**/*.md', { | ||
ignore: ['node_modules/**/*.md', '**/*.changes.md'] | ||
}) | ||
|
||
// Returns the index of the first `*` in `markdown`'s table of contents, or | ||
// `null` if `markdown` doesn't include a table of contents. | ||
function findStartIndex (markdown) { | ||
var match = markdown.match(/\n## Table of Contents\n/) | ||
if (match == null) return null | ||
return match.index + match[0].length - 1 | ||
} | ||
|
||
// Returns the table of contents in `markdown` if it contains one, or `null` | ||
// otherwise. | ||
export function getCurrent (markdown) { | ||
var startIndex = findStartIndex(markdown) | ||
if (startIndex == null) return null | ||
|
||
var endIndex = markdown.indexOf('## ', startIndex) - 1 | ||
if (endIndex < 0) return null | ||
|
||
return markdown.substring(startIndex, endIndex).trim() | ||
} | ||
|
||
/// Returns the expected table of contents for `markdown`. | ||
export function generate (markdown) { | ||
return markdownToc(markdown, { | ||
filter: (string, _, __) => string.indexOf('Table of Contents') === -1, | ||
firsth1: false, | ||
bullets: '*' | ||
}).content | ||
} |
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,29 @@ | ||
import * as toc from './src/toc.mjs' | ||
import * as fs from 'fs' | ||
|
||
toc.files.forEach(function (file) { | ||
var markdown = fs.readFileSync(file).toString() | ||
|
||
var currentToc = toc.getCurrent(markdown) | ||
if (currentToc == null) { | ||
var match = markdown.match('## Table of Contents\n\n') | ||
if (!match) return | ||
|
||
var tocLocation = match.index + match[0].length | ||
|
||
// If there's an empty TOC, fill it in. | ||
fs.writeFileSync( | ||
file, | ||
markdown.substring(0, tocLocation) + | ||
toc.generate(markdown) + '\n\n' + | ||
markdown.substring(tocLocation)) | ||
console.log(`Added TOC to ${file}`) | ||
return | ||
} | ||
|
||
var generatedToc = toc.generate(markdown) | ||
if (currentToc === generatedToc) return | ||
|
||
fs.writeFileSync(file, markdown.replace(currentToc, generatedToc)) | ||
console.log(`Updated TOC in ${file}`) | ||
}) |