core: allow exports with custom content

This commit is contained in:
ammarahm-ed
2023-04-07 02:06:23 +05:00
committed by Ammar Ahmed
parent cd2ca94c3d
commit 14d7ec6b4e

View File

@@ -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<string | false | undefined>}
*/
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.");
}