diff --git a/package-lock.json b/package-lock.json index d0286f4..584d471 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@hashicorp/remark-plugins", - "version": "4.0.0", + "version": "4.0.0-canary.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@hashicorp/remark-plugins", - "version": "3.2.1", + "version": "4.0.0", "license": "MPL-2.0", "dependencies": { "@mdx-js/util": "1.6.22", diff --git a/package.json b/package.json index 25afa07..755e358 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@hashicorp/remark-plugins", "description": "A potpourri of remark plugins used to process .mdx files", - "version": "4.0.0", + "version": "4.0.0-canary.0", "author": "Jeff Escalante", "bugs": "https://github.com/hashicorp/remark-plugins/issues", "contributors": [ diff --git a/plugins/anchor-links/index.js b/plugins/anchor-links/index.js index 63865de..243073f 100644 --- a/plugins/anchor-links/index.js +++ b/plugins/anchor-links/index.js @@ -53,7 +53,6 @@ function processHeading(node, compatibilitySlug, links, headings) { const text = stringifyChildNodes(node) const level = node.depth const title = text - .substring(level + 1) .replace(/<\/?[^>]*>/g, '') // Strip html .replace(/\(\(#.*?\)\)/g, '') // Strip anchor link aliases .replace(/ยป/g, '') // Safeguard against double-running this plugin @@ -292,14 +291,25 @@ function aliasesToNodes(aliases, id) { // not a type that standard remark recognizes. we can't accommodate all // types of custom remark setups, so we simply fall back if it doesn't work function stringifyChildNodes(node) { - let text - try { - text = remark().use(stringify).stringify(node) - } catch (_) { - text = node.children.reduce((m, s) => { - if (s.value) m += s.value - return m - }, '') - } + return getChildNodesText(node) +} + +/** + * Collect text from children nodes. This will visit + * nodes recursively via "depth-first" strategy. + * + * @param {import('unist').Parent | import('unist').Node} node + * @returns {string} + */ +function getChildNodesText(node) { + const text = node.children.reduce((acc, child) => { + if ('children' in child) { + acc += getChildNodesText(child) + } else if ('value' in child) { + acc += child.value + } + return acc + }, '') + return text } diff --git a/plugins/anchor-links/index.test.js b/plugins/anchor-links/index.test.js index 87560c4..b4c9c5e 100644 --- a/plugins/anchor-links/index.test.js +++ b/plugins/anchor-links/index.test.js @@ -459,6 +459,25 @@ describe('anchor-links', () => { ] `) }) + + test('returns only text content', () => { + const headings = [] + execute( + `## context.Context +## \*component.Source +## ~~strikethrough~~ +## _italic_ +## [complex](https://hashicorp.com) heading **element**`, + { headings } + ) + + expect(headings.length).toBe(5) + + expect(headings[1].title).toEqual('*component.Source') + expect(headings[2].title).toEqual('strikethrough') + expect(headings[3].title).toEqual('italic') + expect(headings[4].title).toEqual('complex heading element') + }) }) describe('lists starting with inline code', () => {