From 8b066d46e24afd260abecb4abaaba8a4ad664f9b Mon Sep 17 00:00:00 2001 From: zsviczian Date: Sun, 28 Jul 2024 06:24:30 +0200 Subject: [PATCH] fix rename and skip inline fonts to cache key --- src/EmbeddedFileLoader.ts | 6 +++++- src/ExcalidrawView.ts | 2 +- src/MarkdownPostProcessor.ts | 32 +++++++++++++++++++++++++++----- src/utils/ImageCache.ts | 6 ++++-- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/EmbeddedFileLoader.ts b/src/EmbeddedFileLoader.ts index 07b4c49..eea7350 100644 --- a/src/EmbeddedFileLoader.ts +++ b/src/EmbeddedFileLoader.ts @@ -366,7 +366,10 @@ export class EmbeddedFilesLoader { const hasFilenameParts = Boolean((inFile instanceof EmbeddedFile) && inFile.filenameparts); const filenameParts = hasFilenameParts ? (inFile as EmbeddedFile).filenameparts : null; const cacheKey:ImageKey = { - ...hasFilenameParts? filenameParts : { + ...hasFilenameParts? { + ...filenameParts, + inlineFonts: !exportSettings.skipInliningFonts, + }: { filepath: file.path, hasBlockref: false, hasGroupref: false, @@ -375,6 +378,7 @@ export class EmbeddedFilesLoader { hasFrameref: false, hasClippedFrameref: false, hasSectionref: false, + inlineFonts: !exportSettings.skipInliningFonts, blockref: null, sectionref: null, linkpartReference: null, diff --git a/src/ExcalidrawView.ts b/src/ExcalidrawView.ts index 5b2a7a2..072c1a5 100644 --- a/src/ExcalidrawView.ts +++ b/src/ExcalidrawView.ts @@ -1914,7 +1914,7 @@ export default class ExcalidrawView extends TextFileView { if (!filename) { return; } - const {folderpath} = splitFolderAndFilename(filename); + const {folderpath} = splitFolderAndFilename(this.file.path); this.app.vault.rename(this.file, normalizePath(`${folderpath}/${filename}.md`)); })(); return; diff --git a/src/MarkdownPostProcessor.ts b/src/MarkdownPostProcessor.ts index efe88ea..cc1a870 100644 --- a/src/MarkdownPostProcessor.ts +++ b/src/MarkdownPostProcessor.ts @@ -85,7 +85,14 @@ const _getPNG = async ({imgAttributes,filenameParts,theme,cacheReady,img,file,ex ? 2 : 1; - const cacheKey = {...filenameParts, isDark: theme==="dark", previewImageType: PreviewImageType.PNG, scale, isTransparent: !exportSettings.withBackground}; + const cacheKey = { + ...filenameParts, + isDark: theme==="dark", + previewImageType: PreviewImageType.PNG, + scale, + isTransparent: !exportSettings.withBackground, + inlineFonts: true, //though for PNG this makes no difference, but the key requires it + }; if(cacheReady) { const src = await imageCache.getImageFromCache(cacheKey); @@ -164,7 +171,16 @@ const _getSVGIMG = async ({filenameParts,theme,cacheReady,img,file,exportSetting exportSettings: ExportSettings, loader: EmbeddedFilesLoader, }):Promise => { - const cacheKey = {...filenameParts, isDark: theme==="dark", previewImageType: PreviewImageType.SVGIMG, scale:1, isTransparent: !exportSettings.withBackground}; + exportSettings.skipInliningFonts = false; + const cacheKey = { + ...filenameParts, + isDark: theme==="dark", + previewImageType: PreviewImageType.SVGIMG, + scale:1, + isTransparent: !exportSettings.withBackground, + inlineFonts: !exportSettings.skipInliningFonts, + }; + if(cacheReady) { const src = await imageCache.getImageFromCache(cacheKey); if(src && typeof src === "string") { @@ -183,7 +199,6 @@ const _getSVGIMG = async ({filenameParts,theme,cacheReady,img,file,exportSetting } } - exportSettings.skipInliningFonts = false; const svg = convertSVGStringToElement(( await createSVG( filenameParts.hasGroupref || filenameParts.hasBlockref || filenameParts.hasSectionref || filenameParts.hasFrameref || filenameParts.hasClippedFrameref @@ -223,13 +238,20 @@ const _getSVGNative = async ({filenameParts,theme,cacheReady,containerElement,fi exportSettings: ExportSettings, loader: EmbeddedFilesLoader, }):Promise => { - const cacheKey = {...filenameParts, isDark: theme==="dark", previewImageType: PreviewImageType.SVG, scale:1, isTransparent: !exportSettings.withBackground}; + exportSettings.skipInliningFonts = false; + const cacheKey = { + ...filenameParts, + isDark: theme==="dark", + previewImageType: PreviewImageType.SVG, + scale:1, + isTransparent: !exportSettings.withBackground, + inlineFonts: !exportSettings.skipInliningFonts, + }; let maybeSVG; if(cacheReady) { maybeSVG = await imageCache.getImageFromCache(cacheKey); } - exportSettings.skipInliningFonts = false; const svg = (maybeSVG && (maybeSVG instanceof SVGSVGElement)) ? maybeSVG : convertSVGStringToElement((await createSVG( diff --git a/src/utils/ImageCache.ts b/src/utils/ImageCache.ts index 34f8729..4dfc9ff 100644 --- a/src/utils/ImageCache.ts +++ b/src/utils/ImageCache.ts @@ -21,11 +21,13 @@ export type ImageKey = { previewImageType: PreviewImageType; scale: number; isTransparent: boolean; + inlineFonts: boolean; } & FILENAMEPARTS; const getKey = (key: ImageKey): string => `${key.filepath}#${key.blockref??""}#${key.sectionref??""}#${key.isDark ? 1 : 0}#${ - key.hasGroupref}#${key.hasArearef}#${key.hasFrameref}#${key.hasClippedFrameref}#${key.hasSectionref}#${ + key.hasGroupref}#${key.hasArearef}#${key.hasFrameref}#${key.hasClippedFrameref}#${ + key.hasSectionref}#${key.inlineFonts}#${ key.previewImageType === PreviewImageType.SVGIMG ? 1 : key.previewImageType === PreviewImageType.PNG @@ -172,7 +174,7 @@ class ImageCache { const cursor = (event.target as IDBRequest).result; if(cursor) { const key = cursor.key as string; - const isLegacyKey = key.split("#").length-1 < 11; // introduced hasGroupref, etc. in 1.9.28 // introduced hasClippedFrameref in 2.2.10 + const isLegacyKey = key.split("#").length-1 < 12; // introduced hasGroupref, etc. in 1.9.28 // introduced hasClippedFrameref in 2.2.10 //introduced inlineFonts 2.2.11 const filepath = key.split("#")[0]; const fileExists = files.some((f: TFile) => f.path === filepath); const file = fileExists ? files.find((f: TFile) => f.path === filepath) : null;