mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-27 10:32:58 +02:00
* Patch to implement feature, exporting notes to JSON (#912, issues/912). * Revising based on feedback * Directly calling JSON.stringify on the item
This commit is contained in:
parent
3a9948e528
commit
0eb18d206d
@ -310,4 +310,25 @@ describe('services_InteropService', function() {
|
||||
expect(note2_2.body.indexOf(note1_2.id) >= 0).toBe(true);
|
||||
}));
|
||||
|
||||
it('should export into json format', asyncTest(async () => {
|
||||
const service = new InteropService();
|
||||
let folder1 = await Folder.save({ title: 'folder1' });
|
||||
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
||||
note1 = await Note.load(note1.id);
|
||||
const filePath = exportDir();
|
||||
|
||||
await service.export({ path: filePath, format: 'json' });
|
||||
|
||||
// verify that the json files exist and can be parsed
|
||||
const items = [folder1, note1];
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const jsonFile = filePath + '/' + items[i].id + '.json';
|
||||
let json = await fs.readFile(jsonFile, 'utf-8');
|
||||
let obj = JSON.parse(json);
|
||||
expect(obj.id).toBe(items[i].id);
|
||||
expect(obj.type_).toBe(items[i].type_);
|
||||
expect(obj.title).toBe(items[i].title);
|
||||
expect(obj.body).toBe(items[i].body);
|
||||
}
|
||||
}));
|
||||
});
|
@ -75,9 +75,14 @@ class BaseItem extends BaseModel {
|
||||
return r.total;
|
||||
}
|
||||
|
||||
static systemPath(itemOrId) {
|
||||
if (typeof itemOrId === 'string') return itemOrId + '.md';
|
||||
return itemOrId.id + '.md';
|
||||
static systemPath(itemOrId, extension = null) {
|
||||
if (extension === null)
|
||||
extension = 'md';
|
||||
|
||||
if (typeof itemOrId === 'string')
|
||||
return itemOrId + '.' + extension;
|
||||
else
|
||||
return itemOrId.id + '.' + extension;
|
||||
}
|
||||
|
||||
static isSystemPath(path) {
|
||||
|
@ -58,6 +58,10 @@ class InteropService {
|
||||
format: 'raw',
|
||||
target: 'directory',
|
||||
description: _('Joplin Export Directory'),
|
||||
}, {
|
||||
format: 'json',
|
||||
target: 'directory',
|
||||
description: _('Json Export Directory'),
|
||||
}, {
|
||||
format: 'md',
|
||||
target: 'directory',
|
||||
|
@ -0,0 +1,30 @@
|
||||
const InteropService_Exporter_Base = require('lib/services/InteropService_Exporter_Base');
|
||||
const { basename, filename } = require('lib/path-utils.js');
|
||||
const { shim } = require('lib/shim');
|
||||
|
||||
class InteropService_Exporter_Json extends InteropService_Exporter_Base {
|
||||
|
||||
async init(destDir) {
|
||||
this.destDir_ = destDir;
|
||||
this.resourceDir_ = destDir ? destDir + '/resources' : null;
|
||||
|
||||
await shim.fsDriver().mkdir(this.destDir_);
|
||||
await shim.fsDriver().mkdir(this.resourceDir_);
|
||||
}
|
||||
|
||||
async processItem(ItemClass, item) {
|
||||
const fileName = ItemClass.systemPath(item, "json");
|
||||
const filePath = this.destDir_ + '/' + fileName;
|
||||
const serialized = JSON.stringify(item);
|
||||
await shim.fsDriver().writeFile(filePath, serialized, 'utf-8');
|
||||
}
|
||||
|
||||
async processResource(resource, filePath) {
|
||||
const destResourcePath = this.resourceDir_ + '/' + basename(filePath);
|
||||
await shim.fsDriver().copy(filePath, destResourcePath);
|
||||
}
|
||||
|
||||
async close() {}
|
||||
}
|
||||
|
||||
module.exports = InteropService_Exporter_Json;
|
Loading…
Reference in New Issue
Block a user