| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const html = require('html'); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | const repository = require('../repository'); | 
					
						
							| 
									
										
										
										
											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"); | 
					
						
							|  |  |  | const RESOURCE_DIR = require('../../services/resource_dir').RESOURCE_DIR; | 
					
						
							| 
									
										
										
										
											2020-03-20 16:55:35 +01:00
										 |  |  | const yazl = require("yazl"); | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2019-10-18 22:27:38 +02:00
										 |  |  |  * @param {TaskContext} taskContext | 
					
						
							| 
									
										
										
										
											2019-02-10 22:30:55 +01:00
										 |  |  |  * @param {Branch} branch | 
					
						
							|  |  |  |  * @param {string} format - 'html' or 'markdown' | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function exportToZip(taskContext, branch, format, res) { | 
					
						
							| 
									
										
										
										
											2020-03-20 16:55:35 +01:00
										 |  |  |     const zipFile = new yazl.ZipFile(); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-10 23:01:30 +02: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-10 23:01:30 +02: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) { | 
					
						
							| 
									
										
										
										
											2019-07-11 20:54:57 +02:00
										 |  |  |         const existingExtension = path.extname(baseFileName).toLowerCase(); | 
					
						
							|  |  |  |         let newExtension; | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2019-07-11 20:54:57 +02:00
										 |  |  |             newExtension = mimeTypes.extension(note.mime) || "dat"; | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let fileName = baseFileName; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-30 22:43:03 +01:00
										 |  |  |         // if the note is already named with extension (e.g. "jquery.js"), then it's silly to append exact same extension again
 | 
					
						
							| 
									
										
										
										
											2019-07-11 20:54:57 +02: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; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 16:46:36 +02:00
										 |  |  |         const completeTitle = branch.prefix ? (branch.prefix + ' - ' + note.title) : note.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) { | 
					
						
							| 
									
										
										
										
											2019-09-01 16:46:36 +02: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, | 
					
						
							|  |  |  |                 title: note.title, | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |             title: note.title, | 
					
						
							| 
									
										
										
										
											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-06-20 12:31:38 +02: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         const childBranches = note.getChildBranches(); | 
					
						
							| 
									
										
										
										
											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-06-20 12:31:38 +02: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-25 11:56:55 +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) { | 
					
						
							| 
									
										
										
										
											2019-08-31 22:39:25 +02: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 22:39:25 +02: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")) { | 
					
						
							| 
									
										
										
										
											2019-12-25 11:34:45 +01:00
										 |  |  |                 const cssUrl = "../".repeat(noteMeta.notePath.length - 1) + 'style.css'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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}"> | 
					
						
							|  |  |  | </head> | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  | <body> | 
					
						
							| 
									
										
										
										
											2019-09-01 16:46:36 +02:00
										 |  |  |   <h1>${utils.escapeHtml(title)}</h1> | 
					
						
							| 
									
										
										
										
											2019-08-31 11:15:32 +02:00
										 |  |  | ${content} | 
					
						
							|  |  |  | </body> | 
					
						
							|  |  |  | </html>`; | 
					
						
							| 
									
										
										
										
											2019-01-25 21:34:14 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-06 21:29:23 +01:00
										 |  |  |             return html.prettyPrint(content, {indent_size: 2}); | 
					
						
							| 
									
										
										
										
											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("# ")) { | 
					
						
							| 
									
										
										
										
											2019-09-01 16:46:36 +02:00
										 |  |  |                 markdownContent = '# ' + title + "\r\n" + 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) { | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 16:55:35 +01:00
										 |  |  |             zipFile.addBuffer(content, 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
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         const note = repository.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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 16:55:35 +01:00
										 |  |  |             zipFile.addBuffer(content, filePathPrefix + noteMeta.dataFileName, {mtime: 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 16:55:35 +01:00
										 |  |  |             zipFile.addEmptyDirectory(directoryPath, {mtime: 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) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02: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>'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const escapedTitle = utils.escapeHtml((meta.prefix ? `${meta.prefix} - ` : '') + meta.title); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             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>' | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return html + '</li>'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 16:55:35 +01:00
										 |  |  |         zipFile.addBuffer(prettyHtml, 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>`; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 16:55:35 +01:00
										 |  |  |         zipFile.addBuffer(fullHtml, indexMeta.dataFileName); | 
					
						
							| 
									
										
										
										
											2019-10-21 21:00:27 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     function saveCss(rootMeta, cssMeta) { | 
					
						
							| 
									
										
										
										
											2019-12-25 11:34:45 +01:00
										 |  |  |         const cssContent = fs.readFileSync(RESOURCE_DIR + '/libraries/ckeditor/ckeditor-content.css'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 16:55:35 +01:00
										 |  |  |         zipFile.addBuffer(cssContent, 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
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:38:09 +01:00
										 |  |  |         noteMeta.attributes = noteMeta.attributes.filter(attr => attr.type !== 'relation' || attr.value in noteIdToMeta); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 16:55:35 +01:00
										 |  |  |     zipFile.addBuffer(metaFileJson, "!!!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(); | 
					
						
							| 
									
										
										
										
											2020-03-19 20:07:27 +01:00
										 |  |  |     const zipFileName = (branch.prefix ? (branch.prefix + " - ") : "") + note.title + ".zip"; | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-19 20:07:27 +01:00
										 |  |  |     res.setHeader('Content-Disposition', utils.getContentDisposition(zipFileName)); | 
					
						
							|  |  |  |     res.setHeader('Content-Type', 'application/zip'); | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-20 16:55:35 +01:00
										 |  |  |     zipFile.end(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     zipFile.outputStream.pipe(res); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2020-03-19 20:07:27 +01:00
										 |  |  |     exportToZip | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | }; |