1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop, Cli: Fixes #5213: Import highlighted text from ENEX files

This commit is contained in:
Laurent Cozic 2021-07-20 10:37:44 +01:00
parent b6cf9d1d5a
commit 75cc73cc0c
4 changed files with 21 additions and 10 deletions

View File

@ -0,0 +1 @@
<span style="background-color: rgb(255, 250, 165);-evernote-highlight:true;">I&apos;ll highlight some text.</span>

View File

@ -0,0 +1 @@
==I'll highlight some text.==

View File

@ -13,7 +13,7 @@ import Resource from './models/Resource';
const enexSampleBaseDir = `${supportDir}/../enex_to_md`; const enexSampleBaseDir = `${supportDir}/../enex_to_md`;
describe('EnexToMd', function() { describe('import-enex-md-gen', function() {
beforeEach(async (done) => { beforeEach(async (done) => {
await setupDatabaseAndSynchronizer(1); await setupDatabaseAndSynchronizer(1);
@ -31,7 +31,7 @@ describe('EnexToMd', function() {
const htmlPath = `${enexSampleBaseDir}/${htmlFilename}`; const htmlPath = `${enexSampleBaseDir}/${htmlFilename}`;
const mdPath = `${enexSampleBaseDir}/${filename(htmlFilename)}.md`; const mdPath = `${enexSampleBaseDir}/${filename(htmlFilename)}.md`;
// if (htmlFilename !== 'multiline_inner_text.html') continue; // if (htmlFilename !== 'highlight.html') continue;
const html = await shim.fsDriver().readFile(htmlPath); const html = await shim.fsDriver().readFile(htmlPath);
let expectedMd = await shim.fsDriver().readFile(mdPath); let expectedMd = await shim.fsDriver().readFile(mdPath);

View File

@ -32,6 +32,7 @@ interface ParserStateTag {
name: string; name: string;
visible: boolean; visible: boolean;
isCodeBlock: boolean; isCodeBlock: boolean;
isHighlight: boolean;
} }
interface ParserStateList { interface ParserStateList {
@ -512,6 +513,17 @@ function isCodeBlock(context: any, nodeName: string, attributes: any) {
return false; return false;
} }
function isHighlight(context: any, _nodeName: string, attributes: any) {
if (attributes && attributes.style) {
// Evernote uses various inconsistent CSS prefixes: so far I've found
// "--en", "-en", "-evernote", so I'm guessing "--evernote" probably
// exists too.
const enHighlight = cssValue(context, attributes.style, '-evernote-highlight') || cssValue(context, attributes.style, '--evernote-highlight');
if (enHighlight && enHighlight.toLowerCase() === 'true') return true;
}
return false;
}
function enexXmlToMdArray(stream: any, resources: ResourceEntity[]): Promise<EnexXmlToMdArrayResult> { function enexXmlToMdArray(stream: any, resources: ResourceEntity[]): Promise<EnexXmlToMdArrayResult> {
const remainingResources = resources.slice(); const remainingResources = resources.slice();
@ -588,6 +600,7 @@ function enexXmlToMdArray(stream: any, resources: ResourceEntity[]): Promise<Ene
name: n, name: n,
visible: isVisible, visible: isVisible,
isCodeBlock: isCodeBlock(this, n, nodeAttributes), isCodeBlock: isCodeBlock(this, n, nodeAttributes),
isHighlight: isHighlight(this, n, nodeAttributes),
}; };
state.tags.push(tagInfo); state.tags.push(tagInfo);
@ -667,7 +680,6 @@ function enexXmlToMdArray(stream: any, resources: ResourceEntity[]): Promise<Ene
} else if (n == 'caption') { } else if (n == 'caption') {
if (section.type != 'table') { if (section.type != 'table') {
displaySaxWarning(this, 'Found a <caption> tag outside of a <table>'); displaySaxWarning(this, 'Found a <caption> tag outside of a <table>');
// return;
} }
const newSection: Section = { const newSection: Section = {
@ -687,13 +699,6 @@ function enexXmlToMdArray(stream: any, resources: ResourceEntity[]): Promise<Ene
section.lines.push(newSection); section.lines.push(newSection);
section = newSection; section = newSection;
} else if (tagInfo.isCodeBlock) { } else if (tagInfo.isCodeBlock) {
// state.inPre = false;
// const previousIsPre = state.tags.length ? state.tags[state.tags.length - 1].name === 'pre' : false;
// if (previousIsPre) {
// section.lines.pop();
// }
state.inCode.push(true); state.inCode.push(true);
state.currentCode = ''; state.currentCode = '';
@ -727,6 +732,8 @@ function enexXmlToMdArray(stream: any, resources: ResourceEntity[]): Promise<Ene
section.lines.push(`${indent + container.counter}. `); section.lines.push(`${indent + container.counter}. `);
container.counter++; container.counter++;
} }
} else if (tagInfo.isHighlight) {
section.lines.push('==');
} else if (isStrongTag(n)) { } else if (isStrongTag(n)) {
section.lines.push('**'); section.lines.push('**');
} else if (isStrikeTag(n)) { } else if (isStrikeTag(n)) {
@ -889,6 +896,8 @@ function enexXmlToMdArray(stream: any, resources: ResourceEntity[]): Promise<Ene
// End of note // End of note
} else if (!poppedTag.visible) { } else if (!poppedTag.visible) {
if (section && section.parent) section = section.parent; if (section && section.parent) section = section.parent;
} else if (poppedTag.isHighlight) {
section.lines.push('==');
} else if (poppedTag.isCodeBlock) { } else if (poppedTag.isCodeBlock) {
state.inCode.pop(); state.inCode.pop();