UNPKG

yarle-evernote-to-md

Version:

Yet Another Rope Ladder from Evernote

98 lines 5.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertHtml2MdContent = void 0; const jsdom_1 = require("jsdom"); const turndown_service_1 = require("./utils/turndown-service"); const output_format_1 = require("./output-format"); const get_title_1 = require("./utils/get-title"); const unwrapElement = (node) => { node.replaceWith(...Array.from(node.children)); }; const swapParent = (wrapper) => { const inner = wrapper.parentElement; inner.replaceWith(...Array.from(inner.childNodes)); inner.append(...Array.from(wrapper.childNodes)); wrapper.appendChild(inner); }; const fixTasks = (node) => { // fix bold or italic breaking a task's syntax // i.e. '*[] foo*' instead of '[] *foo*' const spanTasks = Array.from(node.querySelectorAll('span>en-todo')); spanTasks.forEach(swapParent); // fix an anchor breaking a task's syntax // i.e. '[] [foo](bar)' instead of '[[] foo](bar)' const anchorTasks = Array.from(node.querySelectorAll('a>en-todo')); anchorTasks.forEach(swapParent); return node; }; const fixSublistsInContent = (content) => { let cont = content.replace(/<li>/g, '<li><div>'); cont = cont.replace(/<\/li>/g, '</div></li>'); cont = cont.replace(/<li><div>(\s)*<div>/g, '<li><div>'); cont = cont.replace(/<\/div>(\s)*<\/div><\/li>/g, '</div></li>'); return cont; }; const fixSublists = (node) => { var _a; const ulElements = Array.from(node.getElementsByTagName('ul')); const olElements = Array.from(node.getElementsByTagName('ol')); const listElements = ulElements.concat(olElements); listElements.forEach(listNode => { if (listNode.parentElement.tagName === 'LI') { listNode.parentElement.replaceWith(listNode); } if (listNode.previousElementSibling && listNode.previousElementSibling.tagName === 'LI') { // The below moves, not copies. // https://stackoverflow.com/questions/7555442/move-an-element-to-another-parent-after-changing-its-id listNode.previousElementSibling.appendChild(listNode); } }); for (const n of listElements) { const parentElement = n.parentElement; if ((parentElement === null || parentElement === void 0 ? void 0 : parentElement.tagName) === 'DIV' && ((_a = parentElement === null || parentElement === void 0 ? void 0 : parentElement.parentElement) === null || _a === void 0 ? void 0 : _a.tagName) === 'UL') { unwrapElement(parentElement); } // remove nested lists whose only child is another list, i.e. <ul><ul>...</ul></ul> if (((parentElement === null || parentElement === void 0 ? void 0 : parentElement.tagName) === 'UL' || (parentElement === null || parentElement === void 0 ? void 0 : parentElement.tagName) === 'OL') && (parentElement === null || parentElement === void 0 ? void 0 : parentElement.childNodes.length) === 1) { unwrapElement(parentElement); } } // The contents of every EN list item are wrapped by a div element. `<li><div>foo</div></li>` // We need to remove this `<div>`, since it's a block element and will lead to unwanted whitespace otherwise const liElements = Array.from(node.getElementsByTagName('li')); for (const liNode of liElements) { const listNodeDiv = liNode.firstElementChild; if (listNodeDiv && listNodeDiv.tagName === 'DIV') { const childElementsArr = Array.from(listNodeDiv.childNodes); listNodeDiv.replaceWith(...childElementsArr); } } return node; }; const convertHtml2MdContent = (yarleOptions, htmlContent) => { const content = htmlContent.replace(/<!DOCTYPE en-note [^>]*>/, '<!DOCTYPE html>') .replace(/(<a [^>]*)\/>/, '$1></a>').replace(/<div[^\/\<]*\/>/g, ''); const contentNode = new jsdom_1.JSDOM(fixSublistsInContent(content)).window.document .getElementsByTagName('en-note').item(0); let contentInMd = (0, turndown_service_1.getTurndownService)(yarleOptions) .turndown(fixTasks(fixSublists(contentNode))); const newLinePlaceholder = new RegExp('<YARLE_NEWLINE_PLACEHOLDER>', 'g'); contentInMd = contentInMd.replace(newLinePlaceholder, yarleOptions.convertPlainHtmlNewlines ? '\n' : ''); if (yarleOptions.outputFormat === output_format_1.OutputFormat.LogSeqMD) { contentInMd = contentInMd.replace(/\n/g, '\n- ') // add a "- " at each new line // .replace(/\r/g, '\n') .replace(/<br>/g, '[:br]') // fix new line in table .replace(/- \|/g, ' |') // fix table problem .replace(/- __\n/g, '- \n') // fix empty bold/italic .replace(/- \*\*\*\*\n/g, '- \n') .replace(/- _\*\*\*\*_\n/g, '- \n') .replace(/- \*\*__\*\*\n/g, '- \n'); contentInMd = `- ${contentInMd}`; // the first line } return contentInMd && contentInMd !== 'undefined' ? (0, get_title_1.performRegexpOnContent)(yarleOptions, contentInMd) : ''; }; exports.convertHtml2MdContent = convertHtml2MdContent; //# sourceMappingURL=convert-html-to-md.js.map