You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/gatsby-remark-copy-linked-files/README.md
+90-97
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# gatsby-remark-copy-linked-files
2
2
3
-
Copies local files linked to/from Markdown (`.md|.markdown`) files to the root directory (i.e., `public` folder).
3
+
Copies local files linked to/from Markdown (`.md|.markdown`) files to the `public` folder.
4
4
5
5
**A sample markdown file:**
6
6
@@ -16,96 +16,93 @@ Hey everyone, I just made a sweet PDF with lots of interesting stuff in it.
16
16
17
17
**When you build your site:**
18
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.
19
+
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.
20
20
21
21
> **Note**: The `my-awesome-pdf.pdf` file should be in the same directory as the markdown file.
22
22
23
-
---
24
-
25
-
## Install plugin
23
+
## Installation
26
24
27
-
`npm install gatsby-remark-copy-linked-files`
25
+
```shell
26
+
npm install gatsby-remark-copy-linked-files
27
+
```
28
28
29
-
## Add plugin to Gatsby Config
29
+
## Configuration
30
30
31
-
**Default settings:**
31
+
### Default settings
32
32
33
33
Add `gatsby-remark-copy-linked-files` plugin as a plugin to [`gatsby-transformer-remark`](https://www.gatsbyjs.com/plugins/gatsby-transformer-remark/):
## Custom set where to copy the files using `destinationDir`
71
+
## Option: `destinationDir`
76
72
77
-
By default, all files will be copied to the root directory (i.e.,`public` folder) in the following format: `contentHash/fileName.ext`.
73
+
By default, all files will be copied to the root of the`public` folder in the following format: `contentHash/fileName.ext`.
78
74
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`
75
+
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
76
81
77
### Simple usage
82
78
83
79
To change this, set `destinationDir` to a path of your own choosing (i.e., `path/to/dir`).
84
80
85
-
```js
86
-
// In your gatsby-config.js
87
-
plugins: [
88
-
{
89
-
resolve:`gatsby-transformer-remark`,
90
-
options: {
91
-
plugins: [
92
-
{
93
-
resolve:"gatsby-remark-copy-linked-files",
94
-
options: {
95
-
destinationDir:"path/to/dir",
96
-
},
81
+
```js:title=gatsby-config.js
82
+
{
83
+
resolve:`gatsby-transformer-remark`,
84
+
options: {
85
+
plugins: [
86
+
{
87
+
resolve:"gatsby-remark-copy-linked-files",
88
+
options: {
89
+
destinationDir:"path/to/dir",
97
90
},
98
-
],
99
-
},
91
+
},
92
+
],
100
93
},
101
-
]
94
+
}
102
95
```
103
96
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`
97
+
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
98
106
99
### Advanced usage
107
100
108
-
For more advanced control, set `destinationDir` to a function expression using properties `name` and/or `hash` to specify the path.
101
+
For more control, set `destinationDir` to a function expression using properties `name`, `hash`, and `absolutePath` to specify the path.
102
+
103
+
-`name`: The name of the file without the file extension
104
+
-`hash`: The `internal.contentDigest` on the `File` node (guarantees a unique identifier)
105
+
-`absolutePath`: The absolute path to the file, e.g. `/Users/your-name/example/project/src/pages/folder/my-awesome-pdf.pdf`
109
106
110
107
**Examples:**
111
108
@@ -127,10 +124,15 @@ destinationDir: f => `${f.name}/${f.hash}`
127
124
128
125
# save `my-awesome-pdf.pdf` to `public/path/to/dir/hello-my-awesome-pdf+2a0039f3a61f4510f41678438e4c863a_world.pdf`
> **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).
133
+
**Please note:** Make sure you use either `name` or `hash` property in your function expression!
134
+
135
+
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
136
135
137
```js
136
138
# Note:`my-awesome-pdf.pdf` is saved to `public/hello/2a0039f3a61f4510f41678438e4c863a/my-awesome-pdf.pdf`
@@ -140,9 +142,9 @@ destinationDir: _ => `hello`
140
142
destinationDir: `hello`
141
143
```
142
144
143
-
### Caveat: Error thrown if `destinationDir` points outside the root directory (i.e. `public`folder)
145
+
### Caveat: Error thrown if `destinationDir` points outside the `public` folder
144
146
145
-
> **Note:** An error will be thrown if the destination points outside the root directory (i.e. `public` folder).
147
+
**Please note:** An error will be thrown if the destination points outside the `public` folder.
146
148
147
149
**Correct:**
148
150
@@ -163,53 +165,44 @@ destinationDir: f => `${f.hash}`
163
165
**Error thrown:**
164
166
165
167
```js
166
-
# cannot save outside root directory (i.e., outside `public` folder)
168
+
# cannot save outside `public` folder
167
169
destinationDir: `../path/to/dir`
168
170
destinationDir: _ => `../path/to/dir`
169
171
destinationDir: f => `../path/to/dir/${f.name}`
170
172
destinationDir: f => `../${f.hash}`
171
173
```
172
174
173
-
---
174
-
175
175
### Custom set which file types to ignore using `ignoreFileExtensions`
176
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)
177
+
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.
180
178
181
179
To change this, set `ignoreFileExtensions` to an array of extensions to ignore (i.e., an empty array `[]` to ignore nothing).
182
180
183
-
```javascript
184
-
// In your gatsby-config.js
185
-
plugins: [
186
-
{
187
-
resolve: `gatsby-transformer-remark`,
188
-
options: {
189
-
plugins: [
190
-
{
191
-
resolve: "gatsby-remark-copy-linked-files",
192
-
options: {
193
-
// `ignoreFileExtensions` defaults to [`png`, `jpg`, `jpeg`, `bmp`, `tiff`]
194
-
// as we assume you'll use gatsby-remark-images to handle
195
-
// images in markdown as it automatically creates responsive
196
-
// versions of images.
197
-
//
198
-
// If you'd like to not use gatsby-remark-images and just copy your
199
-
// original images to the public directory, set
200
-
// `ignoreFileExtensions` to an empty array.
201
-
ignoreFileExtensions: [],
202
-
},
181
+
```js:title=gatsby-config.js
182
+
{
183
+
resolve:`gatsby-transformer-remark`,
184
+
options: {
185
+
plugins: [
186
+
{
187
+
resolve:"gatsby-remark-copy-linked-files",
188
+
options: {
189
+
// `ignoreFileExtensions` defaults to [`png`, `jpg`, `jpeg`, `bmp`, `tiff`]
190
+
// as we assume you'll use gatsby-remark-images to handle
191
+
// images in markdown as it automatically creates responsive
192
+
// versions of images.
193
+
//
194
+
// If you'd like to not use gatsby-remark-images and just copy your
195
+
// original images to the public directory, set
196
+
// `ignoreFileExtensions` to an empty array.
197
+
ignoreFileExtensions: [],
203
198
},
204
-
],
205
-
},
199
+
},
200
+
],
206
201
},
207
-
]
202
+
}
208
203
```
209
204
210
-
> So now, `[Download it now](image.png)` will be copied to the root dir (i.e. `public` folder)
211
-
212
-
---
205
+
So now, `[Download it now](image.png)` will be copied to the root of the `public` folder.
0 commit comments