Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(export/share): share type for format
Elian Doran06de06b50115
diff --git a/apps/edit-docs/src/edit-docs.ts b/apps/edit-docs/src/edit-docs.tsindex db5d4be0c7..b6a04969fa 100644--- a/apps/edit-docs/src/edit-docs.ts+++ b/apps/edit-docs/src/edit-docs.ts@@ -6,7 +6,7 @@ import { initializeTranslations } from "@triliumnext/server/src/services/i18n.jsimport debounce from "@triliumnext/client/src/services/debounce.js";import { extractZip, importData, initializeDatabase, startElectron } from "./utils.js";import cls from "@triliumnext/server/src/services/cls.js";-import type { AdvancedExportOptions } from "@triliumnext/server/src/services/export/zip/abstract_provider.js";+import type { AdvancedExportOptions, ExportFormat } from "@triliumnext/server/src/services/export/zip/abstract_provider.js";import { parseNoteMetaFile } from "@triliumnext/server/src/services/in_app_help.js";import type NoteMeta from "@triliumnext/server/src/services/meta/note_meta.js";@@ -75,7 +75,7 @@ async function setOptions() {optionsService.setOption("compressImages", "false");}-async function exportData(noteId: string, format: "html" | "markdown", outputPath: string, ignoredFiles?: Set<string>) {+async function exportData(noteId: string, format: ExportFormat, outputPath: string, ignoredFiles?: Set<string>) {const zipFilePath = "output.zip";try {diff --git a/apps/server/src/etapi/notes.ts b/apps/server/src/etapi/notes.tsindex 82280d0b91..941d095663 100644--- a/apps/server/src/etapi/notes.ts+++ b/apps/server/src/etapi/notes.ts@@ -14,6 +14,7 @@ import type { ParsedQs } from "qs";import type { NoteParams } from "../services/note-interface.js";import type { SearchParams } from "../services/search/services/types.js";import type { ValidatorMap } from "./etapi-interface.js";+import type { ExportFormat } from "../services/export/zip/abstract_provider.js";function register(router: Router) {eu.route(router, "get", "/etapi/notes", (req, res, next) => {@@ -157,7 +158,7 @@ function register(router: Router) {// (e.g. branchIds are not seen in UI), that we export "note export" instead.const branch = note.getParentBranches()[0];- zipExportService.exportToZip(taskContext, branch, format as "html" | "markdown", res);+ zipExportService.exportToZip(taskContext, branch, format as ExportFormat, res);});eu.route(router, "post", "/etapi/notes/:noteId/import", (req, res, next) => {diff --git a/apps/server/src/services/export/single.ts b/apps/server/src/services/export/single.tsindex b626bf9193..2748c88506 100644--- a/apps/server/src/services/export/single.ts+++ b/apps/server/src/services/export/single.ts@@ -9,8 +9,9 @@ import type TaskContext from "../task_context.js";import type BBranch from "../../becca/entities/bbranch.js";import type { Response } from "express";import type BNote from "../../becca/entities/bnote.js";+import type { ExportFormat } from "./zip/abstract_provider.js";-function exportSingleNote(taskContext: TaskContext, branch: BBranch, format: "html" | "markdown", res: Response) {+function exportSingleNote(taskContext: TaskContext, branch: BBranch, format: ExportFormat, res: Response) {const note = branch.getNote();if (note.type === "image" || note.type === "file") {@@ -33,7 +34,7 @@ function exportSingleNote(taskContext: TaskContext, branch: BBranch, format: "httaskContext.taskSucceeded();}-export function mapByNoteType(note: BNote, content: string | Buffer<ArrayBufferLike>, format: "html" | "markdown") {+export function mapByNoteType(note: BNote, content: string | Buffer<ArrayBufferLike>, format: ExportFormat) {let payload, extension, mime;if (typeof content !== "string") {diff --git a/apps/server/src/services/export/zip.ts b/apps/server/src/services/export/zip.tsindex 26af3424fb..1bfc1e8423 100644--- a/apps/server/src/services/export/zip.ts+++ b/apps/server/src/services/export/zip.ts@@ -19,12 +19,12 @@ import BBranch from "../../becca/entities/bbranch.js";import type { Response } from "express";import type { NoteMetaFile } from "../meta/note_meta.js";import HtmlExportProvider from "./zip/html.js";-import { AdvancedExportOptions, ZipExportProviderData } from "./zip/abstract_provider.js";+import { AdvancedExportOptions, type ExportFormat, ZipExportProviderData } from "./zip/abstract_provider.js";import MarkdownExportProvider from "./zip/markdown.js";import ShareThemeExportProvider from "./zip/share_theme.js";import type BNote from "../../becca/entities/bnote.js";-async function exportToZip(taskContext: TaskContext, branch: BBranch, format: "html" | "markdown" | "share", res: Response | fs.WriteStream, setHeaders = true, zipExportOptions?: AdvancedExportOptions) {+async function exportToZip(taskContext: TaskContext, branch: BBranch, format: ExportFormat, res: Response | fs.WriteStream, setHeaders = true, zipExportOptions?: AdvancedExportOptions) {if (!["html", "markdown", "share"].includes(format)) {throw new ValidationError(`Only 'html' and 'markdown' allowed as export format, '${format}' given`);}@@ -432,7 +432,7 @@ async function exportToZip(taskContext: TaskContext, branch: BBranch, format: "htaskContext.taskSucceeded();}-async function exportToZipFile(noteId: string, format: "markdown" | "html", zipFilePath: string, zipExportOptions?: AdvancedExportOptions) {+async function exportToZipFile(noteId: string, format: ExportFormat, zipFilePath: string, zipExportOptions?: AdvancedExportOptions) {const fileOutputStream = fs.createWriteStream(zipFilePath);const taskContext = new TaskContext("no-progress-reporting");diff --git a/apps/server/src/services/export/zip/abstract_provider.ts b/apps/server/src/services/export/zip/abstract_provider.tsindex 6ca5fdb9a4..f777ed1cbb 100644--- a/apps/server/src/services/export/zip/abstract_provider.ts+++ b/apps/server/src/services/export/zip/abstract_provider.ts@@ -6,6 +6,8 @@ import mimeTypes from "mime-types";type RewriteLinksFn = (content: string, noteMeta: NoteMeta) => string;+export type ExportFormat = "html" | "markdown" | "share";+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.@@ -49,7 +51,7 @@ export abstract class ZipExportProvider {abstract prepareContent(title: string, content: string | Buffer, noteMeta: NoteMeta, note: BNote | undefined, branch: BBranch): string | Buffer;abstract afterDone(rootMeta: NoteMeta): void;- mapExtension(type: string | null, mime: string, existingExtension: string, format: string) {+ mapExtension(type: string | null, mime: string, existingExtension: string, format: ExportFormat) {// the following two are handled specifically since we always want to have these extensions no matter the automatic detection// and/or existing detected extensions in the note nameif (type === "text" && format === "markdown") {diff --git a/apps/server/src/services/export/zip/share_theme.ts b/apps/server/src/services/export/zip/share_theme.tsindex f8475b0c2c..03bdb68b4d 100644--- a/apps/server/src/services/export/zip/share_theme.ts+++ b/apps/server/src/services/export/zip/share_theme.ts@@ -65,10 +65,6 @@ export default class ShareThemeExportProvider extends ZipExportProvider {this.#saveIndex(rootMeta);}- mapExtension(_type: string | null, _mime: string, _existingExtension: string, _format: string): string | null {- return "html";- }-#saveIndex(rootMeta: NoteMeta) {if (!this.indexMeta?.dataFileName) {return;diff --git a/apps/server/src/services/meta/note_meta.ts b/apps/server/src/services/meta/note_meta.tsindex 33e7a7843a..7a7a9f4b7c 100644--- a/apps/server/src/services/meta/note_meta.ts+++ b/apps/server/src/services/meta/note_meta.ts@@ -1,6 +1,7 @@import type { NoteType } from "@triliumnext/commons";import type AttachmentMeta from "./attachment_meta.js";import type AttributeMeta from "./attribute_meta.js";+import type { ExportFormat } from "../export/zip/abstract_provider.js";export interface NoteMetaFile {formatVersion: number;@@ -19,7 +20,7 @@ export default interface NoteMeta {type?: NoteType;mime?: string;/** 'html' or 'markdown', applicable to text notes only */- format?: "html" | "markdown";+ format?: ExportFormat;dataFileName?: string;dirFileName?: string;/** this file should not be imported (e.g., HTML navigation) */