1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-30 10:36:35 +02:00

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

This commit is contained in:
Laurent Cozic 2019-09-19 23:26:33 +01:00
parent 335b43ead4
commit 558b6443bc

View File

@ -43,6 +43,7 @@ class InteropService {
sources: ['file'],
description: _('Evernote Export File (as Markdown)'),
importerClass: 'InteropService_Importer_EnexToMd',
isDefault: true,
},
{
format: 'enex',
@ -120,14 +121,23 @@ class InteropService {
return this.modules_;
}
// 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();
// console.log(JSON.stringify({modules}, null, 2))
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;
}
newModuleByFormat_(type, format) {
@ -145,10 +155,6 @@ class InteropService {
* but now that we have 2 possible outputs for an `enex` input, we need to be
* explicit with which importer we want to use.
*
* In the long run, 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.
*
* https://github.com/laurent22/joplin/pull/1795#pullrequestreview-281574417
*/
newModuleFromPath_(options) {
@ -205,7 +211,14 @@ class InteropService {
// console.log('options passed to InteropService:');
// console.log(JSON.stringify({options}, null, 2));
const importer = this.newModuleFromPath_(options);
let importer = null;
if (options.modulePath) {
importer = this.newModuleFromPath_(options);
} else {
importer = this.newModuleByFormat_('importer', options.format);
}
await importer.init(options.path, options);
result = await importer.exec(result);