Skip to content

Commit

Permalink
Don't use regex that can be slow
Browse files Browse the repository at this point in the history
  • Loading branch information
tats-u committed Dec 13, 2024
1 parent f3145e8 commit 33439eb
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/inlines.js
Original file line number Diff line number Diff line change
Expand Up @@ -984,13 +984,33 @@ var parseInlines = function(block) {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim#return_value
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#white_space
// Removes only ASCII tab and space.
this.subject = block._string_content.replace(/^[\t \r\n]+|[\t \r\n]+$/g, "")
this.subject = trim(block._string_content)
this.pos = 0;
this.delimiters = null;
this.brackets = null;
while (this.parseInline(block)) {}
block._string_content = null; // allow raw string to be garbage collected
this.processEmphasis(null);

function trim(str) {
let start = 0;
for(; start < str.length; start++) {
if (!isSpace(str[start])) {
break;
}
}
let end = str.length - 1;
for(; end >= start; end--) {
if (!isSpace(str[end])) {
break;
}
}
return str.slice(start, end + 1);

function isSpace(c) {
return c === " " || c === "\t" || c === "\n" || c === "\r";
}
}
};

// The InlineParser object.
Expand Down

0 comments on commit 33439eb

Please sign in to comment.