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

Chore: Add more types to export modules

This commit is contained in:
Laurent Cozic 2023-12-15 18:18:11 +00:00
parent 02658370a6
commit d2fe33183b
13 changed files with 57 additions and 42 deletions

View File

@ -1,7 +1,7 @@
import InteropService from '@joplin/lib/services/interop/InteropService'; import InteropService from '@joplin/lib/services/interop/InteropService';
import CommandService from '@joplin/lib/services/CommandService'; import CommandService from '@joplin/lib/services/CommandService';
import shim from '@joplin/lib/shim'; import shim from '@joplin/lib/shim';
import { ExportOptions, FileSystemItem } from '@joplin/lib/services/interop/types'; import { ExportModuleOutputFormat, ExportOptions, FileSystemItem } from '@joplin/lib/services/interop/types';
import { ExportModule } from '@joplin/lib/services/interop/Module'; import { ExportModule } from '@joplin/lib/services/interop/Module';
import { _ } from '@joplin/lib/locale'; import { _ } from '@joplin/lib/locale';
@ -31,7 +31,7 @@ export default class InteropServiceHelper {
const tempFile = `${Setting.value('tempDir')}/${md5(Date.now() + Math.random())}.html`; const tempFile = `${Setting.value('tempDir')}/${md5(Date.now() + Math.random())}.html`;
const fullExportOptions: ExportOptions = { path: tempFile, const fullExportOptions: ExportOptions = { path: tempFile,
format: 'html', format: ExportModuleOutputFormat.Html,
target: FileSystemItem.File, target: FileSystemItem.File,
sourceNoteIds: [noteId], sourceNoteIds: [noteId],
customCss: '', ...exportOptions }; customCss: '', ...exportOptions };

View File

@ -1,6 +1,6 @@
import { CommandRuntime, CommandDeclaration } from '@joplin/lib/services/CommandService'; import { CommandRuntime, CommandDeclaration } from '@joplin/lib/services/CommandService';
import InteropService from '@joplin/lib/services/interop/InteropService'; import InteropService from '@joplin/lib/services/interop/InteropService';
import { ExportOptions, FileSystemItem } from '@joplin/lib/services/interop/types'; import { ExportModuleOutputFormat, ExportOptions, FileSystemItem } from '@joplin/lib/services/interop/types';
import shim from '@joplin/lib/shim'; import shim from '@joplin/lib/shim';
export const declaration: CommandDeclaration = { export const declaration: CommandDeclaration = {
@ -11,7 +11,7 @@ export const runtime = (): CommandRuntime => {
return { return {
// "targetPath" should be a file for JEX export (because the format can // "targetPath" should be a file for JEX export (because the format can
// contain multiple folders) or a directory otherwise. // contain multiple folders) or a directory otherwise.
execute: async (_context: any, folderIds: string[], format: string, targetPath: string) => { execute: async (_context: any, folderIds: string[], format: ExportModuleOutputFormat, targetPath: string) => {
const exportOptions: ExportOptions = { const exportOptions: ExportOptions = {
sourceFolderIds: folderIds, sourceFolderIds: folderIds,
path: targetPath, path: targetPath,

View File

@ -1,6 +1,6 @@
import { CommandRuntime, CommandDeclaration } from '@joplin/lib/services/CommandService'; import { CommandRuntime, CommandDeclaration } from '@joplin/lib/services/CommandService';
import InteropService from '@joplin/lib/services/interop/InteropService'; import InteropService from '@joplin/lib/services/interop/InteropService';
import { ExportOptions, FileSystemItem } from '@joplin/lib/services/interop/types'; import { ExportModuleOutputFormat, ExportOptions, FileSystemItem } from '@joplin/lib/services/interop/types';
export const declaration: CommandDeclaration = { export const declaration: CommandDeclaration = {
name: 'exportNotes', name: 'exportNotes',
@ -8,7 +8,7 @@ export const declaration: CommandDeclaration = {
export const runtime = (): CommandRuntime => { export const runtime = (): CommandRuntime => {
return { return {
execute: async (_context: any, noteIds: string[], format: string, targetDirectoryPath: string) => { execute: async (_context: any, noteIds: string[], format: ExportModuleOutputFormat, targetDirectoryPath: string) => {
const exportOptions: ExportOptions = { const exportOptions: ExportOptions = {
path: targetDirectoryPath, path: targetDirectoryPath,
format: format, format: format,

View File

@ -1,6 +1,6 @@
import Folder from '@joplin/lib/models/Folder'; import Folder from '@joplin/lib/models/Folder';
import InteropService from '@joplin/lib/services/interop/InteropService'; import InteropService from '@joplin/lib/services/interop/InteropService';
import { ExportOptions, FileSystemItem, OnExportProgressCallback } from '@joplin/lib/services/interop/types'; import { ExportModuleOutputFormat, ExportOptions, FileSystemItem, OnExportProgressCallback } from '@joplin/lib/services/interop/types';
import shim from '@joplin/lib/shim'; import shim from '@joplin/lib/shim';
import { CachesDirectoryPath } from 'react-native-fs'; import { CachesDirectoryPath } from 'react-native-fs';
@ -18,7 +18,7 @@ const exportFolders = async (path: string, onProgress: OnExportProgressCallback)
const exportOptions: ExportOptions = { const exportOptions: ExportOptions = {
sourceFolderIds, sourceFolderIds,
path, path,
format: 'jex', format: ExportModuleOutputFormat.Jex,
target: FileSystemItem.File, target: FileSystemItem.File,
onProgress, onProgress,
}; };

View File

@ -1,5 +1,5 @@
import InteropService from '../../services/interop/InteropService'; import InteropService from '../../services/interop/InteropService';
import { CustomExportContext, CustomImportContext, ModuleType } from '../../services/interop/types'; import { CustomExportContext, CustomImportContext, ExportModuleOutputFormat, ModuleType } from '../../services/interop/types';
import shim from '../../shim'; import shim from '../../shim';
import { fileContentEqual, setupDatabaseAndSynchronizer, switchClient, checkThrowAsync, exportDir, supportDir } from '../../testing/test-utils'; import { fileContentEqual, setupDatabaseAndSynchronizer, switchClient, checkThrowAsync, exportDir, supportDir } from '../../testing/test-utils';
import Folder from '../../models/Folder'; import Folder from '../../models/Folder';
@ -52,7 +52,7 @@ function memoryExportModule() {
const module: Module = makeExportModule({ const module: Module = makeExportModule({
description: 'Memory Export Module', description: 'Memory Export Module',
format: 'memory', format: ExportModuleOutputFormat.Memory,
fileExtensions: ['memory'], fileExtensions: ['memory'],
}, () => { }, () => {
return new InteropService_Exporter_Custom({ return new InteropService_Exporter_Custom({
@ -416,7 +416,7 @@ describe('services_InteropService', () => {
const outDir = exportDir(); const outDir = exportDir();
await service.export({ path: outDir, format: 'md', sourceNoteIds: [note11.id, note21.id] }); await service.export({ path: outDir, format: ExportModuleOutputFormat.Markdown, sourceNoteIds: [note11.id, note21.id] });
// verify that the md files exist // verify that the md files exist
expect(await shim.fsDriver().exists(`${outDir}/folder1`)).toBe(true); expect(await shim.fsDriver().exists(`${outDir}/folder1`)).toBe(true);
@ -441,7 +441,7 @@ describe('services_InteropService', () => {
const outDir = exportDir(); const outDir = exportDir();
await service.export({ path: outDir, format: 'md' }); await service.export({ path: outDir, format: ExportModuleOutputFormat.Markdown });
expect(await shim.fsDriver().exists(`${outDir}/folder1/生活.md`)).toBe(true); expect(await shim.fsDriver().exists(`${outDir}/folder1/生活.md`)).toBe(true);
expect(await shim.fsDriver().exists(`${outDir}/folder1/生活-1.md`)).toBe(true); expect(await shim.fsDriver().exists(`${outDir}/folder1/生活-1.md`)).toBe(true);
@ -461,7 +461,7 @@ describe('services_InteropService', () => {
await service.export({ await service.export({
path: exportDir(), path: exportDir(),
format: 'md', format: ExportModuleOutputFormat.Markdown,
sourceFolderIds: [folder1.id], sourceFolderIds: [folder1.id],
}); });
@ -478,7 +478,7 @@ describe('services_InteropService', () => {
await service.export({ await service.export({
path: exportDir(), path: exportDir(),
format: 'md', format: ExportModuleOutputFormat.Markdown,
sourceFolderIds: [folder1.id], sourceFolderIds: [folder1.id],
includeConflicts: false, includeConflicts: false,
}); });
@ -490,7 +490,7 @@ describe('services_InteropService', () => {
await service.export({ await service.export({
path: exportDir(), path: exportDir(),
format: 'md', format: ExportModuleOutputFormat.Markdown,
sourceFolderIds: [folder1.id], sourceFolderIds: [folder1.id],
includeConflicts: true, includeConflicts: true,
}); });
@ -515,7 +515,7 @@ describe('services_InteropService', () => {
const result = await service.export({ const result = await service.export({
path: exportDir(), path: exportDir(),
format: 'md', format: ExportModuleOutputFormat.Markdown,
}); });
expect(result.warnings.length).toBe(0); expect(result.warnings.length).toBe(0);
@ -533,7 +533,7 @@ describe('services_InteropService', () => {
service.registerModule(module); service.registerModule(module);
await service.export({ await service.export({
format: 'memory', format: ExportModuleOutputFormat.Memory,
}); });
const exportedNote = (result.items.find(i => i.type === ModelType.Note)).object as NoteEntity; const exportedNote = (result.items.find(i => i.type === ModelType.Note)).object as NoteEntity;
@ -603,7 +603,7 @@ describe('services_InteropService', () => {
const module: Module = makeExportModule({ const module: Module = makeExportModule({
type: ModuleType.Exporter, type: ModuleType.Exporter,
description: 'Test Export Module', description: 'Test Export Module',
format: 'testing', format: 'testing' as any,
fileExtensions: ['test'], fileExtensions: ['test'],
}, () => { }, () => {
return new InteropService_Exporter_Custom({ return new InteropService_Exporter_Custom({
@ -630,7 +630,7 @@ describe('services_InteropService', () => {
const service = InteropService.instance(); const service = InteropService.instance();
service.registerModule(module); service.registerModule(module);
await service.export({ await service.export({
format: 'testing', format: 'testing' as any,
path: filePath, path: filePath,
}); });

View File

@ -1,4 +1,4 @@
import { ModuleType, FileSystemItem, ImportModuleOutputFormat, ImportOptions, ExportOptions, ImportExportResult, ExportProgressState } from './types'; import { ModuleType, FileSystemItem, ImportModuleOutputFormat, ImportOptions, ExportOptions, ImportExportResult, ExportProgressState, ExportModuleOutputFormat } from './types';
import shim from '../../shim'; import shim from '../../shim';
import { _ } from '../../locale'; import { _ } from '../../locale';
import BaseItem from '../../models/BaseItem'; import BaseItem from '../../models/BaseItem';
@ -119,32 +119,32 @@ export default class InteropService {
const exportModules = [ const exportModules = [
makeExportModule({ makeExportModule({
format: 'jex', format: ExportModuleOutputFormat.Jex,
fileExtensions: ['jex'], fileExtensions: ['jex'],
target: FileSystemItem.File, target: FileSystemItem.File,
description: _('Joplin Export File'), description: _('Joplin Export File'),
}, () => new InteropService_Exporter_Jex()), }, () => new InteropService_Exporter_Jex()),
makeExportModule({ makeExportModule({
format: 'raw', format: ExportModuleOutputFormat.Raw,
target: FileSystemItem.Directory, target: FileSystemItem.Directory,
description: _('Joplin Export Directory'), description: _('Joplin Export Directory'),
}, () => new InteropService_Exporter_Raw()), }, () => new InteropService_Exporter_Raw()),
makeExportModule({ makeExportModule({
format: 'md', format: ExportModuleOutputFormat.Markdown,
target: FileSystemItem.Directory, target: FileSystemItem.Directory,
description: _('Markdown'), description: _('Markdown'),
}, () => new InteropService_Exporter_Md()), }, () => new InteropService_Exporter_Md()),
makeExportModule({ makeExportModule({
format: 'md_frontmatter', format: ExportModuleOutputFormat.MarkdownFrontMatter,
target: FileSystemItem.Directory, target: FileSystemItem.Directory,
description: _('Markdown + Front Matter'), description: _('Markdown + Front Matter'),
}, () => new InteropService_Exporter_Md_frontmatter()), }, () => new InteropService_Exporter_Md_frontmatter()),
makeExportModule({ makeExportModule({
format: 'html', format: ExportModuleOutputFormat.Html,
fileExtensions: ['html', 'htm'], fileExtensions: ['html', 'htm'],
target: FileSystemItem.File, target: FileSystemItem.File,
isNoteArchive: false, isNoteArchive: false,
@ -153,7 +153,7 @@ export default class InteropService {
}, dynamicRequireModuleFactory('./InteropService_Exporter_Html')), }, dynamicRequireModuleFactory('./InteropService_Exporter_Html')),
makeExportModule({ makeExportModule({
format: 'html', format: ExportModuleOutputFormat.Html,
target: FileSystemItem.Directory, target: FileSystemItem.Directory,
description: _('HTML Directory'), description: _('HTML Directory'),
supportsMobile: false, supportsMobile: false,
@ -301,7 +301,7 @@ export default class InteropService {
public async export(options: ExportOptions): Promise<ImportExportResult> { public async export(options: ExportOptions): Promise<ImportExportResult> {
options = { options = {
format: 'jex', format: ExportModuleOutputFormat.Jex,
...options, ...options,
}; };

View File

@ -6,7 +6,7 @@ import Note from '../../models/Note';
import * as fs from 'fs-extra'; import * as fs from 'fs-extra';
import { tempFilePath } from '../../testing/test-utils'; import { tempFilePath } from '../../testing/test-utils';
import { ContentScriptType } from '../../services/plugins/api/types'; import { ContentScriptType } from '../../services/plugins/api/types';
import { FileSystemItem } from './types'; import { ExportModuleOutputFormat, FileSystemItem } from './types';
import { readFile } from 'fs/promises'; import { readFile } from 'fs/promises';
async function recreateExportDir() { async function recreateExportDir() {
@ -31,7 +31,7 @@ describe('interop/InteropService_Exporter_Html', () => {
await service.export({ await service.export({
path: filePath, path: filePath,
format: 'html', format: ExportModuleOutputFormat.Html,
packIntoSingleFile: false, packIntoSingleFile: false,
}); });
@ -49,7 +49,7 @@ describe('interop/InteropService_Exporter_Html', () => {
const dir = exportDir(); const dir = exportDir();
await service.export({ await service.export({
path: dir, path: dir,
format: 'html', format: ExportModuleOutputFormat.Html,
target: FileSystemItem.Directory, target: FileSystemItem.Directory,
}); });
@ -106,7 +106,7 @@ describe('interop/InteropService_Exporter_Html', () => {
await service.export({ await service.export({
path: filePath, path: filePath,
format: 'html', format: ExportModuleOutputFormat.Html,
packIntoSingleFile: false, packIntoSingleFile: false,
plugins, plugins,
}); });
@ -130,7 +130,7 @@ describe('interop/InteropService_Exporter_Html', () => {
await service.export({ await service.export({
path: filePath, path: filePath,
format: 'html', format: ExportModuleOutputFormat.Html,
packIntoSingleFile: true, packIntoSingleFile: true,
target: FileSystemItem.File, target: FileSystemItem.File,
}); });

View File

@ -11,6 +11,7 @@ import { NoteEntity, ResourceEntity } from '../database/types.js';
import InteropService from './InteropService.js'; import InteropService from './InteropService.js';
import { fileExtension } from '../../path-utils.js'; import { fileExtension } from '../../path-utils.js';
import { readdir } from 'fs/promises'; import { readdir } from 'fs/promises';
import { ExportModuleOutputFormat } from './types.js';
describe('interop/InteropService_Exporter_Md', () => { describe('interop/InteropService_Exporter_Md', () => {
@ -440,7 +441,7 @@ describe('interop/InteropService_Exporter_Md', () => {
await service.export({ await service.export({
path: exportDir(), path: exportDir(),
format: 'md', format: ExportModuleOutputFormat.Markdown,
}); });
const resourceFilename = (await fs.readdir(`${exportDir()}/_resources`))[0]; const resourceFilename = (await fs.readdir(`${exportDir()}/_resources`))[0];
@ -459,7 +460,7 @@ describe('interop/InteropService_Exporter_Md', () => {
await service.export({ await service.export({
path: exportDir(), path: exportDir(),
format: 'md', format: ExportModuleOutputFormat.Markdown,
}); });
const note_body = await shim.fsDriver().readFile(`${exportDir()}/testing/mynote.md`); const note_body = await shim.fsDriver().readFile(`${exportDir()}/testing/mynote.md`);
@ -478,7 +479,7 @@ describe('interop/InteropService_Exporter_Md', () => {
await service.export({ await service.export({
path: exportDir(), path: exportDir(),
format: 'md', format: ExportModuleOutputFormat.Markdown,
}); });
const files = await readdir(`${exportDir()}/_resources`); const files = await readdir(`${exportDir()}/_resources`);

View File

@ -6,6 +6,7 @@ import Tag from '../../models/Tag';
import time from '../../time'; import time from '../../time';
import { fieldOrder } from './InteropService_Exporter_Md_frontmatter'; import { fieldOrder } from './InteropService_Exporter_Md_frontmatter';
import * as fs from 'fs-extra'; import * as fs from 'fs-extra';
import { ExportModuleOutputFormat } from './types';
async function recreateExportDir() { async function recreateExportDir() {
const dir = exportDir(); const dir = exportDir();
@ -19,7 +20,7 @@ describe('interop/InteropService_Exporter_Md_frontmatter', () => {
await service.export({ await service.export({
path: exportDir(), path: exportDir(),
format: 'md_frontmatter', format: ExportModuleOutputFormat.MarkdownFrontMatter,
}); });
return await fs.readFile(path, 'utf8'); return await fs.readFile(path, 'utf8');

View File

@ -38,7 +38,7 @@ describe('Module', () => {
it('should return correct default fullLabel for an ExportModule', () => { it('should return correct default fullLabel for an ExportModule', () => {
const testExportModule = makeExportModule({ const testExportModule = makeExportModule({
format: 'format_test_______TEST', format: 'format_test_______TEST' as any,
description: 'Testing...', description: 'Testing...',
}, () => new InteropService_Exporter_Base()); }, () => new InteropService_Exporter_Base());

View File

@ -2,11 +2,10 @@ import { _ } from '../../locale';
import shim from '../../shim'; import shim from '../../shim';
import InteropService_Exporter_Base from './InteropService_Exporter_Base'; import InteropService_Exporter_Base from './InteropService_Exporter_Base';
import InteropService_Importer_Base from './InteropService_Importer_Base'; import InteropService_Importer_Base from './InteropService_Importer_Base';
import { ExportOptions, FileSystemItem, ImportModuleOutputFormat, ImportOptions, ModuleType } from './types'; import { ExportModuleOutputFormat, ExportOptions, FileSystemItem, ImportModuleOutputFormat, ImportOptions, ModuleType } from './types';
// Metadata shared between importers and exporters. // Metadata shared between importers and exporters.
interface BaseMetadata { interface BaseMetadata {
format: string;
fileExtensions: string[]; fileExtensions: string[];
description: string; description: string;
isDefault: boolean; isDefault: boolean;
@ -26,6 +25,7 @@ interface BaseMetadata {
interface ImportMetadata extends BaseMetadata { interface ImportMetadata extends BaseMetadata {
type: ModuleType.Importer; type: ModuleType.Importer;
format: string;
sources: FileSystemItem[]; sources: FileSystemItem[];
outputFormat: ImportModuleOutputFormat; outputFormat: ImportModuleOutputFormat;
@ -37,6 +37,7 @@ export interface ImportModule extends ImportMetadata {
interface ExportMetadata extends BaseMetadata { interface ExportMetadata extends BaseMetadata {
type: ModuleType.Exporter; type: ModuleType.Exporter;
format: ExportModuleOutputFormat;
target: FileSystemItem; target: FileSystemItem;
} }
@ -46,7 +47,6 @@ export interface ExportModule extends ExportMetadata {
} }
const defaultBaseMetadata = { const defaultBaseMetadata = {
format: '',
fileExtensions: [] as string[], fileExtensions: [] as string[],
description: '', description: '',
isNoteArchive: true, isNoteArchive: true,
@ -69,6 +69,7 @@ export const makeImportModule = (
): ImportModule => { ): ImportModule => {
const importerDefaults: ImportMetadata = { const importerDefaults: ImportMetadata = {
...defaultBaseMetadata, ...defaultBaseMetadata,
format: '',
type: ModuleType.Importer, type: ModuleType.Importer,
sources: [], sources: [],
outputFormat: ImportModuleOutputFormat.Markdown, outputFormat: ImportModuleOutputFormat.Markdown,
@ -99,6 +100,7 @@ export const makeExportModule = (
): ExportModule => { ): ExportModule => {
const exporterDefaults: ExportMetadata = { const exporterDefaults: ExportMetadata = {
...defaultBaseMetadata, ...defaultBaseMetadata,
format: '' as any,
type: ModuleType.Exporter, type: ModuleType.Exporter,
target: FileSystemItem.File, target: FileSystemItem.File,

View File

@ -26,6 +26,17 @@ export enum ImportModuleOutputFormat {
Html = 'html', Html = 'html',
} }
export enum ExportModuleOutputFormat {
Enex = 'enex',
Html = 'html',
Jex = 'jex',
Markdown = 'md',
MarkdownFrontMatter = 'md_frontmatter',
Memory = 'memory',
Pdf = 'pdf',
Raw = 'raw',
}
export interface ImportOptions { export interface ImportOptions {
path?: string; path?: string;
format?: string; format?: string;
@ -44,7 +55,7 @@ export enum ExportProgressState {
export type OnExportProgressCallback = (status: ExportProgressState, progress: number)=> void; export type OnExportProgressCallback = (status: ExportProgressState, progress: number)=> void;
export interface ExportOptions { export interface ExportOptions {
format?: string; format?: ExportModuleOutputFormat;
path?: string; path?: string;
sourceFolderIds?: string[]; sourceFolderIds?: string[];
sourceNoteIds?: string[]; sourceNoteIds?: string[];

View File

@ -23,7 +23,7 @@ export default class JoplinInterop {
public async registerExportModule(module: ExportModule) { public async registerExportModule(module: ExportModule) {
const internalModule = makeExportModule({ const internalModule = makeExportModule({
...module, ...module as any,
type: ModuleType.Exporter, type: ModuleType.Exporter,
fileExtensions: module.fileExtensions ? module.fileExtensions : [], fileExtensions: module.fileExtensions ? module.fileExtensions : [],
}, () => new InteropService_Exporter_Custom(module)); }, () => new InteropService_Exporter_Custom(module));