feat(edit-docs): rewrite links to allow navigation in help

This commit is contained in:
Elian Doran
2025-04-09 15:48:03 +03:00
parent 652fc48a86
commit a88a92d490
2 changed files with 68 additions and 6 deletions

View File

@@ -9,6 +9,7 @@ import type { WriteStream } from "fs";
import debounce from "./src/public/app/services/debounce.js";
import { extractZip, initializeDatabase, startElectron } from "./electron-utils.js";
import cls from "./src/services/cls.js";
import type { AdvancedExportOptions } from "./src/services/export/zip.js";
const NOTE_ID_USER_GUIDE = "pOsGYCXsbNQG";
const markdownPath = path.join("docs", "User Guide");
@@ -69,7 +70,54 @@ async function exportData(format: "html" | "markdown", outputPath: string) {
// First export as zip.
const { exportToZipFile } = (await import("./src/services/export/zip.js")).default;
await exportToZipFile(NOTE_ID_USER_GUIDE, format, zipFilePath);
const exportOpts: AdvancedExportOptions = {};
if (format === "html") {
exportOpts.customRewriteLinks = (originalRewriteLinks, getNoteTargetUrl) => {
return (content: string, noteMeta: NoteMeta) => {
content = content.replace(/src="[^"]*api\/images\/([a-zA-Z0-9_]+)\/[^"]*"/g, (match, targetNoteId) => {
const url = getNoteTargetUrl(targetNoteId, noteMeta);
return url ? `src="${url}"` : match;
});
content = content.replace(/src="[^"]*api\/attachments\/([a-zA-Z0-9_]+)\/image\/[^"]*"/g, (match, targetAttachmentId) => {
const url = findAttachment(targetAttachmentId);
return url ? `src="${url}"` : match;
});
content = content.replace(/href="[^"]*#root[^"]*attachmentId=([a-zA-Z0-9_]+)\/?"/g, (match, targetAttachmentId) => {
const url = findAttachment(targetAttachmentId);
return url ? `href="${url}"` : match;
});
content = content.replace(/href="[^"]*#root[a-zA-Z0-9_\/]*\/([a-zA-Z0-9_]+)[^"]*"/g, (match, targetNoteId) => {
const components = match.split("/");
components[components.length - 1] = `_help_${components[components.length - 1]}`;
return components.join("/");
});
return content;
function findAttachment(targetAttachmentId: string) {
let url;
const attachmentMeta = (noteMeta.attachments || []).find((attMeta) => attMeta.attachmentId === targetAttachmentId);
if (attachmentMeta) {
// easy job here, because attachment will be in the same directory as the note's data file.
url = attachmentMeta.dataFileName;
} else {
console.info(`Could not find attachment meta object for attachmentId '${targetAttachmentId}'`);
}
return url;
}
};
};
}
await exportToZipFile(NOTE_ID_USER_GUIDE, format, zipFilePath, exportOpts);
await extractZip(zipFilePath, outputPath);
} finally {
if (await fsExtra.exists(zipFilePath)) {