[ADF-2135] Updated tables of contents and tools (#2820)

This commit is contained in:
Andy Stark
2018-01-11 10:15:00 +00:00
committed by Eugenio Romano
parent 9e706d68e4
commit 994041fb23
23 changed files with 353 additions and 464 deletions

View File

@@ -8,7 +8,17 @@ const contentsHeading = "Contents";
const minHeadingsForToc = 8;
const maxTocHeadingDepth = 3;
module.exports = process;
module.exports = {
"initPhase": initPhase,
"readPhase": readPhase,
"aggPhase": aggPhase,
"updatePhase": updatePhase
}
function initPhase(aggData) {}
function readPhase(tree, pathname, aggData) {}
function aggPhase(aggData) {}
// Find an existing Contents section or add a new empty one if needed.
// Returns true if section is present/needed, false if not needed.
@@ -47,7 +57,7 @@ function establishContentsSection(mdTree) {
// If there are enough headings for a ToC to be necessary then
// add one in the right place.
if (!foundContentsHeading) {
var newContsHeading = unist.makeHeading(contentsHeading, 2);
var newContsHeading = unist.makeHeading(unist.makeText(contentsHeading), 2);
// If we found another L2 heading then add the Contents in just before it.
if (firstL2HeadingPos != -1) {
@@ -62,21 +72,22 @@ function establishContentsSection(mdTree) {
return numTocHeadings;
}
function process(mdTree, file) {
function updatePhase(tree, pathname, aggData) {
// If we need a contents section then add one or update the existing one.
var numTocHeadings = establishContentsSection(mdTree);
var numTocHeadings = establishContentsSection(tree);
if (numTocHeadings >= minHeadingsForToc) {
var newToc = tocGenerator(mdTree, {heading: contentsHeading, maxDepth: 3});
var newToc = tocGenerator(tree, {heading: contentsHeading, maxDepth: 3});
replaceSection(mdTree, contentsHeading, function(before, oldSection, after) {
replaceSection(tree, contentsHeading, function(before, oldSection, after) {
return [before, newToc.map, after];
});
} else {
// Otherwise, we don't need one, so remove any existing one.
replaceSection(mdTree, contentsHeading, function(before, oldSection, after) {
replaceSection(tree, contentsHeading, function(before, oldSection, after) {
return [after];
});
}
return true;
}