From d1fc69ffbef8bcd876f0d60b3cfeec215c8baee0 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 10 Dec 2024 14:11:38 +0100 Subject: [PATCH] Desktop, Cli: Prevent PDF and HTML export from failing when a plugin references a non-existent file --- packages/htmlpack/src/index.ts | 2 ++ .../interop/InteropService_Exporter_Html.ts | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/htmlpack/src/index.ts b/packages/htmlpack/src/index.ts index 6dae9d4fe..5659ac8be 100644 --- a/packages/htmlpack/src/index.ts +++ b/packages/htmlpack/src/index.ts @@ -1,4 +1,5 @@ import * as fs from 'fs-extra'; +import { pathExistsSync } from 'fs-extra'; const Entities = require('html-entities').AllHtmlEntities; const htmlparser2 = require('@joplin/fork-htmlparser2'); const Datauri = require('datauri/sync'); @@ -102,6 +103,7 @@ const processLinkTag = (baseDir: string, _name: string, attrs: any): string => { const filePath = `${baseDir}/${href}`; + if (!pathExistsSync(filePath)) return null; const content = fs.readFileSync(filePath, 'utf8'); return ``; }; diff --git a/packages/lib/services/interop/InteropService_Exporter_Html.ts b/packages/lib/services/interop/InteropService_Exporter_Html.ts index e8aa08e62..8142cd275 100644 --- a/packages/lib/services/interop/InteropService_Exporter_Html.ts +++ b/packages/lib/services/interop/InteropService_Exporter_Html.ts @@ -15,6 +15,9 @@ const { escapeHtml } = require('../../string-utils.js'); import { assetsToHeaders } from '@joplin/renderer'; import getPluginSettingValue from '../plugins/utils/getPluginSettingValue'; import { LinkRenderingType } from '@joplin/renderer/MdToHtml'; +import Logger from '@joplin/utils/Logger'; + +const logger = Logger.create('InteropService_Exporter_Html'); export default class InteropService_Exporter_Html extends InteropService_Exporter_Base { @@ -136,11 +139,15 @@ export default class InteropService_Exporter_Html extends InteropService_Exporte for (let i = 0; i < result.pluginAssets.length; i++) { const asset = result.pluginAssets[i]; const filePath = asset.pathIsAbsolute ? asset.path : `${libRootPath}/node_modules/@joplin/renderer/assets/${asset.name}`; - const destPath = `${dirname(noteFilePath)}/pluginAssets/${asset.name}`; - const dir = dirname(destPath); - await shim.fsDriver().mkdir(dir); - this.createdDirs_.push(dir); - await shim.fsDriver().copy(filePath, destPath); + if (!(await shim.fsDriver().exists(filePath))) { + logger.warn(`File does not exist and cannot be exported: ${filePath}`); + } else { + const destPath = `${dirname(noteFilePath)}/pluginAssets/${asset.name}`; + const dir = dirname(destPath); + await shim.fsDriver().mkdir(dir); + this.createdDirs_.push(dir); + await shim.fsDriver().copy(filePath, destPath); + } } const fullHtml = `