Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[gatsby-transformer-remark] Not prepending pathPrefix to relative links in Markdown #4900

Closed
ajrussellaudio opened this issue Apr 9, 2018 · 1 comment

Comments

@ajrussellaudio
Copy link

ajrussellaudio commented Apr 9, 2018

Description

gatsby-transformer-remark is not prepending the pathPrefix to relative links in Markdown.

I've read through #3316 which seems to lead to a fix in PR #3823 but it's still not happening for me.

Steps to reproduce

I'm working from gatsby-starter-blog.

I have a Markdown file which only contains the frontmatter and a link to a zip file:

[Download the zip file](example.zip)

The zip file lives next to the Markdown file:

folder
  |- index.md
  |- example.zip

This works locally with gatsby develop.

In production, we are deploying to a Github Pages project page, https://organization.github.io/repo-name so I have configured pathPrefix to '/repo-name'.

Expected result

A link from the generated page to the zip file.

Actual result

gatsby-remark-copy-linked-files does its thing and copies the zip file to the root directory with a hashed (?) name, but the link from the generated page points to https://organization.github.io/example-d53e4361829090bcfadb6c9b040057d8.zip when it should point to https://organization.github.io/repo-name/example-d53e4361829090bcfadb6c9b040057d8.zip

Environment

  • Gatsby version: gatsby@1.9.247
  • gatsby-cli version: 1.1.50
  • Node.js version: v8.4.0
  • Operating System: macOS 10.12.6 (16G1212)

File contents (if changed):

gatsby-config.js:

module.exports = {
  siteMetadata: {
    date: {
      week: 3, // INCREASE THIS EVERY WEEK, 1 to 16
      day: 1, // INCREASE EVERY DAY OF WEEK, 1 to 5
    },
    title: 'CodeClan',
    author: 'CodeClan Instructor Team',
    description: 'Notes for learning at CodeClan',
    siteUrl: 'https://codeclan.com',
  },
  pathPrefix: '/g5_notes',
  plugins: [
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        path: `${__dirname}/lessons`,
        name: 'lessons',
      },
    },
    {
      resolve: `gatsby-transformer-remark`,
      options: {
        plugins: [
          {
            resolve: `gatsby-remark-images`,
            options: {
              maxWidth: 590,
            },
          },
          {
            resolve: `gatsby-remark-responsive-iframe`,
            options: {
              wrapperStyle: `margin-bottom: 1.0725rem`,
            },
          },
          {
            resolve: `gatsby-remark-prismjs`,
            options: {
              classPrefix: 'language-',
            },
          },
          'gatsby-remark-autolink-headers',
          'gatsby-remark-copy-linked-files',
          'gatsby-remark-smartypants',
        ],
      },
    },
    `gatsby-transformer-sharp`,
    `gatsby-plugin-sharp`,
    `gatsby-plugin-offline`,
    `gatsby-plugin-react-helmet`,
    {
      resolve: 'gatsby-plugin-typography',
      options: {
        pathToConfigModule: 'src/utils/typography',
      },
    },
  ],
}

package.json: (I've just realised I've not changed any of the metadata here!)

{
  "name": "gatsby-starter-blog",
  "description": "Starter Gatsby Blog",
  "version": "1.0.0",
  "author": "Kyle Mathews <mathews.kyle@gmail.com>",
  "bugs": {
    "url": "https://github.com/gatsbyjs/gatsby-starter-blog/issues"
  },
  "dependencies": {
    "gatsby": "^1.9.241",
    "gatsby-link": "^1.6.37",
    "gatsby-plugin-offline": "^1.0.14",
    "gatsby-plugin-react-helmet": "^1.0.8",
    "gatsby-plugin-sharp": "^1.6.38",
    "gatsby-plugin-typography": "^1.7.15",
    "gatsby-remark-autolink-headers": "^1.4.13",
    "gatsby-remark-copy-linked-files": "^1.5.28",
    "gatsby-remark-images": "^1.5.54",
    "gatsby-remark-prismjs": "^1.2.17",
    "gatsby-remark-responsive-iframe": "^1.4.17",
    "gatsby-remark-smartypants": "^1.4.11",
    "gatsby-source-filesystem": "^1.5.26",
    "gatsby-transformer-remark": "^1.7.39",
    "gatsby-transformer-sharp": "^1.6.21",
    "lodash": "^4.17.5",
    "npm": "^5.8.0",
    "react-responsive-grid": "^0.3.4",
    "typeface-merriweather": "0.0.43",
    "typeface-montserrat": "0.0.43",
    "typography-theme-wordpress-2016": "^0.15.10"
  },
  "devDependencies": {
    "eslint": "^4.18.2",
    "eslint-plugin-react": "^7.7.0",
    "gh-pages": "^1.1.0",
    "prettier": "^1.11.1"
  },
  "homepage": "https://github.com/gatsbyjs/gatsby-starter-blog#readme",
  "keywords": [
    "gatsby"
  ],
  "license": "MIT",
  "main": "n/a",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/gatsbyjs/gatsby-starter-blog.git"
  },
  "scripts": {
    "dev": "gatsby develop",
    "lint": "./node_modules/.bin/eslint --ext .js,.jsx --ignore-pattern public .",
    "test": "echo \"Error: no test specified\" && exit 1",
    "format": "prettier --trailing-comma es5 --no-semi --single-quote --write 'src/**/*.js' 'src/**/*.md'",
    "develop": "gatsby develop",
    "build": "gatsby build",
    "deploy": "gatsby build --prefix-paths && gh-pages -d public",
    "fix-semi": "eslint --quiet --ignore-pattern node_modules --ignore-pattern public --parser babel-eslint --no-eslintrc --rule '{\"semi\": [2, \"never\"], \"no-extra-semi\": [2]}' --fix gatsby-node.js"
  }
}

gatsby-node.js:

const _ = require('lodash')
const Promise = require('bluebird')
const path = require('path')
const { createFilePath } = require('gatsby-source-filesystem')

exports.createPages = ({ graphql, boundActionCreators }) => {
  const { createPage } = boundActionCreators

  return new Promise((resolve, reject) => {
    const lesson = path.resolve('./src/templates/lesson.js')
    resolve(
      graphql(
        `
          {
            allMarkdownRemark(
              limit: 1000
            ) {
              edges {
                node {
                  fields {
                    slug
                  }
                  frontmatter {
                    title
                    week
                  }
                }
              }
            }
          }
        `
      ).then(result => {
        if (result.errors) {
          console.log(result.errors)
          reject(result.errors)
        }

        // Create lessons pages.
        const lessons = result.data.allMarkdownRemark.edges

        _.each(lessons, (post, index) => {
          const previous =
            index === lessons.length - 1 ? false : lessons[index + 1].node
          const next = index === 0 ? false : lessons[index - 1].node

          createPage({
            path: post.node.fields.slug,
            component: lesson,
            context: {
              slug: post.node.fields.slug,
            },
          })
        })
      })
    )
  })
}

exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
  const { createNodeField } = boundActionCreators

  if (node.internal.type === `MarkdownRemark`) {
    const value = createFilePath({ node, getNode })
    createNodeField({
      name: `slug`,
      node,
      value,
    })
  }
}

gatsby-browser.js: not changed
gatsby-ssr.js: not changed

@ajrussellaudio
Copy link
Author

This seems to be a duplicate of #4148

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant