Skip to content

Commit c8de53a

Browse files
MichaelFroeschenGerrit0
authored andcommitted
feat: preserve spaces in code blocks
1 parent 441fea2 commit c8de53a

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

src/lib/converter/factories/comment.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ export function parseComment(
258258
let inFencedCode = false;
259259
function readLine(line: string) {
260260
line = line.replace(/^\s*\*? ?/, "");
261+
const rawLine = line;
261262
line = line.replace(/\s*$/, "");
262263

263264
if (CODE_FENCE.test(line)) {
@@ -271,7 +272,12 @@ export function parseComment(
271272
return readTagLine(line, tag);
272273
}
273274
}
274-
readBareLine(line);
275+
if (inFencedCode) {
276+
// This will not include code blocks declared with four spaces
277+
readBareLine(rawLine);
278+
} else {
279+
readBareLine(line);
280+
}
275281
}
276282

277283
text = text.replace(/^\s*\/\*+/, "");
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* This is a comment containing a multiline code block
3+
* ```ts
4+
* export function multiply(a: number, b: number) {
5+
* return a * b;
6+
* }
7+
* ```
8+
* @module
9+
*/
10+
11+
export function multiply(a: number, b: number) {
12+
return a * b;
13+
}

src/test/converter/comment/specs.json

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,71 @@
380380
]
381381
}
382382
]
383+
},
384+
{
385+
"id": 42,
386+
"name": "comment4",
387+
"kind": 1,
388+
"kindString": "Module",
389+
"flags": {},
390+
"comment": {
391+
"shortText": "This is a comment containing a multiline code block\n```ts\nexport function multiply(a: number, b: number) {\n return a * b;\n}\n```"
392+
},
393+
"children": [
394+
{
395+
"id": 43,
396+
"name": "multiply",
397+
"kind": 64,
398+
"kindString": "Function",
399+
"flags": {},
400+
"signatures": [
401+
{
402+
"id": 44,
403+
"name": "multiply",
404+
"kind": 4096,
405+
"kindString": "Call signature",
406+
"flags": {},
407+
"parameters": [
408+
{
409+
"id": 45,
410+
"name": "a",
411+
"kind": 32768,
412+
"kindString": "Parameter",
413+
"flags": {},
414+
"type": {
415+
"type": "intrinsic",
416+
"name": "number"
417+
}
418+
},
419+
{
420+
"id": 46,
421+
"name": "b",
422+
"kind": 32768,
423+
"kindString": "Parameter",
424+
"flags": {},
425+
"type": {
426+
"type": "intrinsic",
427+
"name": "number"
428+
}
429+
}
430+
],
431+
"type": {
432+
"type": "intrinsic",
433+
"name": "number"
434+
}
435+
}
436+
]
437+
}
438+
],
439+
"groups": [
440+
{
441+
"title": "Functions",
442+
"kind": 64,
443+
"children": [
444+
43
445+
]
446+
}
447+
]
383448
}
384449
],
385450
"groups": [
@@ -389,7 +454,8 @@
389454
"children": [
390455
1,
391456
32,
392-
37
457+
37,
458+
42
393459
]
394460
}
395461
]

0 commit comments

Comments
 (0)