@@ -166,6 +166,26 @@ func TestRenderCustomCodeBlock(t *testing.T) {
166
166
},
167
167
themeName : "dark" ,
168
168
},
169
+ {
170
+ name : "code with line numbers" ,
171
+ content : "console.log('hello');\n console.log('world');\n var x = 1;" ,
172
+ info : CodeHighlightInfo {
173
+ Language : "javascript" ,
174
+ Ranges : []LineRange {{Start : 1 , End : 1 }},
175
+ ShowLineNumbers : true ,
176
+ },
177
+ themeName : "dark" ,
178
+ },
179
+ {
180
+ name : "code with only line numbers (no highlighting)" ,
181
+ content : "def hello():\n print('hello')\n return True" ,
182
+ info : CodeHighlightInfo {
183
+ Language : "python" ,
184
+ Ranges : nil ,
185
+ ShowLineNumbers : true ,
186
+ },
187
+ themeName : "dark" ,
188
+ },
169
189
}
170
190
171
191
for _ , tt := range tests {
@@ -182,6 +202,13 @@ func TestRenderCustomCodeBlock(t *testing.T) {
182
202
t .Error ("renderCustomCodeBlock returned only whitespace" )
183
203
}
184
204
205
+ // Check for line numbers if requested
206
+ if tt .info .ShowLineNumbers {
207
+ if ! strings .Contains (result , "1" ) {
208
+ t .Error ("renderCustomCodeBlock with ShowLineNumbers should contain line number 1" )
209
+ }
210
+ }
211
+
185
212
// For known languages, check that it contains some recognizable elements
186
213
if tt .info .Language == "javascript" {
187
214
if ! strings .Contains (result , "console" ) && ! strings .Contains (result , "log" ) {
@@ -231,6 +258,45 @@ func TestRenderWithStyle(t *testing.T) {
231
258
}
232
259
}
233
260
261
+ func TestRenderWithStyleLineNumbers (t * testing.T ) {
262
+ content := "console.log('test');\n var x = 1;\n function hello() {\n return 'world';\n }"
263
+ info := CodeHighlightInfo {
264
+ Language : "javascript" ,
265
+ Ranges : []LineRange {{Start : 1 , End : 2 }},
266
+ ShowLineNumbers : true ,
267
+ }
268
+
269
+ lexer := lexers .Get ("javascript" )
270
+ if lexer == nil {
271
+ lexer = lexers .Fallback
272
+ }
273
+ lexer = chroma .Coalesce (lexer )
274
+
275
+ style := styles .Get ("github" )
276
+ if style == nil {
277
+ style = styles .Fallback
278
+ }
279
+
280
+ result := renderWithStyle (content , info , lexer , style )
281
+
282
+ if result == "" {
283
+ t .Error ("renderWithStyle returned empty string" )
284
+ }
285
+
286
+ // Should contain line numbers
287
+ if ! strings .Contains (result , "1" ) {
288
+ t .Error ("renderWithStyle with ShowLineNumbers should contain line number 1" )
289
+ }
290
+ if ! strings .Contains (result , "2" ) {
291
+ t .Error ("renderWithStyle with ShowLineNumbers should contain line number 2" )
292
+ }
293
+
294
+ // Should contain some recognizable elements from the original content
295
+ if ! strings .Contains (result , "console" ) && ! strings .Contains (result , "log" ) && ! strings .Contains (result , "var" ) {
296
+ t .Error ("renderWithStyle result doesn't contain expected javascript elements" )
297
+ }
298
+ }
299
+
234
300
func TestProcessMarkdownWithHighlighting (t * testing.T ) {
235
301
tests := []struct {
236
302
name string
@@ -274,6 +340,24 @@ func TestProcessMarkdownWithHighlighting(t *testing.T) {
274
340
theme : "dark" ,
275
341
wantErr : false ,
276
342
},
343
+ {
344
+ name : "markdown with --numbered flag" ,
345
+ markdown : "```javascript --numbered\n console.log('hello');\n console.log('world');\n ```" ,
346
+ theme : "dark" ,
347
+ wantErr : false ,
348
+ },
349
+ {
350
+ name : "markdown with highlighting and --numbered" ,
351
+ markdown : "```python{1-2} --numbered\n def hello():\n print('hello')\n return True\n ```" ,
352
+ theme : "dark" ,
353
+ wantErr : false ,
354
+ },
355
+ {
356
+ name : "markdown with --numbered and extra spaces" ,
357
+ markdown : "```javascript --numbered \n console.log('test');\n var x = 1;\n ```" ,
358
+ theme : "dark" ,
359
+ wantErr : false ,
360
+ },
277
361
}
278
362
279
363
for _ , tt := range tests {
@@ -296,14 +380,22 @@ func TestProcessMarkdownWithHighlighting(t *testing.T) {
296
380
t .Error ("processMarkdownWithHighlighting result seems too short for highlighted content" )
297
381
}
298
382
}
383
+
384
+ // For code blocks with --numbered, should contain line numbers
385
+ if strings .Contains (tt .markdown , "--numbered" ) {
386
+ if ! strings .Contains (result , "1" ) {
387
+ t .Error ("processMarkdownWithHighlighting with --numbered should contain line number 1" )
388
+ }
389
+ }
299
390
})
300
391
}
301
392
}
302
393
303
394
func TestCodeHighlightInfo (t * testing.T ) {
304
395
info := CodeHighlightInfo {
305
- Language : "javascript" ,
306
- Ranges : []LineRange {{Start : 1 , End : 3 }, {Start : 5 , End : 5 }},
396
+ Language : "javascript" ,
397
+ Ranges : []LineRange {{Start : 1 , End : 3 }, {Start : 5 , End : 5 }},
398
+ ShowLineNumbers : true ,
307
399
}
308
400
309
401
if info .Language != "javascript" {
@@ -318,6 +410,10 @@ func TestCodeHighlightInfo(t *testing.T) {
318
410
t .Errorf ("First range incorrect: got {%d, %d}, expected {1, 3}" ,
319
411
info .Ranges [0 ].Start , info .Ranges [0 ].End )
320
412
}
413
+
414
+ if ! info .ShowLineNumbers {
415
+ t .Error ("Expected ShowLineNumbers to be true" )
416
+ }
321
417
}
322
418
323
419
func TestLineRange (t * testing.T ) {
0 commit comments