gatsby#CreateNodeArgs TypeScript Examples

The following examples show how to use gatsby#CreateNodeArgs. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: on-create-node.ts    From gatsby-project-kb with MIT License 6 votes vote down vote up
onCreateNode = async (
  { cache, node, loadNodeContent, getNode }: CreateNodeArgs,
  _options?: PluginOptions
) => {
  const options = resolveOptions(_options)

  // if we shouldn't process this node, then return
  if (!options.types.includes(node.internal.type)) {
    return
  }

  const content = await loadNodeContent(node)

  const outboundReferences = getReferences(content, (ref) => {
    ref.referrerNode = node
    return ref
  })

  const title = getTitle(node as MdxNode, content)
  const aliases = getAliases(node as MdxNode)

  await clearInboundReferences(cache)
  await setCachedNode(cache, node.id, {
    node,
    outboundReferences,
    title,
    aliases,
  })
}
Example #2
Source File: onCreateNode.ts    From gatsby-plugin-react-i18next with MIT License 4 votes vote down vote up
onCreateNode = async (
  {
    node,
    actions,
    loadNodeContent,
    createNodeId,
    createContentDigest,
    reporter
  }: // @ts-ignore
  CreateNodeArgs<FileSystemNode>,
  {localeJsonSourceName = 'locale', verbose = true}: PluginOptions
) => {
  if (!unstable_shouldOnCreateNode({node})) {
    return;
  }

  const {
    absolutePath,
    internal: {type},
    sourceInstanceName,
    relativeDirectory,
    name,
    id
  } = node;

  // Currently only support file resources
  if (type !== 'File') {
    return;
  }

  // User is not using this feature
  if (localeJsonSourceName == null) {
    return;
  }

  if (sourceInstanceName !== localeJsonSourceName) {
    return;
  }

  let activity;
  if (verbose) {
    activity = reporter.activityTimer(
      `gatsby-plugin-react-i18next: create node: ${relativeDirectory}/${name}`
    );
    activity.start();
  }

  // relativeDirectory name is language name.
  const language = relativeDirectory;
  const content = await loadNodeContent(node);

  // verify & canonicalize indent. (do not care about key order)
  let data: string;
  try {
    data = JSON.stringify(JSON.parse(content), undefined, '');
  } catch {
    const hint = node.absolutePath ? `file ${node.absolutePath}` : `in node ${node.id}`;
    throw new Error(`Unable to parse JSON: ${hint}`);
  }

  const {createNode, createParentChildLink} = actions;

  const localeNode: LocaleNodeInput = {
    id: createNodeId(`${id} >>> Locale`),
    children: [],
    parent: id,
    internal: {
      content: data,
      contentDigest: createContentDigest(data),
      type: `Locale`
    },
    language: language,
    ns: name,
    data,
    fileAbsolutePath: absolutePath
  };

  createNode(localeNode);

  // @ts-ignore
  // staled issue: https://github.com/gatsbyjs/gatsby/issues/19993
  createParentChildLink({parent: node, child: localeNode});

  if (verbose && activity) {
    activity.end();
  }
}