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
Now, running `node example.js` you will see that each line of code is wrapped in a `div`, which has appropriate class names (`code-line`, `numbered-code-line`, `highlighted-code-line`) and line numbering attribute `data-line-number`.
103
+
Now, running `node example.js` you will see that each line of code is wrapped in a `span`, which has appropriate class names (`code-line`, `numbered-code-line`, `highlighted-code-line`) and line numbering attribute `data-line-number`.
Without `rehype-highlight-code-lines`, the lines of code wouldn't be in a `div`.
122
+
Without `rehype-highlight-code-lines`, the lines of code wouldn't be in a `span`.
123
123
124
124
```html
125
125
<pre>
@@ -133,6 +133,92 @@ Without `rehype-highlight-code-lines`, the lines of code wouldn't be in a `div`.
133
133
134
134
***Note:**`hljs` prefix comes from `rehype-highlight`*.
135
135
136
+
## Usage in HTML attributes
137
+
138
+
**`rehype-highlight-code-lines`** runs on `<code>` elements with directives like `showLineNumbers` and range number in curly braces like `{2-4, 8}`. That directives can be passed as a word in markdown (` ```ts showLineNumbers {2-4, 8} `) or as a class and attribute in HTML (`<code class="language-ts show-line-numbers" data-highlight-lines="2-4, 8">`).
139
+
140
+
The inverse occurs when the option `showLineNumbers` is true. All `<code>` are processed and numbered. Then (` ```ts noLineNumbers `), or as a class (`<code class="language-ts no-line-numbers">`) can be used to prevent processing.
141
+
142
+
**The class directives can be with dash or without, or camel cased.**
143
+
144
+
See some example usage as HTML class and attributes *(only opening `<code>` tags are provided, the rest is omitted.)*:
Now, running `node example.js` you will see that each line of code is wrapped in a `span`, which has appropriate class names (`code-line`, `numbered-code-line`, `highlighted-code-line`) and line numbering attribute `data-line-number`.
Now, all code blocks will become numbered. If you want to exclude a specific code block not to be numbered, use `noLineNumbers`.
247
+
Now, all code blocks will become numbered.
248
+
249
+
If you want to exclude a specific code block not to be numbered, use `noLineNumbers`.
163
250
164
251
**\`\`\`[language] noLineNumbers {2}**
165
252
166
253
**\`\`\`[language] noLineNumbers**
167
254
168
255
**\`\`\`noLineNumbers**
169
256
170
-
Sometimes you may want to start the line numbering from a specific number. In that cases, use `showLineNumbers=[number]` in code blocks. For example, below, the code block's line numbering will start from number `8`.
257
+
If you want to exclude a specific code block not to be numbered in HTML fragment (in `<pre>`) use `no-line-numbers` class. In that case, the directive could be with dash, or without, or camel cased.
258
+
259
+
**`<code class="language-ts no-line-numbers">`**
260
+
261
+
**`<code class="language-ts nolinenumbers">`**
262
+
263
+
**`<code class="language-ts noLineNumbers">`**
264
+
265
+
Sometimes you may want to start the line numbering from a specific number. In that cases, use `showLineNumbers=[number]` in code blocks. For example, below, the code block's line numbering will start from number `8`.
171
266
172
267
**\`\`\`[language] {2} showLineNumbers=8**
173
268
174
269
**\`\`\`[language] showLineNumbers=8**
175
270
176
271
**\`\`\`showLineNumbers=8**
177
272
273
+
If you want to start the line numbering from a specific number in HTML fragment (in `<pre>`) use `data-start-numbering` attribute.
274
+
275
+
**`<code class="..." data-start-numbering="8">`**
276
+
178
277
#### `lineContainerTagName` (Deprecated)
179
278
180
279
**It is marked as `@deprecated` and will be removed in the next versions.**
@@ -189,39 +288,6 @@ use(rehypeHighlightLines, {
189
288
});
190
289
```
191
290
192
-
#### `trimBlankLines`
193
-
194
-
It is a **boolean** option. It is designed to delete one blank/empty line at the beginning and one at the end of the code block, if happened due to html parsing `<pre><code /></pre>`.
195
-
196
-
By default, it is `false`.
197
-
198
-
Let's assume you want to highlight `pre`, and `code` element in markdown (not code fence).
199
-
200
-
```markdown
201
-
Here is markdown content
202
-
203
-
<pre><code class="language-javascript">
204
-
console.log("rehype-highlight-code-lines");
205
-
</code></pre>
206
-
```
207
-
208
-
For above markdown, the parsed result (for example via `rehype-parse` and `rehype-stringfy`) is going to contain empty/blank code lines due to nature of `pre` preserving whitespaces. In order to prevent having unintentionally blank lines, use the option `trimBlankLines`.
209
-
210
-
```javascript
211
-
use(rehypeHighlightLines, {
212
-
trimBlankLines:true,
213
-
});
214
-
```
215
-
216
-
Actually, the trimming could have been the default behaviour. However, some developers may intentionally include empty lines at the beginning and at the end in code fences for specific reasons and may want to preserve them.
0 commit comments