fix: make baseUrl optional param

This commit is contained in:
amhsirak
2025-11-20 04:21:41 +05:30
parent 839f9fa5ce
commit 0a7a1eb9b8

View File

@@ -1,8 +1,10 @@
export async function parseMarkdown(
html: string | null | undefined,
baseUrl?: string | null
): Promise<string> {
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
// ---------------------------------------------