Skip to content

Commit

Permalink
Patch to implement feature, exporting notes to JSON (laurent22#912, i…
Browse files Browse the repository at this point in the history
…ssues/912).
  • Loading branch information
Ben Fisher committed Oct 28, 2018
1 parent 4b4d0e8 commit ad88b64
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
21 changes: 21 additions & 0 deletions CliClient/tests/services_InteropService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.props.id).toBe(items[i].id);
expect(obj.props.type_).toBe(items[i].type_);
expect(obj.title).toBe(items[i].title);
expect(obj.body).toBe(items[i].body);
}
}));
});
4 changes: 4 additions & 0 deletions ReactNativeClient/lib/services/InteropService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
80 changes: 80 additions & 0 deletions ReactNativeClient/lib/services/InteropService_Exporter_Json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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 obj = await this.buildPlainObjectForJson_(ItemClass, item);
const fileName = this.getNameWithDifferentExtension_(ItemClass, item, ".json");
const filePath = this.destDir_ + '/' + fileName;
const serialized = JSON.stringify(obj);
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() {}

async buildPlainObjectForJson_(ItemClass, item) {
let output = {};
let shownKeys = ItemClass.fieldNames();
shownKeys.push('type_');

item = ItemClass.filter(item);

if ('title' in item && shownKeys.indexOf('title') >= 0) {
output.title = item.title;
}

if ('body' in item && shownKeys.indexOf('body') >= 0) {
output.body = item.body;
}

output.props = {};

for (let i = 0; i < shownKeys.length; i++) {
let key = shownKeys[i];
if (key == 'title' || key == 'body') continue;

let value = null;
if (typeof key === 'function') {
let r = await key();
key = r.key;
value = r.value;
} else {
value = ItemClass.serialize_format(key, item[key]);
}

output.props[key] = value;
}

return output;
}

getNameWithDifferentExtension_(ItemClass, item, newExtension) {
// e.g., from abc.md to abc.json
const fileName = ItemClass.systemPath(item);
const split = fileName.split(/\./g);
if (split.length === 2) {
return split[0] + newExtension;
} else {
ItemClass.logger().warn("Expected systemPath to look like 'abc.md', but got", fileName);
ItemClass.logger().warn("we'll export to", fileName + newExtension);
return fileName + newExtension;
}
}
}

module.exports = InteropService_Exporter_Json;

0 comments on commit ad88b64

Please sign in to comment.