2020-11-07 17:59:37 +02:00
|
|
|
import shim from '@joplin/lib/shim';
|
2021-01-22 19:41:11 +02:00
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
|
|
|
import Note from '@joplin/lib/models/Note';
|
|
|
|
import Resource from '@joplin/lib/models/Resource';
|
2021-10-01 20:35:27 +02:00
|
|
|
const bridge = require('@electron/remote').require('./bridge').default;
|
2021-01-23 17:51:19 +02:00
|
|
|
import ResourceFetcher from '@joplin/lib/services/ResourceFetcher';
|
2021-05-03 16:13:51 +02:00
|
|
|
import htmlUtils from '@joplin/lib/htmlUtils';
|
2024-06-19 11:54:34 +02:00
|
|
|
import rendererHtmlUtils, { extractHtmlBody, removeWrappingParagraphAndTrailingEmptyElements } from '@joplin/renderer/htmlUtils';
|
2023-07-27 17:05:56 +02:00
|
|
|
import Logger from '@joplin/utils/Logger';
|
2023-08-23 19:16:06 +02:00
|
|
|
import { fileUriToPath } from '@joplin/utils/url';
|
2023-10-31 18:53:47 +02:00
|
|
|
import { MarkupLanguage } from '@joplin/renderer';
|
2024-01-26 21:11:05 +02:00
|
|
|
import { HtmlToMarkdownHandler, MarkupToHtmlHandler } from './types';
|
|
|
|
import markupRenderOptions from './markupRenderOptions';
|
2024-02-08 15:06:18 +02:00
|
|
|
import { fileExtension, filename, safeFileExtension, safeFilename } from '@joplin/utils/path';
|
2020-11-07 17:59:37 +02:00
|
|
|
const joplinRendererUtils = require('@joplin/renderer').utils;
|
2020-05-11 20:26:04 +02:00
|
|
|
const { clipboard } = require('electron');
|
2024-06-04 10:50:18 +02:00
|
|
|
import * as mimeUtils from '@joplin/lib/mime-utils';
|
2020-05-11 20:26:04 +02:00
|
|
|
const md5 = require('md5');
|
2021-05-03 16:13:51 +02:00
|
|
|
const path = require('path');
|
2020-05-02 17:41:07 +02:00
|
|
|
|
2021-05-04 16:00:40 +02:00
|
|
|
const logger = Logger.create('resourceHandling');
|
|
|
|
|
2020-05-02 17:41:07 +02:00
|
|
|
export async function handleResourceDownloadMode(noteBody: string) {
|
|
|
|
if (noteBody && Setting.value('sync.resourceDownloadMode') === 'auto') {
|
|
|
|
const resourceIds = await Note.linkedResourceIds(noteBody);
|
|
|
|
await ResourceFetcher.instance().markForDownload(resourceIds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-11-12 21:13:28 +02:00
|
|
|
export async function commandAttachFileToBody(body: string, filePaths: string[] = null, options: any = null) {
|
2020-05-02 17:41:07 +02:00
|
|
|
options = {
|
|
|
|
createFileURL: false,
|
|
|
|
position: 0,
|
2023-10-31 18:53:47 +02:00
|
|
|
markupLanguage: MarkupLanguage.Markdown,
|
2020-05-02 17:41:07 +02:00
|
|
|
...options,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!filePaths) {
|
2021-11-01 09:38:06 +02:00
|
|
|
filePaths = await bridge().showOpenDialog({
|
2020-05-02 17:41:07 +02:00
|
|
|
properties: ['openFile', 'createDirectory', 'multiSelections'],
|
|
|
|
});
|
|
|
|
if (!filePaths || !filePaths.length) return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < filePaths.length; i++) {
|
|
|
|
const filePath = filePaths[i];
|
|
|
|
try {
|
2021-05-04 16:00:40 +02:00
|
|
|
logger.info(`Attaching ${filePath}`);
|
2020-05-02 17:41:07 +02:00
|
|
|
const newBody = await shim.attachFileToNoteBody(body, filePath, options.position, {
|
|
|
|
createFileURL: options.createFileURL,
|
2023-08-08 16:49:54 +02:00
|
|
|
resizeLargeImages: Setting.value('imageResizing'),
|
2023-10-31 18:53:47 +02:00
|
|
|
markupLanguage: options.markupLanguage,
|
2020-05-02 17:41:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!newBody) {
|
2021-05-04 16:00:40 +02:00
|
|
|
logger.info('File attachment was cancelled');
|
2020-05-02 17:41:07 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
body = newBody;
|
2021-05-04 16:00:40 +02:00
|
|
|
logger.info('File was attached.');
|
2020-05-02 17:41:07 +02:00
|
|
|
} catch (error) {
|
2021-05-04 16:00:40 +02:00
|
|
|
logger.error(error);
|
2020-05-02 17:41:07 +02:00
|
|
|
bridge().showErrorMessageBox(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2020-05-02 17:41:07 +02:00
|
|
|
export function resourcesStatus(resourceInfos: any) {
|
|
|
|
let lowestIndex = joplinRendererUtils.resourceStatusIndex('ready');
|
|
|
|
for (const id in resourceInfos) {
|
|
|
|
const s = joplinRendererUtils.resourceStatus(Resource, resourceInfos[id]);
|
|
|
|
const idx = joplinRendererUtils.resourceStatusIndex(s);
|
|
|
|
if (idx < lowestIndex) lowestIndex = idx;
|
|
|
|
}
|
|
|
|
return joplinRendererUtils.resourceStatusName(lowestIndex);
|
|
|
|
}
|
2020-05-11 20:26:04 +02:00
|
|
|
|
2024-04-05 13:16:49 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
|
2023-08-21 19:37:33 +02:00
|
|
|
export async function getResourcesFromPasteEvent(event: any) {
|
2020-05-11 20:26:04 +02:00
|
|
|
const output = [];
|
|
|
|
const formats = clipboard.availableFormats();
|
|
|
|
for (let i = 0; i < formats.length; i++) {
|
|
|
|
const format = formats[i].toLowerCase();
|
|
|
|
const formatType = format.split('/')[0];
|
|
|
|
|
|
|
|
if (formatType === 'image') {
|
|
|
|
if (event) event.preventDefault();
|
|
|
|
|
|
|
|
const image = clipboard.readImage();
|
|
|
|
|
|
|
|
const fileExt = mimeUtils.toFileExtension(format);
|
|
|
|
const filePath = `${Setting.value('tempDir')}/${md5(Date.now())}.${fileExt}`;
|
|
|
|
|
|
|
|
await shim.writeImageToFile(image, format, filePath);
|
|
|
|
const md = await commandAttachFileToBody('', [filePath]);
|
|
|
|
await shim.fsDriver().remove(filePath);
|
|
|
|
|
2020-06-04 10:08:13 +02:00
|
|
|
if (md) output.push(md);
|
2020-05-11 20:26:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
2021-05-03 16:13:51 +02:00
|
|
|
|
2024-03-06 16:13:24 +02:00
|
|
|
|
|
|
|
const processImagesInPastedHtml = async (html: string) => {
|
2021-05-03 16:13:51 +02:00
|
|
|
const allImageUrls: string[] = [];
|
|
|
|
const mappedResources: Record<string, string> = {};
|
|
|
|
|
|
|
|
htmlUtils.replaceImageUrls(html, (src: string) => {
|
|
|
|
allImageUrls.push(src);
|
|
|
|
});
|
|
|
|
|
2024-02-08 14:51:31 +02:00
|
|
|
const downloadImage = async (imageSrc: string) => {
|
|
|
|
try {
|
2024-02-08 15:06:18 +02:00
|
|
|
const fileExt = safeFileExtension(fileExtension(imageSrc));
|
|
|
|
const name = safeFilename(filename(imageSrc));
|
|
|
|
const pieces = [name ? name : md5(Date.now() + Math.random())];
|
|
|
|
if (fileExt) pieces.push(fileExt);
|
|
|
|
const filePath = `${Setting.value('tempDir')}/${pieces.join('.')}`;
|
2024-02-08 14:51:31 +02:00
|
|
|
await shim.fetchBlob(imageSrc, { path: filePath });
|
|
|
|
const createdResource = await shim.createResourceFromPath(filePath);
|
|
|
|
await shim.fsDriver().remove(filePath);
|
|
|
|
mappedResources[imageSrc] = `file://${encodeURI(Resource.fullPath(createdResource))}`;
|
|
|
|
} catch (error) {
|
|
|
|
logger.warn(`Error creating a resource for ${imageSrc}.`, error);
|
|
|
|
mappedResources[imageSrc] = imageSrc;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const downloadImages: Promise<void>[] = [];
|
|
|
|
|
2021-05-03 16:13:51 +02:00
|
|
|
for (const imageSrc of allImageUrls) {
|
|
|
|
if (!mappedResources[imageSrc]) {
|
2024-02-08 14:51:31 +02:00
|
|
|
logger.info(`processPastedHtml: Processing image ${imageSrc}`);
|
2021-05-03 16:13:51 +02:00
|
|
|
try {
|
|
|
|
if (imageSrc.startsWith('file')) {
|
2021-11-22 19:17:28 +02:00
|
|
|
const imageFilePath = path.normalize(fileUriToPath(imageSrc));
|
2021-05-03 16:13:51 +02:00
|
|
|
const resourceDirPath = path.normalize(Setting.value('resourceDir'));
|
|
|
|
|
|
|
|
if (imageFilePath.startsWith(resourceDirPath)) {
|
|
|
|
mappedResources[imageSrc] = imageSrc;
|
|
|
|
} else {
|
|
|
|
const createdResource = await shim.createResourceFromPath(imageFilePath);
|
2021-05-10 11:12:29 +02:00
|
|
|
mappedResources[imageSrc] = `file://${encodeURI(Resource.fullPath(createdResource))}`;
|
2021-05-03 16:13:51 +02:00
|
|
|
}
|
2024-02-08 14:51:31 +02:00
|
|
|
} else if (imageSrc.startsWith('data:')) {
|
2023-07-29 17:31:36 +02:00
|
|
|
mappedResources[imageSrc] = imageSrc;
|
2021-05-03 16:13:51 +02:00
|
|
|
} else {
|
2024-02-08 14:51:31 +02:00
|
|
|
downloadImages.push(downloadImage(imageSrc));
|
2021-05-03 16:13:51 +02:00
|
|
|
}
|
2021-05-04 16:00:40 +02:00
|
|
|
} catch (error) {
|
2024-02-08 14:51:31 +02:00
|
|
|
logger.warn(`processPastedHtml: Error creating a resource for ${imageSrc}.`, error);
|
2021-05-03 16:13:51 +02:00
|
|
|
mappedResources[imageSrc] = imageSrc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 14:51:31 +02:00
|
|
|
await Promise.all(downloadImages);
|
|
|
|
|
2024-03-06 16:13:24 +02:00
|
|
|
return htmlUtils.replaceImageUrls(html, (src: string) => mappedResources[src]);
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function processPastedHtml(html: string, htmlToMd: HtmlToMarkdownHandler | null, mdToHtml: MarkupToHtmlHandler | null) {
|
|
|
|
// When copying text from eg. GitHub, the HTML might contain non-breaking
|
|
|
|
// spaces instead of regular spaces. If these non-breaking spaces are
|
|
|
|
// inserted into the TinyMCE editor (using insertContent), they will be
|
|
|
|
// dropped. So here we convert them to regular spaces.
|
|
|
|
// https://stackoverflow.com/a/31790544/561309
|
|
|
|
html = html.replace(/[\u202F\u00A0]/g, ' ');
|
|
|
|
|
|
|
|
html = await processImagesInPastedHtml(html);
|
|
|
|
|
2024-01-26 21:11:05 +02:00
|
|
|
// TinyMCE can accept any type of HTML, including HTML that may not be preserved once saved as
|
|
|
|
// Markdown. For example the content may have a dark background which would be supported by
|
|
|
|
// TinyMCE, but lost once the note is saved. So here we convert the HTML to Markdown then back
|
|
|
|
// to HTML to ensure that the content we paste will be handled correctly by the app.
|
|
|
|
if (htmlToMd && mdToHtml) {
|
2024-11-10 01:58:15 +02:00
|
|
|
const md = await htmlToMd(MarkupLanguage.Markdown, html, '', { preserveColorStyles: Setting.value('editor.pastePreserveColors') });
|
2024-01-26 21:11:05 +02:00
|
|
|
html = (await mdToHtml(MarkupLanguage.Markdown, md, markupRenderOptions({ bodyOnly: true }))).html;
|
2024-06-19 11:54:34 +02:00
|
|
|
|
|
|
|
// When plugins that add to the end of rendered content are installed, bodyOnly can
|
|
|
|
// fail to remove the wrapping paragraph. This works around that issue by removing
|
|
|
|
// the wrapping paragraph in more cases. See issue #10061.
|
|
|
|
if (!md.trim().includes('\n')) {
|
|
|
|
html = removeWrappingParagraphAndTrailingEmptyElements(html);
|
|
|
|
}
|
2024-01-26 21:11:05 +02:00
|
|
|
}
|
|
|
|
|
2024-03-06 16:13:24 +02:00
|
|
|
return extractHtmlBody(rendererHtmlUtils.sanitizeHtml(html, {
|
|
|
|
allowedFilePrefixes: [Setting.value('resourceDir')],
|
|
|
|
}));
|
2021-05-03 16:13:51 +02:00
|
|
|
}
|