refactor(export/zip): extract prepare content into providers

This commit is contained in:
Elian Doran
2025-06-23 16:22:42 +03:00
parent e529633b8b
commit 55bb2fdb9b
4 changed files with 77 additions and 62 deletions

View File

@@ -1,11 +1,30 @@
import { Archiver } from "archiver";
import type { default as NoteMeta, NoteMetaFile } from "../../meta/note_meta.js";
type RewriteLinksFn = (content: string, noteMeta: NoteMeta) => string;
export interface AdvancedExportOptions {
/**
* If `true`, then only the note's content will be kept. If `false` (default), then each page will have its own <html> template.
*/
skipHtmlTemplate?: boolean;
/**
* Provides a custom function to rewrite the links found in HTML or Markdown notes. This method is called for every note imported, if it's of the right type.
*
* @param originalRewriteLinks the original rewrite links function. Can be used to access the default behaviour without having to reimplement it.
* @param getNoteTargetUrl the method to obtain a note's target URL, used internally by `originalRewriteLinks` but can be used here as well.
* @returns a function to rewrite the links in HTML or Markdown notes.
*/
customRewriteLinks?: (originalRewriteLinks: RewriteLinksFn, getNoteTargetUrl: (targetNoteId: string, sourceMeta: NoteMeta) => string | null) => RewriteLinksFn;
}
export interface ZipExportProviderData {
getNoteTargetUrl: (targetNoteId: string, sourceMeta: NoteMeta) => string | null;
metaFile: NoteMetaFile;
rootMeta: NoteMeta;
archive: Archiver;
zipExportOptions?: AdvancedExportOptions;
}
export abstract class ZipExportProvider {
@@ -14,14 +33,17 @@ export abstract class ZipExportProvider {
getNoteTargetUrl: (targetNoteId: string, sourceMeta: NoteMeta) => string | null;
rootMeta: NoteMeta;
archive: Archiver;
zipExportOptions?: AdvancedExportOptions;
constructor(data: ZipExportProviderData) {
this.metaFile = data.metaFile;
this.getNoteTargetUrl = data.getNoteTargetUrl;
this.rootMeta = data.rootMeta;
this.archive = data.archive;
this.zipExportOptions = data.zipExportOptions;
}
abstract prepareMeta(): void;
abstract prepareContent(title: string, content: string | Buffer, noteMeta: NoteMeta): string | Buffer;
abstract afterDone(): void;
}