| 
									
										
										
										
											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'); | 
					
						
							| 
									
										
										
										
											2018-11-24 20:58:38 +01:00
										 |  |  | const tar = require('tar-stream'); | 
					
						
							| 
									
										
										
										
											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"); | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-10-18 22:27:38 +02:00
										 |  |  | async function exportToTar(taskContext, branch, format, res) { | 
					
						
							| 
									
										
										
										
											2018-11-24 20:58:38 +01:00
										 |  |  |     const pack = tar.pack(); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  |     async function getNoteMeta(branch, parentMeta, existingFileNames) { | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |         const note = await branch.getNote(); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (await note.hasLabel('excludeFromExport')) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 16:46:36 +02:00
										 |  |  |         const completeTitle = branch.prefix ? (branch.prefix + ' - ' + note.title) : note.title; | 
					
						
							|  |  |  |         const baseFileName = sanitize(completeTitle); | 
					
						
							|  |  |  |         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
 | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |             attributes: (await note.getOwnedAttributes()).map(attribute => { | 
					
						
							|  |  |  |                 return { | 
					
						
							|  |  |  |                     type: attribute.type, | 
					
						
							|  |  |  |                     name: attribute.name, | 
					
						
							|  |  |  |                     value: attribute.value, | 
					
						
							|  |  |  |                     isInheritable: attribute.isInheritable, | 
					
						
							|  |  |  |                     position: attribute.position | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  |         const childBranches = await note.getChildBranches(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2019-09-01 22:09:55 +02:00
										 |  |  |         if (available && ((await 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) { | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  |                 const note = await 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-08-31 11:15:32 +02:00
										 |  |  |             url += meta.dirFileName + '/'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |         url += 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-08-31 22:39:25 +02:00
										 |  |  |             return url ? `src="${encodeURIComponent(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-08-31 22:39:25 +02:00
										 |  |  |             return url ? `href="${encodeURIComponent(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")) { | 
					
						
							|  |  |  |                 content = `<html>
 | 
					
						
							|  |  |  | <head><meta charset="utf-8"></head> | 
					
						
							|  |  |  | <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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  |     async 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-09-01 20:35:55 +02:00
										 |  |  |             let content = `<p>This is a clone of a note. Go to its <a href="${encodeURIComponent(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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  |             pack.entry({name: filePathPrefix + noteMeta.dataFileName, size: content.length}, content); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         const note = await 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) { | 
					
						
							| 
									
										
										
										
											2019-09-01 16:46:36 +02:00
										 |  |  |             const content = prepareContent(noteMeta.title, await note.getContent(), noteMeta); | 
					
						
							| 
									
										
										
										
											2018-11-24 14:44:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 10:12:42 +02:00
										 |  |  |             pack.entry({name: filePathPrefix + noteMeta.dataFileName, size: content.length}, content); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             pack.entry({name: directoryPath, type: 'directory'}); | 
					
						
							| 
									
										
										
										
											2018-11-25 15:17:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             for (const childMeta of noteMeta.children) { | 
					
						
							| 
									
										
										
										
											2018-11-26 22:22:16 +01:00
										 |  |  |                 await saveNote(childMeta, directoryPath + '/'); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 19:02:48 +02:00
										 |  |  |     function saveNavigation(rootMeta) { | 
					
						
							|  |  |  |         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); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 html += `<a href="${targetUrl}">${escapedTitle}</a>`; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             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>'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const fullHtml = '<html><head><meta charset="utf-8"></head><body><ul>' + saveNavigationInner(rootMeta) + '</ul></body></html>' | 
					
						
							|  |  |  |         const prettyHtml = html.prettyPrint(fullHtml, {indent_size: 2}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         pack.entry({name: navigationMeta.dataFileName, size: prettyHtml.length}, prettyHtml); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const rootMeta = await getNoteMeta(branch, { notePath: [] }, ['navigation']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const navigationMeta = { | 
					
						
							|  |  |  |         noImport: true, | 
					
						
							|  |  |  |         dataFileName: "navigation." + (format === 'html' ? 'html' : 'md') | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     const metaFile = { | 
					
						
							| 
									
										
										
										
											2018-11-27 10:31:55 +01:00
										 |  |  |         formatVersion: 1, | 
					
						
							|  |  |  |         appVersion: packageInfo.version, | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |         files: [ | 
					
						
							| 
									
										
										
										
											2019-10-20 19:02:48 +02:00
										 |  |  |             rootMeta, | 
					
						
							|  |  |  |             navigationMeta | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01: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'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pack.entry({name: "!!!meta.json", size: metaFileJson.length}, metaFileJson); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 19:02:48 +02:00
										 |  |  |     await saveNote(rootMeta, ''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await saveNavigation(rootMeta, navigationMeta); | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |     pack.finalize(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  |     const note = await branch.getNote(); | 
					
						
							| 
									
										
										
										
											2019-01-13 10:22:17 +01:00
										 |  |  |     const tarFileName = (branch.prefix ? (branch.prefix + " - ") : "") + note.title + ".tar"; | 
					
						
							| 
									
										
										
										
											2018-11-25 22:09:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 10:22:17 +01:00
										 |  |  |     res.setHeader('Content-Disposition', utils.getContentDisposition(tarFileName)); | 
					
						
							| 
									
										
										
										
											2018-11-16 12:12:04 +01:00
										 |  |  |     res.setHeader('Content-Type', 'application/tar'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     pack.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 = { | 
					
						
							|  |  |  |     exportToTar | 
					
						
							|  |  |  | }; |