All: Improved handling of empty links when importing ENEX files. Fixed minor layout issues in Electron app

pull/82/head
Laurent Cozic 2017-12-07 00:57:36 +00:00
parent ed914c6907
commit 7008daf92a
5 changed files with 45 additions and 23 deletions

View File

@ -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,

View File

@ -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 ? (
<div style={styles.messageBox}>
<span style={theme.textStyle}>
{_('Some items cannot be synchronised.')} <a href="#" onClick={() => { onViewDisabledItemsClick() }}>{_('View them now')}</a>

View File

@ -100,7 +100,7 @@ class MdToHtml {
const href = this.getAttr_(attrs, 'src');
if (!Resource.isResourceUrl(href)) {
return '<span>' + href + '</span><img title="' + htmlentities(title) + '" src="' + href + '"/>';
return '<img title="' + htmlentities(title) + '" src="' + href + '"/>';
}
const resourceId = Resource.urlToId(href);

View File

@ -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);

View File

@ -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;