yarle-evernote-to-md
Version:
Yet Another Rope Ladder from Evernote
179 lines • 9.16 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNotesPath = exports.setPaths = exports.clearMdNotesDistDir = exports.clearResourceDistDir = exports.clearResourceDir = exports.getAbsoluteResourceDir = exports.createRootOutputDir = exports.getRelativeResourceDir = exports.getHtmlFileLink = exports.getHtmlFilePath = exports.getJsonFilePath = exports.getMdFilePath = exports.truncatFileName = exports.getResourceDir = exports.paths = void 0;
const fs_1 = __importDefault(require("fs"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const path = __importStar(require("path"));
const yarle_1 = require("../yarle");
const filename_utils_1 = require("./filename-utils");
const output_format_1 = require("./../output-format");
const runtime_properties_1 = require("./../runtime-properties");
const loggerInfo_1 = require("./loggerInfo");
exports.paths = {};
const MAX_PATH = 249;
const getResourceDir = (dstPath, note) => {
return (0, filename_utils_1.getNoteName)(dstPath, note).replace(/\s/g, '_').substr(0, 50);
};
exports.getResourceDir = getResourceDir;
const truncatFileName = (fileName, uniqueId) => {
if (fileName.length <= 11) {
throw Error('FATAL: note folder directory path exceeds the OS limitation. Please pick a destination closer to the root folder.');
}
const fullPath = `${(0, exports.getNotesPath)()}${path.sep}${fileName}`;
return fullPath.length < MAX_PATH ? fileName : `${fileName.slice(0, MAX_PATH - 11)}_${uniqueId}.md`;
};
exports.truncatFileName = truncatFileName;
const truncateFilePath = (note, fileName, fullFilePath) => {
const noteIdNameMap = runtime_properties_1.RuntimePropertiesSingleton.getInstance();
const noteIdMap = noteIdNameMap.getNoteIdNameMapByNoteTitle((0, filename_utils_1.normalizeFilenameString)(note.title))[0] || { uniqueEnd: (0, filename_utils_1.getUniqueId)() };
if (fileName.length <= 11) {
throw Error('FATAL: note folder directory path exceeds the OS limitation. Please pick a destination closer to the root folder.');
}
return `${fullFilePath.slice(0, MAX_PATH - 11)}_${noteIdMap.uniqueEnd}.md`;
// -11 is the nanoid 5 char +_+ the max possible extension of the note (.md vs .html)
};
const getFilePath = (dstPath, note, extension) => {
const fileName = (0, filename_utils_1.getNoteFileName)(dstPath, note, extension);
const fullFilePath = `${dstPath}${path.sep}${(0, filename_utils_1.normalizeFilenameString)(fileName)}`;
return fullFilePath.length < MAX_PATH ? fullFilePath : truncateFilePath(note, fileName, fullFilePath);
};
const getMdFilePath = (note) => {
return getFilePath(exports.paths.mdPath, note, 'md');
};
exports.getMdFilePath = getMdFilePath;
const getJsonFilePath = (note) => {
return getFilePath(exports.paths.mdPath, note, 'json');
};
exports.getJsonFilePath = getJsonFilePath;
const getHtmlFilePath = (note) => {
return getFilePath(exports.paths.resourcePath, note, 'html');
};
exports.getHtmlFilePath = getHtmlFilePath;
const getHtmlFileLink = (note) => {
const filePath = (0, exports.getHtmlFilePath)(note);
const relativePath = `.${filePath.slice(exports.paths.resourcePath.lastIndexOf(path.sep))}`;
if (yarle_1.yarleOptions.posixHtmlPath && path.sep !== path.posix.sep) {
return relativePath.split(path.sep).join(path.posix.sep);
}
return relativePath;
};
exports.getHtmlFileLink = getHtmlFileLink;
const clearDistDir = (dstPath) => {
if (fs_1.default.existsSync(dstPath)) {
fs_extra_1.default.removeSync(dstPath);
}
fs_1.default.mkdirSync(dstPath);
};
const getRelativeResourceDir = (note) => {
const enexFolder = `${path.sep}${yarle_1.yarleOptions.resourcesDir}`;
if (yarle_1.yarleOptions.haveGlobalResources) {
return `..${enexFolder}`;
}
return yarle_1.yarleOptions.haveEnexLevelResources
? `.${enexFolder}`
: `.${enexFolder}${path.sep}${(0, exports.getResourceDir)(exports.paths.mdPath, note)}.resources`;
};
exports.getRelativeResourceDir = getRelativeResourceDir;
const createRootOutputDir = () => {
const outputDir = path.isAbsolute(yarle_1.yarleOptions.outputDir)
? yarle_1.yarleOptions.outputDir
: `${process.cwd()}${path.sep}${yarle_1.yarleOptions.outputDir}`;
fs_extra_1.default.mkdirsSync(outputDir);
};
exports.createRootOutputDir = createRootOutputDir;
const getAbsoluteResourceDir = (note) => {
if (yarle_1.yarleOptions.haveGlobalResources) {
return path.resolve(exports.paths.resourcePath, '..', '..', yarle_1.yarleOptions.resourcesDir);
}
return yarle_1.yarleOptions.haveEnexLevelResources
? exports.paths.resourcePath
: `${exports.paths.resourcePath}${path.sep}${(0, exports.getResourceDir)(exports.paths.mdPath, note)}.resources`;
};
exports.getAbsoluteResourceDir = getAbsoluteResourceDir;
const resourceDirClears = new Map();
const clearResourceDir = (note) => {
const resPath = (0, exports.getAbsoluteResourceDir)(note);
if (!resourceDirClears.has(resPath)) {
resourceDirClears.set(resPath, 0);
}
const clears = resourceDirClears.get(resPath);
// we're sharing a resource dir, so we can can't clean it more than once
if ((yarle_1.yarleOptions.haveEnexLevelResources || yarle_1.yarleOptions.haveGlobalResources) && clears >= 1) {
return;
}
clearDistDir(resPath);
resourceDirClears.set(resPath, clears + 1);
};
exports.clearResourceDir = clearResourceDir;
const clearResourceDistDir = () => {
clearDistDir(exports.paths.resourcePath);
};
exports.clearResourceDistDir = clearResourceDistDir;
const clearMdNotesDistDir = () => {
clearDistDir(exports.paths.mdPath);
};
exports.clearMdNotesDistDir = clearMdNotesDistDir;
const setPaths = (enexSource) => {
// loggerInfo('setting paths');
const enexFolder = enexSource.split(path.sep);
// loggerInfo(`enex folder split: ${JSON.stringify(enexFolder)}`);
let enexFile = (enexFolder.length >= 1 ? enexFolder[enexFolder.length - 1] : enexFolder[0]).split(/.enex$/)[0];
enexFile = (0, filename_utils_1.normalizeFilenameString)(enexFile);
// loggerInfo(`enex file: ${enexFile}`);
const outputDir = path.isAbsolute(yarle_1.yarleOptions.outputDir)
? yarle_1.yarleOptions.outputDir
: `${process.cwd()}${path.sep}${yarle_1.yarleOptions.outputDir}`;
exports.paths.mdPath = `${outputDir}${path.sep}notes${path.sep}`;
exports.paths.resourcePath = `${outputDir}${path.sep}notes${path.sep}${yarle_1.yarleOptions.resourcesDir}`;
// loggerInfo(`Skip enex filename from output? ${yarleOptions.skipEnexFileNameFromOutputPath}`);
if (!yarle_1.yarleOptions.skipEnexFileNameFromOutputPath) {
exports.paths.mdPath = `${exports.paths.mdPath}${enexFile}`;
// loggerInfo(`mdPath: ${paths.mdPath}`);
exports.paths.resourcePath = `${outputDir}${path.sep}notes${path.sep}${enexFile}${path.sep}${yarle_1.yarleOptions.resourcesDir}`;
}
if (yarle_1.yarleOptions.outputFormat === output_format_1.OutputFormat.LogSeqMD) {
const folderName = yarle_1.yarleOptions.logseqSettings.journalNotes ? 'journal' : 'pages';
exports.paths.mdPath = `${outputDir}${path.sep}${folderName}${path.sep}`;
exports.paths.resourcePath = `${outputDir}${path.sep}${yarle_1.yarleOptions.resourcesDir}`;
}
fs_extra_1.default.mkdirsSync(exports.paths.mdPath);
if ((!yarle_1.yarleOptions.haveEnexLevelResources && !yarle_1.yarleOptions.haveGlobalResources) ||
yarle_1.yarleOptions.outputFormat === output_format_1.OutputFormat.LogSeqMD) {
fs_extra_1.default.mkdirsSync(exports.paths.resourcePath);
}
(0, loggerInfo_1.loggerInfo)(`path ${exports.paths.mdPath} created`);
// clearDistDir(paths.simpleMdPath);
// clearDistDir(paths.complexMdPath);
};
exports.setPaths = setPaths;
const getNotesPath = () => {
return exports.paths.mdPath;
};
exports.getNotesPath = getNotesPath;
//# sourceMappingURL=folder-utils.js.map