6.14.0 • Published 6 months ago

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

Weekly downloads
54,058
License
MIT
Repository
github
Last release
6 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 />
@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-1722test-gatsby-theme-grande@draftbox-co/gatsby-wordpress-novela-theme@draftbox-co/gatsby-ghost-novela-theme@dschau/gatsby-theme-blog@dseoane/gatsby-theme-blog-core@codeanit.com/theme@cthsiao/gatsby-theme-novela-mod@commitd/gatsby-theme-docs@committed/gatsby-theme-docs@commercetools-docs/gatsby-theme-docs@commercetools-docs/gatsby-theme-commercetools-docs@coursemaker/gatsby-theme-coursemaker@dotnetthailand/gatsby-theme-minimal-portfolio@boomerang-io/gatsby-theme-boomerang@cangir/gatsby-theme-cms-corejesielviana-gatsby-theme-docs-corejay-blog@divramod/gatsby-theme-ts-mdx@arcblock/gatsby-config@arcblock/gatsby-theme-docs@arcblock/gatsby-theme-www@appbaseio/reactive-manual-v3@appbaseio/reactive-manual-vue@availity/gatsby-theme@availity/gatsby-theme-docs@adobe/gatsby-theme-aio@alec.brunelle/gatsby-theme-novela@alicloud/gatsby-theme-console-doc@beccanelson/gatsby-theme-writer@agney/gatsby-theme-advent@amberleyromo/gatsby-theme-blog@bjerra/gatsby-theme-bd-salong@binance-name/gatsby-theme-novela@celcomdesign/iconography@chr.fritsch/gatsby-theme-blog@chrismwilliams/gatsby-theme-cactus@chakrihacker/gatsby-theme-fyndx@clifmo/gatsby-theme-novela1kohei110secondsofcode-custom@adamquadmon/gatsby-theme-lucifero@codinators/gatsby-theme-blog@codynhat/gatsby-theme-cactus@brainhubeu/gatsby-docs-kit@dexgroup/gatsby-theme-blog@daniel.husar/gatsby-theme-spring@davidway/gatsby-theme-novelaygh-website@hangindev/gatsby-theme-courses@foudroyer/honey@foudroyer/purity@fr8/gatsby-theme@gatsbyjs/gatsby-theme-website@getzeroday/gatsby-theme-novela@kumarvaralakshmi/gatsby-theme-ghost@kopilot/gatsby-theme-apollo-docs@lbsonley/gatsby-theme-technical-blog@inthepocket/hubble-docs-theme@m00n/gatsby-starter-docs-material-ui@mosaic-io/gatsby-theme-novela-local-blog-fork@mtdev/gatsby-theme-terminal@marscollective/gatsby-theme-core@narative/gatsby-theme-novela@negati-ve/gatsby-theme-novela@nickymeuleman/gatsby-theme-blog@nicolasbarbe/gatsby-theme-aio@na399/gatsby-theme-carbon@eshlox/gatsby-theme-axii@eggheadio/gatsby-theme-egghead-blog@mnemonicorg/gatsby-theme-archive-site@mklabs/gatsby-theme-docs-core@jiratki/gatsby-theme-minimal-portfolio@jlongster/gatsby-theme-novela@josefaidt/gatsby-theme@joshkennedy00/gatsby-theme-blog-remix@imedadel/gatsby-theme-docs@jckr/ocular-gatsby
6.14.0

8 months ago

6.15.0-next.0

8 months ago

6.13.2

10 months ago

6.14.0-next.2

1 year ago

6.13.1

1 year ago

6.14.0-next.1

1 year ago

6.13.0

2 years ago

6.14.0-next.0

2 years ago

6.12.0

2 years ago

6.12.0-next.0

2 years ago

6.11.0-next.0

2 years ago

6.11.0-next.1

2 years ago

6.13.0-next.0

2 years ago

6.11.0

2 years ago

6.10.0

2 years ago

6.10.0-next.0

2 years ago

6.10.0-next.1

2 years ago

6.10.0-next.2

2 years ago

6.9.0

2 years ago

6.9.0-next.0

2 years ago

6.9.0-next.1

2 years ago

6.7.0

2 years ago

6.8.0

2 years ago

6.8.0-next.0

2 years ago

6.6.0-next.0

2 years ago

6.5.0-next.1

2 years ago

6.5.0

2 years ago

6.6.0

2 years ago

6.4.0

2 years ago

6.7.0-next.0

2 years ago

6.3.0-next.0

3 years ago

6.0.0

3 years ago

6.5.0-next.0

2 years ago

6.4.0-next.0

3 years ago

6.4.0-next.1

2 years ago

4.12.0

