fix dumb mistake

This commit is contained in:
sup39 2022-11-04 19:54:45 +09:00
parent 5f067b3797
commit 034ade8929
3 changed files with 17 additions and 3 deletions

7
index.d.ts vendored
View file

@ -2,9 +2,14 @@ export = ExportHeadings;
/** @type {import('unified').Plugin<[Partial<ExportHeadingsOptions>?], import('hast').Root>} */
declare const ExportHeadings: import('unified').Plugin<[Partial<ExportHeadingsOptions>?], import('hast').Root>;
declare namespace ExportHeadings {
export { ExportHeadingsOptions };
export { ExportHeadingsOptions, HeadingInfo };
}
type ExportHeadingsOptions = {
tags: string[];
name: string;
};
type HeadingInfo = {
tagName: string;
label: string;
id: string;
};

View file

@ -3,6 +3,12 @@
* tags: string[]
* name: string
* }} ExportHeadingsOptions
*
* @typedef {{
* tagName: string
* label: string
* id: string
* }} HeadingInfo
*/
/** @type {(e: import('hast').RootContent) => string[]} */
@ -15,6 +21,7 @@ const ExportHeadings = ({
tags = ['h2'],
name = 'headings',
} = {}) => ({children}) => {
/** @type {any[]} */
const items = [];
children.forEach(node => {
if (node.type !== 'element' || !tags.includes(node.tagName)) return;
@ -24,6 +31,7 @@ const ExportHeadings = ({
if (node.properties == null) node.properties = {id};
else node.properties.id = id;
// push item
/** @type {HeadingInfo} */
const item = {tagName: node.tagName, label: innerText, id};
items.push({
type: 'ObjectExpression',
@ -34,6 +42,7 @@ const ExportHeadings = ({
computed: false,
key: {type: 'Identifier', name},
value: {type: 'Literal', value},
kind: 'init',
})),
});
});
@ -53,7 +62,7 @@ const ExportHeadings = ({
id: {type: 'Identifier', name},
init: {
type: 'ArrayExpression',
elements: [],
elements: items,
},
}],
},

View file

@ -1,6 +1,6 @@
{
"name": "@sup39/rehype-mdx-export-headings",
"version": "0.1.0",
"version": "0.1.1",
"author": "sup39",
"repository": "https://github.com/sup39/rehype-mdx-export-headings",
"description": "A rehype plugin to extract headings (e.g. h2, h3) from mdx, assign them ids, and export headings list",