From a259ff8e9c08b169cc767ea4f450ce3e509877bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Pito=C5=88=C3=A1k?= Date: Wed, 23 Nov 2022 16:28:14 +0100 Subject: [PATCH] docs: add onCreateNode params and example (#27503) Co-authored-by: LekoArts --- packages/gatsby/index.d.ts | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/gatsby/index.d.ts b/packages/gatsby/index.d.ts index 80751b15560bc..6120ac8a0c25e 100644 --- a/packages/gatsby/index.d.ts +++ b/packages/gatsby/index.d.ts @@ -431,12 +431,34 @@ export interface GatsbyNode< * transform nodes created by other plugins should implement this API. * * See also the documentation for `createNode` - * and [`createNodeField`](https://www.gatsbyjs.com/docs/actions/#createNodeField) + * and [`createNodeField`](https://www.gatsbyjs.com/docs/reference/config-files/actions/#createNodeField) + * @param {object} $0 + * @param {object} $0.node A node object. + * @param {object} $0.actions + * @param {function} $0.actions.createNode Create a new node. + * @param {function} $0.actions.createNodeField Extend another node. The new node field is placed under the fields key on the extended node object. * @example - * exports.onCreateNode = ({ node, actions }) => { - * const { createNode, createNodeField } = actions - * // Transform the new node here and create a new node or - * // create a new node field. + * exports.onCreateNode = ({ node, getNode, actions }) => { + * const { createNodeField } = actions + * + * if (node.internal.type === `MarkdownRemark`) { + * const nodePath = node.fileAbsolutePath + * + * if (nodePath.match(/\/blog\//)) { + * const postSlug = createFilePath({ + * node, + * getNode, + * basePath: `src/content`, + * trailingSlash: true, + * }) + * + * createNodeField({ + * node, + * name: `slug`, + * value: `/blog/${postSlug}/`, + * }) + * } + * } * } */ onCreateNode?(