You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

49 lines
1.3 KiB

type MarkdownExportFileNameInput = {
slug: string;
id: number;
};
function isAbsoluteOrSpecialUrl(url: string): boolean {
return /^(https?:)?\/\//i.test(url) || /^data:/i.test(url);
}
export function normalizeMarkdownImageUrls(markdown: string, origin: string): string {
const normalizedOrigin = origin.replace(/\/+$/, "");
return markdown.replace(/!\[([^\]]*)\]\(([^)\s]+)([^)]*)\)/g, (full, alt, rawUrl, rest) => {
if (isAbsoluteOrSpecialUrl(rawUrl)) {
return full;
}
if (!rawUrl.startsWith("/")) {
return full;
}
return `![${alt}](${normalizedOrigin}${rawUrl}${rest})`;
});
}
export function buildMarkdownExportFileName(input: MarkdownExportFileNameInput): string {
const base = input.slug.trim() || `post-${input.id}`;
return `${base}.md`;
}
export function downloadMarkdownFile(filename: string, content: string): void {
if (typeof window === "undefined" || typeof document === "undefined") {
return;
}
const blob = new Blob([content], { type: "text/markdown;charset=utf-8" });
const url = URL.createObjectURL(blob);
const anchor = document.createElement("a");
try {
anchor.href = url;
anchor.download = filename;
anchor.style.display = "none";
document.body.appendChild(anchor);
anchor.click();
} finally {
anchor.remove();
URL.revokeObjectURL(url);
}
}