mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 04:51:31 +08:00 
			
		
		
		
	inline file attachments when exporting single HTML file
This commit is contained in:
		
							parent
							
								
									235b779dec
								
							
						
					
					
						commit
						f4b5d43899
					
				| @ -360,8 +360,6 @@ class Froca { | |||||||
|         opts.preview = !!opts.preview; |         opts.preview = !!opts.preview; | ||||||
|         const key = `${entityType}-${entityId}-${opts.preview}`; |         const key = `${entityType}-${entityId}-${opts.preview}`; | ||||||
| 
 | 
 | ||||||
|         console.log(key); |  | ||||||
| 
 |  | ||||||
|         if (!this.blobPromises[key]) { |         if (!this.blobPromises[key]) { | ||||||
|             this.blobPromises[key] = server.get(`${entityType}/${entityId}/blob?preview=${opts.preview}`) |             this.blobPromises[key] = server.get(`${entityType}/${entityId}/blob?preview=${opts.preview}`) | ||||||
|                 .then(row => new FBlob(row)) |                 .then(row => new FBlob(row)) | ||||||
|  | |||||||
| @ -23,13 +23,16 @@ function exportSingleNote(taskContext, branch, format, res) { | |||||||
| 
 | 
 | ||||||
|     if (note.type === 'text') { |     if (note.type === 'text') { | ||||||
|         if (format === 'html') { |         if (format === 'html') { | ||||||
|             content = inlineAttachmentImages(content); |             content = inlineAttachments(content); | ||||||
| 
 | 
 | ||||||
|             if (!content.toLowerCase().includes("<html")) { |             if (!content.toLowerCase().includes("<html")) { | ||||||
|                 content = `<html><head><meta charset="utf-8"></head><body>${content}</body></html>`; |                 content = `<html><head><meta charset="utf-8"></head><body>${content}</body></html>`; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             payload = html.prettyPrint(content, {indent_size: 2}); |             payload = content.length < 100_000 | ||||||
|  |                 ? html.prettyPrint(content, {indent_size: 2}) | ||||||
|  |                 : content; | ||||||
|  | 
 | ||||||
|             extension = 'html'; |             extension = 'html'; | ||||||
|             mime = 'text/html'; |             mime = 'text/html'; | ||||||
|         } |         } | ||||||
| @ -61,31 +64,41 @@ function exportSingleNote(taskContext, branch, format, res) { | |||||||
|     taskContext.taskSucceeded(); |     taskContext.taskSucceeded(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function inlineAttachmentImages(content) { | function inlineAttachments(content) { | ||||||
|     const re = /src="[^"]*api\/attachments\/([a-zA-Z0-9_]+)\/image\/?[^"]+"/g; |     content = content.replace(/src="[^"]*api\/attachments\/([a-zA-Z0-9_]+)\/image\/?[^"]+"/g, (match, attachmentId) => { | ||||||
|     let match; |         const attachment = becca.getAttachment(attachmentId); | ||||||
| 
 |         if (!attachment || !attachment.mime.startsWith('image/')) { | ||||||
|     while (match = re.exec(content)) { |             return match; | ||||||
|         const attachment = becca.getAttachment(match[1]); |  | ||||||
|         if (!attachment) { |  | ||||||
|             continue; |  | ||||||
|         } |  | ||||||
| 
 |  | ||||||
|         if (!attachment.mime.startsWith('image/')) { |  | ||||||
|             continue; |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         const attachmentContent = attachment.getContent(); |         const attachmentContent = attachment.getContent(); | ||||||
|         if (!Buffer.isBuffer(attachmentContent)) { |         if (!Buffer.isBuffer(attachmentContent)) { | ||||||
|             continue; |             return match; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         const base64Content = attachmentContent.toString('base64'); |         const base64Content = attachmentContent.toString('base64'); | ||||||
|         const srcValue = `data:${attachment.mime};base64,${base64Content}`; |         const srcValue = `data:${attachment.mime};base64,${base64Content}`; | ||||||
| 
 | 
 | ||||||
|         content = content.replaceAll(match[0], `src="${srcValue}"`); |         return `src="${srcValue}"`; | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|  |     content = content.replace(/href="[^"]*#root[^"]*attachmentId=([a-zA-Z0-9_]+)\/?"/g, (match, attachmentId) => { | ||||||
|  |         const attachment = becca.getAttachment(attachmentId); | ||||||
|  |         if (!attachment) { | ||||||
|  |             return match; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  |         const attachmentContent = attachment.getContent(); | ||||||
|  |         if (!Buffer.isBuffer(attachmentContent)) { | ||||||
|  |             return match; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         const base64Content = attachmentContent.toString('base64'); | ||||||
|  |         const hrefValue = `data:${attachment.mime};base64,${base64Content}`; | ||||||
|  | 
 | ||||||
|  |         return `href="${hrefValue}" download="${utils.escapeHtml(attachment.title)}"`; | ||||||
|  |     }); | ||||||
|  | 
 | ||||||
|     return content; |     return content; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -280,6 +280,26 @@ async function exportToZip(taskContext, branch, format, res, setHeaders = true) | |||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|         content = content.replace(/src="[^"]*api\/attachments\/([a-zA-Z0-9_]+)\/image\/[^"]*"/g, (match, targetAttachmentId) => { |         content = content.replace(/src="[^"]*api\/attachments\/([a-zA-Z0-9_]+)\/image\/[^"]*"/g, (match, targetAttachmentId) => { | ||||||
|  |             const url = findAttachment(targetAttachmentId); | ||||||
|  | 
 | ||||||
|  |             return url ? `src="${url}"` : match; | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         content = content.replace(/href="[^"]*#root[^"]*attachmentId=([a-zA-Z0-9_]+)\/?"/g, (match, targetAttachmentId) => { | ||||||
|  |             const url = findAttachment(targetAttachmentId); | ||||||
|  | 
 | ||||||
|  |             return url ? `href="${url}"` : match; | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         content = content.replace(/href="[^"]*#root[a-zA-Z0-9_\/]*\/([a-zA-Z0-9_]+)[^"]*"/g, (match, targetNoteId) => { | ||||||
|  |             const url = getNoteTargetUrl(targetNoteId, noteMeta); | ||||||
|  | 
 | ||||||
|  |             return url ? `href="${url}"` : match; | ||||||
|  |         }); | ||||||
|  | 
 | ||||||
|  |         return content; | ||||||
|  | 
 | ||||||
|  |         function findAttachment(targetAttachmentId) { | ||||||
|             let url; |             let url; | ||||||
| 
 | 
 | ||||||
|             const attachmentMeta = noteMeta.attachments.find(attMeta => attMeta.attachmentId === targetAttachmentId); |             const attachmentMeta = noteMeta.attachments.find(attMeta => attMeta.attachmentId === targetAttachmentId); | ||||||
| @ -289,17 +309,8 @@ async function exportToZip(taskContext, branch, format, res, setHeaders = true) | |||||||
|             } else { |             } else { | ||||||
|                 log.info(`Could not find attachment meta object for attachmentId '${targetAttachmentId}'`); |                 log.info(`Could not find attachment meta object for attachmentId '${targetAttachmentId}'`); | ||||||
|             } |             } | ||||||
| 
 |             return url; | ||||||
|             return url ? `src="${url}"` : match; |         } | ||||||
|         }); |  | ||||||
| 
 |  | ||||||
|         content = content.replace(/href="[^"]*#root[a-zA-Z0-9_\/]*\/([a-zA-Z0-9_]+)\/?"/g, (match, targetNoteId) => { |  | ||||||
|             const url = getNoteTargetUrl(targetNoteId, noteMeta); |  | ||||||
| 
 |  | ||||||
|             return url ? `href="${url}"` : match; |  | ||||||
|         }); |  | ||||||
| 
 |  | ||||||
|         return content; |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
| @ -339,7 +350,7 @@ async function exportToZip(taskContext, branch, format, res, setHeaders = true) | |||||||
| </html>`; | </html>`; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             return content.length < 100000 |             return content.length < 100_000 | ||||||
|                 ? html.prettyPrint(content, {indent_size: 2}) |                 ? html.prettyPrint(content, {indent_size: 2}) | ||||||
|                 : content; |                 : content; | ||||||
|         } else if (noteMeta.format === 'markdown') { |         } else if (noteMeta.format === 'markdown') { | ||||||
| @ -451,7 +462,9 @@ ${markdownContent}`; | |||||||
|     <ul>${saveNavigationInner(rootMeta)}</ul> |     <ul>${saveNavigationInner(rootMeta)}</ul> | ||||||
| </body> | </body> | ||||||
| </html>`; | </html>`; | ||||||
|         const prettyHtml = html.prettyPrint(fullHtml, {indent_size: 2}); |         const prettyHtml = fullHtml.length < 100_000 | ||||||
|  |             ? html.prettyPrint(fullHtml, {indent_size: 2}) | ||||||
|  |             : fullHtml; | ||||||
| 
 | 
 | ||||||
|         archive.append(prettyHtml, { name: navigationMeta.dataFileName }); |         archive.append(prettyHtml, { name: navigationMeta.dataFileName }); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -103,8 +103,8 @@ function fillInAdditionalProperties(entityChange) { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // fill in some extra data needed by the frontend
 |     // fill in some extra data needed by the frontend
 | ||||||
|     // first try to use becca which works for non-deleted entities
 |     // first try to use becca, which works for non-deleted entities
 | ||||||
|     // only when that fails try to load from database
 |     // only when that fails, try to load from the database
 | ||||||
|     if (entityChange.entityName === 'attributes') { |     if (entityChange.entityName === 'attributes') { | ||||||
|         entityChange.entity = becca.getAttribute(entityChange.entityId); |         entityChange.entity = becca.getAttribute(entityChange.entityId); | ||||||
| 
 | 
 | ||||||
| @ -150,11 +150,7 @@ function fillInAdditionalProperties(entityChange) { | |||||||
|     } else if (entityChange.entityName === 'blobs') { |     } else if (entityChange.entityName === 'blobs') { | ||||||
|         entityChange.noteIds = sql.getColumn("SELECT noteId FROM notes WHERE blobId = ? AND isDeleted = 0", [entityChange.entityId]); |         entityChange.noteIds = sql.getColumn("SELECT noteId FROM notes WHERE blobId = ? AND isDeleted = 0", [entityChange.entityId]); | ||||||
|     } else if (entityChange.entityName === 'attachments') { |     } else if (entityChange.entityName === 'attachments') { | ||||||
|         entityChange.entity = sql.getRow(` |         entityChange.entity = becca.getAttachment(entityChange.entityId, {includeContentLength: true}); | ||||||
|             SELECT attachments.*, LENGTH(blobs.content)  |  | ||||||
|             FROM attachments  |  | ||||||
|             JOIN blobs ON blobs.blobId = attachments.blobId |  | ||||||
|             WHERE attachmentId = ?`, [entityChange.entityId]);
 |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     if (entityChange.entity instanceof AbstractBeccaEntity) { |     if (entityChange.entity instanceof AbstractBeccaEntity) { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zadam
						zadam