single markdown file import, #166

This commit is contained in:
azivner 2018-09-03 13:40:40 +02:00
parent 4a58357a9a
commit a3feaa13b3
2 changed files with 49 additions and 5 deletions

View File

@ -26,12 +26,17 @@ $("#import-upload").change(async function() {
url: baseApiUrl + 'notes/' + importNoteId + '/import', url: baseApiUrl + 'notes/' + importNoteId + '/import',
headers: server.getHeaders(), headers: server.getHeaders(),
data: formData, data: formData,
dataType: 'json',
type: 'POST', type: 'POST',
contentType: false, // NEEDED, DON'T OMIT THIS contentType: false, // NEEDED, DON'T OMIT THIS
processData: false, // NEEDED, DON'T OMIT THIS processData: false, // NEEDED, DON'T OMIT THIS
}).fail((xhr, status, error) => alert('Import error: ' + xhr.responseText)); })
.fail((xhr, status, error) => alert('Import error: ' + xhr.responseText))
.done(async note => {
await treeService.reload();
await treeService.reload(); await treeService.activateNote(note.noteId);
});
}); });
export default { export default {

View File

@ -24,11 +24,14 @@ async function importToBranch(req) {
const extension = path.extname(file.originalname).toLowerCase(); const extension = path.extname(file.originalname).toLowerCase();
if (extension === '.tar') { if (extension === '.tar') {
await importTar(file, parentNoteId); return await importTar(file, parentNoteId);
} }
else if (extension === '.opml') { else if (extension === '.opml') {
return await importOpml(file, parentNoteId); return await importOpml(file, parentNoteId);
} }
else if (extension === '.md') {
return await importMarkdown(file, parentNoteId);
}
else { else {
return [400, `Unrecognized extension ${extension}, must be .tar or .opml`]; return [400, `Unrecognized extension ${extension}, must be .tar or .opml`];
} }
@ -48,6 +51,8 @@ async function importOutline(outline, parentNoteId) {
for (const childOutline of (outline.outline || [])) { for (const childOutline of (outline.outline || [])) {
await importOutline(childOutline, note.noteId); await importOutline(childOutline, note.noteId);
} }
return note;
} }
async function importOpml(file, parentNoteId) { async function importOpml(file, parentNoteId) {
@ -68,10 +73,16 @@ async function importOpml(file, parentNoteId) {
} }
const outlines = xml.opml.body[0].outline || []; const outlines = xml.opml.body[0].outline || [];
let returnNote = null;
for (const outline of outlines) { for (const outline of outlines) {
await importOutline(outline, parentNoteId); const note = await importOutline(outline, parentNoteId);
// first created note will be activated after import
returnNote = returnNote || note;
} }
return returnNote;
} }
async function importTar(file, parentNoteId) { async function importTar(file, parentNoteId) {
@ -85,7 +96,7 @@ async function importTar(file, parentNoteId) {
writer: new commonmark.HtmlRenderer() writer: new commonmark.HtmlRenderer()
}; };
await importNotes(ctx, files, parentNoteId); const note = await importNotes(ctx, files, parentNoteId);
// 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
@ -102,6 +113,8 @@ async function importTar(file, parentNoteId) {
await attributeService.createAttribute(attr); await attributeService.createAttribute(attr);
} }
return note;
} }
function getFileName(name) { function getFileName(name) {
@ -197,6 +210,8 @@ async function parseImportFile(file) {
} }
async function importNotes(ctx, files, parentNoteId) { async function importNotes(ctx, files, parentNoteId) {
let returnNote = null;
for (const file of files) { for (const file of files) {
let note; let note;
@ -255,10 +270,34 @@ async function importNotes(ctx, files, parentNoteId) {
} }
} }
// first created note will be activated after import
returnNote = returnNote || note;
if (file.children.length > 0) { if (file.children.length > 0) {
await importNotes(ctx, file.children, note.noteId); await importNotes(ctx, file.children, note.noteId);
} }
} }
return returnNote;
}
async function importMarkdown(file, parentNoteId) {
const markdownContent = file.buffer.toString("UTF-8");
const reader = new commonmark.Parser();
const writer = new commonmark.HtmlRenderer();
const parsed = reader.parse(markdownContent);
const htmlContent = writer.render(parsed);
const title = file.originalname.substr(0, file.originalname.length - 3); // strip .md extension
const {note} = await noteService.createNote(parentNoteId, title, htmlContent, {
type: 'text',
mime: 'text/html'
});
return note;
} }
module.exports = { module.exports = {