6.13.1 • Published 3 months ago

gatsby-remark-copy-linked-files v6.13.1

Weekly downloads
54,058
License
MIT
Repository
github
Last release
3 months ago

gatsby-remark-copy-linked-files

Copies local files linked to/from Markdown (.md|.markdown) files to the public folder.

A sample markdown file:

---
title: My awesome blog post
---

Hey everyone, I just made a sweet PDF with lots of interesting stuff in it.

[Download it now](my-awesome-pdf.pdf)

When you build your site:

The my-awesome-pdf.pdf file will be copied to the public folder (i.e., public/some-really-long-contenthash/my-awesome-pdf.pdf) and the generated HTML page will be modified to point to it.

Note: The my-awesome-pdf.pdf file should be in the same directory as the markdown file.

Installation

npm install gatsby-remark-copy-linked-files

Configuration

Default settings

Add gatsby-remark-copy-linked-files plugin as a plugin to gatsby-transformer-remark:

module.exports = {
  plugins: [
    {
      resolve: `gatsby-transformer-remark`,
      options: {
        plugins: [`gatsby-remark-copy-linked-files`],
      },
    },
  ],
}

Custom settings

module.exports = {
  plugins: [
    {
      resolve: `gatsby-transformer-remark`,
      options: {
        plugins: [
          {
            resolve: `gatsby-remark-copy-linked-files`,
            options: {
              destinationDir: `path/to/dir`,
              ignoreFileExtensions: [`png`, `jpg`, `jpeg`, `bmp`, `tiff`],
            },
          },
        ],
      },
    },
  ],
}

Option: destinationDir

By default, all files will be copied to the root of the public folder in the following format: contentHash/fileName.ext.

For example, [Download it now](my-awesome-pdf.pdf) will copy the file my-awesome-pdf.pdf to something like public/2a0039f3a61f4510f41678438e4c863a/my-awesome-pdf.pdf

Simple usage

To change this, set destinationDir to a path of your own choosing (i.e., path/to/dir).

{
  resolve: `gatsby-transformer-remark`,
  options: {
    plugins: [
      {
        resolve: "gatsby-remark-copy-linked-files",
        options: {
          destinationDir: "path/to/dir",
        },
      },
    ],
  },
}

So now, [Download it now](my-awesome-pdf.pdf) will copy the file my-awesome-pdf.pdf to public/path/to/dir/2a0039f3a61f4510f41678438e4c863a/my-awesome-pdf.pdf

Advanced usage

For more control, set destinationDir to a function expression using properties name, hash, and absolutePath to specify the path.

  • name: The name of the file without the file extension
  • hash: The internal.contentDigest on the File node (guarantees a unique identifier)
  • absolutePath: The absolute path to the file, e.g. /Users/your-name/example/project/src/pages/folder/my-awesome-pdf.pdf

Examples:

# save `my-awesome-pdf.pdf` to `public/my-awesome-pdf.pdf`
destinationDir: f => `${f.name}`

# save `my-awesome-pdf.pdf` to `public/2a0039f3a61f4510f41678438e4c863a.pdf`
destinationDir: f => `${f.hash}`

# save `my-awesome-pdf.pdf` to `public/downloads/2a0039f3a61f4510f41678438e4c863a/my-awesome-pdf.pdf`
destinationDir: f => `downloads/${f.hash}/${f.name}`

# save `my-awesome-pdf.pdf` to `public/downloads/2a0039f3a61f4510f41678438e4c863a-my-awesome-pdf.pdf`
destinationDir: f => `downloads/${f.hash}-${f.name}`

# save `my-awesome-pdf.pdf` to `public/my-awesome-pdf/2a0039f3a61f4510f41678438e4c863a.pdf`
destinationDir: f => `${f.name}/${f.hash}`

# save `my-awesome-pdf.pdf` to `public/path/to/dir/hello-my-awesome-pdf+2a0039f3a61f4510f41678438e4c863a_world.pdf`
destinationDir: f => `path/to/dir/hello-${f.name}+${f.hash}_world`

