UNPKG

yarle-evernote-to-md

Version:

Yet Another Rope Ladder from Evernote

141 lines 8.09 kB
"use strict"; 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyLinks = void 0; /* istanbul ignore file */ // tslint:disable:no-console const fs = __importStar(require("fs")); const path = __importStar(require("path")); const runtime_properties_1 = require("./../runtime-properties"); const folder_utils_1 = require("./folder-utils"); const escape_string_regexp_1 = require("./escape-string-regexp"); const get_all_output_files_1 = require("./get-all-output-files"); const is_tana_output_1 = require("./tana/is-tana-output"); const file_utils_1 = require("./file-utils"); const filename_utils_1 = require("./filename-utils"); const LanguageFactory_1 = require("./../outputLanguages/LanguageFactory"); const constants_1 = require("./../constants"); const output_format_1 = require("./../output-format"); const applyLinks = (options, outputNotebookFolders) => { var _a; const linkNameMap = runtime_properties_1.RuntimePropertiesSingleton.getInstance(); const allLinks = linkNameMap.getAllNoteIdNameMap(); const allconvertedFiles = []; for (const outputFolder of outputNotebookFolders) { (0, get_all_output_files_1.getAllOutputFilesWithExtension)(outputFolder, allconvertedFiles, undefined); } for (const [linkName, linkProps] of Object.entries(allLinks)) { const uniqueId = linkProps.uniqueEnd; let fileName = linkProps['title']; if (allconvertedFiles.find(fn => fn.includes(uniqueId))) { fileName = (0, folder_utils_1.truncatFileName)(fileName, uniqueId); } if (options.useLevenshteinForLinks) { const fileNames = allconvertedFiles.map(convertedFileName => { return convertedFileName.split(path.sep).reverse()[0].split('.')[0]; }); fileName = (0, filename_utils_1.getClosestFileName)(fileName, fileNames); } const notebookName = linkProps['notebookName']; const encodedFileName = options.urlEncodeFileNamesAndLinks ? encodeURI(fileName) : fileName; let linkDoesntExist = !allconvertedFiles.find(convertedFile => convertedFile.endsWith(`${encodedFileName}.md`)); for (const notebookFolder of outputNotebookFolders) { let realFileName = encodedFileName; let realFileNameInContent = encodedFileName; if (notebookName && (!notebookFolder.endsWith(notebookName) || options.outputFormat === output_format_1.OutputFormat.LogSeqMD)) { realFileName = `${notebookName}${encodedFileName}`; realFileNameInContent = `${notebookName}/${encodedFileName}`; } const filesInOutputDir = fs.readdirSync(notebookFolder); console.log(`Files in output dir: ${JSON.stringify(filesInOutputDir)}`); console.log(`notebookFolder: ${notebookFolder}`); console.log(`realFileName: ${realFileName}`); const langaugeFactory = new LanguageFactory_1.LanguageFactory(); const targetLanguage = langaugeFactory.createLanguage(options.outputFormat); const extension = targetLanguage.noteExtension; const targetFiles = filesInOutputDir.filter(file => { return path.extname(file).toLowerCase() === extension; }); for (const targetFile of targetFiles) { const fileContent = fs.readFileSync(`${notebookFolder}${path.sep}${targetFile}`, 'UTF-8'); let updatedContent = fileContent; if ((0, is_tana_output_1.isTanaOutput)()) { const tanaNote = JSON.parse(updatedContent); const linkedNode = (_a = tanaNote.nodes) === null || _a === void 0 ? void 0 : _a.find((note) => note.name === realFileNameInContent); if (linkedNode) { const linkItem = linkNameMap.getNoteIdNameMapByNoteTitle(realFileNameInContent); linkedNode.uid = linkItem[0].uniqueEnd; updatedContent = JSON.stringify(tanaNote); } } if (options.keepEvernoteLinkIfNoNoteFound) { if (linkDoesntExist) { const regexp = new RegExp(`<YARLE_EVERNOTE_LINK>(.)*<-->`); // replace updatedContent = updatedContent.replace(regexp, ''); updatedContent = updatedContent.replace('</YARLE_EVERNOTE_LINK>', ''); } else { const regexp = new RegExp(`<-->(.)*<\/YARLE_EVERNOTE_LINK>`); // replace updatedContent = updatedContent.replace(regexp, ''); updatedContent = updatedContent.replace('<YARLE_EVERNOTE_LINK>', ''); } } else { const regexp = new RegExp((0, escape_string_regexp_1.escapeStringRegexp)(linkName), 'g'); updatedContent = updatedContent.replace(regexp, realFileNameInContent); } if ((`${fileName}.md` === targetFile || targetFile === linkProps.title) && linkProps.noteName === constants_1.TOCNoteName && (notebookFolder.endsWith(notebookName) || options.outputFormat === output_format_1.OutputFormat.LogSeqMD)) { // TODO APPLY EVERNOTE LINK const evernoteInternalLinkPlaceholderRegExp = new RegExp('<YARLE_EVERNOTE_LINK_PLACEHOLDER>', 'g'); updatedContent = updatedContent.replace(evernoteInternalLinkPlaceholderRegExp, linkProps['url']); // TODO APPLY EVERNOTE GUID const evernoteGuidPlaceholderRegExp = new RegExp('<YARLE_EVERNOTE_GUID_PLACEHOLDER>', 'g'); updatedContent = updatedContent.replace(evernoteGuidPlaceholderRegExp, linkProps['guid']); } if (fileContent !== updatedContent) { const filePath = `${notebookFolder}${path.sep}${targetFile}`; (0, file_utils_1.updateFileContentSafely)(filePath, updatedContent); } } } } const unrecognizable = "Unrecognizable"; for (const targetFile of allconvertedFiles) { const fileContent = fs.readFileSync(targetFile, 'UTF-8'); // TODO APPLY EVERNOTE LINK const evernoteInternalLinkPlaceholderRegExp = new RegExp('<YARLE_EVERNOTE_LINK_PLACEHOLDER>', 'g'); let updatedContent = fileContent.replace(evernoteInternalLinkPlaceholderRegExp, unrecognizable); // TODO APPLY EVERNOTE GUID const evernoteGuidPlaceholderRegExp = new RegExp('<YARLE_EVERNOTE_GUID_PLACEHOLDER>', 'g'); updatedContent = updatedContent.replace(evernoteGuidPlaceholderRegExp, unrecognizable); if (fileContent !== updatedContent) { (0, file_utils_1.updateFileContentSafely)(targetFile, updatedContent); } } }; exports.applyLinks = applyLinks; //# sourceMappingURL=apply-links.js.map