mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-19 03:57:01 +02:00
feat(v2): support rightToc, emoji, slug for docs (#1382)
* add remark-slug and remark-emoji * implement right TOC remark plugin * use rehype-slug ecosystem instead of remark for perf * first rough implementation for right toc * nits * remove unwanted changes * fix left border styling * remove depths * inline snapshot
This commit is contained in:
parent
37897ffc96
commit
745f32b010
12 changed files with 570 additions and 53 deletions
46
packages/docusaurus-mdx-loader/src/rightToc/search.js
Normal file
46
packages/docusaurus-mdx-loader/src/rightToc/search.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
/**
|
||||
* 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 toString = require('mdast-util-to-string');
|
||||
const visit = require('unist-util-visit');
|
||||
const slugs = require('github-slugger')();
|
||||
|
||||
// Visit all headings. We `slug` all headings (to account for
|
||||
// duplicates), but only take h2 and h3 headings.
|
||||
const search = node => {
|
||||
const headings = [];
|
||||
let current = -1;
|
||||
let currentDepth = 0;
|
||||
|
||||
slugs.reset();
|
||||
|
||||
const onHeading = (child, index, parent) => {
|
||||
const value = toString(child);
|
||||
const id =
|
||||
child.data && child.data.hProperties && child.data.hProperties.id;
|
||||
const slug = slugs.slug(id || value);
|
||||
|
||||
if (parent !== node || !value || child.depth > 3 || child.depth < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
const entry = {value, id: slug, children: []};
|
||||
|
||||
if (!headings.length || currentDepth >= child.depth) {
|
||||
headings.push(entry);
|
||||
current += 1;
|
||||
currentDepth = child.depth;
|
||||
} else {
|
||||
headings[current].children.push(entry);
|
||||
}
|
||||
};
|
||||
|
||||
visit(node, 'heading', onHeading);
|
||||
|
||||
return headings;
|
||||
};
|
||||
|
||||
module.exports = search;
|
Loading…
Add table
Add a link
Reference in a new issue