UNPKG

yarle-evernote-to-md

Version:

Yet Another Rope Ladder from Evernote

126 lines 6.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convert2TanaNode = exports.convertChild = exports.cleanTanaContent = void 0; const runtime_properties_1 = require("../../runtime-properties"); const constants_1 = require("../../constants"); const create_new_tana_file_1 = require("./create-new-tana-file"); const create_tana_node_1 = require("./create-tana-node"); const cleanTanaContent = (content, valueToClean) => { return content.replace(valueToClean, ''); }; exports.cleanTanaContent = cleanTanaContent; const convertString2TanaNode = (content, data, addTags = false) => { const linkNameMap = runtime_properties_1.RuntimePropertiesSingleton.getInstance(); const link = linkNameMap.getNoteIdNameMapByNoteTitle(content); const uid = link && link[0] ? link[0].uniqueEnd : 'uuid' + Math.random(); const tanaNode = (0, create_tana_node_1.createTanaNode)('node', content, data, uid); if (addTags) { const tags = JSON.parse(data.tags); tanaNode.supertags = tags; } return tanaNode; }; const convertString2TanaCheckbox = (content, todoState, data) => { const checkboxNode = (0, create_tana_node_1.createTanaNode)('node', (0, exports.cleanTanaContent)(content, todoState === 'todo' ? constants_1.checkboxTodo : constants_1.checkboxDone), data, undefined); checkboxNode.todoState = todoState; return checkboxNode; }; const convertString2TanaCodeblock = (content, data) => { return (0, create_tana_node_1.createTanaNode)('codeblock', (0, exports.cleanTanaContent)(content, constants_1.tanaCodeBlock), data, undefined); }; const splitAndCleanArray = (content, splitBy) => { return content.split(splitBy).filter(contentElem => contentElem !== ''); }; const convertString2TanaTable = (content, data) => { const mainTableNode = (0, create_tana_node_1.createTanaNode)('node', 'Table', data, undefined); const rows = splitAndCleanArray(content, constants_1.tanaTableRowBlock); const columns = []; for (const [rowIndex, row] of rows.entries()) { const cols = splitAndCleanArray(row, constants_1.tanaTableColBlock); if (rowIndex === 0) { // create fields for this first row for (const col of cols) { columns.push((0, create_tana_node_1.createTanaNode)('field', col, data, undefined)); } } else { let rowNode; for (const [index, col] of cols.entries()) { if (index === 0) { const rowItems = row.split(constants_1.tanaTableColBlock); rowNode = (0, create_tana_node_1.createTanaNode)('node', rowItems && rowItems.length >= 1 ? rowItems[1] : '', data, undefined); } else { const cellNode = (0, create_tana_node_1.createTanaNode)('node', col, data, undefined); const columnField = Object.assign(Object.assign({}, columns[index]), { uid: 'uid' + Math.random() }); columnField.children = []; columnField.children.push(cellNode); rowNode.children.push(columnField); } //rowNode.children.push(cellNode) } mainTableNode.children.push(rowNode); } } return mainTableNode; }; const convertChild = (data, child) => { // if it is a link const regex = /\evernote:\/\/[^)]*\)/g; const found = child.match(regex); const linkNameMap = runtime_properties_1.RuntimePropertiesSingleton.getInstance(); let convertedChild = child.includes(constants_1.tanaCodeBlock) ? convertString2TanaCodeblock(child, data) : convertString2TanaNode(child, data); if (child.startsWith(constants_1.checkboxTodo)) convertedChild = convertString2TanaCheckbox(child, 'todo', data); if (child.startsWith(constants_1.checkboxDone)) convertedChild = convertString2TanaCheckbox(child, 'done', data); if (child.startsWith(constants_1.tanaTableBlock)) convertedChild = convertString2TanaTable(child.replace(new RegExp(constants_1.tanaTableBlock, 'g'), ''), data); if (found && found.length > 0) { for (const link of found) { const pureLink = link.slice(0, -1); const linkItem = linkNameMap.getNoteIdNameMap()[pureLink]; convertedChild.refs.push(linkItem.uniqueEnd); convertedChild.name = convertedChild.name.replace(pureLink, `[[${linkItem.uniqueEnd}]]`); } } return convertedChild; }; exports.convertChild = convertChild; const convert2TanaNode = (data) => { const firstLevelChildren = data.appliedMarkdownContent.replace(/(\n|\r\n)+/g, '\n').split('\n'); const rootContent = data.title; const childrenNodes = []; const parentCandidates = []; const levelRecognizerRegexp = new RegExp("^\t+"); for (const child of firstLevelChildren) { const matchedLevel = child.match(levelRecognizerRegexp); const convertedChild = (0, exports.convertChild)(data, child); const deepLevel = (matchedLevel === null || matchedLevel === void 0 ? void 0 : matchedLevel[0]) ? matchedLevel === null || matchedLevel === void 0 ? void 0 : matchedLevel[0].split('').length : 0; if (deepLevel > 0) { convertedChild.name = convertedChild.name.replace(levelRecognizerRegexp, ''); parentCandidates[deepLevel - 1].children.push(convertedChild); } else childrenNodes.push(convertedChild); parentCandidates[deepLevel] = convertedChild; } const tif = Object.assign(Object.assign({}, (0, create_new_tana_file_1.createNewTanaFile)()), { nodes: [Object.assign(Object.assign({}, convertString2TanaNode(rootContent, data, data.tags ? true : false)), { children: childrenNodes })], summary: { leafNodes: childrenNodes.filter(child => { var _a; return ((_a = child.children) === null || _a === void 0 ? void 0 : _a.length) === 0; }).length, topLevelNodes: 0, totalNodes: childrenNodes.length + 1, calendarNodes: 0, fields: 0, brokenRefs: 0, } }); if (data.tags) { const tags = JSON.parse(data.tags); const superTags = tags.map((tag) => { return { uid: tag, name: tag }; }); tif.supertags = superTags; } return tif; }; exports.convert2TanaNode = convert2TanaNode; //# sourceMappingURL=convert-to-tana-node.js.map