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 CommandService from '@joplin/lib/services/CommandService';
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 { _ } from '@joplin/lib/locale';
@ -31,7 +31,7 @@ export default class InteropServiceHelper {
const tempFile = `${Setting.value('tempDir')}/${md5(Date.now() + Math.random())}.html`;
const fullExportOptions: ExportOptions = { path: tempFile,
format: 'html',
format: ExportModuleOutputFormat.Html,
target: FileSystemItem.File,
sourceNoteIds: [noteId],
customCss: '', ...exportOptions };

View File

@ -1,6 +1,6 @@
import { CommandRuntime, CommandDeclaration } from '@joplin/lib/services/CommandService';
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';
export const declaration: CommandDeclaration = {
@ -11,7 +11,7 @@ export const runtime = (): CommandRuntime => {
return {
// "targetPath" should be a file for JEX export (because the format can
// 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 = {
sourceFolderIds: folderIds,
path: targetPath,

View File

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

View File

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

View File

@ -1,5 +1,5 @@
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 { fileContentEqual, setupDatabaseAndSynchronizer, switchClient, checkThrowAsync, exportDir, supportDir } from '../../testing/test-utils';
import Folder from '../../models/Folder';
@ -52,7 +52,7 @@ function memoryExportModule() {
const module: Module = makeExportModule({
description: 'Memory Export Module',
format: 'memory',
format: ExportModuleOutputFormat.Memory,
fileExtensions: ['memory'],
}, () => {
return new InteropService_Exporter_Custom({
@ -416,7 +416,7 @@ describe('services_InteropService', () => {
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
expect(await shim.fsDriver().exists(`${outDir}/folder1`)).toBe(true);
@ -441,7 +441,7 @@ describe('services_InteropService', () => {
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/生活-1.md`)).toBe(true);
@ -461,7 +461,7 @@ describe('services_InteropService', () => {
await service.export({
path: exportDir(),
format: 'md',
format: ExportModuleOutputFormat.Markdown,
sourceFolderIds: [folder1.id],
});
@ -478,7 +478,7 @@ describe('services_InteropService', () => {
await service.export({
path: exportDir(),
format: 'md',
format: ExportModuleOutputFormat.Markdown,
sourceFolderIds: [folder1.id],
includeConflicts: false,
});
@ -490,7 +490,7 @@ describe('services_InteropService', () => {
await service.export({
path: exportDir(),
format: 'md',
format: ExportModuleOutputFormat.Markdown,
sourceFolderIds: [folder1.id],
includeConflicts: true,
});
@ -515,7 +515,7 @@ describe('services_InteropService', () => {
const result = await service.export({
path: exportDir(),
format: 'md',
format: ExportModuleOutputFormat.Markdown,
});
expect(result.warnings.length).toBe(0);
@ -533,7 +533,7 @@ describe('services_InteropService', () => {
service.registerModule(module);
await service.export({
format: 'memory',
format: ExportModuleOutputFormat.Memory,
});
const exportedNote = (result.items.find(i => i.type === ModelType.Note)).object as NoteEntity;
@ -603,7 +603,7 @@ describe('services_InteropService', () => {
const module: Module = makeExportModule({
type: ModuleType.Exporter,
description: 'Test Export Module',
format: 'testing',
format: 'testing' as any,
fileExtensions: ['test'],
}, () => {
return new InteropService_Exporter_Custom({
@ -630,7 +630,7 @@ describe('services_InteropService', () => {
const service = InteropService.instance();
service.registerModule(module);
await service.export({
format: 'testing',
format: 'testing' as any,
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 { _ } from '../../locale';
import BaseItem from '../../models/BaseItem';
@ -119,32 +119,32 @@ export default class InteropService {
const exportModules = [
makeExportModule({
format: 'jex',
format: ExportModuleOutputFormat.Jex,
fileExtensions: ['jex'],
target: FileSystemItem.File,
description: _('Joplin Export File'),
}, () => new InteropService_Exporter_Jex()),
makeExportModule({
format: 'raw',
format: ExportModuleOutputFormat.Raw,
target: FileSystemItem.Directory,
description: _('Joplin Export Directory'),
}, () => new InteropService_Exporter_Raw()),
makeExportModule({
format: 'md',
format: ExportModuleOutputFormat.Markdown,
target: FileSystemItem.Directory,
description: _('Markdown'),
}, () => new InteropService_Exporter_Md()),
makeExportModule({
format: 'md_frontmatter',
format: ExportModuleOutputFormat.MarkdownFrontMatter,
target: FileSystemItem.Directory,
description: _('Markdown + Front Matter'),
}, () => new InteropService_Exporter_Md_frontmatter()),
makeExportModule({
format: 'html',
format: ExportModuleOutputFormat.Html,
fileExtensions: ['html', 'htm'],
target: FileSystemItem.File,
isNoteArchive: false,
@ -153,7 +153,7 @@ export default class InteropService {
}, dynamicRequireModuleFactory('./InteropService_Exporter_Html')),
makeExportModule({
format: 'html',
format: ExportModuleOutputFormat.Html,
target: FileSystemItem.Directory,
description: _('HTML Directory'),
supportsMobile: false,
@ -301,7 +301,7 @@ export default class InteropService {
public async export(options: ExportOptions): Promise<ImportExportResult> {
options = {
format: 'jex',
format: ExportModuleOutputFormat.Jex,
...options,
};

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -26,6 +26,17 @@ export enum ImportModuleOutputFormat {
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 {
path?: string;
format?: string;
@ -44,7 +55,7 @@ export enum ExportProgressState {
export type OnExportProgressCallback = (status: ExportProgressState, progress: number)=> void;
export interface ExportOptions {
format?: string;
format?: ExportModuleOutputFormat;
path?: string;
sourceFolderIds?: string[];
sourceNoteIds?: string[];

View File

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