Skip to content

Commit 326a264

Browse files
committed
use array of strings to trim instead of regexp
1 parent 2ad8b4c commit 326a264

3 files changed

Lines changed: 6 additions & 12 deletions

File tree

src/Tokenizer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
8484
const text = cap[0].replace(this.rules.other.codeRemoveIndent, '');
8585
return {
8686
type: 'code',
87-
raw: rtrim(cap[0], this.rules.other.reverseBlankLines),
87+
raw: rtrim(cap[0], ['\n', '\t', ' ']),
8888
codeBlockStyle: 'indented',
8989
text: !this.options.pedantic
90-
? rtrim(text, this.rules.other.reverseBlankLines)
90+
? rtrim(text, ['\n', '\t', ' '])
9191
: text,
9292
};
9393
}

src/helpers.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,20 @@ export function splitCells(tableRow: string, count?: number) {
8585
* @param str
8686
* @param c
8787
*/
88-
export function rtrim(str: string, c: string | RegExp) {
88+
export function rtrim(str: string, c: string | string[]) {
8989
const l = str.length;
9090
if (l === 0) {
9191
return '';
9292
}
9393

94-
if (c instanceof RegExp) {
95-
const reversedStr = [...str].reverse().join('');
96-
const match = c.exec(reversedStr);
97-
if (match) {
98-
return str.slice(0, l - match[0].length);
99-
}
100-
return str;
94+
if (typeof c === 'string') {
95+
c = [c];
10196
}
10297

10398
let suffLen = 0;
10499
while (suffLen < l) {
105100
const currChar = str.charAt(l - suffLen - 1);
106-
if (currChar === c) {
101+
if (c.includes(currChar)) {
107102
suffLen++;
108103
} else {
109104
break;

src/rules.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ try {
2929

3030
export const other = {
3131
codeRemoveIndent: /^(?: {1,4}| {0,3}\t)/gm,
32-
reverseBlankLines: /^(?:[^\S\n]*\n)*/,
3332
outputLinkReplace: /\\([\[\]])/g,
3433
indentCodeCompensation: /^(\s+)(?:```)/,
3534
beginningSpace: /^\s+/,

0 commit comments

Comments
 (0)