Skip to content

Commit

Permalink
V3/remark todo (#29453)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Kane <matt@gatsbyjs.com>
Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
  • Loading branch information
3 people authored Feb 18, 2021
1 parent 107926a commit 8bd42aa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby-transformer-remark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Using the following GraphQL query you'll be able to get the table of contents

### Configuring the tableOfContents

By default the tableOfContents is using the field `slug` to generate absolute URLs. You can however provide another field using the pathToSlugField parameter. **Note** that providing a non existing field will cause the result to be null. You can also pass `absolute: false` to generate relative path. To alter the default values for tableOfContents generation, include values for `heading` (string) and/or `maxDepth` (number 1 to 6) in GraphQL query. If a value for `heading` is given, the first heading that matches will be omitted and the toc is generated from the next heading of the same depth onwards. Value for `maxDepth` sets the maximum depth of the toc (i.e. if a maxDepth of 3 is set, only h1 to h3 headings will appear in the toc).
By default, `absolute` is set to false, generating a relative path. If you'd like to generate an absolute path, pass `absolute: true`. In that case, be sure to pass the `pathToSlugField` parameter, often `fields.slug`, to create absolute URLs. **Note** that providing a non-existent field will cause the result to be null. To alter the default values for tableOfContents generation, include values for `heading` (string) and/or `maxDepth` (number 1 to 6) in GraphQL query. If a value for `heading` is given, the first heading that matches will be omitted and the toc is generated from the next heading of the same depth onwards. Value for `maxDepth` sets the maximum depth of the toc (i.e. if a maxDepth of 3 is set, only h1 to h3 headings will appear in the toc).

```graphql
{
Expand Down
28 changes: 15 additions & 13 deletions packages/gatsby-transformer-remark/src/__tests__/extend-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ async function queryResult(
fragment,
{ additionalParameters = {}, pluginOptions = {} }
) {
const cache = {
// GatsbyCache
get: async () => null,
set: async () => null,
}
const extendNodeTypeFields = await extendNodeType(
{
type: { name: `MarkdownRemark` },
cache: {
// GatsbyCache
get: async () => null,
set: async () => null,
},
cache,
getCache: () => cache,
getNodesByType: type => [],
...additionalParameters,
},
Expand Down Expand Up @@ -935,7 +937,7 @@ some text
some other text
`,
`tableOfContents
`tableOfContents(absolute: true, pathToSlugField: "fields.slug")
frontmatter {
title
}`,
Expand Down Expand Up @@ -963,7 +965,7 @@ some other text
final text
`,
`tableOfContents(pathToSlugField: "frontmatter.title")
`tableOfContents(pathToSlugField: "frontmatter.title", absolute: true)
frontmatter {
title
}`,
Expand All @@ -990,7 +992,7 @@ some other text
final text
`,
`tableOfContents(absolute: false)
`tableOfContents
frontmatter {
title
}`,
Expand All @@ -1012,7 +1014,7 @@ some text
## second title
some other text`,
`tableOfContents(pathToSlugField: "frontmatter.title", maxDepth: 1)
`tableOfContents(pathToSlugField: "frontmatter.title", maxDepth: 1, absolute: true)
frontmatter {
title
}`,
Expand All @@ -1036,7 +1038,7 @@ some text
## second title
some other text`,
`tableOfContents(pathToSlugField: "frontmatter.title")
`tableOfContents(pathToSlugField: "frontmatter.title", absolute: true)
frontmatter {
title
}`,
Expand Down Expand Up @@ -1071,7 +1073,7 @@ some other text
# third title
final text`,
`tableOfContents(pathToSlugField: "frontmatter.title", heading: "first title")
`tableOfContents(pathToSlugField: "frontmatter.title", heading: "first title", absolute: true)
frontmatter {
title
}`,
Expand Down Expand Up @@ -1101,7 +1103,7 @@ Content - content
### Embedding \`<code>\` Tags
It's easier than you may imagine`,
`tableOfContents(pathToSlugField: "frontmatter.title")
`tableOfContents(pathToSlugField: "frontmatter.title", absolute: true)
frontmatter {
title
}`,
Expand All @@ -1125,7 +1127,7 @@ Content - content
### Embedding \`<code>\` Tags
It's easier than you may imagine`,
`tableOfContents(pathToSlugField: "frontmatter.title")
`tableOfContents(pathToSlugField: "frontmatter.title", absolute: true)
frontmatter {
title
}`,
Expand Down
18 changes: 3 additions & 15 deletions packages/gatsby-transformer-remark/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ const tableOfContentsCacheKey = (node, appliedTocOptions) =>
const withPathPrefix = (url, pathPrefix) =>
(pathPrefix + url).replace(/\/\//, `/`)

// TODO: remove this check with next major release
const safeGetCache = ({ getCache, cache }) => id => {
if (!getCache) {
return cache
}
return getCache(id)
}

/**
* Map that keeps track of generation of AST to not generate it multiple
* times in parallel.
Expand Down Expand Up @@ -87,7 +79,7 @@ module.exports = function remarkExtendNodeType(
getNode,
getNodesByType,
cache,
getCache: possibleGetCache,
getCache,
reporter,
...rest
},
Expand All @@ -99,8 +91,6 @@ module.exports = function remarkExtendNodeType(
pluginsCacheStr = pluginOptions.plugins.map(p => p.name).join(``)
pathPrefixCacheStr = basePath || ``

const getCache = safeGetCache({ cache, getCache: possibleGetCache })

return new Promise((resolve, reject) => {
// Setup Remark.
const {
Expand Down Expand Up @@ -645,15 +635,13 @@ module.exports = function remarkExtendNodeType(
tableOfContents: {
type: `String`,
args: {
// TODO:(v3) set default value to false
absolute: {
type: `Boolean`,
defaultValue: true,
defaultValue: false,
},
// TODO:(v3) set default value to empty string
pathToSlugField: {
type: `String`,
defaultValue: `fields.slug`,
defaultValue: ``,
},
maxDepth: `Int`,
heading: `String`,
Expand Down

0 comments on commit 8bd42aa

Please sign in to comment.