Skip to content

Commit e136da7

Browse files
authored
fix: preserve internal tabs in list item content (#4086)
1 parent 4eb2d97 commit e136da7

34 files changed

Lines changed: 193 additions & 4 deletions

File tree

‎src/Tokenizer.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,11 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
331331
raw = cap[0];
332332
src = src.substring(raw.length);
333333

334-
let line = expandTabs(cap[2].split('\n', 1)[0], cap[1].length);
334+
const firstLine = cap[2].split('\n', 1)[0];
335+
const bulletIndent = cap[1].length;
336+
let line = this.options.pedantic
337+
? expandTabs(firstLine, bulletIndent)
338+
: firstLine.replace(this.rules.other.leadingSpaceTab, whitespace => expandTabs(whitespace, bulletIndent));
335339
let nextLine = src.split('\n', 1)[0];
336340
let blankLine = !line.trim();
337341

@@ -340,12 +344,12 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
340344
indent = 2;
341345
itemContents = line.trimStart();
342346
} else if (blankLine) {
343-
indent = cap[1].length + 1;
347+
indent = bulletIndent + 1;
344348
} else {
345349
indent = line.search(this.rules.other.nonSpaceChar); // Find first non-space char
346350
indent = indent > 4 ? 1 : indent; // Treat indented code blocks (> 4 spaces) as having only 1 indent
347351
itemContents = line.slice(indent);
348-
indent += cap[1].length;
352+
indent += bulletIndent;
349353
}
350354

351355
if (blankLine && this.rules.other.blankLine.test(nextLine)) { // Items begin with at most one blank line
@@ -373,7 +377,7 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
373377
nextLine = nextLine.replace(this.rules.other.listReplaceNesting, ' ');
374378
nextLineWithoutTabs = nextLine;
375379
} else {
376-
nextLineWithoutTabs = nextLine.replace(this.rules.other.tabCharGlobal, ' ');
380+
nextLineWithoutTabs = nextLine.replace(this.rules.other.leadingSpaceTab, whitespace => whitespace.replace(this.rules.other.tabCharGlobal, ' '));
377381
}
378382

379383
// End list item if found code fences

‎src/rules.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const other = {
5252
nonSpaceChar: /[^ ]/,
5353
newLineCharGlobal: /\n/g,
5454
tabCharGlobal: /\t/g,
55+
leadingSpaceTab: /^[ \t]+/,
5556
multipleSpaceGlobal: /\s+/g,
5657
blankLine: /^[ \t]*$/,
5758
doubleBlankLine: /\n[ \t]*\n[ \t]*$/,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<ul>
2+
<li><p>item</p>
3+
<p><code>foo bar</code></p>
4+
</li>
5+
</ul>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
gfm: false
3+
renderExact: true
4+
---
5+
- item
6+
7+
`foo bar`
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<ul>
2+
<li><p>item</p>
3+
<p><code>foo bar</code></p>
4+
</li>
5+
</ul>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
gfm: true
3+
renderExact: true
4+
---
5+
- item
6+
7+
`foo bar`
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<ul>
2+
<li><pre><code>foo bar
3+
</code></pre>
4+
</li>
5+
</ul>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
gfm: false
3+
renderExact: true
4+
---
5+
- ```
6+
foo bar
7+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<ul>
2+
<li><pre><code>foo bar
3+
</code></pre>
4+
</li>
5+
</ul>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
gfm: true
3+
renderExact: true
4+
---
5+
- ```
6+
foo bar
7+
```

0 commit comments

Comments
 (0)