2020-10-09 19:35:46 +02:00
|
|
|
const InteropService_Exporter_Base = require('lib/services/interop/InteropService_Exporter_Base').default;
|
2020-02-08 01:36:25 +02:00
|
|
|
const { basename, dirname, friendlySafeFilename } = require('lib/path-utils.js');
|
2018-09-04 12:59:09 +02:00
|
|
|
const BaseModel = require('lib/BaseModel');
|
|
|
|
const Folder = require('lib/models/Folder');
|
|
|
|
const Note = require('lib/models/Note');
|
2020-10-09 19:35:46 +02:00
|
|
|
const shim = require('lib/shim').default;
|
|
|
|
const markdownUtils = require('lib/markdownUtils').default;
|
2018-09-04 12:59:09 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
export default class InteropService_Exporter_Md extends InteropService_Exporter_Base {
|
|
|
|
async init(destDir:string) {
|
2018-09-04 12:59:09 +02:00
|
|
|
this.destDir_ = destDir;
|
2019-09-19 23:51:18 +02:00
|
|
|
this.resourceDir_ = destDir ? `${destDir}/_resources` : null;
|
2018-09-04 12:59:09 +02:00
|
|
|
this.createdDirs_ = [];
|
|
|
|
|
|
|
|
await shim.fsDriver().mkdir(this.destDir_);
|
|
|
|
await shim.fsDriver().mkdir(this.resourceDir_);
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
async makeDirPath_(item:any, pathPart:string = null, findUniqueFilename:boolean = true) {
|
2018-09-04 12:59:09 +02:00
|
|
|
let output = '';
|
|
|
|
while (true) {
|
|
|
|
if (item.type_ === BaseModel.TYPE_FOLDER) {
|
2018-11-21 02:36:23 +02:00
|
|
|
if (pathPart) {
|
2019-09-19 23:51:18 +02:00
|
|
|
output = `${pathPart}/${output}`;
|
2018-11-21 02:36:23 +02:00
|
|
|
} else {
|
2019-09-19 23:51:18 +02:00
|
|
|
output = `${friendlySafeFilename(item.title, null, true)}/${output}`;
|
2020-07-12 20:13:08 +02:00
|
|
|
if (findUniqueFilename) output = await shim.fsDriver().findUniqueFilename(output);
|
2018-11-21 02:36:23 +02:00
|
|
|
}
|
2018-09-04 12:59:09 +02:00
|
|
|
}
|
|
|
|
if (!item.parent_id) return output;
|
|
|
|
item = await Folder.load(item.parent_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
async relaceLinkedItemIdsByRelativePaths_(item:any) {
|
2020-01-18 15:16:14 +02:00
|
|
|
const relativePathToRoot = await this.makeDirPath_(item, '..');
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const newBody = await this.replaceResourceIdsByRelativePaths_(item.body, relativePathToRoot);
|
2020-01-18 15:16:14 +02:00
|
|
|
return await this.replaceNoteIdsByRelativePaths_(newBody, relativePathToRoot);
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
async replaceResourceIdsByRelativePaths_(noteBody:string, relativePathToRoot:string) {
|
2020-01-18 15:16:14 +02:00
|
|
|
const linkedResourceIds = await Note.linkedResourceIds(noteBody);
|
2018-11-21 02:36:23 +02:00
|
|
|
const resourcePaths = this.context() && this.context().resourcePaths ? this.context().resourcePaths : {};
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
const createRelativePath = function(resourcePath:string) {
|
2020-01-18 15:16:14 +02:00
|
|
|
return `${relativePathToRoot}_resources/${basename(resourcePath)}`;
|
|
|
|
};
|
|
|
|
return await this.replaceItemIdsByRelativePaths_(noteBody, linkedResourceIds, resourcePaths, createRelativePath);
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
async replaceNoteIdsByRelativePaths_(noteBody:string, relativePathToRoot:string) {
|
2020-01-18 15:16:14 +02:00
|
|
|
const linkedNoteIds = await Note.linkedNoteIds(noteBody);
|
|
|
|
const notePaths = this.context() && this.context().notePaths ? this.context().notePaths : {};
|
2018-11-21 02:36:23 +02:00
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
const createRelativePath = function(notePath:string) {
|
2020-09-22 13:06:19 +02:00
|
|
|
return markdownUtils.escapeLinkUrl(`${relativePathToRoot}${notePath}`.trim());
|
2020-01-18 15:16:14 +02:00
|
|
|
};
|
|
|
|
return await this.replaceItemIdsByRelativePaths_(noteBody, linkedNoteIds, notePaths, createRelativePath);
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
async replaceItemIdsByRelativePaths_(noteBody:string, linkedItemIds:string[], paths:any, fn_createRelativePath:Function) {
|
2020-01-18 15:16:14 +02:00
|
|
|
let newBody = noteBody;
|
|
|
|
|
|
|
|
for (let i = 0; i < linkedItemIds.length; i++) {
|
|
|
|
const id = linkedItemIds[i];
|
2020-03-14 01:46:14 +02:00
|
|
|
const itemPath = fn_createRelativePath(paths[id]);
|
2020-01-18 15:16:14 +02:00
|
|
|
newBody = newBody.replace(new RegExp(`:/${id}`, 'g'), itemPath);
|
2018-11-21 02:36:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return newBody;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
async prepareForProcessingItemType(itemType:number, itemsToExport:any[]) {
|
|
|
|
if (itemType === BaseModel.TYPE_NOTE) {
|
2020-01-18 15:16:14 +02:00
|
|
|
// Create unique file path for the note
|
2020-10-09 19:35:46 +02:00
|
|
|
const context:any = {
|
2020-01-18 15:16:14 +02:00
|
|
|
notePaths: {},
|
|
|
|
};
|
|
|
|
for (let i = 0; i < itemsToExport.length; i++) {
|
|
|
|
const itemType = itemsToExport[i].type;
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
if (itemType !== itemType) continue;
|
2020-01-18 15:16:14 +02:00
|
|
|
|
|
|
|
const itemOrId = itemsToExport[i].itemOrId;
|
|
|
|
const note = typeof itemOrId === 'object' ? itemOrId : await Note.load(itemOrId);
|
|
|
|
|
|
|
|
if (!note) continue;
|
|
|
|
|
2020-07-12 20:13:08 +02:00
|
|
|
let notePath = `${await this.makeDirPath_(note, null, false)}${friendlySafeFilename(note.title, null, true)}.md`;
|
2020-01-18 15:16:14 +02:00
|
|
|
notePath = await shim.fsDriver().findUniqueFilename(`${this.destDir_}/${notePath}`, Object.values(context.notePaths));
|
|
|
|
context.notePaths[note.id] = notePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip the absolute path to export dir and keep only the relative paths
|
|
|
|
const destDir = this.destDir_;
|
|
|
|
Object.keys(context.notePaths).map(function(id) {
|
|
|
|
context.notePaths[id] = context.notePaths[id].substr(destDir.length + 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.updateContext(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
async processItem(_itemType:number, item:any) {
|
2018-09-04 12:59:09 +02:00
|
|
|
if ([BaseModel.TYPE_NOTE, BaseModel.TYPE_FOLDER].indexOf(item.type_) < 0) return;
|
|
|
|
|
2020-01-18 15:16:14 +02:00
|
|
|
if (item.type_ === BaseModel.TYPE_FOLDER) {
|
|
|
|
const dirPath = `${this.destDir_}/${await this.makeDirPath_(item)}`;
|
2018-09-04 12:59:09 +02:00
|
|
|
|
2020-01-18 15:16:14 +02:00
|
|
|
if (this.createdDirs_.indexOf(dirPath) < 0) {
|
|
|
|
await shim.fsDriver().mkdir(dirPath);
|
|
|
|
this.createdDirs_.push(dirPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (item.type_ === BaseModel.TYPE_NOTE) {
|
|
|
|
const notePaths = this.context() && this.context().notePaths ? this.context().notePaths : {};
|
2020-03-14 01:46:14 +02:00
|
|
|
const noteFilePath = `${this.destDir_}/${notePaths[item.id]}`;
|
2018-09-04 12:59:09 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const noteBody = await this.relaceLinkedItemIdsByRelativePaths_(item);
|
2018-11-21 02:36:23 +02:00
|
|
|
const modNote = Object.assign({}, item, { body: noteBody });
|
|
|
|
const noteContent = await Note.serializeForEdit(modNote);
|
2020-02-08 01:36:25 +02:00
|
|
|
await shim.fsDriver().mkdir(dirname(noteFilePath));
|
2018-09-04 12:59:09 +02:00
|
|
|
await shim.fsDriver().writeFile(noteFilePath, noteContent, 'utf-8');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:35:46 +02:00
|
|
|
async processResource(_resource:any, filePath:string) {
|
2019-09-19 23:51:18 +02:00
|
|
|
const destResourcePath = `${this.resourceDir_}/${basename(filePath)}`;
|
2018-09-04 12:59:09 +02:00
|
|
|
await shim.fsDriver().copy(filePath, destResourcePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
async close() {}
|
|
|
|
}
|