mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-14 09:37:37 +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
78
packages/docusaurus-mdx-loader/src/rightToc/index.js
Normal file
78
packages/docusaurus-mdx-loader/src/rightToc/index.js
Normal file
|
@ -0,0 +1,78 @@
|
|||
/**
|
||||
* 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 {parse} = require('@babel/parser');
|
||||
const traverse = require('@babel/traverse').default;
|
||||
const stringifyObject = require('stringify-object');
|
||||
const search = require('./search');
|
||||
|
||||
const parseOptions = {
|
||||
plugins: ['jsx'],
|
||||
sourceType: 'module',
|
||||
};
|
||||
const isImport = child => child.type === 'import';
|
||||
const hasImports = index => index > -1;
|
||||
const isExport = child => child.type === 'export';
|
||||
|
||||
const isTarget = (child, name) => {
|
||||
let found = false;
|
||||
const ast = parse(child.value, parseOptions);
|
||||
traverse(ast, {
|
||||
VariableDeclarator: path => {
|
||||
if (path.node.id.name === name) {
|
||||
found = true;
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return found;
|
||||
};
|
||||
|
||||
const getOrCreateExistingTargetIndex = (children, name) => {
|
||||
let importsIndex = -1;
|
||||
let targetIndex = -1;
|
||||
|
||||
children.forEach((child, index) => {
|
||||
if (isImport(child)) {
|
||||
importsIndex = index;
|
||||
} else if (isExport(child) && isTarget(child, name)) {
|
||||
targetIndex = index;
|
||||
}
|
||||
});
|
||||
|
||||
if (targetIndex === -1) {
|
||||
const target = {
|
||||
default: false,
|
||||
type: 'export',
|
||||
value: `export const ${name} = [];`,
|
||||
};
|
||||
|
||||
targetIndex = hasImports(importsIndex) ? importsIndex + 1 : 0;
|
||||
children.splice(targetIndex, 0, target);
|
||||
}
|
||||
|
||||
return targetIndex;
|
||||
};
|
||||
|
||||
const plugin = (options = {}) => {
|
||||
const name = options.name || 'rightToc';
|
||||
|
||||
const transformer = node => {
|
||||
const headings = search(node);
|
||||
const {children} = node;
|
||||
const targetIndex = getOrCreateExistingTargetIndex(children, name);
|
||||
|
||||
if (headings && headings.length) {
|
||||
children[targetIndex].value = `export const ${name} = ${stringifyObject(
|
||||
headings,
|
||||
)};`;
|
||||
}
|
||||
};
|
||||
|
||||
return transformer;
|
||||
};
|
||||
|
||||
module.exports = plugin;
|
Loading…
Add table
Add a link
Reference in a new issue