mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	export to tar now preserves image and note links
This commit is contained in:
		| @@ -155,20 +155,15 @@ async function exportToTar(exportContext, branch, format, res) { | |||||||
|         return meta; |         return meta; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     function findImageLinks(content, noteMeta) { |     function getTargetUrl(targetNoteId, sourceMeta) { | ||||||
|         try { |         const targetMeta = noteIdToMeta[targetNoteId]; | ||||||
|             return content.replace(/src="[^"]*api\/images\/([a-zA-Z0-9]+)\/[^"]*"/g, (_, targetNoteId) => { |  | ||||||
|                 const targetNoteMeta = noteIdToMeta[targetNoteId]; |  | ||||||
|  |  | ||||||
|                 if (!targetNoteMeta) { |         if (!targetMeta) { | ||||||
|             return null; |             return null; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|                 const targetPath = targetNoteMeta.notePath.slice(); |         const targetPath = targetMeta.notePath.slice(); | ||||||
|                 const sourcePath = noteMeta.notePath.slice(); |         const sourcePath = sourceMeta.notePath.slice(); | ||||||
|  |  | ||||||
|                 console.log("targetPath", targetPath); |  | ||||||
|                 console.log("sourcePath", sourcePath); |  | ||||||
|  |  | ||||||
|         // > 1 for edge case that targetPath and sourcePath are exact same (link to itself) |         // > 1 for edge case that targetPath and sourcePath are exact same (link to itself) | ||||||
|         while (targetPath.length > 1 && sourcePath.length > 1 && targetPath[0] === sourcePath[0]) { |         while (targetPath.length > 1 && sourcePath.length > 1 && targetPath[0] === sourcePath[0]) { | ||||||
| @@ -176,9 +171,6 @@ async function exportToTar(exportContext, branch, format, res) { | |||||||
|             sourcePath.shift(); |             sourcePath.shift(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|                 console.log("targetPath", targetPath); |  | ||||||
|                 console.log("sourcePath", sourcePath); |  | ||||||
|  |  | ||||||
|         let url = "../".repeat(sourcePath.length - 1); |         let url = "../".repeat(sourcePath.length - 1); | ||||||
|  |  | ||||||
|         for (let i = 0; i < targetPath.length - 1; i++) { |         for (let i = 0; i < targetPath.length - 1; i++) { | ||||||
| @@ -191,15 +183,23 @@ async function exportToTar(exportContext, branch, format, res) { | |||||||
|  |  | ||||||
|         url += meta.dataFileName; |         url += meta.dataFileName; | ||||||
|  |  | ||||||
|                 console.log("URL", url); |  | ||||||
|  |  | ||||||
|         return url; |         return url; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     function findLinks(content, noteMeta) { | ||||||
|  |         content = content.replace(/src="[^"]*api\/images\/([a-zA-Z0-9]+)\/[^"]*"/g, (_, targetNoteId) => { | ||||||
|  |             const url = getTargetUrl(targetNoteId, noteMeta); | ||||||
|  |  | ||||||
|  |             return `src="${url}"`; | ||||||
|         }); |         }); | ||||||
|         } |  | ||||||
|         catch (e) { |         content = content.replace(/href="[^"]*#root[a-zA-Z0-9\/]*\/([a-zA-Z0-9]+)\/?"/g, (_, targetNoteId) => { | ||||||
|             log.error("Could not parse links from", content); |             const url = getTargetUrl(targetNoteId, noteMeta); | ||||||
|             throw e; |  | ||||||
|         } |             return `href="${url}"`; | ||||||
|  |         }); | ||||||
|  |  | ||||||
|  |         return content; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     async function prepareContent(note, noteMeta) { |     async function prepareContent(note, noteMeta) { | ||||||
| @@ -208,18 +208,30 @@ async function exportToTar(exportContext, branch, format, res) { | |||||||
|         if (['html', 'markdown'].includes(noteMeta.format)) { |         if (['html', 'markdown'].includes(noteMeta.format)) { | ||||||
|             content = content.toString(); |             content = content.toString(); | ||||||
|  |  | ||||||
|             findImageLinks(content, noteMeta); |             content = findLinks(content, noteMeta); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (noteMeta.format === 'html') { |         if (noteMeta.format === 'html') { | ||||||
|             if (!content.toLowerCase().includes("<html")) { |             if (!content.substr(0, 100).toLowerCase().includes("<html")) { | ||||||
|                 note.content = '<html><head><meta charset="utf-8"></head><body>' + content + '</body></html>'; |                 content = `<html> | ||||||
|  | <head><meta charset="utf-8"></head> | ||||||
|  | <body> | ||||||
|  |   <h1>${utils.escapeHtml(note.title)}</h1> | ||||||
|  | ${content} | ||||||
|  | </body> | ||||||
|  | </html>`; | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             return html.prettyPrint(content, {indent_size: 2}); |             return html.prettyPrint(content, {indent_size: 2}); | ||||||
|         } |         } | ||||||
|         else if (noteMeta.format === 'markdown') { |         else if (noteMeta.format === 'markdown') { | ||||||
|             return turndownService.turndown(content); |             let markdownContent = turndownService.turndown(content); | ||||||
|  |  | ||||||
|  |             if (markdownContent.trim().length > 0 && !markdownContent.startsWith("# ")) { | ||||||
|  |                 markdownContent = '# ' + note.title + "\r\n" + markdownContent; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             return markdownContent; | ||||||
|         } |         } | ||||||
|         else { |         else { | ||||||
|             return content; |             return content; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user