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
Embeds the contents of specified files within Prism-formatted HTML blocks.
3
+
Embeds the contents of specified files as code snippets.
4
4
5
-
## Overview
5
+
## Installation
6
+
7
+
**Note**: This plugin depends on [gatsby-remark-prismjs](https://www.gatsbyjs.org/packages/gatsby-remark-prismjs/) and [gatsby-transformer-remark](https://www.gatsbyjs.org/packages/gatsby-transformer-remark/) plugins
|`directory`| Specify the directory where the code snippet files are located. If this option is omitted, this plugin will look for the code snippet files in the same directory as the markdown file. |
49
+
50
+
### Example 1: Specify that code snippet files are under the root directory
51
+
52
+
```js
53
+
// In gatsby-config.js
54
+
{
55
+
resolve:`gatsby-remark-embed-snippet`,
56
+
options: {
57
+
directory:`${__dirname}`
58
+
}
59
+
},
60
+
```
61
+
62
+
### Example 2: Specify that code snippet files are under a directory called `snippets`
63
+
64
+
```js
65
+
// In gatsby-config.js
66
+
{
67
+
resolve:`gatsby-remark-embed-snippet`,
68
+
options: {
69
+
directory:`${__dirname}/snippets/`
70
+
}
71
+
},
72
+
```
73
+
74
+
## Sample Usage I
75
+
76
+
Suppose you have the following file/folder structure and you want to embed `javascript-code.js` and `html-code.html` files as code snippets inside the Markdown file `index.md`.
77
+
78
+
```none
79
+
.
80
+
├── content
81
+
│ └── my-first-post
82
+
│ ├── index.md
83
+
│ ├── javascript-code.js
84
+
│ └── html-code.html
15
85
```
16
86
17
-
The following markdown syntax could be used to embed the contents of these
18
-
files:
87
+
Add the following syntax to the Markdown file `index.md` to embed `javascript-code.js` and `html-code.html` as code snippets:
88
+
89
+
**`index.md`:**
19
90
20
91
```md
21
92
# Sample JavaScript
22
93
23
-
`embed:sample-javascript-file.js`
94
+
`embed:javascript-code.js`
24
95
25
96
# Sample HTML
26
97
27
-
`embed:sample-html-file.html`
98
+
`embed:html-code.html`
28
99
```
29
100
30
-
The resulting HTML for the above markdown would look something like this:
101
+
The resulting HTML generated from the Markdown file above would look something like this:
31
102
32
103
```html
33
104
<h1>Sample JavaScript</h1>
34
105
<divclass="gatsby-highlight">
35
106
<preclass="language-jsx">
36
107
<code>
37
-
<!-- Embedded content here ... -->
108
+
<!-- Embedded javascript-code.js content here ... -->
38
109
</code>
39
110
</pre>
40
111
</div>
@@ -43,19 +114,82 @@ The resulting HTML for the above markdown would look something like this:
43
114
<divclass="gatsby-highlight">
44
115
<preclass="language-html">
45
116
<code>
46
-
<!-- Embedded content here ... -->
117
+
<!-- Embedded html-code.html content here ... -->
118
+
</code>
119
+
</pre>
120
+
</div>
121
+
```
122
+
123
+
## Sample Usage II
124
+
125
+
Suppose you have the following file/folder structure and you want to embed `javascript-code.js` and `html-code.html` files as code snippets inside the Markdown file `my-first-post.md`.
126
+
127
+
```none
128
+
.
129
+
├── content
130
+
│ └── my-first-post.md
131
+
├── snippets
132
+
│ ├── javascript-code.js
133
+
│ └── html-code.html
134
+
```
135
+
136
+
Use `directory` plugin option to tell `gatsby-remark-embed-snippet` plugin that code snippet files are located under a directory called `snippets`:
137
+
138
+
```js
139
+
// gatsby-config.js
140
+
{
141
+
resolve:`gatsby-remark-embed-snippet`,
142
+
options: {
143
+
directory:`${__dirname}/snippets/`,
144
+
},
145
+
},
146
+
```
147
+
148
+
Add the following syntax to the Markdown file `my-first-post.md` to embed `javascript-code.js` and `html-code.html` as code snippets:
149
+
150
+
**`my-first-post.md`:**
151
+
152
+
```md
153
+
# Sample JavaScript 2
154
+
155
+
`embed:javascript-code.js`
156
+
157
+
# Sample HTML 2
158
+
159
+
`embed:html-code.html`
160
+
```
161
+
162
+
The resulting HTML generated from the markdown file above would look something like this:
163
+
164
+
```html
165
+
<h1>Sample JavaScript 2</h1>
166
+
<divclass="gatsby-highlight">
167
+
<preclass="language-jsx">
168
+
<code>
169
+
<!-- Embedded javascript-code.js content here ... -->
170
+
</code>
171
+
</pre>
172
+
</div>
173
+
174
+
<h1>Sample HTML 2</h1>
175
+
<divclass="gatsby-highlight">
176
+
<preclass="language-html">
177
+
<code>
178
+
<!-- Embedded html-code.html content here ... -->
47
179
</code>
48
180
</pre>
49
181
</div>
50
182
```
51
183
184
+
## Code snippet syntax highlighting
185
+
52
186
### Highlighting Lines
53
187
54
188
You can also specify specific lines for Prism to highlight using
55
189
`highlight-line` and `highlight-next-line` comments. You can also specify a
56
190
range of lines to highlight, relative to a `highlight-range` comment.
57
191
58
-
#### JavaScript example
192
+
**JavaScript example**:
59
193
60
194
```js
61
195
importReactfrom"react"
@@ -72,7 +206,7 @@ ReactDOM.render(
72
206
)
73
207
```
74
208
75
-
#### CSS example
209
+
**CSS example**:
76
210
77
211
```css
78
212
html {
@@ -86,22 +220,23 @@ html {
86
220
}
87
221
```
88
222
89
-
#### HTML example
223
+
**HTML example**:
90
224
225
+
<!-- prettier-ignore-start -->
91
226
```html
92
227
<html>
93
228
<body>
94
-
<h1>highlight me</h1>
95
-
<!-- highlight-line -->
229
+
<h1>highlight me</h1> <!-- highlight-line -->
96
230
<p>
97
231
<!-- highlight-next-line -->
98
232
And me
99
233
</p>
100
234
</body>
101
235
</html>
102
236
```
237
+
<!-- prettier-ignore-end -->
103
238
104
-
#### YAML example
239
+
**YAML example**:
105
240
106
241
```yaml
107
242
foo: "highlighted"# highlight-line
@@ -115,7 +250,7 @@ quz: "highlighted"
115
250
116
251
It's also possible to specify a range of lines to be hidden.
117
252
118
-
#### JavaScript example
253
+
**JavaScript example**:
119
254
120
255
```js
121
256
// hide-range{1-2}
@@ -157,74 +292,3 @@ function App() {
157
292
)
158
293
}
159
294
```
160
-
161
-
## Installation
162
-
163
-
`npm install --save gatsby-remark-embed-snippet`
164
-
165
-
## How to use
166
-
167
-
**Important**: This module must appear before `gatsby-remark-prismjs` in your
168
-
plugins array, or the markup will have already been transformed into a code
169
-
block and this plugin will fail to detect it and inline the file.
170
-
For further information about its options, visit the `gatsby-remark-prismjs`
0 commit comments