| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const html = require('html'); | 
					
						
							| 
									
										
										
										
											2019-12-22 10:57:55 +01:00
										 |  |  | const dateUtils = require('../date_utils'); | 
					
						
							| 
									
										
										
										
											2018-11-30 22:43:03 +01:00
										 |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  | const mimeTypes = require('mime-types'); | 
					
						
							| 
									
										
										
										
											2019-09-30 20:56:32 +02:00
										 |  |  | const mdService = require('./md'); | 
					
						
							| 
									
										
										
										
											2018-11-27 10:31:55 +01:00
										 |  |  | const packageInfo = require('../../../package.json'); | 
					
						
							| 
									
										
										
										
											2019-01-13 10:22:17 +01:00
										 |  |  | const utils = require('../utils'); | 
					
						
							| 
									
										
										
										
											2019-09-01 22:09:55 +02:00
										 |  |  | const protectedSessionService = require('../protected_session'); | 
					
						
							| 
									
										
										
										
											2019-01-13 12:21:17 +01:00
										 |  |  | const sanitize = require("sanitize-filename"); | 
					
						
							| 
									
										
										
										
											2019-12-25 11:34:45 +01:00
										 |  |  | const fs = require("fs"); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:10:03 +02:00
										 |  |  | const becca = require("../../becca/becca"); | 
					
						
							| 
									
										
										
										
											2019-12-25 11:34:45 +01:00
										 |  |  | const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR; | 
					
						
							| 
									
										
										
										
											2021-05-14 22:57:44 +02:00
										 |  |  | const archiver = require('archiver'); | 
					
						
							| 
									
										
										
										
											2022-02-09 22:39:37 +01:00
										 |  |  | const log = require("../log"); | 
					
						
							| 
									
										
										
										
											2022-12-29 21:15:34 +01:00
										 |  |  | const TaskContext = require("../task_context"); | 
					
						
							|  |  |  | const ValidationError = require("../../errors/validation_error"); | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2019-10-18 22:27:38 +02:00
										 |  |  |  * @param {TaskContext} taskContext | 
					
						
							| 
									
										
										
										
											2023-01-03 14:31:46 +01:00
										 |  |  |  * @param {BBranch} branch | 
					
						
							| 
									
										
										
										
											2019-02-10 22:30:55 +01:00
										 |  |  |  * @param {string} format - 'html' or 'markdown' | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2022-12-29 23:12:38 +01:00
										 |  |  | async function exportToZip(taskContext, branch, format, res, setHeaders = true) { | 
					
						
							| 
									
										
										
										
											2022-12-29 21:15:34 +01:00
										 |  |  |     if (!['html', 'markdown'].includes(format)) { | 
					
						
							|  |  |  |         throw new ValidationError(`Only 'html' and 'markdown' allowed as export format, '${format}' given`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 22:57:44 +02:00
										 |  |  |     const archive = archiver('zip', { | 
					
						
							|  |  |  |         zlib: { level: 9 } // Sets the compression level.
 | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:38:09 +01:00
										 |  |  |     const noteIdToMeta = {}; | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     function getUniqueFilename(existingFileNames, fileName) { | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  |         const lcFileName = fileName.toLowerCase(); | 
					
						
							| 
									
										
										
										
											2018-11-25 10:26:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         if (lcFileName in existingFileNames) { | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  |             let index; | 
					
						
							|  |  |  |             let newName; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             do { | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |                 index = existingFileNames[lcFileName]++; | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |                 newName = `${index}_${lcFileName}`; | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             while (newName in existingFileNames); | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |             return `${index}_${fileName}`; | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             existingFileNames[lcFileName] = 1; | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return fileName; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-25 10:26:45 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     function getDataFileName(note, baseFileName, existingFileNames) { | 
					
						
							| 
									
										
										
										
											2021-12-17 22:03:00 +01:00
										 |  |  |         let fileName = baseFileName; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-15 22:35:54 +01:00
										 |  |  |         let existingExtension = path.extname(fileName).toLowerCase(); | 
					
						
							|  |  |  |         let newExtension; | 
					
						
							| 
									
										
										
										
											2022-02-09 22:39:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-17 22:03:00 +01:00
										 |  |  |         if (fileName.length > 30) { | 
					
						
							|  |  |  |             fileName = fileName.substr(0, 30); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-11 20:54:57 +02:00
										 |  |  |         // following two are handled specifically since we always want to have these extensions no matter the automatic detection
 | 
					
						
							|  |  |  |         // and/or existing detected extensions in the note name
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         if (note.type === 'text' && format === 'markdown') { | 
					
						
							| 
									
										
										
										
											2019-07-11 20:54:57 +02:00
										 |  |  |             newExtension = 'md'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (note.type === 'text' && format === 'html') { | 
					
						
							|  |  |  |             newExtension = 'html'; | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-30 22:43:03 +01:00
										 |  |  |         else if (note.mime === 'application/x-javascript' || note.mime === 'text/javascript') { | 
					
						
							| 
									
										
										
										
											2019-07-11 20:54:57 +02:00
										 |  |  |             newExtension = 'js'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (existingExtension.length > 0) { // if the page already has an extension, then we'll just keep it
 | 
					
						
							|  |  |  |             newExtension = null; | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2021-12-17 22:03:00 +01:00
										 |  |  |             if (note.mime?.toLowerCase()?.trim() === "image/jpg") { | 
					
						
							|  |  |  |                 newExtension = 'jpg'; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 newExtension = mimeTypes.extension(note.mime) || "dat"; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-05-15 13:22:54 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-29 22:15:57 +02:00
										 |  |  |         // if the note is already named with extension (e.g. "jquery"), then it's silly to append exact same extension again
 | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |         if (newExtension && existingExtension !== `.${newExtension.toLowerCase()}`) { | 
					
						
							|  |  |  |             fileName += `.${newExtension}`; | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return getUniqueFilename(existingFileNames, fileName); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     function getNoteMeta(branch, parentMeta, existingFileNames) { | 
					
						
							|  |  |  |         const note = branch.getNote(); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         if (note.hasOwnedLabel('excludeFromExport')) { | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-08 21:50:16 +01:00
										 |  |  |         const title = note.getTitleOrProtected(); | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |         const completeTitle = branch.prefix ? (`${branch.prefix} - ${title}`) : title; | 
					
						
							| 
									
										
										
										
											2019-12-01 11:49:14 +01:00
										 |  |  |         let baseFileName = sanitize(completeTitle); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (baseFileName.length > 200) { // actual limit is 256 bytes(!) but let's be conservative
 | 
					
						
							|  |  |  |             baseFileName = baseFileName.substr(0, 200); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 16:46:36 +02:00
										 |  |  |         const notePath = parentMeta.notePath.concat([note.noteId]); | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:38:09 +01:00
										 |  |  |         if (note.noteId in noteIdToMeta) { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |             const fileName = getUniqueFilename(existingFileNames, `${baseFileName}.clone.${format === 'html' ? 'html' : 'md'}`); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return { | 
					
						
							| 
									
										
										
										
											2018-11-26 14:47:46 +01:00
										 |  |  |                 isClone: true, | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |                 noteId: note.noteId, | 
					
						
							| 
									
										
										
										
											2019-09-01 16:46:36 +02:00
										 |  |  |                 notePath: notePath, | 
					
						
							| 
									
										
										
										
											2022-01-08 21:50:16 +01:00
										 |  |  |                 title: note.getTitleOrProtected(), | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |                 prefix: branch.prefix, | 
					
						
							| 
									
										
										
										
											2019-09-01 16:46:36 +02:00
										 |  |  |                 dataFileName: fileName, | 
					
						
							|  |  |  |                 type: 'text', // export will have text description,
 | 
					
						
							|  |  |  |                 format: format | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             }; | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         const meta = { | 
					
						
							| 
									
										
										
										
											2018-11-26 14:47:46 +01:00
										 |  |  |             isClone: false, | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |             noteId: note.noteId, | 
					
						
							| 
									
										
										
										
											2019-09-01 16:46:36 +02:00
										 |  |  |             notePath: notePath, | 
					
						
							| 
									
										
										
										
											2022-01-08 21:50:16 +01:00
										 |  |  |             title: note.getTitleOrProtected(), | 
					
						
							| 
									
										
										
										
											2018-11-26 23:39:43 +01:00
										 |  |  |             notePosition: branch.notePosition, | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |             prefix: branch.prefix, | 
					
						
							|  |  |  |             isExpanded: branch.isExpanded, | 
					
						
							|  |  |  |             type: note.type, | 
					
						
							|  |  |  |             mime: note.mime, | 
					
						
							| 
									
										
										
										
											2019-03-12 20:58:31 +01:00
										 |  |  |             // we don't export utcDateCreated and utcDateModified of any entity since that would be a bit misleading
 | 
					
						
							| 
									
										
										
										
											2020-12-21 23:00:39 +01:00
										 |  |  |             attributes: note.getOwnedAttributes().map(attribute => ({ | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |                     type: attribute.type, | 
					
						
							|  |  |  |                     name: attribute.name, | 
					
						
							|  |  |  |                     value: attribute.value, | 
					
						
							|  |  |  |                     isInheritable: attribute.isInheritable, | 
					
						
							|  |  |  |                     position: attribute.position | 
					
						
							| 
									
										
										
										
											2020-02-28 22:07:08 +01:00
										 |  |  |             })) | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-18 22:27:38 +02:00
										 |  |  |         taskContext.increaseProgressCount(); | 
					
						
							| 
									
										
										
										
											2019-02-10 22:30:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  |         if (note.type === 'text') { | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             meta.format = format; | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:38:09 +01:00
										 |  |  |         noteIdToMeta[note.noteId] = meta; | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-15 09:04:44 +01:00
										 |  |  |         const childBranches = note.getChildBranches() | 
					
						
							| 
									
										
										
										
											2022-12-21 16:11:00 +01:00
										 |  |  |             .filter(branch => branch.noteId !== '_hidden'); | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 22:09:55 +02:00
										 |  |  |         const available = !note.isProtected || protectedSessionService.isProtectedSessionAvailable(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         // if it's a leaf then we'll export it even if it's empty
 | 
					
						
							| 
									
										
										
										
											2020-12-22 22:30:04 +01:00
										 |  |  |         if (available && (note.getContent().length > 0 || childBranches.length === 0)) { | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             meta.dataFileName = getDataFileName(note, baseFileName, existingFileNames); | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         if (childBranches.length > 0) { | 
					
						
							|  |  |  |             meta.dirFileName = getUniqueFilename(existingFileNames, baseFileName); | 
					
						
							|  |  |  |             meta.children = []; | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 22:22:16 +01:00
										 |  |  |             // namespace is shared by children in the same note
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             const childExistingNames = {}; | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             for (const childBranch of childBranches) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |                 const note = getNoteMeta(childBranch, meta, childExistingNames); | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |                 // can be undefined if export is disabled for this note
 | 
					
						
							|  |  |  |                 if (note) { | 
					
						
							|  |  |  |                     meta.children.push(note); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         return meta; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |     function getTargetUrl(targetNoteId, sourceMeta) { | 
					
						
							|  |  |  |         const targetMeta = noteIdToMeta[targetNoteId]; | 
					
						
							| 
									
										
										
										
											2019-02-06 21:29:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |         if (!targetMeta) { | 
					
						
							|  |  |  |             return null; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |         const targetPath = targetMeta.notePath.slice(); | 
					
						
							|  |  |  |         const sourcePath = sourceMeta.notePath.slice(); | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |         // > 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]) { | 
					
						
							|  |  |  |             targetPath.shift(); | 
					
						
							|  |  |  |             sourcePath.shift(); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |         let url = "../".repeat(sourcePath.length - 1); | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |         for (let i = 0; i < targetPath.length - 1; i++) { | 
					
						
							|  |  |  |             const meta = noteIdToMeta[targetPath[i]]; | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |             url += `${encodeURIComponent(meta.dirFileName)}/`; | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |         const meta = noteIdToMeta[targetPath[targetPath.length - 1]]; | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 16:46:36 +02:00
										 |  |  |         // link can target note which is only "folder-note" and as such will not have a file in an export
 | 
					
						
							| 
									
										
										
										
											2019-12-25 11:56:55 +01:00
										 |  |  |         url += encodeURIComponent(meta.dataFileName || meta.dirFileName); | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return url; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |     function findLinks(content, noteMeta) { | 
					
						
							| 
									
										
										
										
											2022-12-29 10:25:49 +01:00
										 |  |  |         content = content.replace(/src="[^"]*api\/images\/([a-zA-Z0-9_]+)\/[^"]*"/g, (match, targetNoteId) => { | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |             const url = getTargetUrl(targetNoteId, noteMeta); | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-25 11:56:55 +01:00
										 |  |  |             return url ? `src="${url}"` : match; | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 10:25:49 +01:00
										 |  |  |         content = content.replace(/href="[^"]*#root[a-zA-Z0-9_\/]*\/([a-zA-Z0-9_]+)\/?"/g, (match, targetNoteId) => { | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |             const url = getTargetUrl(targetNoteId, noteMeta); | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-25 11:56:55 +01:00
										 |  |  |             return url ? `href="${url}"` : match; | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return content; | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 16:46:36 +02:00
										 |  |  |     function prepareContent(title, content, noteMeta) { | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  |         if (['html', 'markdown'].includes(noteMeta.format)) { | 
					
						
							|  |  |  |             content = content.toString(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |             content = findLinks(content, noteMeta); | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (noteMeta.format === 'html') { | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |             if (!content.substr(0, 100).toLowerCase().includes("<html")) { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |                 const cssUrl = `${"../".repeat(noteMeta.notePath.length - 1)}style.css`; | 
					
						
							| 
									
										
										
										
											2019-12-25 11:34:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-06 21:30:41 +02:00
										 |  |  |                 // <base> element will make sure external links are openable - https://github.com/zadam/trilium/issues/1289#issuecomment-704066809
 | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |                 content = `<html>
 | 
					
						
							| 
									
										
										
										
											2019-12-25 11:34:45 +01:00
										 |  |  | <head> | 
					
						
							|  |  |  |     <meta charset="utf-8"> | 
					
						
							| 
									
										
										
										
											2020-10-05 22:23:03 +02:00
										 |  |  |     <meta name="viewport" content="width=device-width, initial-scale=1"> | 
					
						
							| 
									
										
										
										
											2019-12-25 11:34:45 +01:00
										 |  |  |     <link rel="stylesheet" href="${cssUrl}"> | 
					
						
							| 
									
										
										
										
											2020-10-06 21:30:41 +02:00
										 |  |  |     <base target="_parent"> | 
					
						
							| 
									
										
										
										
											2019-12-25 11:34:45 +01:00
										 |  |  | </head> | 
					
						
							| 
									
										
										
										
											2023-01-12 16:37:30 +01:00
										 |  |  | <body> | 
					
						
							| 
									
										
										
										
											2023-01-13 23:54:47 +01:00
										 |  |  |   <div class="content"> | 
					
						
							|  |  |  |       <h1>${utils.escapeHtml(title)}</h1> | 
					
						
							|  |  |  |        | 
					
						
							|  |  |  |       <div class="ck-content">${content}</div> | 
					
						
							|  |  |  |   </div> | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  | </body> | 
					
						
							|  |  |  | </html>`; | 
					
						
							| 
									
										
										
										
											2019-01-25 21:34:14 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 22:39:37 +01:00
										 |  |  |             return content.length < 100000 | 
					
						
							|  |  |  |                 ? html.prettyPrint(content, {indent_size: 2}) | 
					
						
							|  |  |  |                 : content; | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  |         else if (noteMeta.format === 'markdown') { | 
					
						
							| 
									
										
										
										
											2019-09-30 20:56:32 +02:00
										 |  |  |             let markdownContent = mdService.toMarkdown(content); | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (markdownContent.trim().length > 0 && !markdownContent.startsWith("# ")) { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |                 markdownContent = `# ${title}\r
 | 
					
						
							|  |  |  | ${markdownContent}`;
 | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return markdownContent; | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2019-02-06 21:29:23 +01:00
										 |  |  |             return content; | 
					
						
							| 
									
										
										
										
											2018-11-25 10:26:45 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     // noteId => file path
 | 
					
						
							|  |  |  |     const notePaths = {}; | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     function saveNote(noteMeta, filePathPrefix) { | 
					
						
							| 
									
										
										
										
											2022-02-09 22:39:37 +01:00
										 |  |  |         log.info(`Exporting note ${noteMeta.noteId}`); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 14:47:46 +01:00
										 |  |  |         if (noteMeta.isClone) { | 
					
						
							| 
									
										
										
										
											2019-09-01 16:46:36 +02:00
										 |  |  |             const targetUrl = getTargetUrl(noteMeta.noteId, noteMeta); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-25 11:56:55 +01:00
										 |  |  |             let content = `<p>This is a clone of a note. Go to its <a href="${targetUrl}">primary location</a>.</p>`; | 
					
						
							| 
									
										
										
										
											2019-09-01 16:46:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             content = prepareContent(noteMeta.title, content, noteMeta); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 22:57:44 +02:00
										 |  |  |             archive.append(content, { name: filePathPrefix + noteMeta.dataFileName }); | 
					
						
							| 
									
										
										
										
											2020-03-19 20:07:27 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-02 11:23:58 +02:00
										 |  |  |         const note = becca.getNote(noteMeta.noteId); | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  |         notePaths[note.noteId] = filePathPrefix + (noteMeta.dataFileName || noteMeta.dirFileName); | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         if (noteMeta.dataFileName) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |             const content = prepareContent(noteMeta.title, note.getContent(), noteMeta); | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 22:57:44 +02:00
										 |  |  |             archive.append(content, { name: filePathPrefix + noteMeta.dataFileName, date: dateUtils.parseDateTime(note.utcDateModified) }); | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-18 22:27:38 +02:00
										 |  |  |         taskContext.increaseProgressCount(); | 
					
						
							| 
									
										
										
										
											2019-02-10 22:30:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         if (noteMeta.children && noteMeta.children.length > 0) { | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  |             const directoryPath = filePathPrefix + noteMeta.dirFileName; | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 22:57:44 +02:00
										 |  |  |             // create directory
 | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |             archive.append('', { name: `${directoryPath}/`, date: dateUtils.parseDateTime(note.utcDateModified) }); | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             for (const childMeta of noteMeta.children) { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |                 saveNote(childMeta, `${directoryPath}/`); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     function saveNavigation(rootMeta, navigationMeta) { | 
					
						
							| 
									
										
										
										
											2019-10-20 19:02:48 +02:00
										 |  |  |         function saveNavigationInner(meta) { | 
					
						
							|  |  |  |             let html = '<li>'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |             const escapedTitle = utils.escapeHtml(`${meta.prefix ? `${meta.prefix} - ` : ''}${meta.title}`); | 
					
						
							| 
									
										
										
										
											2019-10-20 19:02:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (meta.dataFileName) { | 
					
						
							|  |  |  |                 const targetUrl = getTargetUrl(meta.noteId, rootMeta); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-21 21:00:27 +02:00
										 |  |  |                 html += `<a href="${targetUrl}" target="detail">${escapedTitle}</a>`; | 
					
						
							| 
									
										
										
										
											2019-10-20 19:02:48 +02:00
										 |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 html += escapedTitle; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (meta.children && meta.children.length > 0) { | 
					
						
							|  |  |  |                 html += '<ul>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 for (const child of meta.children) { | 
					
						
							|  |  |  |                     html += saveNavigationInner(child); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 html += '</ul>' | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |             return `${html}</li>`; | 
					
						
							| 
									
										
										
										
											2019-10-20 19:02:48 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-25 11:34:45 +01:00
										 |  |  |         const fullHtml = `<html>
 | 
					
						
							|  |  |  | <head> | 
					
						
							|  |  |  |     <meta charset="utf-8"> | 
					
						
							|  |  |  |     <link rel="stylesheet" href="style.css"> | 
					
						
							|  |  |  | </head> | 
					
						
							|  |  |  | <body> | 
					
						
							|  |  |  |     <ul>${saveNavigationInner(rootMeta)}</ul> | 
					
						
							|  |  |  | </body> | 
					
						
							|  |  |  | </html>`; | 
					
						
							| 
									
										
										
										
											2019-10-20 19:02:48 +02:00
										 |  |  |         const prettyHtml = html.prettyPrint(fullHtml, {indent_size: 2}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 22:57:44 +02:00
										 |  |  |         archive.append(prettyHtml, { name: navigationMeta.dataFileName }); | 
					
						
							| 
									
										
										
										
											2019-10-20 19:02:48 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     function saveIndex(rootMeta, indexMeta) { | 
					
						
							| 
									
										
										
										
											2019-10-21 21:00:27 +02:00
										 |  |  |         let firstNonEmptyNote; | 
					
						
							|  |  |  |         let curMeta = rootMeta; | 
					
						
							| 
									
										
										
										
											2019-10-20 19:02:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-21 21:00:27 +02:00
										 |  |  |         while (!firstNonEmptyNote) { | 
					
						
							|  |  |  |             if (curMeta.dataFileName) { | 
					
						
							|  |  |  |                 firstNonEmptyNote = getTargetUrl(curMeta.noteId, rootMeta); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (curMeta.children && curMeta.children.length > 0) { | 
					
						
							|  |  |  |                 curMeta = curMeta.children[0]; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const fullHtml = `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 | 
					
						
							|  |  |  | <html> | 
					
						
							|  |  |  | <head> | 
					
						
							|  |  |  |     <meta charset="utf-8"> | 
					
						
							| 
									
										
										
										
											2020-10-05 22:23:03 +02:00
										 |  |  |     <meta name="viewport" content="width=device-width, initial-scale=1"> | 
					
						
							| 
									
										
										
										
											2019-10-21 21:00:27 +02:00
										 |  |  | </head> | 
					
						
							|  |  |  | <frameset cols="25%,75%"> | 
					
						
							|  |  |  |     <frame name="navigation" src="navigation.html"> | 
					
						
							|  |  |  |     <frame name="detail" src="${firstNonEmptyNote}"> | 
					
						
							|  |  |  | </frameset> | 
					
						
							|  |  |  | </html>`; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 22:57:44 +02:00
										 |  |  |         archive.append(fullHtml, { name: indexMeta.dataFileName }); | 
					
						
							| 
									
										
										
										
											2019-10-21 21:00:27 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     function saveCss(rootMeta, cssMeta) { | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |         const cssContent = fs.readFileSync(`${RESOURCE_DIR}/libraries/ckeditor/ckeditor-content.css`); | 
					
						
							| 
									
										
										
										
											2019-12-25 11:34:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 22:57:44 +02:00
										 |  |  |         archive.append(cssContent, { name: cssMeta.dataFileName }); | 
					
						
							| 
									
										
										
										
											2019-12-25 11:34:45 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-21 21:00:27 +02:00
										 |  |  |     const existingFileNames = format === 'html' ? ['navigation', 'index'] : []; | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const rootMeta = getNoteMeta(branch, { notePath: [] }, existingFileNames); | 
					
						
							| 
									
										
										
										
											2019-10-20 19:02:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     const metaFile = { | 
					
						
							| 
									
										
										
										
											2018-11-27 10:31:55 +01:00
										 |  |  |         formatVersion: 1, | 
					
						
							|  |  |  |         appVersion: packageInfo.version, | 
					
						
							| 
									
										
										
										
											2019-10-21 21:00:27 +02:00
										 |  |  |         files: [ rootMeta ] | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-25 11:34:45 +01:00
										 |  |  |     let navigationMeta, indexMeta, cssMeta; | 
					
						
							| 
									
										
										
										
											2019-10-21 21:00:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (format === 'html') { | 
					
						
							|  |  |  |         navigationMeta = { | 
					
						
							|  |  |  |             noImport: true, | 
					
						
							|  |  |  |             dataFileName: "navigation.html" | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         metaFile.files.push(navigationMeta); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         indexMeta = { | 
					
						
							|  |  |  |             noImport: true, | 
					
						
							|  |  |  |             dataFileName: "index.html" | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         metaFile.files.push(indexMeta); | 
					
						
							| 
									
										
										
										
											2019-12-25 11:34:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         cssMeta = { | 
					
						
							|  |  |  |             noImport: true, | 
					
						
							|  |  |  |             dataFileName: "style.css" | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         metaFile.files.push(cssMeta); | 
					
						
							| 
									
										
										
										
											2019-10-21 21:00:27 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:38:09 +01:00
										 |  |  |     for (const noteMeta of Object.values(noteIdToMeta)) { | 
					
						
							| 
									
										
										
										
											2019-08-19 20:12:00 +02:00
										 |  |  |         // filter out relations which are not inside this export
 | 
					
						
							| 
									
										
										
										
											2022-12-23 23:08:30 +01:00
										 |  |  |         noteMeta.attributes = noteMeta.attributes.filter(attr => { | 
					
						
							|  |  |  |             if (attr.type !== 'relation') { | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } else if (attr.value in noteIdToMeta) { | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } else if (attr.value === 'root' || attr.value?.startsWith("_")) { | 
					
						
							|  |  |  |                 // relations to "named" noteIds can be preserved
 | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:38:09 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 19:02:48 +02:00
										 |  |  |     if (!rootMeta) { // corner case of disabled export for exported note
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         res.sendStatus(400); | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     const metaFileJson = JSON.stringify(metaFile, null, '\t'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 22:57:44 +02:00
										 |  |  |     archive.append(metaFileJson, { name: "!!!meta.json" }); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     saveNote(rootMeta, ''); | 
					
						
							| 
									
										
										
										
											2019-10-20 19:02:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-21 21:00:27 +02:00
										 |  |  |     if (format === 'html') { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         saveNavigation(rootMeta, navigationMeta); | 
					
						
							|  |  |  |         saveIndex(rootMeta, indexMeta); | 
					
						
							|  |  |  |         saveCss(rootMeta, cssMeta); | 
					
						
							| 
									
										
										
										
											2019-10-21 21:00:27 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const note = branch.getNote(); | 
					
						
							| 
									
										
										
										
											2022-12-21 15:19:05 +01:00
										 |  |  |     const zipFileName = `${branch.prefix ? `${branch.prefix} - ` : ""}${note.getTitleOrProtected()}.zip`; | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 21:15:34 +01:00
										 |  |  |     if (setHeaders) { | 
					
						
							|  |  |  |         res.setHeader('Content-Disposition', utils.getContentDisposition(zipFileName)); | 
					
						
							|  |  |  |         res.setHeader('Content-Type', 'application/zip'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-14 22:57:44 +02:00
										 |  |  |     archive.pipe(res); | 
					
						
							| 
									
										
										
										
											2022-12-29 23:12:38 +01:00
										 |  |  |     await archive.finalize(); | 
					
						
							| 
									
										
										
										
											2019-02-10 22:30:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-18 22:27:38 +02:00
										 |  |  |     taskContext.taskSucceeded(); | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 23:12:38 +01:00
										 |  |  | async function exportToZipFile(noteId, format, zipFilePath) { | 
					
						
							| 
									
										
										
										
											2022-12-29 21:15:34 +01:00
										 |  |  |     const fileOutputStream = fs.createWriteStream(zipFilePath); | 
					
						
							|  |  |  |     const taskContext = new TaskContext('no-progress-reporting'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const note = becca.getNote(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!note) { | 
					
						
							|  |  |  |         throw new ValidationError(`Note ${noteId} not found.`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-29 23:12:38 +01:00
										 |  |  |     await exportToZip(taskContext, note.getParentBranches()[0], format, fileOutputStream, false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     log.info(`Exported '${noteId}' with format '${format}' to '${zipFilePath}'`); | 
					
						
							| 
									
										
										
										
											2022-12-29 21:15:34 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2022-12-29 21:15:34 +01:00
										 |  |  |     exportToZip, | 
					
						
							|  |  |  |     exportToZipFile | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | }; |