import dayjs from "dayjs"; import sax from "sax"; import stream from "stream"; import { Throttle } from "stream-throttle"; import log from "../log.js"; import { md5, escapeHtml, fromBase64 } from "../utils.js"; import date_utils from "../date_utils.js"; import sql from "../sql.js"; import noteService from "../notes.js"; import imageService from "../image.js"; import protectedSessionService from "../protected_session.js"; import htmlSanitizer from "../html_sanitizer.js"; import sanitizeAttributeName from "../sanitize_attribute_name.js"; import type TaskContext from "../task_context.js"; import type BNote from "../../becca/entities/bnote.js"; import type { File } from "./common.js"; import type { AttributeType } from "@triliumnext/commons"; /** * date format is e.g. 20181121T193703Z or 2013-04-14T16:19:00.000Z (Mac evernote, see #3496) * @returns trilium date format, e.g. 2013-04-14 16:19:00.000Z */ function parseDate(text: string) { // convert ISO format to the "20181121T193703Z" format text = text.replace(/[-:]/g, ""); // insert - and : to convert it to trilium format text = text.substr(0, 4) + "-" + text.substr(4, 2) + "-" + text.substr(6, 2) + " " + text.substr(9, 2) + ":" + text.substr(11, 2) + ":" + text.substr(13, 2) + ".000Z"; return text; } interface Attribute { type: AttributeType; name: string; value: string; } interface Resource { title: string; content?: Buffer | string; mime?: string; attributes: Attribute[]; } interface Note { title: string; attributes: Attribute[]; utcDateCreated: string; utcDateModified: string; noteId: string; blobId: string; content: string; resources: Resource[]; } let note: Partial = {}; let resource: Resource; function importEnex(taskContext: TaskContext<"importNotes">, file: File, parentNote: BNote): Promise { const saxStream = sax.createStream(true); const rootNoteTitle = file.originalname.toLowerCase().endsWith(".enex") ? file.originalname.substr(0, file.originalname.length - 5) : file.originalname; // root note is new note into all ENEX/notebook's notes will be imported const rootNote = noteService.createNewNote({ parentNoteId: parentNote.noteId, title: rootNoteTitle, content: "", type: "text", mime: "text/html", isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable() }).note; function extractContent(content: string) { const openingNoteIndex = content.indexOf(""); if (openingNoteIndex !== -1) { content = content.substr(openingNoteIndex + 9); } const closingNoteIndex = content.lastIndexOf(""); if (closingNoteIndex !== -1) { content = content.substr(0, closingNoteIndex); } content = content.trim(); // workaround for https://github.com/ckeditor/ckeditor5-list/issues/116 content = content.replace(/
  • \s*
    /g, "
  • "); content = content.replace(/<\/div>\s*<\/li>/g, "
  • "); // workaround for https://github.com/ckeditor/ckeditor5-list/issues/115 content = content.replace(/