fix(export/share): note children preview links not working

This commit is contained in:
Elian Doran 2025-06-23 19:00:20 +03:00
parent 0efdf65202
commit 8523050ab2
No known key found for this signature in database
6 changed files with 14 additions and 8 deletions

View File

@ -35,6 +35,7 @@ async function exportToZip(taskContext: TaskContext, branch: BBranch, format: "h
});
const noteIdToMeta: Record<string, NoteMeta> = {};
const rewriteFn = (zipExportOptions?.customRewriteLinks ? zipExportOptions?.customRewriteLinks(rewriteLinks, getNoteTargetUrl) : rewriteLinks);
function buildProvider() {
const providerData: ZipExportProviderData = {
@ -42,7 +43,8 @@ async function exportToZip(taskContext: TaskContext, branch: BBranch, format: "h
metaFile,
archive,
rootMeta: rootMeta!,
branch
branch,
rewriteFn
};
switch (format) {
case "html":
@ -275,8 +277,6 @@ async function exportToZip(taskContext: TaskContext, branch: BBranch, format: "h
return url;
}
const rewriteFn = (zipExportOptions?.customRewriteLinks ? zipExportOptions?.customRewriteLinks(rewriteLinks, getNoteTargetUrl) : rewriteLinks);
function rewriteLinks(content: string, noteMeta: NoteMeta): string {
content = content.replace(/src="[^"]*api\/images\/([a-zA-Z0-9_]+)\/[^"]*"/g, (match, targetNoteId) => {
const url = getNoteTargetUrl(targetNoteId, noteMeta);
@ -325,9 +325,6 @@ async function exportToZip(taskContext: TaskContext, branch: BBranch, format: "h
}
content = provider.prepareContent(title, content, noteMeta, note, branch);
if (isText) {
content = rewriteFn(content as string, noteMeta);
}
return content;
}

View File

@ -28,6 +28,7 @@ export interface ZipExportProviderData {
rootMeta: NoteMeta;
archive: Archiver;
zipExportOptions?: AdvancedExportOptions;
rewriteFn: RewriteLinksFn;
}
export abstract class ZipExportProvider {
@ -37,6 +38,7 @@ export abstract class ZipExportProvider {
rootMeta: NoteMeta;
archive: Archiver;
zipExportOptions?: AdvancedExportOptions;
rewriteFn: RewriteLinksFn;
constructor(data: ZipExportProviderData) {
this.branch = data.branch;
@ -45,6 +47,7 @@ export abstract class ZipExportProvider {
this.rootMeta = data.rootMeta;
this.archive = data.archive;
this.zipExportOptions = data.zipExportOptions;
this.rewriteFn = data.rewriteFn;
}
abstract prepareMeta(): void;

View File

@ -62,7 +62,11 @@ export default class HtmlExportProvider extends ZipExportProvider {
</html>`;
}
return content.length < 100_000 ? html.prettyPrint(content, { indent_size: 2 }) : content;
if (content.length < 100_000) {
content = html.prettyPrint(content, { indent_size: 2 })
}
content = this.rewriteFn(content as string, noteMeta);
return content;
} else {
return content;
}

View File

@ -15,6 +15,7 @@ export default class MarkdownExportProvider extends ZipExportProvider {
${markdownContent}`;
}
markdownContent = this.rewriteFn(markdownContent, noteMeta);
return markdownContent;
} else {
return content;

View File

@ -51,6 +51,7 @@ export default class ShareThemeExportProvider extends ZipExportProvider {
if (note) {
content = renderNoteForExport(note, branch, basePath);
content = this.rewriteFn(content, noteMeta);
}
return content;

View File

@ -137,7 +137,7 @@ content = content.replaceAll(headingRe, (...match) => {
const action = note.type === "book" ? "getChildNotes" : "getVisibleChildNotes";
for (const childNote of note[action]()) {
const isExternalLink = childNote.hasLabel("shareExternal") || childNote.hasLabel("shareExternalLink");
const linkHref = isExternalLink ? childNote.getLabelValue("shareExternal") ?? childNote.getLabelValue("shareExternalLink") : `./${childNote.shareId}`;
const linkHref = isExternalLink ? childNote.getLabelValue("shareExternal") ?? childNote.getLabelValue("shareExternalLink") : `./${childNote.shareId ?? "#root/" + childNote.noteId}`;
const target = isExternalLink ? ` target="_blank" rel="noopener noreferrer"` : "";
%>
<li>