Skip to content

Commit

Permalink
Simplify global matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergei Zharinov committed Aug 12, 2021
1 parent 0d46dd5 commit 7d375a0
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions lib/platform/github/massage-markdown-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,14 @@ function collectLinkPosition(input: string, matches: UrlMatch[]): Plugin {
});
}
} else if (tree.type === 'text') {
let text: string = tree.value;
let match = urlRegex.exec(text);
let currentOffset = 0;
while (match) {
const globalUrlReg = new RegExp(urlRegex, 'g');
const urlMatches = [...tree.value.matchAll(globalUrlReg)];
for (const match of urlMatches) {
const [url] = match;

currentOffset += match.index;
const start = startOffset + currentOffset;

currentOffset += url.length;
const end = startOffset + currentOffset;

const start = startOffset + match.index;
const end = start + url.length;
const newUrl = massageLink(url);
matches.push({ start, end, replaceTo: `[${url}](${newUrl})` });

text = text.slice(currentOffset);
match = urlRegex.exec(text);
}
} else if (hasKey('children', tree)) {
tree.children.forEach((child: Content) => {
Expand Down

0 comments on commit 7d375a0

Please sign in to comment.