mirror of https://github.com/laurent22/joplin.git
All: Fixes #718: Allow recursively importing Markdown folder
parent
d35e3163ca
commit
756f3e627c
|
@ -21,37 +21,55 @@ class InteropService_Importer_Md extends InteropService_Importer_Base {
|
||||||
async exec(result) {
|
async exec(result) {
|
||||||
let parentFolderId = null;
|
let parentFolderId = null;
|
||||||
|
|
||||||
const supportedFileExtension = this.metadata().fileExtensions;
|
const sourcePath = rtrimSlashes(this.sourcePath_);
|
||||||
|
|
||||||
const filePaths = [];
|
const filePaths = [];
|
||||||
if (await shim.fsDriver().isDirectory(this.sourcePath_)) {
|
if (await shim.fsDriver().isDirectory(sourcePath)) {
|
||||||
const stats = await shim.fsDriver().readDirStats(this.sourcePath_);
|
|
||||||
for (let i = 0; i < stats.length; i++) {
|
|
||||||
const stat = stats[i];
|
|
||||||
if (supportedFileExtension.indexOf(fileExtension(stat.path).toLowerCase()) >= 0) {
|
|
||||||
filePaths.push(this.sourcePath_ + '/' + stat.path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.options_.destinationFolder) {
|
if (!this.options_.destinationFolder) {
|
||||||
const folderTitle = await Folder.findUniqueItemTitle(basename(rtrimSlashes(this.sourcePath_)));
|
const folderTitle = await Folder.findUniqueItemTitle(basename(sourcePath));
|
||||||
const folder = await Folder.save({ title: folderTitle });
|
const folder = await Folder.save({ title: folderTitle });
|
||||||
parentFolderId = folder.id;
|
parentFolderId = folder.id;
|
||||||
} else {
|
} else {
|
||||||
parentFolderId = this.options_.destinationFolder.id;
|
parentFolderId = this.options_.destinationFolder.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.importDirectory(sourcePath, parentFolderId);
|
||||||
} else {
|
} else {
|
||||||
if (!this.options_.destinationFolder) throw new Error(_('Please specify the notebook where the notes should be imported to.'));
|
if (!this.options_.destinationFolder) throw new Error(_('Please specify the notebook where the notes should be imported to.'));
|
||||||
parentFolderId = this.options_.destinationFolder.id
|
parentFolderId = this.options_.destinationFolder.id
|
||||||
filePaths.push(this.sourcePath_);
|
filePaths.push(sourcePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let i = 0; i < filePaths.length; i++) {
|
for (let i = 0; i < filePaths.length; i++) {
|
||||||
const path = filePaths[i];
|
await this.importFile(filePaths[i], parentFolderId);
|
||||||
const stat = await shim.fsDriver().stat(path);
|
}
|
||||||
if (!stat) throw new Error('Cannot read ' + path);
|
|
||||||
const title = filename(path);
|
return result;
|
||||||
const body = await shim.fsDriver().readFile(path);
|
}
|
||||||
|
|
||||||
|
async importDirectory(dirPath, parentFolderId) {
|
||||||
|
console.info('Import: ' + dirPath);
|
||||||
|
|
||||||
|
const supportedFileExtension = this.metadata().fileExtensions;
|
||||||
|
const stats = await shim.fsDriver().readDirStats(dirPath);
|
||||||
|
for (let i = 0; i < stats.length; i++) {
|
||||||
|
const stat = stats[i];
|
||||||
|
|
||||||
|
if (stat.isDirectory()) {
|
||||||
|
const folderTitle = await Folder.findUniqueItemTitle(basename(stat.path));
|
||||||
|
const folder = await Folder.save({ title: folderTitle, parent_id: parentFolderId });
|
||||||
|
this.importDirectory(dirPath + '/' + basename(stat.path), folder.id);
|
||||||
|
} else if (supportedFileExtension.indexOf(fileExtension(stat.path).toLowerCase()) >= 0) {
|
||||||
|
this.importFile(dirPath + '/' + stat.path, parentFolderId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async importFile(filePath, parentFolderId) {
|
||||||
|
const stat = await shim.fsDriver().stat(filePath);
|
||||||
|
if (!stat) throw new Error('Cannot read ' + filePath);
|
||||||
|
const title = filename(filePath);
|
||||||
|
const body = await shim.fsDriver().readFile(filePath);
|
||||||
const note = {
|
const note = {
|
||||||
parent_id: parentFolderId,
|
parent_id: parentFolderId,
|
||||||
title: title,
|
title: title,
|
||||||
|
@ -64,9 +82,6 @@ class InteropService_Importer_Md extends InteropService_Importer_Base {
|
||||||
await Note.save(note, { autoTimestamp: false });
|
await Note.save(note, { autoTimestamp: false });
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = InteropService_Importer_Md;
|
module.exports = InteropService_Importer_Md;
|
Loading…
Reference in New Issue