Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
Merge branch 'master' into dev
zadamb9b8b353424f
diff --cc src/services/import/zip.jsindex d1997f31fa,45a78a1907..262ef4691c--- a/src/services/import/zip.js+++ b/src/services/import/zip.js@@@ -234,94 -242,134 +241,102 @@@ async function importZip(taskContext, freturn targetNoteId;}- function saveNote(filePath, content) {- const {parentNoteMeta, noteMeta} = getMeta(filePath);-- if (noteMeta && noteMeta.noImport) {- return;+ function processTextNoteContent(content, noteTitle, filePath, noteMeta) {+ function isUrlAbsolute(url) {+ return /^(?:[a-z]+:)?\/\//i.test(url);}- const noteId = getNoteId(noteMeta, filePath);- const parentNoteId = getParentNoteId(filePath, parentNoteMeta);-- if (!parentNoteId) {- throw new Error(`Cannot find parentNoteId for ${filePath}`);- }-- if (noteMeta && noteMeta.isClone) {- if (!becca.getBranchFromChildAndParent(noteId, parentNoteId)) {- new Branch({- noteId,- parentNoteId,- isExpanded: noteMeta.isExpanded,- prefix: noteMeta.prefix,- notePosition: noteMeta.notePosition- }).save();+ content = content.replace(/<h1>([^<]*)<\/h1>/gi, (match, text) => {+ if (noteTitle.trim() === text.trim()) {+ return ""; // remove whole H1 tag+ } else {+ return `<h2>${text}</h2>`;}+ });- return;- }-- let {type, mime} = noteMeta ? noteMeta : detectFileTypeAndMime(taskContext, filePath);-- if (type !== 'file' && type !== 'image') {- content = content.toString("UTF-8");- }-- type = resolveNoteType(type);+ content = htmlSanitizer.sanitize(content);- if ((noteMeta && noteMeta.format === 'markdown')- || (!noteMeta && taskContext.data.textImportedAsText && ['text/markdown', 'text/x-markdown'].includes(mime))) {- const parsed = mdReader.parse(content);- content = mdWriter.render(parsed);- }-- const noteTitle = utils.getNoteTitle(filePath, taskContext.data.replaceUnderscoresWithSpaces, noteMeta);+ content = content.replace(/<html.*<body[^>]*>/gis, "");+ content = content.replace(/<\/body>.*<\/html>/gis, "");- if (type === 'text') {- function isUrlAbsolute(url) {- return /^(?:[a-z]+:)?\/\//i.test(url);+ content = content.replace(/src="([^"]*)"/g, (match, url) => {+ try {+ url = decodeURIComponent(url);+ } catch (e) {+ log.error(`Cannot parse image URL '${url}', keeping original (${e}).`);+ return `src="${url}"`;}- content = content.replace(/<h1>([^<]*)<\/h1>/gi, (match, text) => {- if (noteTitle.trim() === text.trim()) {- return ""; // remove whole H1 tag- }- else {- return `<h2>${text}</h2>`;- }- });-- content = htmlSanitizer.sanitize(content);-- content = content.replace(/<html.*<body[^>]*>/gis, "");- content = content.replace(/<\/body>.*<\/html>/gis, "");+ if (isUrlAbsolute(url) || url.startsWith("/")) {+ return match;+ }- content = content.replace(/src="([^"]*)"/g, (match, url) => {- try {- url = decodeURIComponent(url);- } catch (e) {- log.error(`Cannot parse image URL '${url}', keeping original (${e}).`);- return `src="${url}"`;- }+ const targetNoteId = getNoteIdFromRelativeUrl(url, filePath);- if (isUrlAbsolute(url) || url.startsWith("/")) {- return match;- }++ if (!targetNoteId) {++ return match;++ }+- const targetNoteId = getNoteIdFromRelativeUrl(url, filePath);+ return `src="api/images/${targetNoteId}/${path.basename(url)}"`;+ });- if (!targetNoteId) {- return match;- }+ content = content.replace(/href="([^"]*)"/g, (match, url) => {+ try {+ url = decodeURIComponent(url);+ } catch (e) {+ log.error(`Cannot parse link URL '${url}', keeping original (${e}).`);+ return `href="${url}"`;+ }- return `src="api/images/${targetNoteId}/${path.basename(url)}"`;- });+ if (url.startsWith('#') // already a note path (probably)+ || isUrlAbsolute(url)) {+ return match;+ }- content = content.replace(/href="([^"]*)"/g, (match, url) => {- try {- url = decodeURIComponent(url);- } catch (e) {- log.error(`Cannot parse link URL '${url}', keeping original (${e}).`);- return `href="${url}"`;- }+ const targetNoteId = getNoteIdFromRelativeUrl(url, filePath);- if (url.startsWith('#') || isUrlAbsolute(url)) {- return match;- }++ if (!targetNoteId) {++ return match;++ }+- const targetNoteId = getNoteIdFromRelativeUrl(url, filePath);+ return `href="#root/${targetNoteId}"`;+ });- if (!targetNoteId) {- return match;- }+ content = content.replace(/data-note-path="([^"]*)"/g, (match, notePath) => {+ const noteId = notePath.split("/").pop();- return `href="#root/${targetNoteId}"`;- });+ let targetNoteId;- content = content.replace(/data-note-path="([^"]*)"/g, (match, notePath) => {- const noteId = notePath.split("/").pop();+ if (noteId === 'root' || noteId.startsWith("_")) { // named noteIds stay identical across instances+ targetNoteId = noteId;+ } else {+ targetNoteId = noteIdMap[noteId];+ }- let targetNoteId;+ return `data-note-path="root/${targetNoteId}"`;+ });- if (noteId === 'root' || noteId.startsWith("_")) { // named noteIds stay identical across instances- targetNoteId = noteId;- } else {- targetNoteId = noteIdMap[noteId];- }+ if (noteMeta) {+ const includeNoteLinks = (noteMeta.attributes || [])+ .filter(attr => attr.type === 'relation' && attr.name === 'includeNoteLink');- return `data-note-path="root/${targetNoteId}"`;- });+ for (const link of includeNoteLinks) {+ // no need to escape the regexp find string since it's a noteId which doesn't contain any special characters+ content = content.replace(new RegExp(link.value, "g"), getNewNoteId(link.value));+ }+ }+ return content;+ }- if (noteMeta) {- const includeNoteLinks = (noteMeta.attributes || [])- .filter(attr => attr.type === 'relation' && attr.name === 'includeNoteLink');+ function processNoteContent(noteMeta, type, mime, content, noteTitle, filePath) {+ if (noteMeta?.format === 'markdown'+ || (!noteMeta && taskContext.data.textImportedAsText && ['text/markdown', 'text/x-markdown'].includes(mime))) {+ const parsed = mdReader.parse(content);+ content = mdWriter.render(parsed);+ }- for (const link of includeNoteLinks) {- // no need to escape the regexp find string since it's a noteId which doesn't contain any special characters- content = content.replace(new RegExp(link.value, "g"), getNewNoteId(link.value));- }- }+ if (type === 'text') {+ content = processTextNoteContent(content, noteTitle, filePath, noteMeta);}if (type === 'relationMap' && noteMeta) {