Desktop, Cli: Fixed import of ENEX files that contain invisible sections

Ref: https://discourse.joplinapp.org/t/code-block-imports-from-evernote/13113/4
pull/4215/head
Laurent Cozic 2020-12-19 16:18:07 +00:00
parent f53a7d3a8a
commit b8493baa5e
3 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,3 @@
<p>This is visible<br/>This too
<div style='display:none !important;visibility:collapse !important;height:0 !important;white-space:nowrap;width:100%;overflow:hidden'>This is hidden <div>and this sub-tag <strong>too</strong></div></div>
This is visible again</p>

View File

@ -0,0 +1,4 @@
This is visible
This too<div style="display: none;">This is hidden
and this sub-tag **too**
</div>This is visible again

View File

@ -377,6 +377,12 @@ function attributeToLowerCase(node) {
return output;
}
function isInvisibleBlock(attributes) {
const style = attributes.style;
if (!style) return false;
return !!style.match(/display:[\s\S]*none/);
}
function isSpanWithStyle(attributes) {
if (attributes != undefined) {
if ('style' in attributes) {
@ -389,6 +395,8 @@ function isSpanWithStyle(attributes) {
function isSpanStyleBold(attributes) {
const style = attributes.style;
if (!style) return false;
if (style.includes('font-weight: bold;')) {
return true;
} else if (style.search(/font-family:.*,Bold.*;/) != -1) {
@ -424,6 +432,7 @@ function enexXmlToMdArray(stream, resources) {
lists: [],
anchorAttributes: [],
spanAttributes: [],
tags: [],
};
const options = {};
@ -474,6 +483,12 @@ function enexXmlToMdArray(stream, resources) {
saxStream.on('opentag', function(node) {
const nodeAttributes = attributeToLowerCase(node);
const n = node.name.toLowerCase();
const isVisible = !isInvisibleBlock(nodeAttributes);
state.tags.push({
name: n,
visible: isVisible,
});
const currentList = state.lists && state.lists.length ? state.lists[state.lists.length - 1] : null;
@ -488,6 +503,14 @@ function enexXmlToMdArray(stream, resources) {
if (n == 'en-note') {
// Start of note
} else if (isInvisibleBlock(nodeAttributes)) {
const newSection = {
type: 'hidden',
lines: [],
parent: section,
};
section.lines.push(newSection);
section = newSection;
} else if (isBlockTag(n)) {
section.lines.push(BLOCK_OPEN);
} else if (n == 'table') {
@ -719,8 +742,12 @@ function enexXmlToMdArray(stream, resources) {
saxStream.on('closetag', function(n) {
n = n ? n.toLowerCase() : n;
const poppedTag = state.tags.pop();
if (n == 'en-note') {
// End of note
} else if (!poppedTag.visible) {
if (section && section.parent) section = section.parent;
} else if (isNewLineOnlyEndTag(n)) {
section.lines.push(BLOCK_CLOSE);
} else if (n == 'td' || n == 'th') {
@ -1109,6 +1136,14 @@ async function enexXmlToMd(xmlString, resources, options = {}) {
mdLines = mdLines.concat(tableLines);
} else if (typeof line === 'object' && line.type === 'code') {
mdLines = mdLines.concat(line.lines);
} else if (typeof line === 'object' && line.type === 'hidden') {
// ENEX notes sometimes have hidden tags. We could strip off these
// sections but in the spirit of preserving all data we wrap them in
// a hidden tag too.
let hiddenLines = ['<div style="display: none;">'];
hiddenLines = hiddenLines.concat(line.lines);
hiddenLines.push('</div>');
mdLines = mdLines.concat(hiddenLines);
} else if (typeof line === 'object') {
console.warn('Unhandled object type:', line);
mdLines = mdLines.concat(line.lines);