From 7008daf92a8f2d1bd076a941989392f28b2e0090 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Thu, 7 Dec 2017 00:57:36 +0000 Subject: [PATCH] All: Improved handling of empty links when importing ENEX files. Fixed minor layout issues in Electron app --- ElectronClient/app/gui/ImportScreen.jsx | 32 ++++++++++++--------- ElectronClient/app/gui/MainScreen.jsx | 5 ++-- ReactNativeClient/lib/MdToHtml.js | 2 +- ReactNativeClient/lib/import-enex-md-gen.js | 25 +++++++++++++--- ReactNativeClient/lib/import-enex.js | 4 +-- 5 files changed, 45 insertions(+), 23 deletions(-) diff --git a/ElectronClient/app/gui/ImportScreen.jsx b/ElectronClient/app/gui/ImportScreen.jsx index 3c80b1c7d3..4cb82bd442 100644 --- a/ElectronClient/app/gui/ImportScreen.jsx +++ b/ElectronClient/app/gui/ImportScreen.jsx @@ -41,19 +41,24 @@ class ImportScreenComponent extends React.Component { const messages = this.state.messages.slice(); let found = false; - for (let i = 0; i < messages.length; i++) { - if (messages[i].key === key) { - messages[i].text = text; - found = true; - break; - } - } - - if (!found) messages.push({ key: key, text: text }); + messages.push({ key: key, text: text }); this.setState({ messages: messages }); } + uniqueMessages() { + let output = []; + const messages = this.state.messages.slice(); + let foundKeys = []; + for (let i = messages.length - 1; i >= 0; i--) { + const msg = messages[i]; + if (foundKeys.indexOf(msg.key) >= 0) continue; + foundKeys.push(msg.key); + output.unshift(msg); + } + return output; + } + async doImport() { const filePath = this.props.filePath; const folderTitle = await Folder.findUniqueFolderTitle(filename(filePath)); @@ -77,10 +82,9 @@ class ImportScreenComponent extends React.Component { this.addMessage('progress', lastProgress); }, onError: (error) => { - const messages = this.state.messages.slice(); - let s = error.trace ? error.trace : error.toString(); - messages.push({ key: 'error_' + (progressCount++), text: s }); - this.addMessage('error_' + (progressCount++), lastProgress); + // Don't display the error directly because most of the time it doesn't matter + // (eg. for weird broken HTML, but the note is still imported) + console.warn('When importing ENEX file', error); }, } @@ -95,7 +99,7 @@ class ImportScreenComponent extends React.Component { render() { const theme = themeStyle(this.props.theme); const style = this.props.style; - const messages = this.state.messages; + const messages = this.uniqueMessages(); const messagesStyle = { padding: 10, diff --git a/ElectronClient/app/gui/MainScreen.jsx b/ElectronClient/app/gui/MainScreen.jsx index 744996f102..56efddfd4d 100644 --- a/ElectronClient/app/gui/MainScreen.jsx +++ b/ElectronClient/app/gui/MainScreen.jsx @@ -288,8 +288,9 @@ class MainScreenComponent extends React.Component { const promptOptions = this.state.promptOptions; const folders = this.props.folders; const notes = this.props.notes; + const messageBoxVisible = this.props.hasDisabledSyncItems; - const styles = this.styles(this.props.theme, style.width, style.height, true); + const styles = this.styles(this.props.theme, style.width, style.height, messageBoxVisible); const theme = themeStyle(this.props.theme); const headerButtons = []; @@ -342,7 +343,7 @@ class MainScreenComponent extends React.Component { }); } - const messageComp = this.props.hasDisabledSyncItems ? ( + const messageComp = messageBoxVisible ? (
{_('Some items cannot be synchronised.')} { onViewDisabledItemsClick() }}>{_('View them now')} diff --git a/ReactNativeClient/lib/MdToHtml.js b/ReactNativeClient/lib/MdToHtml.js index 2fabcb7fbb..c181d56129 100644 --- a/ReactNativeClient/lib/MdToHtml.js +++ b/ReactNativeClient/lib/MdToHtml.js @@ -100,7 +100,7 @@ class MdToHtml { const href = this.getAttr_(attrs, 'src'); if (!Resource.isResourceUrl(href)) { - return '' + href + ''; + return ''; } const resourceId = Resource.urlToId(href); diff --git a/ReactNativeClient/lib/import-enex-md-gen.js b/ReactNativeClient/lib/import-enex-md-gen.js index fa862050b6..93bdc5c2e3 100644 --- a/ReactNativeClient/lib/import-enex-md-gen.js +++ b/ReactNativeClient/lib/import-enex-md-gen.js @@ -214,7 +214,7 @@ function isAnchor(n) { } function isIgnoredEndTag(n) { - return n=="en-note" || n=="en-todo" || n=="span" || n=="body" || n=="html" || n=="font" || n=="br" || n=='hr' || n=='s' || n == 'tbody' || n == 'sup' || n == 'img' || n == 'abbr' || n == 'cite' || n == 'thead' || n == 'small' || n == 'tt' || n == 'sub'; + return n=="en-note" || n=="en-todo" || n=="span" || n=="body" || n=="html" || n=="font" || n=="br" || n=='hr' || n == 'tbody' || n == 'sup' || n == 'img' || n == 'abbr' || n == 'cite' || n == 'thead' || n == 'small' || n == 'tt' || n == 'sub'; } function isListTag(n) { @@ -541,6 +541,10 @@ function enexXmlToMdArray(stream, resources) { if (section.lines.length < 1) throw new Error('Invalid anchor tag closing'); // Sanity check, but normally not possible + const pushEmptyAnchor = (url) => { + section.lines.push('[link](' + url + ')'); + } + // When closing the anchor tag, check if there's is any text content. If not // put the URL as is (don't wrap it in [](url)). The markdown parser, using // GitHub flavour, will turn this URL into a link. This is to generate slightly @@ -548,14 +552,15 @@ function enexXmlToMdArray(stream, resources) { let previous = section.lines[section.lines.length - 1]; if (previous == '[') { section.lines.pop(); - section.lines.push(url); + pushEmptyAnchor(url); } else if (!previous || previous == url) { section.lines.pop(); section.lines.pop(); - section.lines.push(url); + pushEmptyAnchor(url); } else { // Need to remove any new line character between the current ']' and the previous '[' // otherwise it won't render properly. + let allSpaces = true; for (let i = section.lines.length - 1; i >= 0; i--) { const c = section.lines[i]; if (c === '[') { @@ -563,10 +568,22 @@ function enexXmlToMdArray(stream, resources) { } else { if (c === BLOCK_CLOSE || c === BLOCK_OPEN || c === NEWLINE) { section.lines[i] = SPACE; + } else { + if (!isWhiteSpace(c)) allSpaces = false; } } } - section.lines.push('](' + url + ')'); + + if (allSpaces) { + for (let i = section.lines.length - 1; i >= 0; i--) { + const c = section.lines.pop(); + if (c === '[') break; + } + //section.lines.push(url); + pushEmptyAnchor(url); + } else { + section.lines.push('](' + url + ')'); + } } } else if (isListTag(n)) { section.lines.push(BLOCK_CLOSE); diff --git a/ReactNativeClient/lib/import-enex.js b/ReactNativeClient/lib/import-enex.js index 40fbf0617a..5fa08ed2fb 100644 --- a/ReactNativeClient/lib/import-enex.js +++ b/ReactNativeClient/lib/import-enex.js @@ -222,9 +222,9 @@ function importEnex(parentFolderId, filePath, importOptions = null) { const body = await enexXmlToMd(contentStream, note.resources); delete note.bodyXml; - // console.info('-----------------------------------------------------------'); + // console.info('*************************************************************************'); // console.info(body); - // console.info('-----------------------------------------------------------'); + // console.info('*************************************************************************'); note.id = uuid.create(); note.parent_id = parentFolderId;