diff --git a/index.d.ts b/index.d.ts index 98ba89d..4d4e351 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2,9 +2,14 @@ export = ExportHeadings; /** @type {import('unified').Plugin<[Partial?], import('hast').Root>} */ declare const ExportHeadings: import('unified').Plugin<[Partial?], 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; +}; diff --git a/index.js b/index.js index 8ad3910..009103f 100644 --- a/index.js +++ b/index.js @@ -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, }, }], }, diff --git a/package.json b/package.json index 21d21b7..50822c4 100644 --- a/package.json +++ b/package.json @@ -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",