Skip to content

Commit

Permalink
Use absolute links in remark toc (gatsbyjs#2856)
Browse files Browse the repository at this point in the history
* Avoid assuming default tabWidth in user Prettier config

* Add slugs to markdown TOC

* Try to make TOC prefix-paths aware

* Correctly add prefix paths to links in Remark TOC

* Fix linting and use correct pathPrefix variable
  • Loading branch information
Swizec authored and KyleAMathews committed Nov 14, 2017
1 parent d220fca commit f7df369
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"trailingComma": "es5",
"semi": false
"trailingComma": "es5",
"semi": false,
"tabWidth": 2
}
15 changes: 15 additions & 0 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,23 @@ module.exports = (
} else {
const ast = await getAST(markdownNode)
const tocAst = mdastToToc(ast)

let toc
if (tocAst.map) {
const addSlugToUrl = function(node) {
if (node.url) {
node.url = [pathPrefix, markdownNode.fields.slug, node.url]
.join(`/`)
.replace(/\/\//g, `/`)
}
if (node.children) {
node.children = node.children.map(node => addSlugToUrl(node))
}

return node
}
tocAst.map = addSlugToUrl(tocAst.map)

toc = hastToHTML(toHAST(tocAst.map))
} else {
toc = ``
Expand Down

0 comments on commit f7df369

Please sign in to comment.