Skip to content

Commit

Permalink
fix:(anchor-links): don't extract markdown (#44)
Browse files Browse the repository at this point in the history
* fix:(anchor-links): don't extract markdown

* 4.0.0-canary.0

* feat: update implementation

* remove only
  • Loading branch information
thiskevinwang authored Sep 27, 2022
1 parent 28ddcea commit 100b058
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
30 changes: 20 additions & 10 deletions plugins/anchor-links/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
19 changes: 19 additions & 0 deletions plugins/anchor-links/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 100b058

Please sign in to comment.