Skip to content

Commit

Permalink
fix(api): improve template link detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Redm4x committed Aug 28, 2024
1 parent 6ad71ce commit c19b0e3
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions apps/api/src/services/external/templateReposService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,17 +545,20 @@ function getLinuxServerTemplateSummary(readme: string) {
// Replaces local links with absolute links
function replaceLinks(markdown: string, owner: string, repo: string, version: string, folder: string) {
let newMarkdown = markdown;
const linkRegex = /!?\[([^[]+)\]\((.*?)\)/gm;
const linkRegex = /!?\[([^[]*)\]\((.*?)\)/gm;
const matches = newMarkdown.matchAll(linkRegex);
for (const match of matches) {
const url = match[2].startsWith("/") ? match[2].substring(1) : match[2];
const originalUrl = match[2];
const url = originalUrl.replace(/^\.?\//, "");

if (isUrlAbsolute(url)) continue;

const isPicture = match[0].startsWith("!");
const absoluteUrl = isPicture
? `https://raw.githubusercontent.com/${owner}/${repo}/${version}/${folder}/` + url
: `https://github.com/${owner}/${repo}/blob/${version}/${folder}/` + url;

newMarkdown = newMarkdown.split("(" + url + ")").join("(" + absoluteUrl + ")");
newMarkdown = newMarkdown.split("(" + originalUrl + ")").join("(" + absoluteUrl + ")");
}

return newMarkdown;
Expand Down

0 comments on commit c19b0e3

Please sign in to comment.