fix(export/share): use right extension for images

This commit is contained in:
Elian Doran
2025-06-24 19:53:21 +03:00
parent 06de06b501
commit fded714f18
3 changed files with 22 additions and 3 deletions

View File

@@ -23,6 +23,7 @@ import { AdvancedExportOptions, type ExportFormat, ZipExportProviderData } from
import MarkdownExportProvider from "./zip/markdown.js"; import MarkdownExportProvider from "./zip/markdown.js";
import ShareThemeExportProvider from "./zip/share_theme.js"; import ShareThemeExportProvider from "./zip/share_theme.js";
import type BNote from "../../becca/entities/bnote.js"; import type BNote from "../../becca/entities/bnote.js";
import { NoteType } from "@triliumnext/commons";
async function exportToZip(taskContext: TaskContext, branch: BBranch, format: ExportFormat, 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)) { if (!["html", "markdown", "share"].includes(format)) {
@@ -77,7 +78,7 @@ async function exportToZip(taskContext: TaskContext, branch: BBranch, format: Ex
} }
} }
function getDataFileName(type: string | null, mime: string, baseFileName: string, existingFileNames: Record<string, number>): string { function getDataFileName(type: NoteType | null, mime: string, baseFileName: string, existingFileNames: Record<string, number>): string {
let fileName = baseFileName.trim(); let fileName = baseFileName.trim();
// Crop fileName to avoid its length exceeding 30 and prevent cutting into the extension. // Crop fileName to avoid its length exceeding 30 and prevent cutting into the extension.

View File

@@ -3,6 +3,7 @@ import type { default as NoteMeta, NoteMetaFile } from "../../meta/note_meta.js"
import type BNote from "../../../becca/entities/bnote.js"; import type BNote from "../../../becca/entities/bnote.js";
import type BBranch from "../../../becca/entities/bbranch.js"; import type BBranch from "../../../becca/entities/bbranch.js";
import mimeTypes from "mime-types"; import mimeTypes from "mime-types";
import { NoteType } from "@triliumnext/commons";
type RewriteLinksFn = (content: string, noteMeta: NoteMeta) => string; type RewriteLinksFn = (content: string, noteMeta: NoteMeta) => string;
@@ -51,7 +52,16 @@ export abstract class ZipExportProvider {
abstract prepareContent(title: string, content: string | Buffer, noteMeta: NoteMeta, note: BNote | undefined, branch: BBranch): string | Buffer; abstract prepareContent(title: string, content: string | Buffer, noteMeta: NoteMeta, note: BNote | undefined, branch: BBranch): string | Buffer;
abstract afterDone(rootMeta: NoteMeta): void; abstract afterDone(rootMeta: NoteMeta): void;
mapExtension(type: string | null, mime: string, existingExtension: string, format: ExportFormat) { /**
* Determines the extension of the resulting file for a specific note type.
*
* @param type the type of the note.
* @param mime the mime type of the note.
* @param existingExtension the existing extension, including the leading period character.
* @param format the format requested for export (e.g. HTML, Markdown).
* @returns an extension *without* the leading period character, or `null` to preserve the existing extension instead.
*/
mapExtension(type: NoteType | 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 // 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 name // and/or existing detected extensions in the note name
if (type === "text" && format === "markdown") { if (type === "text" && format === "markdown") {

View File

@@ -1,6 +1,6 @@
import { join } from "path"; import { join } from "path";
import NoteMeta, { NoteMetaFile } from "../../meta/note_meta"; import NoteMeta, { NoteMetaFile } from "../../meta/note_meta";
import { ZipExportProvider } from "./abstract_provider.js"; import { ExportFormat, ZipExportProvider } from "./abstract_provider.js";
import { RESOURCE_DIR } from "../../resource_dir"; import { RESOURCE_DIR } from "../../resource_dir";
import { getResourceDir, isDev } from "../../utils"; import { getResourceDir, isDev } from "../../utils";
import fs from "fs"; import fs from "fs";
@@ -65,6 +65,14 @@ export default class ShareThemeExportProvider extends ZipExportProvider {
this.#saveIndex(rootMeta); this.#saveIndex(rootMeta);
} }
mapExtension(type: string | null, mime: string, existingExtension: string, format: ExportFormat): string | null {
if (mime.startsWith("image/")) {
return null;
}
return "html";
}
#saveIndex(rootMeta: NoteMeta) { #saveIndex(rootMeta: NoteMeta) {
if (!this.indexMeta?.dataFileName) { if (!this.indexMeta?.dataFileName) {
return; return;