Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gatsby-transform-remark): populate id to headings from markdownAST #23546

Merged
merged 9 commits into from
May 6, 2020
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,23 @@ Object {
}
`;

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

exports[`Headings are generated correctly from schema returns value 1`] = `
Object {
"headings": Array [
Expand Down
29 changes: 29 additions & 0 deletions packages/gatsby-transformer-remark/src/__tests__/extend-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,35 @@ describe(`Headings are generated correctly from schema`, () => {
}
)

bootstrapTest(
`returns null id, value and depth`,
`
# first title

## second title
`,
`headings {
id
value
depth
}`,
node => {
expect(node).toMatchSnapshot()
expect(node.headings).toEqual([
{
id: null,
value: `first title`,
depth: 1,
},
{
id: null,
value: `second title`,
depth: 2,
},
])
}
)

bootstrapTest(
`returns value with inlineCode`,
`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const typeDefs = `
type MarkdownHeading {
id: String
value: String
depth: Int
}
Expand Down
2 changes: 2 additions & 0 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const {
} = require(`./hast-processing`)
const codeHandler = require(`./code-handler`)
const { timeToRead } = require(`./utils/time-to-read`)
const { getHeadingID } = require(`./utils/get-heading-id`)
bongnv marked this conversation as resolved.
Show resolved Hide resolved

let fileNodes
let pluginsCacheStr = ``
Expand Down Expand Up @@ -271,6 +272,7 @@ module.exports = (
const ast = await getAST(markdownNode)
const headings = select(ast, `heading`).map(heading => {
return {
id: getHeadingID(heading),
value: mdastToString(heading),
depth: heading.depth,
}
Expand Down