Skip to content

Commit

Permalink
fix(gatsby-transformer-remark): Handle headings with nested text (#11881
Browse files Browse the repository at this point in the history
)

* Create failing test for #11879

* fix(gatsby-transformer-remark): Return value from headings with backticks
Closes #11879

* Add failing test for @stefanprobst use case

* Use mdast-util-to-string
  • Loading branch information
diegohaz authored and sidharthachatterjee committed Feb 22, 2019
1 parent a0a04e0 commit 4c0c5c0
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/gatsby-transformer-remark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"hast-util-to-html": "^4.0.0",
"lodash": "^4.17.10",
"mdast-util-to-hast": "^3.0.0",
"mdast-util-to-string": "^1.0.5",
"mdast-util-toc": "^2.0.1",
"remark": "^9.0.0",
"remark-parse": "^5.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,47 @@ Object {
}
`;

exports[`Headings are generated correctly from schema returns value 1`] = `
Object {
"headings": Array [
Object {
"depth": 1,
"value": "first title",
},
Object {
"depth": 2,
"value": "second title",
},
],
}
`;

exports[`Headings are generated correctly from schema returns value with inlineCode 1`] = `
Object {
"headings": Array [
Object {
"depth": 1,
"value": "first title",
},
Object {
"depth": 2,
"value": "second title",
},
],
}
`;

exports[`Headings are generated correctly from schema returns value with mixed text 1`] = `
Object {
"headings": Array [
Object {
"depth": 1,
"value": "An important heading with inline code and text",
},
],
}
`;

exports[`Links are correctly prefixed correctly prefixes links 1`] = `
Object {
"html": "<p>This is <a href=\\"/prefix/path/to/page1\\">a link</a>.</p>
Expand Down
74 changes: 74 additions & 0 deletions packages/gatsby-transformer-remark/src/__tests__/extend-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,77 @@ This is [a reference]
{ additionalParameters: { pathPrefix: `/prefix` } }
)
})

describe(`Headings are generated correctly from schema`, () => {
bootstrapTest(
`returns value`,
`
# first title
## second title
`,
`headings {
value
depth
}`,
node => {
expect(node).toMatchSnapshot()
expect(node.headings).toEqual([
{
value: `first title`,
depth: 1,
},
{
value: `second title`,
depth: 2,
},
])
}
)

bootstrapTest(
`returns value with inlineCode`,
`
# first title
## \`second title\`
`,
`headings {
value
depth
}`,
node => {
expect(node).toMatchSnapshot()
expect(node.headings).toEqual([
{
value: `first title`,
depth: 1,
},
{
value: `second title`,
depth: 2,
},
])
}
)

bootstrapTest(
`returns value with mixed text`,
`
# An **important** heading with \`inline code\` and text
`,
`headings {
value
depth
}`,
node => {
expect(node).toMatchSnapshot()
expect(node.headings).toEqual([
{
value: `An important heading with inline code and text`,
depth: 1,
},
])
}
)
})
3 changes: 2 additions & 1 deletion packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const visit = require(`unist-util-visit`)
const toHAST = require(`mdast-util-to-hast`)
const hastToHTML = require(`hast-util-to-html`)
const mdastToToc = require(`mdast-util-toc`)
const mdastToString = require(`mdast-util-to-string`)
const Promise = require(`bluebird`)
const unified = require(`unified`)
const parse = require(`remark-parse`)
Expand Down Expand Up @@ -268,7 +269,7 @@ module.exports = (
const ast = await getAST(markdownNode)
const headings = select(ast, `heading`).map(heading => {
return {
value: _.first(select(heading, `text`).map(text => text.value)),
value: mdastToString(heading),
depth: heading.depth,
}
})
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12910,7 +12910,7 @@ mdast-util-to-nlcst@^3.2.0:
unist-util-position "^3.0.0"
vfile-location "^2.0.0"

mdast-util-to-string@^1.0.0, mdast-util-to-string@^1.0.2:
mdast-util-to-string@^1.0.0, mdast-util-to-string@^1.0.2, mdast-util-to-string@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/mdast-util-to-string/-/mdast-util-to-string-1.0.5.tgz#3552b05428af22ceda34f156afe62ec8e6d731ca"
integrity sha512-2qLt/DEOo5F6nc2VFScQiHPzQ0XXcabquRJxKMhKte8nt42o08HUxNDPk7tt0YPxnWjAT11I1SYi0X0iPnfI5A==
Expand Down

0 comments on commit 4c0c5c0

Please sign in to comment.