diff --git a/server/src/markdownify/markdown.ts b/server/src/markdownify/markdown.ts index 0aeca9e7..9ee7c720 100644 --- a/server/src/markdownify/markdown.ts +++ b/server/src/markdownify/markdown.ts @@ -1,8 +1,10 @@ export async function parseMarkdown( html: string | null | undefined, + baseUrl?: string | null ): Promise { const TurndownService = require("turndown"); const { gfm } = require("joplin-turndown-plugin-gfm"); + const { URL } = require('url'); const t = new TurndownService(); t.addRule("inlineLink", { @@ -11,7 +13,18 @@ export async function parseMarkdown( node.nodeName === "A" && node.getAttribute("href"), replacement: (content: string, node: any) => { - const href = node.getAttribute("href").trim(); + let href = node.getAttribute("href").trim(); + + // Convert relative URLs to absolute if baseUrl is provided + if (baseUrl && isRelativeUrl(href)) { + try { + const url = new URL(href, baseUrl); + href = url.toString(); + } catch (err) { + // If URL construction fails, keep the original href + } + } + const title = node.title ? ` "${node.title}"` : ""; return `[${content.trim()}](${href}${title})\n`; }, @@ -30,6 +43,10 @@ export async function parseMarkdown( } } +function isRelativeUrl(url: string): boolean { + return !url.includes('://') && !url.startsWith('mailto:') && !url.startsWith('tel:'); +} + // --------------------------------------------- // Helpers // ---------------------------------------------