Skip to content

Commit ded5243

Browse files
authored
Merge pull request #75 from skiende74/feat/show-line-numbers-option-languages
Feat: Add Option to Show Line Numbers for Specific Languages
2 parents b4063ff + 42b071b commit ded5243

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,14 @@ Note: The language must be first registered with [refractor].
205205

206206
#### options.showLineNumbers
207207

208-
Type: `boolean`.
209-
Default: `false`.
208+
Type: `boolean | string[]`
209+
Default: `false`
210+
211+
By default, line numbers will only be displayed for code block cells with a meta property that includes 'showLineNumbers'. To control the starting line number, use `showLineNumbers=X`, where `X` is the starting line number as a meta property for the code block.
210212

211-
By default, line numbers will only be displayed for code block cells with a meta property that includes 'showLineNumbers'. To control the starting line number use `showLineNumbers=X`, where `X` is the starting line number as a meta property for the code block.
213+
If you would like to show line numbers for all code blocks without specifying the meta property, set this to `true`.
212214

213-
If you would like to show line numbers for all code blocks, without specifying the meta property, set this to `true`.
215+
Alternatively, you can specify an array of languages for which the line numbers should be shown. For example, setting the option as `showLineNumbers: ['typescript']` will display line numbers only for code blocks with the language `typescript`, while other languages (e.g., `text`) will not display line numbers.
214216

215217
**Note**: This will wrongly assign a language class and the class might appear as `language-{1,3}` or `language-showLineNumbers`, but allow the language highlighting and line number function to work. An possible approach would be to add a placeholder like `unknown` so the `div` will have `class="language-unknown"`
216218

src/generator.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @typedef {import('hast').Root} Root
44
* @typedef Options options
55
* Configuration.
6-
* @property {boolean} [showLineNumbers]
6+
* @property {boolean|string[]} [showLineNumbers]
77
* Set `showLineNumbers` to `true` to always display line number
88
* @property {boolean} [ignoreMissing]
99
* Set `ignoreMissing` to `true` to ignore unsupported languages and line highlighting when no language is specified
@@ -276,11 +276,14 @@ const rehypePrismGenerator = (refractor) => {
276276
line.children = treeExtract.children
277277

278278
// Line number
279-
if (
279+
const isShowNumbers =
280280
(meta.toLowerCase().includes('showLineNumbers'.toLowerCase()) ||
281-
options.showLineNumbers) &&
281+
options.showLineNumbers === true ||
282+
(typeof options.showLineNumbers === 'object' &&
283+
options.showLineNumbers.includes(lang))) &&
282284
!falseShowLineNumbersStr.some((str) => meta.toLowerCase().includes(str))
283-
) {
285+
286+
if (isShowNumbers) {
284287
line.properties.line = [(i + startingLineNumber).toString()]
285288
line.properties.className.push('line-number')
286289
}

test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,40 @@ test('not show line number when showLineNumbers=false', async () => {
244244
assert.not(result.match(/line="2"/g))
245245
})
246246

247+
test('show line numbers when showLineNumbers=string[] includes target language', async () => {
248+
const result = processHtml(
249+
dedent`
250+
<div>
251+
<pre>
252+
<code class="language-typescript code-highlight">x = 6
253+
y = 7
254+
</code>
255+
</pre>
256+
</div>
257+
`,
258+
{ showLineNumbers: ['typescript', 'py'] }
259+
).trim()
260+
assert.ok(result.match(/line="1"/g))
261+
assert.ok(result.match(/line="2"/g))
262+
})
263+
264+
test('not show line numbers when showLineNumbers=string[] does not include target language', async () => {
265+
const result = processHtml(
266+
dedent`
267+
<div>
268+
<pre>
269+
<code class="language-javascript code-highlight">x = 6
270+
y = 7
271+
</code>
272+
</pre>
273+
</div>
274+
`,
275+
{ showLineNumbers: ['typescript', 'py'] }
276+
).trim()
277+
assert.not(result.match(/line="1"/g))
278+
assert.not(result.match(/line="2"/g))
279+
})
280+
247281
test('not show line number when showLineNumbers={false}', async () => {
248282
const meta = 'showLineNumbers={false}'
249283
const result = processHtml(

0 commit comments

Comments
 (0)