# save `src/pages/custom-folder/my-awesome-pdf.pdf` to `public/custom-folder/my-awesome-pdf.pdf`
# Note: Import `path` to use this example https://nodejs.org/api/path.html
destinationDir: f => `${path.dirname(path.relative(path.join(__dirname, `src`, `pages`), f.absolutePath))}/${f.name}`

Please note: Make sure you use either name or hash property in your function expression!

If you don't include both name and hash properties in your function expression, gatsby-remark-copy-linked-files plugin will resolve the function expression to a string value and use default settings as a fallback mechanism to prevent your local files from getting copied with the same name (causing files to get overwritten).

# Note: `my-awesome-pdf.pdf` is saved to `public/hello/2a0039f3a61f4510f41678438e4c863a/my-awesome-pdf.pdf`
# because `name` and `hash` properties are not referenced in the function expression.
# So these function expressions are treated the same way
destinationDir: _ => `hello`
destinationDir: `hello`

Caveat: Error thrown if destinationDir points outside the public folder

Please note: An error will be thrown if the destination points outside the public folder.

Correct:

# saves to `public/path/to/dir/`
destinationDir: `path/to/dir`

# saves to `public/path/to/dir/`
destinationDir: _ => `path/to/dir`

# saves to `public/path/to/dir/fileName.ext`
destinationDir: f => `path/to/dir/${f.name}`

# saves to `public/contentHash.ext`
destinationDir: f => `${f.hash}`

Error thrown:

# cannot save outside `public` folder
destinationDir: `../path/to/dir`
destinationDir: _ => `../path/to/dir`
destinationDir: f => `../path/to/dir/${f.name}`
destinationDir: f => `../${f.hash}`

Custom set which file types to ignore using ignoreFileExtensions

By default, the file types that this plugin ignores are: png, jpg, jpeg, bmp, tiff. For example, [Download it now](image.png) will be ignored and not copied to the root of the public folder.

To change this, set ignoreFileExtensions to an array of extensions to ignore (i.e., an empty array [] to ignore nothing).

{
  resolve: `gatsby-transformer-remark`,
  options: {
    plugins: [
      {
        resolve: "gatsby-remark-copy-linked-files",
        options: {
          // `ignoreFileExtensions` defaults to [`png`, `jpg`, `jpeg`, `bmp`, `tiff`]
          // as we assume you'll use gatsby-remark-images to handle
          // images in markdown as it automatically creates responsive
          // versions of images.
          //
          // If you'd like to not use gatsby-remark-images and just copy your
          // original images to the public directory, set
          // `ignoreFileExtensions` to an empty array.
          ignoreFileExtensions: [],
        },
      },
    ],
  },
}

So now, [Download it now](image.png) will be copied to the root of the public folder.

Supported Markdown tags

  • img - ![Image](my-img.png)
  • link - [Link](myFile.txt)

Supported HTML tags

  • <img />
  • <video />
  • <audio />
  • <a />
