From b989b1be3a18980560c9d02f7cef4865820fc7e4 Mon Sep 17 00:00:00 2001 From: Redm4x <2829180+Redm4x@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:45:09 -0400 Subject: [PATCH] fix(api): improve template link detection --- apps/api/src/services/external/templateReposService.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/apps/api/src/services/external/templateReposService.ts b/apps/api/src/services/external/templateReposService.ts index 0685ec361..eb08afa76 100644 --- a/apps/api/src/services/external/templateReposService.ts +++ b/apps/api/src/services/external/templateReposService.ts @@ -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;