Skip to content

Commit

Permalink
add a fallback for smart heading stringification
Browse files Browse the repository at this point in the history
  • Loading branch information
jescalan committed Mar 12, 2020
1 parent e55b8f4 commit c20b68e
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions plugins/anchor-links/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,22 @@ module.exports = function anchorLinksPlugin({

function processHeading(node, compatibilitySlug, links) {
// a heading can contain multiple nodes including text, html, etc
// we stringify the node here to get its literal text contents
const text = remark()
.use(stringify)
.stringify(node)
// we try to stringify the node here to get its literal text contents
// if that fails due to nonstandard nodes etc. we take a simpler route
// for example, if using mdx, html nodes are encoded as "jsx" which is
// 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
let text
try {
text = remark()
.use(stringify)
.stringify(node)
} catch (_) {
text = node.children.reduce((m, s) => {
if (s.value) m += s.value
return m
}, '')
}

// generate the slug and add a target element to the headline
const slug = generateSlug(text, links)
Expand Down

0 comments on commit c20b68e

Please sign in to comment.