@commercetools-docs/gatsby-theme-docs@pil0t/gatsby-theme-novela@thirdwave-network/thirdwave-gatsby-theme@studiobear/gatsby-theme-ionic@studiobear/gatsby-theme-blog@kyrelldixon/gatsby-theme-novela@one-builder/gatsby-theme-ob-master@studiobear/gatsby-theme-blog-ionicgatsby-theme-ob-master@component-driven/gatsby-theme-content-collectionsgatsby-theme-lumencustom-gatsby-theme-novelagatsby-theme-jdoe@stvad/gatsby-theme-garden@ekampf/gatsby-theme-networked-thought@fengshangwuqi/gatsby-config@sandalboyz/gatsby-theme-sandalboyzgatsby-theme-sandalboyz@roman-hotsiy/redocly-dev-portal@yiqilaitech/gatsby-theme-coregatsby-theme-serena-v2@hesburgh-wse/gatsby-theme-marblebrawl-capped-webpage@everything-registry/sub-chunk-1722em-docs-engineeldh.cogatsby-philipps-foam-themegatsby-plugin-bluewingsgatsby-docs-startergatsby-elizium-gitbook-startergatsby-theme-nodeschoolfluent-rn-websitegat-themegatsby-blog-stater-with-typescriptgatsby-boilerplategatsby-all-pack-theme-starterfreight-trust-gatsby-freight-themejesielviana-gatsby-theme-docs-corejay-blog@gatsbyjs/gatsby-theme-websiteklair.co@getzeroday/gatsby-theme-novelagatsby-theme-academicgatsby-theme-academic-modifiedgatsby-theme-advancedgatsby-theme-amaranthgatsby-starter-tailwind-mdx-bloggatsby-starter-yonggatsby-starter-zoomkodinggatsby-starter-hero-bloggatsby-starter-lammagatsby-starter-lumengatsby-starter-morning-dewgatsby-starter-netlify-tailwindgatsby-theme-carbongatsby-theme-carbon-basegatsby-starter-bloggatsby-starter-blog-dev-mdxgatsby-starter-blog-no-stylesgatsby-starter-blog-storyblokgatsby-starter-bootstrapgatsby-starter-caspergatsby-starter-events-listgatsby-theme-caspergatsby-theme-casper-v3gatsby-theme-catalyst-coregatsby-theme-blog-ml-coregatsby-theme-blog-oceangatsby-theme-blogmicgatsby-theme-bluebase-docsgatsby-theme-beddygatsby-skeleton-startergatsby-roamgraphql-bloghive-honeycombhubble-docs-themegatsby-theme-foundrygatsby_theme_unicorngatsby-v2-starter-lumengatsby-v2-typescript-startergatsby-theme-ant-docsgatsby-theme-antd-docsgatsby-theme-antdsitegatsby-theme-coursesgatsby-theme-copogatsby-theme-digettgatsby-theme-digett-landinggatsby-theme-primer-wikigatsby-theme-private-spheregatsby-theme-projectagatsby-theme-styled-mdxgatsby-theme-tatogatsby-theme-oculargatsby-theme-official-docsgatsby-theme-operettagatsby-theme-patternflygatsby-theme-pawan-bloggatsby-theme-personal-bloggatsby-theme-serenagatsby-theme-simple-blog
6.14.0-next.2

3 months ago

6.13.1

3 months ago

6.14.0-next.1

4 months ago

6.13.0

4 months ago

6.14.0-next.0

4 months ago

6.12.0

8 months ago

6.12.0-next.0

10 months ago

6.11.0-next.0

11 months ago

6.11.0-next.1

11 months ago

6.13.0-next.0

9 months ago

6.11.0

10 months ago

6.10.0

11 months ago

6.10.0-next.0

1 year ago

6.10.0-next.1

12 months ago

6.10.0-next.2

12 months ago

6.9.0

1 year ago

6.9.0-next.0

1 year ago

6.9.0-next.1

1 year ago

6.7.0

1 year ago

6.8.0

1 year ago

6.8.0-next.0

1 year ago

6.6.0-next.0

1 year ago

6.5.0-next.1

1 year ago

6.5.0

1 year ago

6.6.0

1 year ago

6.4.0

1 year ago

6.7.0-next.0

1 year ago

6.3.0-next.0

1 year ago

6.0.0

1 year ago

6.5.0-next.0

1 year ago

6.4.0-next.0

1 year ago

6.4.0-next.1

1 year ago

4.12.0

1 year ago

6.0.0-next.0

2 years ago

6.0.0-next.1

2 years ago

6.0.0-next.2

1 year ago

6.1.0-next.0

1 year ago

6.1.0

1 year ago

6.2.0

1 year ago

6.3.0

1 year ago

5.25.0

1 year ago

6.2.0-next.0

1 year ago

5.23.0-next.0

2 years ago

5.22.0

2 years ago

5.24.0-next.0

2 years ago

5.24.0-next.1

2 years ago

5.23.0

2 years ago

5.24.0

2 years ago

5.25.0-next.0

2 years ago

5.20.0

2 years ago

5.16.0-next.0

2 years ago

5.19.0-next.0

2 years ago

5.21.0

2 years ago

5.19.0-next.1

2 years ago

5.15.0

2 years ago

5.17.0-next.0

2 years ago

5.16.0

2 years ago

5.17.0

2 years ago

5.22.0-next.0

2 years ago

