1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/packages/lib/services/interop/InteropService_Exporter_Html.ts

138 lines
4.7 KiB
TypeScript
Raw Normal View History

2020-11-05 18:58:23 +02:00
const InteropService_Exporter_Base = require('./InteropService_Exporter_Base').default;
const { basename, friendlySafeFilename, rtrimSlashes } = require('../../path-utils');
const BaseModel = require('../../BaseModel').default;
const Folder = require('../../models/Folder');
const Note = require('../../models/Note');
const Setting = require('../../models/Setting').default;
const shim = require('../../shim').default;
const { themeStyle } = require('../../theme');
const { dirname } = require('../../path-utils');
const { escapeHtml } = require('../../string-utils.js');
const markupLanguageUtils = require('../../markupLanguageUtils').default;
const { assetsToHeaders } = require('@joplinapp/renderer');
export default class InteropService_Exporter_Html extends InteropService_Exporter_Base {
async init(path:string, options:any = {}) {
this.customCss_ = options.customCss ? options.customCss : '';
if (this.metadata().target === 'file') {
this.destDir_ = dirname(path);
this.filePath_ = path;
} else {
this.destDir_ = path;
this.filePath_ = null;
}
this.createdDirs_ = [];
this.resourceDir_ = this.destDir_ ? `${this.destDir_}/_resources` : null;
await shim.fsDriver().mkdir(this.destDir_);
this.markupToHtml_ = markupLanguageUtils.newMarkupToHtml();
this.resources_ = [];
this.style_ = themeStyle(Setting.THEME_LIGHT);
}
async makeDirPath_(item:any, pathPart:string = null) {
let output = '';
while (true) {
if (item.type_ === BaseModel.TYPE_FOLDER) {
if (pathPart) {
output = `${pathPart}/${output}`;
} else {
output = `${friendlySafeFilename(item.title, null, true)}/${output}`;
output = await shim.fsDriver().findUniqueFilename(output);
}
}
if (!item.parent_id) return output;
item = await Folder.load(item.parent_id);
}
}
async processNoteResources_(item:any) {
const target = this.metadata().target;
const linkedResourceIds = await Note.linkedResourceIds(item.body);
const relativePath = target === 'directory' ? rtrimSlashes(await this.makeDirPath_(item, '..')) : '';
const resourcePaths = this.context() && this.context().resourcePaths ? this.context().resourcePaths : {};
let newBody = item.body;
for (let i = 0; i < linkedResourceIds.length; i++) {
const id = linkedResourceIds[i];
const resourceContent = `${relativePath ? `${relativePath}/` : ''}_resources/${basename(resourcePaths[id])}`;
newBody = newBody.replace(new RegExp(`:/${id}`, 'g'), resourceContent);
}
return newBody;
}
async processItem(_itemType:number, item:any) {
if ([BaseModel.TYPE_NOTE, BaseModel.TYPE_FOLDER].indexOf(item.type_) < 0) return;
let dirPath = '';
if (!this.filePath_) {
dirPath = `${this.destDir_}/${await this.makeDirPath_(item)}`;
if (this.createdDirs_.indexOf(dirPath) < 0) {
await shim.fsDriver().mkdir(dirPath);
this.createdDirs_.push(dirPath);
}
}
if (item.type_ === BaseModel.TYPE_NOTE) {
let noteFilePath = '';
if (this.filePath_) {
noteFilePath = this.filePath_;
} else {
noteFilePath = `${dirPath}/${friendlySafeFilename(item.title, null, true)}.html`;
noteFilePath = await shim.fsDriver().findUniqueFilename(noteFilePath);
}
const bodyMd = await this.processNoteResources_(item);
const result = await this.markupToHtml_.render(item.markup_language, bodyMd, this.style_, {
resources: this.resources_,
plainResourceRendering: true,
userCss: this.customCss_,
});
const noteContent = [];
if (item.title) noteContent.push(`<div class="exported-note-title">${escapeHtml(item.title)}</div>`);
if (result.html) noteContent.push(result.html);
// We need to export all the plugin assets too and refer them from the header
// The source path is a bit hard-coded but shouldn't change.
for (let i = 0; i < result.pluginAssets.length; i++) {
const asset = result.pluginAssets[i];
const filePath = `${dirname(dirname(dirname(__dirname)))}/gui/note-viewer/pluginAssets/${asset.name}`;
const destPath = `${dirname(noteFilePath)}/pluginAssets/${asset.name}`;
await shim.fsDriver().mkdir(dirname(destPath));
await shim.fsDriver().copy(filePath, destPath);
}
const fullHtml = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
${assetsToHeaders(result.pluginAssets, { asHtml: true })}
<title>${escapeHtml(item.title)}</title>
</head>
<body>
<div class="exported-note">${noteContent.join('\n\n')}</div>
</body>
</html>
`;
await shim.fsDriver().writeFile(noteFilePath, fullHtml, 'utf-8');
}
}
async processResource(resource:any, filePath:string) {
const destResourcePath = `${this.resourceDir_}/${basename(filePath)}`;
await shim.fsDriver().copy(filePath, destResourcePath);
this.resources_.push(resource);
}
async close() {}
}