Skip to content

Commit 7d19665

Browse files
authored
fix: only convert leading tabs to spaces (#1559) (#2434)
* fix: non leading-tabs in markdown content (#1559) Only replaces tabs at the beginning of a block construct. Tabs in the middle of the item are unaffected. All tests passing. Tabs in both GFM and CommonMark at 100% fixes #1559 * update new/html_comments.html to preserve tab * combine redundant if condition * add test for tab immediately after blockquote character
1 parent 3dc35bb commit 7d19665

6 files changed

+14
-8
lines changed

src/Lexer.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ export class Lexer {
115115
*/
116116
lex(src) {
117117
src = src
118-
.replace(/\r\n|\r/g, '\n')
119-
.replace(/\t/g, ' ');
118+
.replace(/\r\n|\r/g, '\n');
120119

121120
this.blockTokens(src, this.tokens);
122121

@@ -133,8 +132,13 @@ export class Lexer {
133132
*/
134133
blockTokens(src, tokens = []) {
135134
if (this.options.pedantic) {
136-
src = src.replace(/^ +$/gm, '');
135+
src = src.replace(/\t/g, ' ').replace(/^ +$/gm, '');
136+
} else {
137+
src = src.replace(/^( *)(\t+)/gm, (_, leading, tabs) => {
138+
return leading + ' '.repeat(tabs.length);
139+
});
137140
}
141+
138142
let token, lastToken, cutSrc, lastParagraphClipped;
139143

140144
while (src) {

src/Tokenizer.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export class Tokenizer {
151151
blockquote(src) {
152152
const cap = this.rules.block.blockquote.exec(src);
153153
if (cap) {
154-
const text = cap[0].replace(/^ *> ?/gm, '');
154+
const text = cap[0].replace(/^ *>[ \t]?/gm, '');
155155

156156
return {
157157
type: 'blockquote',
@@ -187,7 +187,7 @@ export class Tokenizer {
187187
}
188188

189189
// Get next list item
190-
const itemRegex = new RegExp(`^( {0,3}${bull})((?: [^\\n]*)?(?:\\n|$))`);
190+
const itemRegex = new RegExp(`^( {0,3}${bull})((?:[\t ][^\\n]*)?(?:\\n|$))`);
191191

192192
// Check if current bullet point can start a new List Item
193193
while (src) {

src/rules.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ export const block = {
1111
newline: /^(?: *(?:\n|$))+/,
1212
code: /^( {4}[^\n]+(?:\n(?: *(?:\n|$))*)?)+/,
1313
fences: /^ {0,3}(`{3,}(?=[^`\n]*\n)|~{3,})([^\n]*)\n(?:|([\s\S]*?)\n)(?: {0,3}\1[~`]* *(?=\n|$)|$)/,
14-
hr: /^ {0,3}((?:- *){3,}|(?:_ *){3,}|(?:\* *){3,})(?:\n+|$)/,
14+
hr: /^ {0,3}((?:-[\t ]*){3,}|(?:_[ \t]*){3,}|(?:\*[ \t]*){3,})(?:\n+|$)/,
1515
heading: /^ {0,3}(#{1,6})(?=\s|$)(.*)(?:\n+|$)/,
1616
blockquote: /^( {0,3}> ?(paragraph|[^\n]*)(?:\n|$))+/,
17-
list: /^( {0,3}bull)( [^\n]+?)?(?:\n|$)/,
17+
list: /^( {0,3}bull)([ \t][^\n]+?)?(?:\n|$)/,
1818
html: '^ {0,3}(?:' // optional indentation
1919
+ '<(script|pre|style|textarea)[\\s>][\\s\\S]*?(?:</\\1>[^\\n]*\\n+|$)' // (1)
2020
+ '|comment[^\\n]*(\\n+|$)' // (2)

test/specs/new/html_comments.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ <h3 id="example-9">Example 9</h3>
3737
<h3 id="example-10">Example 10</h3>
3838

3939
<!-- multi
40-
line
40+
line
4141
comment
4242
-->
4343

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<blockquote><p>test</p></blockquote>
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
> test

0 commit comments

Comments
 (0)