5.18.0

2 years ago

5.19.0

2 years ago

5.21.0-next.0

2 years ago

5.18.0-next.1

2 years ago

5.18.0-next.0

2 years ago

5.20.0-next.0

2 years ago

5.15.0-next.0

2 years ago

5.14.0-next.1

2 years ago

5.14.0-next.0

2 years ago

5.14.0-next.2

2 years ago

5.13.0

2 years ago

5.14.0

2 years ago

5.10.0-next.2

2 years ago

5.13.0-next.0

2 years ago

5.11.0-next.0

2 years ago

5.11.0-next.1

2 years ago

5.10.0

2 years ago

5.11.0

2 years ago

5.12.1

2 years ago

5.12.0

2 years ago

5.12.0-next.0

2 years ago

5.12.0-next.1

2 years ago

5.10.0-next.1

2 years ago

5.10.0-next.0

2 years ago

5.9.0

2 years ago

5.8.0-next.0

2 years ago

5.8.0

2 years ago

5.5.0-next.0

2 years ago

5.3.0

2 years ago

5.4.0

2 years ago

5.6.0-next.0

2 years ago

5.5.0

2 years ago

5.4.0-next.0

2 years ago

5.7.0-next.0

2 years ago

5.6.0

2 years ago

5.9.0-next.0

2 years ago

5.7.0

2 years ago

5.2.0

2 years ago

5.3.0-next.0

2 years ago

5.1.0

2 years ago

5.2.0-next.0

2 years ago

5.1.0-next.0

3 years ago

5.0.0

2 years ago

5.0.0-zz-next.2

3 years ago

4.11.0

3 years ago

5.0.0-zz-next.8

3 years ago

5.0.0-zz-next.1

3 years ago

5.0.0-next.1

3 years ago

5.0.0-next.0

3 years ago

4.11.0-next.2

3 years ago

4.11.0-next.1

3 years ago

4.10.0

3 years ago

4.11.0-next.0

3 years ago

4.9.0

3 years ago

4.10.0-next.0

3 years ago

4.9.0-next.3

3 years ago

4.9.0-next.1

3 years ago

4.9.0-next.2

3 years ago

4.8.0

3 years ago

4.9.0-next.0

3 years ago

4.7.0

3 years ago

4.8.0-next.0

3 years ago

4.7.0-next.1

3 years ago

4.6.0

3 years ago

4.7.0-next.0

3 years ago

4.5.0

3 years ago

4.6.0-next.0

3 years ago

4.5.0-next.0

3 years ago

4.5.0-next.1

3 years ago

4.4.1

3 years ago

4.4.0

3 years ago

4.3.0

3 years ago

4.4.0-next.2

3 years ago

4.4.0-next.1

3 years ago

4.3.0-next.0

3 years ago

4.3.0-next.1

3 years ago

4.2.0-next.0

3 years ago

4.2.1

3 years ago

4.2.0

3 years ago

4.4.0-next.0

3 years ago

4.1.0

3 years ago

4.0.0

3 years ago

4.0.0-next.0

3 years ago

4.1.0-next.0

3 years ago

3.3.0-next.1

3 years ago

3.2.0

3 years ago

3.3.0-next.0

3 years ago

3.1.0

3 years ago

3.2.0-next.0

3 years ago

3.1.0-next.2

3 years ago

3.0.0

3 years ago

3.1.0-next.1

3 years ago

3.1.0-next.0

3 years ago

3.0.0-v3rc.0

3 years ago

3.0.0-next.0

3 years ago

2.10.0

3 years ago

2.11.0-next.0

3 years ago

2.9.0

3 years ago

2.10.0-next.0

3 years ago

2.8.0

3 years ago

2.9.0-next.0

3 years ago

2.7.0

3 years ago

2.8.0-next.0

3 years ago

2.6.0

3 years ago

2.7.0-next.0

3 years ago

2.5.0

3 years ago

2.6.0-next.0

3 years ago

2.4.0

3 years ago

2.4.0-next.1

3 years ago

2.5.0-next.0

3 years ago

2.4.0-next.0

3 years ago

2.3.19

4 years ago

