yarle-evernote-to-md
Version:
Yet Another Rope Ladder from Evernote
141 lines • 5.61 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTanaOutput = void 0;
const fs = __importStar(require("fs"));
const _ = __importStar(require("lodash"));
const get_all_output_files_1 = require("../get-all-output-files");
const create_new_tana_file_1 = require("./create-new-tana-file");
const create_tana_node_1 = require("./create-tana-node");
const tanaNoteFileName = 'notes-in-TIF.json';
const createTanaOutput = (options, outputNotebookFolders) => {
const allconvertedFiles = [];
for (const outputFolder of outputNotebookFolders) {
(0, get_all_output_files_1.getAllOutputFilesWithExtension)(outputFolder, allconvertedFiles, 'json');
}
for (const convertedFile of allconvertedFiles) {
// load and parse mergedTanaNotes, or create if
const mergedNotes = getMergedTanaNotes(options);
const convertedTanaNote = JSON.parse(fs.readFileSync(convertedFile, 'UTF-8'));
updateMergedNotes(mergedNotes, convertedTanaNote);
saveMergedTanaNotes(options, mergedNotes);
}
};
exports.createTanaOutput = createTanaOutput;
const cleanChildren = (nodes) => {
const cleanNodes = _.cloneDeep(nodes);
for (const node of cleanNodes)
node.children = [];
return cleanNodes;
};
const updateMergedNotes = (mergedNotes, convertedTanaNote) => {
mergedNotes.nodes.push(convertedTanaNote.nodes[0]);
const fieldNodes = addFieldNodesToRoot(convertedTanaNote.nodes[0]);
mergedNotes.attributes.push(...getAttributes(fieldNodes));
mergedNotes.nodes.push(...cleanChildren(fieldNodes));
// handling attributes
//handling supertags
if (convertedTanaNote.supertags) {
for (const supertag of convertedTanaNote.supertags) {
if (!mergedNotes.supertags.find(mergedSupertag => mergedSupertag.uid === supertag.uid))
mergedNotes.supertags.push(supertag);
}
}
mergedNotes.summary.leafNodes += convertedTanaNote.summary.leafNodes;
mergedNotes.summary.totalNodes += convertedTanaNote.summary.totalNodes;
mergedNotes.summary.topLevelNodes += 1;
mergedNotes.summary.fields = fieldNodes.length;
};
const getAttributes = (fieldNodes) => {
const fieldAttributes = [];
for (const field of fieldNodes) {
for (const valueChild of field.children) {
const storedAttribute = fieldAttributes.find(attribute => attribute.name === field.name);
if (storedAttribute) {
storedAttribute.count += 1;
storedAttribute.values.push(valueChild.name);
}
else {
fieldAttributes.push(createNewTanaAttribute(field.name, valueChild.name));
}
}
}
return fieldAttributes;
};
const createNewTanaAttribute = (name, value) => {
return {
name,
values: [value],
count: 1,
};
};
//
const addFieldNodesToRoot = (tanaNode) => {
const fields = [];
deepFind([tanaNode], (node) => {
return node.type === 'field';
}, fields);
//return fields || []
const fieldNodes = [];
for (const field of fields) {
const foundField = fieldNodes.find(fieldNode => fieldNode.name === field.name);
if (foundField) {
field.refs.push(foundField.uid);
foundField.children.push(...field.children);
}
else {
const node = (0, create_tana_node_1.createTanaNode)('node', field.name, { createdAt: '' + new Date(field.createdAt).toISOString().slice(0, 10), updatedAt: '' + new Date(field.editedAt).toISOString().slice(0, 10), }, undefined);
node.children = [...field.children];
fieldNodes.push(node);
field.refs.push(node.uid);
}
}
return fieldNodes;
};
const deepFind = (arr, searchFuncion, fields) => {
for (const obj of arr) {
if (searchFuncion(obj)) {
fields.push(obj);
}
if (obj.children) {
deepFind(obj.children, searchFuncion, fields);
}
}
return null;
};
const getMergedTanaNotes = (options) => {
let mergedTanaNote;
try {
mergedTanaNote = JSON.parse(fs.readFileSync(`${options.outputDir}/${tanaNoteFileName}`, 'UTF-8'));
}
catch (error) {
mergedTanaNote = (0, create_new_tana_file_1.createNewTanaFile)();
}
return mergedTanaNote;
};
const saveMergedTanaNotes = (options, mergedNotes) => {
fs.writeFileSync(`${options.outputDir}/${tanaNoteFileName}`, JSON.stringify(mergedNotes));
};
//# sourceMappingURL=create-tana-output.js.map