|
1 | 1 | # gatsby-remark-copy-linked-files
|
2 | 2 |
|
3 |
| -Copies local files linked to/from markdown to your `public` folder. |
| 3 | +Copies local files linked to/from Markdown (`.md|.markdown`) files to the root directory (i.e., `public` folder). |
4 | 4 |
|
5 |
| -## Install |
| 5 | +**A sample markdown file:** |
| 6 | + |
| 7 | +```md |
| 8 | +--- |
| 9 | +title: My awesome blog post |
| 10 | +--- |
| 11 | + |
| 12 | +Hey everyone, I just made a sweet PDF with lots of interesting stuff in it. |
| 13 | + |
| 14 | +[Download it now](my-awesome-pdf.pdf) |
| 15 | +``` |
| 16 | + |
| 17 | +**When you build your site:** |
| 18 | + |
| 19 | +The `my-awesome-pdf.pdf` file will be copied to the root directory (i.e., `public/some-really-long-contenthash/my-awesome-pdf.pdf`) and the generated HTML page will be modified to point to it. |
| 20 | + |
| 21 | +> **Note**: The `my-awesome-pdf.pdf` file should be in the same directory as the markdown file. |
| 22 | +
|
| 23 | +--- |
| 24 | + |
| 25 | +## Install plugin |
6 | 26 |
|
7 | 27 | `npm install --save gatsby-remark-copy-linked-files`
|
8 | 28 |
|
9 |
| -## How to use |
| 29 | +## Add plugin to Gatsby Config |
| 30 | + |
| 31 | +**Default settings:** |
10 | 32 |
|
11 |
| -#### Basic usage |
| 33 | +Add `gatsby-remark-copy-linked-files` plugin as a plugin to [`gatsby-transformer-remark`](https://www.gatsbyjs.org/packages/gatsby-transformer-remark/): |
12 | 34 |
|
13 | 35 | ```javascript
|
14 | 36 | // In your gatsby-config.js
|
| 37 | + |
| 38 | +// add plugin by name only |
15 | 39 | plugins: [
|
16 | 40 | {
|
17 | 41 | resolve: `gatsby-transformer-remark`,
|
18 | 42 | options: {
|
19 |
| - plugins: ["gatsby-remark-copy-linked-files"], |
| 43 | + plugins: [`gatsby-remark-copy-linked-files`], |
20 | 44 | },
|
21 | 45 | },
|
22 | 46 | ]
|
23 | 47 | ```
|
24 | 48 |
|
25 |
| -### How to change the directory the files are added to. |
| 49 | +**Custom settings:** |
26 | 50 |
|
27 |
| -By default, all files will be copied to the root of the `public` dir, but you |
28 |
| -can choose a different location using the `destinationDir` option. Provide a |
29 |
| -path, relative to the `public` directory. The path must be within the public |
30 |
| -directory, so `path/to/dir` is fine, but `../../dir` is not. |
| 51 | +```js |
| 52 | +// In your gatsby-config.js |
31 | 53 |
|
| 54 | +// add plugin by name and options |
| 55 | +plugins: [ |
| 56 | + { |
| 57 | + resolve: `gatsby-transformer-remark`, |
| 58 | + options: { |
| 59 | + plugins: [ |
| 60 | + { |
| 61 | + resolve: `gatsby-remark-copy-linked-files`, |
| 62 | + options: { |
| 63 | + destinationDir: `path/to/dir`, |
| 64 | + ignoreFileExtensions: [`png`, `jpg`, `jpeg`, `bmp`, `tiff`], |
| 65 | + }, |
| 66 | + }, |
| 67 | + ], |
| 68 | + }, |
| 69 | + }, |
| 70 | +] |
32 | 71 | ```
|
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +## Custom set where to copy the files using `destinationDir` |
| 76 | + |
| 77 | +By default, all files will be copied to the root directory (i.e., `public` folder) in the following format: `contentHash/fileName.ext`. |
| 78 | + |
| 79 | +> 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` |
| 80 | +
|
| 81 | +### Simple usage |
| 82 | + |
| 83 | +To change this, set `destinationDir` to a path of your own choosing (i.e., `path/to/dir`). |
| 84 | + |
| 85 | +```js |
33 | 86 | // In your gatsby-config.js
|
34 | 87 | plugins: [
|
35 | 88 | {
|
36 | 89 | resolve: `gatsby-transformer-remark`,
|
37 | 90 | options: {
|
38 | 91 | plugins: [
|
39 | 92 | {
|
40 |
| - resolve: 'gatsby-remark-copy-linked-files', |
| 93 | + resolve: "gatsby-remark-copy-linked-files", |
41 | 94 | options: {
|
42 |
| - destinationDir: 'path/to/dir', |
43 |
| - } |
44 |
| - } |
45 |
| - ] |
46 |
| - } |
47 |
| - } |
| 95 | + destinationDir: "path/to/dir", |
| 96 | + }, |
| 97 | + }, |
| 98 | + ], |
| 99 | + }, |
| 100 | + }, |
48 | 101 | ]
|
49 | 102 | ```
|
50 | 103 |
|
51 |
| -### How to override which file types are ignored |
| 104 | +> 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` |
| 105 | +
|
| 106 | +### Advanced usage |
| 107 | + |
| 108 | +For more advanced control, set `destinationDir` to a function expression using properties `name` and/or `hash` to specify the path. |
| 109 | + |
| 110 | +**Examples:** |
| 111 | + |
| 112 | +```js |
| 113 | +# save `my-awesome-pdf.pdf` to `public/my-awesome-pdf.pdf` |
| 114 | +destinationDir: f => `${f.name}` |
| 115 | + |
| 116 | +# save `my-awesome-pdf.pdf` to `public/2a0039f3a61f4510f41678438e4c863a.pdf` |
| 117 | +destinationDir: f => `${f.hash}` |
| 118 | + |
| 119 | +# save `my-awesome-pdf.pdf` to `public/downloads/2a0039f3a61f4510f41678438e4c863a/my-awesome-pdf.pdf` |
| 120 | +destinationDir: f => `downloads/${f.hash}/${f.name}` |
| 121 | + |
| 122 | +# save `my-awesome-pdf.pdf` to `public/downloads/2a0039f3a61f4510f41678438e4c863a-my-awesome-pdf.pdf` |
| 123 | +destinationDir: f => `downloads/${f.hash}-${f.name}` |
| 124 | + |
| 125 | +# save `my-awesome-pdf.pdf` to `public/my-awesome-pdf/2a0039f3a61f4510f41678438e4c863a.pdf` |
| 126 | +destinationDir: f => `${f.name}/${f.hash}` |
| 127 | + |
| 128 | +# save `my-awesome-pdf.pdf` to `public/path/to/dir/hello-my-awesome-pdf+2a0039f3a61f4510f41678438e4c863a_world.pdf` |
| 129 | +destinationDir: f => `path/to/dir/hello-${f.name}+${f.hash}_world` |
| 130 | +``` |
| 131 | + |
| 132 | +> **Note:** Make sure you use either `name` or `hash` property in your function expression! |
| 133 | +> 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). |
| 134 | +
|
| 135 | +```js |
| 136 | +# Note: `my-awesome-pdf.pdf` is saved to `public/hello/2a0039f3a61f4510f41678438e4c863a/my-awesome-pdf.pdf` |
| 137 | +# because `name` and `hash` properties are not referenced in the function expression. |
| 138 | +# So these function expressions are treated the same way |
| 139 | +destinationDir: _ => `hello` |
| 140 | +destinationDir: `hello` |
| 141 | +``` |
| 142 | + |
| 143 | +### Caveat: Error thrown if `destinationDir` points outside the root directory (i.e. `public` folder) |
| 144 | + |
| 145 | +> **Note:** An error will be thrown if the destination points outside the root directory (i.e. `public` folder). |
| 146 | + |
| 147 | +**Correct:** |
| 148 | + |
| 149 | +```js |
| 150 | +# saves to `public/path/to/dir/` |
| 151 | +destinationDir: `path/to/dir` |
| 152 | + |
| 153 | +# saves to `public/path/to/dir/` |
| 154 | +destinationDir: _ => `path/to/dir` |
| 155 | + |
| 156 | +# saves to `public/path/to/dir/fileName.ext` |
| 157 | +destinationDir: f => `path/to/dir/${f.name}` |
| 158 | +
|
| 159 | +# saves to `public/contentHash.ext` |
| 160 | +destinationDir: f => `${f.hash}` |
| 161 | +``` |
| 162 | + |
| 163 | +**Error thrown:** |
| 164 | + |
| 165 | +```js |
| 166 | +# cannot save outside root directory (i.e., outside `public` folder) |
| 167 | +destinationDir: `../path/to/dir` |
| 168 | +destinationDir: _ => `../path/to/dir` |
| 169 | +destinationDir: f => `../path/to/dir/${f.name}` |
| 170 | +destinationDir: f => `../${f.hash}` |
| 171 | +``` |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +### Custom set which file types to ignore using `ignoreFileExtensions` |
| 176 | + |
| 177 | +By default, the file types that this plugin ignores are: `png`, `jpg`, `jpeg`, `bmp`, `tiff`. |
| 178 | + |
| 179 | +> For example, `[Download it now](image.png)` will be ignored and not copied to the root dir (i.e. `public` folder) |
| 180 | + |
| 181 | +To change this, set `ignoreFileExtensions` to an array of extensions to ignore (i.e., an empty array `[]` to ignore nothing). |
52 | 182 |
|
53 | 183 | ```javascript
|
54 | 184 | // In your gatsby-config.js
|
@@ -77,28 +207,14 @@ plugins: [
|
77 | 207 | ]
|
78 | 208 | ```
|
79 | 209 |
|
80 |
| -Then in your Markdown files, link to the file you desire to reference. |
81 |
| - |
82 |
| -E.g. |
| 210 | +> So now, `[Download it now](image.png)` will be copied to the root dir (i.e. `public` folder) |
83 | 211 |
|
84 |
| -```markdown |
85 | 212 | ---
|
86 |
| -title: My awesome blog post |
87 |
| ---- |
88 |
| - |
89 |
| -Hey everyone, I just made a sweet PDF with lots of interesting stuff in it. |
90 |
| - |
91 |
| -[Download it now](my-awesome-pdf.pdf) |
92 |
| -``` |
93 |
| - |
94 |
| -`my-awesome-pdf.pdf` should be in the same directory as the markdown file. When |
95 |
| -you build your site, the file will be copied to the `public` folder and the |
96 |
| -markdown HTML will be modified to point to it. |
97 | 213 |
|
98 | 214 | ### Supported Markdown tags
|
99 | 215 |
|
100 |
| -- img |
101 |
| -- link |
| 216 | +- img - `` |
| 217 | +- link - `[Link](myFile.txt)` |
102 | 218 |
|
103 | 219 | ### Supported HTML tags
|
104 | 220 |
|
|
0 commit comments