mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-09 23:27:28 +02:00
Move Docusaurus 1 files into directory (#966)
* Move Docusaurus 1 into v1 directory * Update Circle CI commands for new v1 dir * Remove OC * Fix tests
This commit is contained in:
parent
9d4a5d5359
commit
f2927a9fc4
291 changed files with 7591 additions and 6532 deletions
76
v1/lib/core/toc.js
Normal file
76
v1/lib/core/toc.js
Normal file
|
@ -0,0 +1,76 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
const Remarkable = require('remarkable');
|
||||
const mdToc = require('markdown-toc');
|
||||
const toSlug = require('./toSlug');
|
||||
|
||||
const TABLE_OF_CONTENTS_TOKEN = '<AUTOGENERATED_TABLE_OF_CONTENTS>';
|
||||
|
||||
/**
|
||||
* Returns a table of content from the headings
|
||||
*
|
||||
* @return array
|
||||
* Array of heading objects with `hashLink`, `content` and `children` fields
|
||||
*
|
||||
*/
|
||||
function getTOC(content, headingTags = 'h2', subHeadingTags = 'h3') {
|
||||
const tagToLevel = tag => Number(tag.slice(1));
|
||||
const headingLevels = [].concat(headingTags).map(tagToLevel);
|
||||
const subHeadingLevels = subHeadingTags
|
||||
? [].concat(subHeadingTags).map(tagToLevel)
|
||||
: [];
|
||||
const allowedHeadingLevels = headingLevels.concat(subHeadingLevels);
|
||||
const md = new Remarkable();
|
||||
const headings = mdToc(content).json;
|
||||
const toc = [];
|
||||
const context = {};
|
||||
let current;
|
||||
|
||||
headings.forEach(heading => {
|
||||
// we need always generate slugs to ensure, that we will have consistent
|
||||
// slug indexes for headings with the same names
|
||||
const hashLink = toSlug(heading.content, context);
|
||||
if (!allowedHeadingLevels.includes(heading.lvl)) {
|
||||
return;
|
||||
}
|
||||
const rawContent = mdToc.titleize(heading.content);
|
||||
const entry = {
|
||||
hashLink,
|
||||
rawContent,
|
||||
content: md.renderInline(rawContent),
|
||||
children: [],
|
||||
};
|
||||
if (headingLevels.includes(heading.lvl)) {
|
||||
toc.push(entry);
|
||||
current = entry;
|
||||
} else if (current) {
|
||||
current.children.push(entry);
|
||||
}
|
||||
});
|
||||
return toc;
|
||||
}
|
||||
|
||||
// takes the content of a doc article and returns the content with a table of
|
||||
// contents inserted
|
||||
function insertTOC(rawContent) {
|
||||
if (!rawContent || rawContent.indexOf(TABLE_OF_CONTENTS_TOKEN) === -1) {
|
||||
return rawContent;
|
||||
}
|
||||
const filterRe = /^`[^`]*`/;
|
||||
const headers = getTOC(rawContent, 'h3', null);
|
||||
const tableOfContents = headers
|
||||
.filter(header => filterRe.test(header.rawContent))
|
||||
.map(header => ` - [${header.rawContent}](#${header.hashLink})`)
|
||||
.join('\n');
|
||||
return rawContent.replace(TABLE_OF_CONTENTS_TOKEN, tableOfContents);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getTOC,
|
||||
insertTOC,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue