danielmahon / gatsby-remark-relative-images

Convert markdown image src(s) to be relative for gatsby-remark-images.
https://www.npmjs.com/package/gatsby-remark-relative-images
BSD Zero Clause License
79 stars 29 forks source link

support also non-file markdown remark nodes #43

Open rburgst opened 4 years ago

rburgst commented 4 years ago

Currently it is not easily possible to have markdown in frontmatter tags while using this plugin (e.g. using netlifyCMS with a markdown fields other than Body). Currently I am creating new nodes for these frontmatter fields

exports.onCreateNode = async ({ node, actions, getNode }) => {
  const { createNode, createNodeField } = actions;

  if (node.internal.type === `MarkdownRemark`) {
    if (node.frontmatter && node.frontmatter.myMarkDownField) {
      const infoNode = await createNode({
        id: `md-${node.id}-info`,
        parent: node.id,
        children: [],
        internal: {
          type: `InfoMarkdown`,
          mediaType: `text/markdown`,
          content: node.frontmatter.myMarkDownField,
          contentDigest: node.internal.contentDigest,
        },
      });

(similar to the suggestion here https://github.com/GraphCMS/gatsby-source-graphcms/issues/44#issuecomment-382309512)

However transformer remark only copies the absolutePath in case its a File node (which it clearly isnt). So I would need some way to indicate the absolute path in the remark node or maybe have some other field conveying the path. Currently the code only works if the markdown node has a fileAbsolutePath (see https://github.com/danielmahon/gatsby-remark-relative-images/blob/master/src/on-create-node.ts#L29), which is not the case for me and therefore the plugin crashes currently.


"gatsby-remark-relative-images" threw an error while running the onCreateNode lifecycle:

The "path" argument must be of type string. Received type undefined

  Error: TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined

  - validators.js:112 validateString
    internal/validators.js:112:11

  - on-create-node.js:15 Object.exports.onCreateNode
    [gatsby-starter-netlify-cms]/[gatsby-remark-relative-images]/dist/on-create-node.js:15:42

  - api-runner-node.js:330 runAPI
    [gatsby-starter-netlify-cms]/[gatsby]/src/utils/api-runner-node.js:330:22

  - api-runner-node.js:440 Promise.catch.decorateEvent.pluginName
    [gatsby-starter-netlify-cms]/[gatsby]/src/utils/api-runner-node.js:440:17

  - From previous event:

  - api-runner-node.js:440 Promise.catch.decorateEvent.pluginName
    [gatsby-starter-netlify-cms]/[gatsby]/src/utils/api-runner-node.js:440:9

  - From previous event:

  - api-runner-node.js:439
    [gatsby-starter-netlify-cms]/[gatsby]/src/utils/api-runner-node.js:439:14
rburgst commented 4 years ago

I added a PR fixing this months ago, any chance to get this merged?