This commit is contained in:
2024-03-22 03:47:51 +05:30
parent 8bcf3d211e
commit 89819f6fe2
28440 changed files with 3211033 additions and 2 deletions

View File

@@ -0,0 +1,15 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
type Plugin = any;
export type TOCItem = {
readonly value: string;
readonly id: string;
readonly level: number;
};
declare const plugin: Plugin;
export default plugin;
//# sourceMappingURL=index.d.ts.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/toc/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAoBH,KAAK,MAAM,GAAG,GAAG,CAAC;AAElB,MAAM,MAAM,OAAO,GAAG;IACpB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB,CAAC;AAuDF,QAAA,MAAM,MAAM,EAAE,MAiCb,CAAC;AAEF,eAAe,MAAM,CAAC"}

View File

@@ -0,0 +1,111 @@
"use strict";
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const parser_1 = require("@babel/parser");
const traverse_1 = tslib_1.__importDefault(require("@babel/traverse"));
const stringify_object_1 = tslib_1.__importDefault(require("stringify-object"));
const utils_1 = require("../utils");
const parseOptions = {
plugins: ['jsx'],
sourceType: 'module',
};
const isImport = (child) => child.type === 'mdxjsEsm' && child.value.startsWith('import');
const hasImports = (index) => index > -1;
const isExport = (child) => child.type === 'mdxjsEsm' && child.value.startsWith('export');
const isTarget = (child, name) => {
let found = false;
const ast = (0, parser_1.parse)(child.value, parseOptions);
(0, traverse_1.default)(ast, {
VariableDeclarator: (path) => {
if (path.node.id.name === name) {
found = true;
}
},
});
return found;
};
const getOrCreateExistingTargetIndex = async (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 = await createExportNode(name, []);
targetIndex = hasImports(importsIndex) ? importsIndex + 1 : 0;
children.splice(targetIndex, 0, target);
}
return targetIndex;
};
const plugin = function plugin(options = {}) {
const name = options.name || 'toc';
return async (root) => {
const { toString } = await import('mdast-util-to-string');
const { visit } = await import('unist-util-visit');
const headings = [];
visit(root, 'heading', (child) => {
const value = toString(child);
// depth:1 headings are titles and not included in the TOC
if (!value || child.depth < 2) {
return;
}
headings.push({
value: (0, utils_1.toValue)(child, toString),
id: child.data.id,
level: child.depth,
});
});
const { children } = root;
const targetIndex = await getOrCreateExistingTargetIndex(children, name);
if (headings?.length) {
children[targetIndex] = await createExportNode(name, headings);
}
};
};
exports.default = plugin;
async function createExportNode(name, object) {
const { valueToEstree } = await import('estree-util-value-to-estree');
return {
type: 'mdxjsEsm',
value: `export const ${name} = ${(0, stringify_object_1.default)(object)}`,
data: {
estree: {
type: 'Program',
body: [
{
type: 'ExportNamedDeclaration',
declaration: {
type: 'VariableDeclaration',
declarations: [
{
type: 'VariableDeclarator',
id: {
type: 'Identifier',
name,
},
init: valueToEstree(object),
},
],
kind: 'const',
},
specifiers: [],
source: null,
},
],
sourceType: 'module',
},
},
};
}
//# sourceMappingURL=index.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/toc/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,0CAAwD;AACxD,uEAAuC;AACvC,gFAA+C;AAC/C,oCAAiC;AAuBjC,MAAM,YAAY,GAAkB;IAClC,OAAO,EAAE,CAAC,KAAK,CAAC;IAChB,UAAU,EAAE,QAAQ;CACrB,CAAC;AAEF,MAAM,QAAQ,GAAG,CAAC,KAAU,EAAoB,EAA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