From 14d7ec6b4e27b3f4b53fd48fd1b02f329914fe19 Mon Sep 17 00:00:00 2001 From: ammarahm-ed Date: Fri, 7 Apr 2023 02:06:23 +0500 Subject: [PATCH] core: allow exports with custom content --- packages/core/models/note.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/core/models/note.js b/packages/core/models/note.js index 904e32768..3148e36d3 100644 --- a/packages/core/models/note.js +++ b/packages/core/models/note.js @@ -79,10 +79,12 @@ export default class Note { /** * * @param {"html"|"md"|"txt"} format - Format to export into - * @param {string?} rawContent - Use this raw content instead of generating itself + * @param {string} [contentItem=undefined] + * @param {boolean} [template=true] + * @param {string} [rawHTML=undefined] rawHTML - Use this raw content instead of generating itself * @returns {Promise} */ - async export(to = "html", rawContent) { + async export(to = "html", contentItem, template = true, rawHTML) { if (to !== "txt" && !(await checkIsUserPremium(CHECK_IDS.noteExport))) return false; @@ -94,7 +96,8 @@ export default class Note { createdOn: formatDate(this.data.dateCreated), tags: this.tags.join(", ") }; - const contentItem = await this._db.content.raw(this._note.contentId); + contentItem = + contentItem || (await this._db.content.raw(this._note.contentId)); if (!contentItem) return false; const { data, type } = await this._db.content.downloadMedia( `export-${this.id}`, @@ -105,14 +108,20 @@ export default class Note { switch (to) { case "html": - templateData.content = rawContent || content.toHTML(); - return HTMLBuilder.buildHTML(templateData); + templateData.content = rawHTML || content.toHTML(); + return template + ? HTMLBuilder.buildHTML(templateData) + : templateData.content; case "txt": - templateData.content = rawContent || content.toTXT(); - return TextBuilder.buildText(templateData); + templateData.content = rawHTML || content.toTXT(); + return template + ? TextBuilder.buildText(templateData) + : templateData.content; case "md": - templateData.content = rawContent || content.toMD(); - return MarkdownBuilder.buildMarkdown(templateData); + templateData.content = rawHTML || content.toMD(); + return template + ? MarkdownBuilder.buildMarkdown(templateData) + : templateData.content; default: throw new Error("Export format not supported."); }