Skip to content

Commit

Permalink
Replace use of startsWith and endsWith with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
jhildenbiddle committed Dec 14, 2020
1 parent 68af334 commit 06eec11
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
14 changes: 7 additions & 7 deletions lib/marked.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,11 @@ var Tokenizer_1 = class Tokenizer {
let text = cap[2].trim();

// remove trailing #s
if (text.endsWith('#')) {
if (/#$/.test(text)) {
const trimmed = rtrim$1(text, '#');
if (this.options.pedantic) {
text = trimmed.trim();
} else if (!trimmed || trimmed.endsWith(' ')) {
} else if (!trimmed || / $/.test(trimmed)) {
// CommonMark requires space before trailing #s
text = trimmed.trim();
}
Expand Down Expand Up @@ -785,9 +785,9 @@ var Tokenizer_1 = class Tokenizer {
const cap = this.rules.inline.link.exec(src);
if (cap) {
const trimmedUrl = cap[2].trim();
if (!this.options.pedantic && trimmedUrl.startsWith('<')) {
if (!this.options.pedantic && /^</.test(trimmedUrl)) {
// commonmark requires matching angle brackets
if (!trimmedUrl.endsWith('>')) {
if (!(/>$/.test(trimmedUrl))) {
return;
}

Expand Down Expand Up @@ -822,8 +822,8 @@ var Tokenizer_1 = class Tokenizer {
}

href = href.trim();
if (href.startsWith('<')) {
if (this.options.pedantic && !trimmedUrl.endsWith('>')) {
if (/^</.test(href)) {
if (this.options.pedantic && !(/>$/.test(trimmedUrl))) {
// pedantic allows starting angle bracket without ending angle bracket
href = href.slice(1);
} else {
Expand Down Expand Up @@ -906,7 +906,7 @@ var Tokenizer_1 = class Tokenizer {
if (cap) {
let text = cap[2].replace(/\n/g, ' ');
const hasNonSpaceChars = /[^ ]/.test(text);
const hasSpaceCharsOnBothEnds = text.startsWith(' ') && text.endsWith(' ');
const hasSpaceCharsOnBothEnds = /^ /.test(text) && / $/.test(text);
if (hasNonSpaceChars && hasSpaceCharsOnBothEnds) {
text = text.substring(1, text.length - 1);
}
Expand Down
14 changes: 7 additions & 7 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,12 @@
if (cap) {
var text = cap[2].trim(); // remove trailing #s

if (text.endsWith('#')) {
if (/#$/.test(text)) {
var trimmed = rtrim$1(text, '#');

if (this.options.pedantic) {
text = trimmed.trim();
} else if (!trimmed || trimmed.endsWith(' ')) {
} else if (!trimmed || / $/.test(trimmed)) {
// CommonMark requires space before trailing #s
text = trimmed.trim();
}
Expand Down Expand Up @@ -879,9 +879,9 @@
if (cap) {
var trimmedUrl = cap[2].trim();

if (!this.options.pedantic && trimmedUrl.startsWith('<')) {
if (!this.options.pedantic && /^</.test(trimmedUrl)) {
// commonmark requires matching angle brackets
if (!trimmedUrl.endsWith('>')) {
if (!/>$/.test(trimmedUrl)) {
return;
} // ending angle bracket cannot be escaped

Expand Down Expand Up @@ -921,8 +921,8 @@

href = href.trim();

if (href.startsWith('<')) {
if (this.options.pedantic && !trimmedUrl.endsWith('>')) {
if (/^</.test(href)) {
if (this.options.pedantic && !/>$/.test(trimmedUrl)) {
// pedantic allows starting angle bracket without ending angle bracket
href = href.slice(1);
} else {
Expand Down Expand Up @@ -1017,7 +1017,7 @@
if (cap) {
var text = cap[2].replace(/\n/g, ' ');
var hasNonSpaceChars = /[^ ]/.test(text);
var hasSpaceCharsOnBothEnds = text.startsWith(' ') && text.endsWith(' ');
var hasSpaceCharsOnBothEnds = /^ /.test(text) && / $/.test(text);

if (hasNonSpaceChars && hasSpaceCharsOnBothEnds) {
text = text.substring(1, text.length - 1);
Expand Down
2 changes: 1 addition & 1 deletion marked.min.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions src/Tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ module.exports = class Tokenizer {
let text = cap[2].trim();

// remove trailing #s
if (text.endsWith('#')) {
if (/#$/.test(text)) {
const trimmed = rtrim(text, '#');
if (this.options.pedantic) {
text = trimmed.trim();
} else if (!trimmed || trimmed.endsWith(' ')) {
} else if (!trimmed || / $/.test(trimmed)) {
// CommonMark requires space before trailing #s
text = trimmed.trim();
}
Expand Down Expand Up @@ -471,9 +471,9 @@ module.exports = class Tokenizer {
const cap = this.rules.inline.link.exec(src);
if (cap) {
const trimmedUrl = cap[2].trim();
if (!this.options.pedantic && trimmedUrl.startsWith('<')) {
if (!this.options.pedantic && /^</.test(trimmedUrl)) {
// commonmark requires matching angle brackets
if (!trimmedUrl.endsWith('>')) {
if (!(/>$/.test(trimmedUrl))) {
return;
}

Expand Down Expand Up @@ -508,8 +508,8 @@ module.exports = class Tokenizer {
}

href = href.trim();
if (href.startsWith('<')) {
if (this.options.pedantic && !trimmedUrl.endsWith('>')) {
if (/^</.test(href)) {
if (this.options.pedantic && !(/>$/.test(trimmedUrl))) {
// pedantic allows starting angle bracket without ending angle bracket
href = href.slice(1);
} else {
Expand Down Expand Up @@ -592,7 +592,7 @@ module.exports = class Tokenizer {
if (cap) {
let text = cap[2].replace(/\n/g, ' ');
const hasNonSpaceChars = /[^ ]/.test(text);
const hasSpaceCharsOnBothEnds = text.startsWith(' ') && text.endsWith(' ');
const hasSpaceCharsOnBothEnds = /^ /.test(text) && / $/.test(text);
if (hasNonSpaceChars && hasSpaceCharsOnBothEnds) {
text = text.substring(1, text.length - 1);
}
Expand Down

0 comments on commit 06eec11

Please sign in to comment.