2.3.18

4 years ago

2.3.17

4 years ago

2.3.16

4 years ago

2.3.15

4 years ago

2.3.14

4 years ago

2.3.13

4 years ago

2.3.12

4 years ago

2.3.11

4 years ago

2.3.10

4 years ago

2.3.9

4 years ago

2.3.7

4 years ago

2.3.6

4 years ago

2.3.5

4 years ago

2.3.4

4 years ago

2.3.3

4 years ago

2.3.2

4 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.4

4 years ago

2.2.3

4 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.40

4 years ago

2.1.39

4 years ago

2.1.38

4 years ago

2.1.37

4 years ago

2.1.36

4 years ago

2.1.35

4 years ago

2.1.33

4 years ago

2.1.31

4 years ago

2.1.30

4 years ago

2.1.29

4 years ago

2.1.28

5 years ago

2.1.27

5 years ago

2.1.26

5 years ago

2.1.25

5 years ago

2.1.24

5 years ago

2.1.23

5 years ago

2.1.21

5 years ago

2.1.20

5 years ago

2.1.19

5 years ago

2.1.18

5 years ago

2.1.17

5 years ago

2.1.16

5 years ago

2.1.15

5 years ago

2.1.14

5 years ago

2.1.13

5 years ago

2.1.12

5 years ago

2.1.11

5 years ago

2.1.10

5 years ago

2.1.9

5 years ago

2.1.8

5 years ago

2.1.7

5 years ago

2.1.6

5 years ago

2.1.5

5 years ago

2.1.4

5 years ago

2.1.3

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.13

5 years ago

2.0.12

5 years ago

2.0.11

5 years ago

2.0.10

5 years ago

2.0.9

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

6 years ago

2.0.0-rc.5

6 years ago

2.0.0-rc.4

6 years ago

2.0.0-rc.3

6 years ago

2.0.0-rc.1

6 years ago

2.0.0-rc.0

6 years ago

2.0.0-beta.3

6 years ago

2.0.0-beta.2

6 years ago

2.0.0-beta.1

6 years ago

2.0.0-beta.0

6 years ago

1.5.37

6 years ago

2.0.0-alpha.2

6 years ago

1.5.28-13

6 years ago

1.5.36

6 years ago

1.5.28-12

6 years ago

1.5.28-11

6 years ago

1.5.35

6 years ago

1.5.34

6 years ago

1.5.28-10

6 years ago

1.5.28-9

6 years ago

1.5.33

6 years ago

1.5.28-8

6 years ago

1.5.28-6

6 years ago

1.5.28-5

6 years ago

1.5.28-4

6 years ago

1.5.28-3

6 years ago

1.5.32

6 years ago

1.5.31

6 years ago

1.5.28-2

6 years ago

1.5.28-1

6 years ago

1.5.30

6 years ago

1.5.29

6 years ago

1.5.28

6 years ago

1.5.28-0

6 years ago

1.5.27

6 years ago

1.5.26

6 years ago

1.5.25

6 years ago

1.5.24

6 years ago

1.5.23

6 years ago

1.5.22

6 years ago

1.5.21

6 years ago

1.5.20

6 years ago

1.5.19

6 years ago

1.5.18

6 years ago

1.5.17

6 years ago

1.5.16

6 years ago

1.5.15

6 years ago

1.5.14

6 years ago

1.5.13

6 years ago

1.5.12

6 years ago

1.5.11

6 years ago

1.5.10

7 years ago

1.5.9

7 years ago

1.5.8

7 years ago

1.5.7

7 years ago

1.5.6

7 years ago

1.5.5

7 years ago

1.5.4

7 years ago

1.5.3

7 years ago

2.0.0

7 years ago

1.5.2

7 years ago

1.5.1

7 years ago

1.5.0

7 years ago

1.4.1

7 years ago

1.3.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

1.0.0-beta.6

7 years ago

1.0.0-beta.1

7 years ago

1.0.0-alpha.23

7 years ago

1.0.0-alpha.22

7 years ago

1.0.0-alpha17

7 years ago

1.0.0-alpha15

7 years ago

1.0.0-alpha14

7 years ago