1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-06 09:19:22 +02:00

Desktop: Fix import interop service (#1887)

* Revert "Revert "Desktop: Add ENEX to HTML export (#1795)""

This reverts commit 50b66cceca.

* Revert "Revert "Desktop, Cli: Fixed interop service so that it still allow auto-detecting importer based on format (required for Cli and for test units)""

This reverts commit c7c57ab2a5.

* Fix the .md importer

* Add comment re future refactor

* Rm importerClass for .md importer

* Fix EnexToMd module name
This commit is contained in:
Devon Zuegel
2019-09-23 14:18:30 -07:00
committed by Laurent Cozic
parent 8a8ecaade3
commit 172d925f0f
24 changed files with 1293 additions and 683 deletions

View File

@@ -13,8 +13,7 @@ const { toTitleCase } = require('lib/string-utils');
class InteropService {
constructor() {
this.modules_ = null;
}
this.modules_ = null; }
modules() {
if (this.modules_) return this.modules_;
@@ -42,7 +41,17 @@ class InteropService {
format: 'enex',
fileExtensions: ['enex'],
sources: ['file'],
description: _('Evernote Export File'),
description: _('Evernote Export File (as Markdown)'),
importerClass: 'InteropService_Importer_EnexToMd',
isDefault: true,
},
{
format: 'enex',
fileExtensions: ['enex'],
sources: ['file'],
description: _('Evernote Export File (as HTML)'),
// TODO: Consider doing this the same way as the multiple `md` importers are handled
importerClass: 'InteropService_Importer_EnexToHtml',
},
];
@@ -112,21 +121,57 @@ class InteropService {
return this.modules_;
}
moduleByFormat_(type, format) {
// Find the module that matches the given type ("importer" or "exporter")
// and the given format. Some formats can have multiple assocated importers
// or exporters, such as ENEX. In this case, the one marked as "isDefault"
// is returned. This is useful to auto-detect the module based on the format.
// For more precise matching, newModuleFromPath_ should be used.
findModuleByFormat_(type, format) {
const modules = this.modules();
const matches = [];
for (let i = 0; i < modules.length; i++) {
const m = modules[i];
if (m.format === format && m.type === type) return modules[i];
if (m.format === format && m.type === type) matches.push(modules[i]);
}
return null;
const output = matches.find(m => !!m.isDefault);
if (output) return output;
return matches.length ? matches[0] : null;
}
newModule_(type, format) {
const module = this.moduleByFormat_(type, format);
if (!module) throw new Error(_('Cannot load "%s" module for format "%s"', type, format));
const ModuleClass = require(module.path);
/**
* NOTE TO FUTURE SELF: It might make sense to simply move all the existing
* formatters to the `newModuleFromPath_` approach, so that there's only one way
* to do this mapping. This isn't a priority right now (per the convo in:
* https://github.com/laurent22/joplin/pull/1795#discussion_r322379121) but
* we can do it if it ever becomes necessary.
*/
newModuleByFormat_(type, format) {
const moduleMetadata = this.findModuleByFormat_(type, format);
if (!moduleMetadata) throw new Error(_('Cannot load "%s" module for format "%s"', type, format));
const ModuleClass = require(moduleMetadata.path);
const output = new ModuleClass();
output.setMetadata(module);
output.setMetadata(moduleMetadata);
return output;
}
/**
* The existing `newModuleByFormat_` fn would load by the input format. This
* was fine when there was a 1-1 mapping of input formats to output formats,
* but now that we have 2 possible outputs for an `enex` input, we need to be
* explicit with which importer we want to use.
*
* https://github.com/laurent22/joplin/pull/1795#pullrequestreview-281574417
*/
newModuleFromPath_(type, options) {
if (!options || !options.modulePath) {
throw new Error('Cannot load module without a defined path to load from.');
}
const ModuleClass = require(options.modulePath);
const output = new ModuleClass();
const moduleMetadata = this.findModuleByFormat_(type, options.format);
output.setMetadata({options, ...moduleMetadata}); // TODO: Check that this metadata is equivalent to module above
return output;
}
@@ -173,7 +218,17 @@ class InteropService {
let result = { warnings: [] };
const importer = this.newModule_('importer', options.format);
// console.log('options passed to InteropService:');
// console.log(JSON.stringify({options}, null, 2));
let importer = null;
if (options.modulePath) {
importer = this.newModuleFromPath_('importer', options);
} else {
importer = this.newModuleByFormat_('importer', options.format);
}
await importer.init(options.path, options);
result = await importer.exec(result);
@@ -248,7 +303,7 @@ class InteropService {
await queueExportItem(BaseModel.TYPE_TAG, exportedTagIds[i]);
}
const exporter = this.newModule_('exporter', exportFormat);
const exporter = this.newModuleByFormat_('exporter', exportFormat);
await exporter.init(exportPath);
const typeOrder = [BaseModel.TYPE_FOLDER, BaseModel.TYPE_RESOURCE, BaseModel.TYPE_NOTE, BaseModel.TYPE_TAG, BaseModel.TYPE_NOTE_TAG];

View File

@@ -0,0 +1,22 @@
const InteropService_Importer_Base = require('lib/services/InteropService_Importer_Base');
const Folder = require('lib/models/Folder.js');
const { filename } = require('lib/path-utils.js');
class InteropService_Importer_EnexToHtml extends InteropService_Importer_Base {
async exec(result) {
const { importEnex } = require('lib/import-enex');
let folder = this.options_.destinationFolder;
if (!folder) {
const folderTitle = await Folder.findUniqueItemTitle(filename(this.sourcePath_));
folder = await Folder.save({ title: folderTitle });
}
await importEnex(folder.id, this.sourcePath_, {...this.options_, outputFormat: 'html'});
return result;
}
}
module.exports = InteropService_Importer_EnexToHtml;

View File

@@ -2,7 +2,7 @@ const InteropService_Importer_Base = require('lib/services/InteropService_Import
const Folder = require('lib/models/Folder.js');
const { filename } = require('lib/path-utils.js');
class InteropService_Importer_Enex extends InteropService_Importer_Base {
class InteropService_Importer_EnexToMd extends InteropService_Importer_Base {
async exec(result) {
const { importEnex } = require('lib/import-enex');
@@ -19,4 +19,4 @@ class InteropService_Importer_Enex extends InteropService_Importer_Base {
}
}
module.exports = InteropService_Importer_Enex;
module.exports = InteropService_Importer_EnexToMd;