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,40 @@ Object {
}
`;

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

exports[`Headings are generated correctly from schema returns null id if heading has no id 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
71 changes: 71 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,77 @@ describe(`Headings are generated correctly from schema`, () => {
}
)

bootstrapTest(
`returns null id if heading has no id`,
`
# 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 id if heading has one`,
`
# first title

## second title
`,
`headings {
id
value
depth
}`,
node => {
expect(node).toMatchSnapshot()
expect(node.headings).toEqual([
{
id: `first-title`,
value: `first title`,
depth: 1,
},
{
id: `second-title`,
value: `second title`,
depth: 2,
},
])
},
{
pluginOptions: {
plugins: [
// to pass subplugin we need to use object with `resolve` and `pluginOptions`
// (this is what gatsby core internally turns plugin entries to + gatsby core always set empty object {}
// if options were not provided and lot of plugins rely on this and are not checking for options existence)
{
resolve: require.resolve(`gatsby-remark-autolink-headers/src`),
pluginOptions: {},
},
],
},
}
)

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 @@ -21,6 +21,7 @@ const {
findLastTextNode,
} = require(`./hast-processing`)
const codeHandler = require(`./code-handler`)
const { getHeadingID } = require(`./utils/get-heading-id`)
bongnv marked this conversation as resolved.
Show resolved Hide resolved
const { timeToRead } = require(`./utils/time-to-read`)

let fileNodes
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
15 changes: 15 additions & 0 deletions packages/gatsby-transformer-remark/src/utils/get-heading-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export const getHeadingID = heading => {
const data = heading.data
if (data) {
if (data.id) return data.id
if (data.htmlAttributes && data.htmlAttributes.id) {
return data.htmlAttributes.id
}

if (data.hProperties && data.hProperties.id) {
return data.hProperties.id
}
}

return null
}