mirror of
https://github.com/zadam/trilium.git
synced 2025-11-18 03:00:41 +01:00
enex import recognizes images, media references are converted to links
This commit is contained in:
@@ -4,6 +4,7 @@ const xml2js = require('xml2js');
|
||||
const log = require("./log");
|
||||
const utils = require("./utils");
|
||||
const noteService = require("./notes");
|
||||
const imageService = require("./image");
|
||||
|
||||
// date format is e.g. 20181121T193703Z
|
||||
function parseDate(text) {
|
||||
@@ -205,20 +206,50 @@ async function importEnex(file, parentNote) {
|
||||
// following is workaround for this issue: https://github.com/Leonidas-from-XIV/node-xml2js/issues/484
|
||||
content = extractContent(xmlObject['en-note']);
|
||||
|
||||
const resp = await noteService.createNote(rootNote.noteId, title, content, {
|
||||
const noteEntity = (await noteService.createNote(rootNote.noteId, title, content, {
|
||||
attributes,
|
||||
dateCreated,
|
||||
type: 'text',
|
||||
mime: 'text/html'
|
||||
});
|
||||
})).note;
|
||||
|
||||
for (const resource of resources) {
|
||||
await noteService.createNote(resp.note.noteId, resource.title, resource.content, {
|
||||
attributes: resource.attributes,
|
||||
type: 'file',
|
||||
mime: resource.mime
|
||||
});
|
||||
const hash = utils.md5(resource.content);
|
||||
|
||||
const mediaRegex = new RegExp(`<en-media hash="${hash}"[^>]*>`, 'g');
|
||||
|
||||
if (resource.mime.startsWith("image/")) {
|
||||
const originalName = "image." + resource.mime.substr(6);
|
||||
|
||||
const { url } = await imageService.saveImage(resource.content, originalName, noteEntity.noteId);
|
||||
|
||||
const imageLink = `<img src="${url}">`;
|
||||
|
||||
noteEntity.content = noteEntity.content.replace(mediaRegex, imageLink);
|
||||
|
||||
if (!note.content.includes(imageLink)) {
|
||||
// if there wasn't any match for the reference, we'll add the image anyway
|
||||
// otherwise image would be removed since no note would include it
|
||||
note.content += imageLink;
|
||||
}
|
||||
}
|
||||
else {
|
||||
const resourceNote = (await noteService.createNote(noteEntity.noteId, resource.title, resource.content, {
|
||||
attributes: resource.attributes,
|
||||
type: 'file',
|
||||
mime: resource.mime
|
||||
})).note;
|
||||
|
||||
const resourceLink = `<a href="#root/${resourceNote.noteId}">${utils.escapeHtml(resource.title)}</a>`;
|
||||
|
||||
noteEntity.content = noteEntity.content.replace(mediaRegex, resourceLink);
|
||||
}
|
||||
}
|
||||
|
||||
// save updated content with links to files/images
|
||||
await noteEntity.save();
|
||||
|
||||
console.log(noteEntity.content);
|
||||
}
|
||||
|
||||
saxStream.on("closetag", async tag => {
|
||||
|
||||
@@ -11,14 +11,14 @@ const jimp = require('jimp');
|
||||
const imageType = require('image-type');
|
||||
const sanitizeFilename = require('sanitize-filename');
|
||||
|
||||
async function saveImage(file, noteId) {
|
||||
const resizedImage = await resize(file.buffer);
|
||||
async function saveImage(buffer, originalName, noteId) {
|
||||
const resizedImage = await resize(buffer);
|
||||
const optimizedImage = await optimize(resizedImage);
|
||||
|
||||
const imageFormat = imageType(optimizedImage);
|
||||
|
||||
const fileNameWithouExtension = file.originalname.replace(/\.[^/.]+$/, "");
|
||||
const fileName = sanitizeFilename(fileNameWithouExtension + "." + imageFormat.ext);
|
||||
const fileNameWithoutExtension = originalName.replace(/\.[^/.]+$/, "");
|
||||
const fileName = sanitizeFilename(fileNameWithoutExtension + "." + imageFormat.ext);
|
||||
|
||||
const image = await new Image({
|
||||
format: imageFormat.ext,
|
||||
@@ -32,7 +32,11 @@ async function saveImage(file, noteId) {
|
||||
imageId: image.imageId
|
||||
}).save();
|
||||
|
||||
return {fileName, imageId: image.imageId};
|
||||
return {
|
||||
fileName,
|
||||
imageId: image.imageId,
|
||||
url: `/api/images/${image.imageId}/${fileName}`
|
||||
};
|
||||
}
|
||||
|
||||
const MAX_SIZE = 1000;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
const crypto = require('crypto');
|
||||
const randtoken = require('rand-token').generator({source: 'crypto'});
|
||||
const unescape = require('unescape');
|
||||
const escape = require('escape-html');
|
||||
|
||||
function newEntityId() {
|
||||
return randomString(12);
|
||||
@@ -16,6 +17,10 @@ function randomSecureToken(bytes = 32) {
|
||||
return crypto.randomBytes(bytes).toString('base64');
|
||||
}
|
||||
|
||||
function md5(content) {
|
||||
return crypto.createHash('md5').update(content).digest('hex');
|
||||
}
|
||||
|
||||
function toBase64(plainText) {
|
||||
return Buffer.from(plainText).toString('base64');
|
||||
}
|
||||
@@ -59,6 +64,10 @@ async function stopWatch(what, func) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
function escapeHtml(str) {
|
||||
return escape(str);
|
||||
}
|
||||
|
||||
function unescapeHtml(str) {
|
||||
return unescape(str);
|
||||
}
|
||||
@@ -108,6 +117,7 @@ function union(a, b) {
|
||||
module.exports = {
|
||||
randomSecureToken,
|
||||
randomString,
|
||||
md5,
|
||||
newEntityId,
|
||||
toBase64,
|
||||
fromBase64,
|
||||
@@ -117,6 +127,7 @@ module.exports = {
|
||||
isEmptyOrWhitespace,
|
||||
sanitizeSql,
|
||||
stopWatch,
|
||||
escapeHtml,
|
||||
unescapeHtml,
|
||||
toObject,
|
||||
stripTags,
|
||||
|
||||
Reference in New Issue
Block a user