3 years ago

6.0.0-next.0

3 years ago

6.0.0-next.1

3 years ago

6.0.0-next.2

3 years ago

6.1.0-next.0

3 years ago

6.1.0

3 years ago

6.2.0

3 years ago

6.3.0

3 years ago

5.25.0

3 years ago

6.2.0-next.0

3 years ago

5.23.0-next.0

3 years ago

5.22.0

3 years ago

5.24.0-next.0

3 years ago

5.24.0-next.1

3 years ago

5.23.0

3 years ago

5.24.0

3 years ago

5.25.0-next.0

3 years ago

5.20.0

3 years ago

5.16.0-next.0

3 years ago

5.19.0-next.0

3 years ago

5.21.0

3 years ago

5.19.0-next.1

3 years ago

5.15.0

3 years ago

5.17.0-next.0

3 years ago

5.16.0

3 years ago

5.17.0

3 years ago

5.22.0-next.0

3 years ago

5.18.0

3 years ago

5.19.0

3 years ago

5.21.0-next.0

3 years ago

5.18.0-next.1

3 years ago

5.18.0-next.0

3 years ago

5.20.0-next.0

3 years ago

5.15.0-next.0

3 years ago

5.14.0-next.1

3 years ago

5.14.0-next.0

3 years ago

5.14.0-next.2

3 years ago

5.13.0

3 years ago

5.14.0

3 years ago

5.10.0-next.2

3 years ago

5.13.0-next.0

3 years ago

5.11.0-next.0

3 years ago

5.11.0-next.1

3 years ago

5.10.0

3 years ago

5.11.0

3 years ago

5.12.1

3 years ago

5.12.0

3 years ago

5.12.0-next.0

3 years ago

5.12.0-next.1

3 years ago

5.10.0-next.1

3 years ago

5.10.0-next.0

3 years ago

5.9.0

3 years ago

5.8.0-next.0

3 years ago

5.8.0

3 years ago

5.5.0-next.0

4 years ago

5.3.0

4 years ago

5.4.0

4 years ago

5.6.0-next.0

3 years ago

5.5.0

3 years ago

5.4.0-next.0

4 years ago

5.7.0-next.0

3 years ago

5.6.0

3 years ago

5.9.0-next.0

3 years ago

5.7.0

3 years ago

5.2.0

4 years ago

5.3.0-next.0

4 years ago

5.1.0

4 years ago

5.2.0-next.0

4 years ago

5.1.0-next.0

4 years ago

5.0.0

4 years ago

5.0.0-zz-next.2

4 years ago

4.11.0

4 years ago

5.0.0-zz-next.8

4 years ago

5.0.0-zz-next.1

4 years ago

5.0.0-next.1

4 years ago

5.0.0-next.0

4 years ago

4.11.0-next.2

4 years ago

4.11.0-next.1

4 years ago

4.10.0

4 years ago

4.11.0-next.0

4 years ago

4.9.0

4 years ago

4.10.0-next.0

4 years ago

4.9.0-next.3

4 years ago

4.9.0-next.1

4 years ago

4.9.0-next.2

4 years ago

4.8.0

4 years ago

4.9.0-next.0

4 years ago

4.7.0

4 years ago

4.8.0-next.0

4 years ago

4.7.0-next.1

4 years ago

4.6.0

4 years ago

4.7.0-next.0

4 years ago

4.5.0

4 years ago

4.6.0-next.0

4 years ago

4.5.0-next.0

4 years ago

4.5.0-next.1

4 years ago

4.4.1

4 years ago

4.4.0

4 years ago

4.3.0

4 years ago

4.4.0-next.2

4 years ago

4.4.0-next.1

4 years ago

4.3.0-next.0

4 years ago

4.3.0-next.1

4 years ago

4.2.0-next.0

4 years ago

4.2.1

4 years ago

4.2.0

4 years ago

4.4.0-next.0

4 years ago

4.1.0

4 years ago

4.0.0

4 years ago

4.0.0-next.0

4 years ago

4.1.0-next.0

4 years ago

3.3.0-next.1

4 years ago

3.2.0

4 years ago

3.3.0-next.0

4 years ago

3.1.0

4 years ago

3.2.0-next.0

4 years ago

3.1.0-next.2

4 years ago

3.0.0

4 years ago

3.1.0-next.1

4 years ago

3.1.0-next.0

4 years ago

3.0.0-v3rc.0

4 years ago

3.0.0-next.0

4 years ago

2.10.0

4 years ago

2.11.0-next.0

4 years ago

2.9.0

4 years ago

2.10.0-next.0

4 years ago

2.8.0

4 years ago

2.9.0-next.0

