mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 04:51:31 +08:00 
			
		
		
		
	markdown import
This commit is contained in:
		
							parent
							
								
									9bdd4437f2
								
							
						
					
					
						commit
						c41b809720
					
				| @ -92,10 +92,9 @@ async function exportToTar(branch, res) { | |||||||
|     const pack = tar.pack(); |     const pack = tar.pack(); | ||||||
| 
 | 
 | ||||||
|     const exportedNoteIds = []; |     const exportedNoteIds = []; | ||||||
|     const name = await exportNoteInner(branch.branchId, ''); |     const name = await exportNoteInner(branch, ''); | ||||||
| 
 | 
 | ||||||
|     async function exportNoteInner(branchId, directory) { |     async function exportNoteInner(branch, directory) { | ||||||
|         const branch = await repository.getBranch(branchId); |  | ||||||
|         const note = await branch.getNote(); |         const note = await branch.getNote(); | ||||||
|         const childFileName = directory + sanitize(note.title); |         const childFileName = directory + sanitize(note.title); | ||||||
| 
 | 
 | ||||||
| @ -138,8 +137,14 @@ async function exportToTar(branch, res) { | |||||||
| 
 | 
 | ||||||
|         exportedNoteIds.push(note.noteId); |         exportedNoteIds.push(note.noteId); | ||||||
| 
 | 
 | ||||||
|         for (const child of await note.getChildBranches()) { |         const childBranches = await note.getChildBranches(); | ||||||
|             await exportNoteInner(child.branchId, childFileName + "/"); | 
 | ||||||
|  |         if (childBranches.length > 0) { | ||||||
|  |             saveDirectory(childFileName); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         for (const childBranch of childBranches) { | ||||||
|  |             await exportNoteInner(childBranch, childFileName + "/"); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return childFileName; |         return childFileName; | ||||||
| @ -157,6 +162,10 @@ async function exportToTar(branch, res) { | |||||||
|         pack.entry({name: childFileName + ".meta", size: metadataJson.length}, metadataJson); |         pack.entry({name: childFileName + ".meta", size: metadataJson.length}, metadataJson); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     function saveDirectory(childFileName) { | ||||||
|  |         pack.entry({name: childFileName, type: 'directory'}); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     pack.finalize(); |     pack.finalize(); | ||||||
| 
 | 
 | ||||||
|     res.setHeader('Content-Disposition', 'file; filename="' + name + '.tar"'); |     res.setHeader('Content-Disposition', 'file; filename="' + name + '.tar"'); | ||||||
| @ -187,7 +196,13 @@ async function exportToMarkdown(branch, res) { | |||||||
| 
 | 
 | ||||||
|         saveDataFile(childFileName, note); |         saveDataFile(childFileName, note); | ||||||
| 
 | 
 | ||||||
|         for (const childNote of await note.getChildNotes()) { |         const childNotes = await note.getChildNotes(); | ||||||
|  | 
 | ||||||
|  |         if (childNotes.length > 0) { | ||||||
|  |             saveDirectory(childFileName); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         for (const childNote of childNotes) { | ||||||
|             await exportNoteInner(childNote, childFileName + "/"); |             await exportNoteInner(childNote, childFileName + "/"); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| @ -219,6 +234,10 @@ async function exportToMarkdown(branch, res) { | |||||||
|         pack.entry({name: childFileName + ".md", size: markdown.length}, markdown); |         pack.entry({name: childFileName + ".md", size: markdown.length}, markdown); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     function saveDirectory(childFileName) { | ||||||
|  |         pack.entry({name: childFileName, type: 'directory'}); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     pack.finalize(); |     pack.finalize(); | ||||||
| 
 | 
 | ||||||
|     res.setHeader('Content-Disposition', 'file; filename="' + name + '.tar"'); |     res.setHeader('Content-Disposition', 'file; filename="' + name + '.tar"'); | ||||||
|  | |||||||
| @ -1,6 +1,7 @@ | |||||||
| "use strict"; | "use strict"; | ||||||
| 
 | 
 | ||||||
| const repository = require('../../services/repository'); | const repository = require('../../services/repository'); | ||||||
|  | const log = require('../../services/log'); | ||||||
| const attributeService = require('../../services/attributes'); | const attributeService = require('../../services/attributes'); | ||||||
| const noteService = require('../../services/notes'); | const noteService = require('../../services/notes'); | ||||||
| const Branch = require('../../entities/branch'); | const Branch = require('../../entities/branch'); | ||||||
| @ -80,7 +81,12 @@ async function importTar(file, parentNoteId) { | |||||||
|     const noteIdMap = {}; |     const noteIdMap = {}; | ||||||
|     const attributes = []; |     const attributes = []; | ||||||
| 
 | 
 | ||||||
|     await importNotes(files, parentNoteId, noteIdMap, attributes); |     const markdown = { | ||||||
|  |         reader: new commonmark.Parser(), | ||||||
|  |         writer: new commonmark.HtmlRenderer() | ||||||
|  |     }; | ||||||
|  | 
 | ||||||
|  |     await importNotes(markdown, files, parentNoteId, noteIdMap, attributes); | ||||||
| 
 | 
 | ||||||
|     // we save attributes after importing notes because we need to have all the relation
 |     // we save attributes after importing notes because we need to have all the relation
 | ||||||
|     // targets already existing
 |     // targets already existing
 | ||||||
| @ -106,13 +112,19 @@ function getFileName(name) { | |||||||
|         key = "data"; |         key = "data"; | ||||||
|         name = name.substr(0, name.length - 4); |         name = name.substr(0, name.length - 4); | ||||||
|     } |     } | ||||||
|  |     else if (name.endsWith(".md")) { | ||||||
|  |         key = "markdown"; | ||||||
|  |         name = name.substr(0, name.length - 3); | ||||||
|  |     } | ||||||
|     else if (name.endsWith((".meta"))) { |     else if (name.endsWith((".meta"))) { | ||||||
|         key = "meta"; |         key = "meta"; | ||||||
|         name = name.substr(0, name.length - 5); |         name = name.substr(0, name.length - 5); | ||||||
|     } |     } | ||||||
|     else { |     else { | ||||||
|         throw new Error("Unknown file type in import archive: " + name); |         // this is supposed to be directory
 | ||||||
|  |         key = "data"; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|     return {name, key}; |     return {name, key}; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -129,6 +141,7 @@ async function parseImportFile(file) { | |||||||
| 
 | 
 | ||||||
|         if (!file) { |         if (!file) { | ||||||
|             file = fileMap[name] = { |             file = fileMap[name] = { | ||||||
|  |                 name: path.basename(name), | ||||||
|                 children: [] |                 children: [] | ||||||
|             }; |             }; | ||||||
| 
 | 
 | ||||||
| @ -177,13 +190,25 @@ async function parseImportFile(file) { | |||||||
|     }); |     }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function importNotes(files, parentNoteId, noteIdMap, attributes) { | async function importNotes(markdown, files, parentNoteId, noteIdMap, attributes) { | ||||||
|     for (const file of files) { |     for (const file of files) { | ||||||
|         if (file.meta && file.meta.version !== 1) { |         let note; | ||||||
|  | 
 | ||||||
|  |         if (file.markdown) { | ||||||
|  |             const parsed = markdown.reader.parse(file.markdown.toString("UTF-8")); | ||||||
|  |             const content = markdown.writer.render(parsed); | ||||||
|  | 
 | ||||||
|  |             note = (await noteService.createNote(parentNoteId, file.name, content, { | ||||||
|  |                 type: 'text', | ||||||
|  |                 mime: 'text/html' | ||||||
|  |             })).note; | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             if (file.meta.version !== 1) { | ||||||
|                 throw new Error("Can't read meta data version " + file.meta.version); |                 throw new Error("Can't read meta data version " + file.meta.version); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|         if (file.meta && file.meta.clone) { |             if (file.meta.clone) { | ||||||
|                 await new Branch({ |                 await new Branch({ | ||||||
|                     parentNoteId: parentNoteId, |                     parentNoteId: parentNoteId, | ||||||
|                     noteId: noteIdMap[file.meta.noteId], |                     noteId: noteIdMap[file.meta.noteId], | ||||||
| @ -193,15 +218,15 @@ async function importNotes(files, parentNoteId, noteIdMap, attributes) { | |||||||
|                 return; |                 return; | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|         if (!file.meta || file.meta.type !== 'file') { |             if (file.meta.type !== 'file') { | ||||||
|                 file.data = file.data.toString("UTF-8"); |                 file.data = file.data.toString("UTF-8"); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|         const {note} = await noteService.createNote(parentNoteId, file.meta.title, file.data, { |             note = (await noteService.createNote(parentNoteId, file.meta.title, file.data, { | ||||||
|                 type: file.meta.type, |                 type: file.meta.type, | ||||||
|                 mime: file.meta.mime, |                 mime: file.meta.mime, | ||||||
|                 prefix: file.meta.prefix |                 prefix: file.meta.prefix | ||||||
|         }); |             })).note; | ||||||
| 
 | 
 | ||||||
|             noteIdMap[file.meta.noteId] = note.noteId; |             noteIdMap[file.meta.noteId] = note.noteId; | ||||||
| 
 | 
 | ||||||
| @ -215,9 +240,10 @@ async function importNotes(files, parentNoteId, noteIdMap, attributes) { | |||||||
|                     position: attribute.position |                     position: attribute.position | ||||||
|                 }); |                 }); | ||||||
|             } |             } | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
|         if (file.children.length > 0) { |         if (file.children.length > 0) { | ||||||
|             await importNotes(file.children, note.noteId, noteIdMap, attributes); |             await importNotes(markdown, file.children, note.noteId, noteIdMap, attributes); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 azivner
						azivner