4 years ago

2.7.0

5 years ago

2.8.0-next.0

5 years ago

2.6.0

5 years ago

2.7.0-next.0

5 years ago

2.5.0

5 years ago

2.6.0-next.0

5 years ago

2.4.0

5 years ago

2.4.0-next.1

5 years ago

2.5.0-next.0

5 years ago

2.4.0-next.0

5 years ago

2.3.19

5 years ago

2.3.18

5 years ago

2.3.17

5 years ago

2.3.16

5 years ago

2.3.15

5 years ago

2.3.14

5 years ago

2.3.13

5 years ago

2.3.12

5 years ago

2.3.11

5 years ago

2.3.10

5 years ago

2.3.9

5 years ago

2.3.7

5 years ago

2.3.6

5 years ago

2.3.5

5 years ago

2.3.4

5 years ago

2.3.3

5 years ago

2.3.2

5 years ago

2.3.1

5 years ago

2.3.0

5 years ago

2.2.4

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.40

5 years ago

2.1.39

5 years ago

2.1.38

5 years ago

2.1.37

5 years ago

2.1.36

5 years ago

2.1.35

5 years ago

2.1.33

6 years ago

2.1.31

6 years ago

2.1.30

6 years ago

2.1.29

6 years ago

2.1.28

6 years ago

2.1.27

6 years ago

2.1.26

6 years ago

2.1.25

6 years ago

2.1.24

6 years ago

2.1.23

6 years ago

2.1.21

6 years ago

2.1.20

6 years ago

2.1.19

6 years ago

2.1.18

6 years ago

2.1.17

6 years ago

2.1.16

6 years ago

2.1.15

6 years ago

2.1.14

6 years ago

2.1.13

6 years ago

2.1.12

6 years ago

2.1.11

6 years ago

2.1.10

6 years ago

2.1.9

6 years ago

2.1.8

6 years ago

2.1.7

6 years ago

2.1.6

6 years ago

2.1.5

6 years ago

2.1.4

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.13

6 years ago

2.0.12

6 years ago

2.0.11

6 years ago

2.0.10

6 years ago

2.0.9

6 years ago

2.0.8

7 years ago

2.0.7

7 years ago

2.0.6

7 years ago

2.0.5

7 years ago

2.0.0-rc.5

7 years ago

2.0.0-rc.4

7 years ago

2.0.0-rc.3

7 years ago

2.0.0-rc.1

7 years ago

2.0.0-rc.0

7 years ago

2.0.0-beta.3

7 years ago

2.0.0-beta.2

7 years ago

2.0.0-beta.1

7 years ago

2.0.0-beta.0

7 years ago

1.5.37

7 years ago

2.0.0-alpha.2

7 years ago

1.5.28-13

7 years ago

1.5.36

7 years ago

1.5.28-12

7 years ago

1.5.28-11

7 years ago

1.5.35

7 years ago

1.5.34

7 years ago

1.5.28-10

7 years ago

1.5.28-9

7 years ago

1.5.33

7 years ago

1.5.28-8

7 years ago

1.5.28-6

7 years ago

1.5.28-5

7 years ago

1.5.28-4

7 years ago

1.5.28-3

7 years ago

1.5.32

7 years ago

1.5.31

7 years ago

1.5.28-2

7 years ago

1.5.28-1

7 years ago

1.5.30

7 years ago

1.5.29

7 years ago

1.5.28

7 years ago

1.5.28-0

7 years ago

1.5.27

7 years ago

1.5.26

7 years ago

1.5.25

8 years ago

1.5.24

8 years ago

1.5.23

8 years ago

1.5.22

8 years ago

1.5.21

8 years ago

1.5.20

8 years ago

1.5.19

8 years ago

1.5.18

8 years ago

1.5.17

8 years ago

1.5.16

8 years ago

1.5.15

8 years ago

1.5.14

8 years ago

1.5.13

8 years ago

1.5.12

8 years ago

1.5.11

8 years ago

1.5.10

8 years ago

1.5.9

8 years ago

1.5.8

8 years ago

1.5.7

8 years ago

1.5.6

8 years ago

1.5.5

8 years ago

1.5.4

8 years ago

1.5.3

8 years ago

2.0.0

8 years ago

1.5.2

8 years ago

1.5.1

8 years ago

1.5.0

8 years ago

1.4.1

8 years ago

1.3.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

1.0.0-beta.6

8 years ago

1.0.0-beta.1

8 years ago

1.0.0-alpha.23

8 years ago

1.0.0-alpha.22

8 years ago

1.0.0-alpha17

8 years ago

1.0.0-alpha15

8 years ago

1.0.0-alpha14

8 years ago