diff --git a/CliClient/locales/fr_FR.po b/CliClient/locales/fr_FR.po index a0ea0606b..05cbce08c 100644 --- a/CliClient/locales/fr_FR.po +++ b/CliClient/locales/fr_FR.po @@ -14,6 +14,8 @@ msgstr "" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" "X-Generator: Poedit 2.0.3\n" +"POT-Creation-Date: \n" +"PO-Revision-Date: \n" msgid "To delete a tag, untag the associated notes." msgstr "Pour supprimer une vignette, enlever là des notes associées." @@ -766,7 +768,7 @@ msgid "" "how the notes or notebooks were originally encrypted." msgstr "" "Note : seule une clef maître va être utilisée pour le cryptage (celle " -"marquée comme \"actif\" ci-dessus). N'importe quel clef peut-être utilisée " +"marquée comme \"actif\" ci-dessus). N'importe quelle clef peut être utilisée " "pour le décryptage, selon la façon dont les notes ou carnets étaient cryptés " "à l'origine." diff --git a/ReactNativeClient/lib/HtmlToMarkdownParser.js b/ReactNativeClient/lib/HtmlToMarkdownParser.js new file mode 100644 index 000000000..075c45f92 --- /dev/null +++ b/ReactNativeClient/lib/HtmlToMarkdownParser.js @@ -0,0 +1,16 @@ +const { enexXmlToMd } = require('lib/import-enex-md-gen.js'); +const stringToStream = require('string-to-stream') + +class HtmlToMarkdownParser { + + async parse(html, options = {}) { + if (!options.baseUrl) options.baseUrl = ''; + + const contentStream = stringToStream(html); + const markdown = await enexXmlToMd(contentStream, [], options); + return markdown; + } + +} + +module.exports = HtmlToMarkdownParser; \ No newline at end of file diff --git a/ReactNativeClient/lib/import-enex-md-gen.js b/ReactNativeClient/lib/import-enex-md-gen.js index 03bb0dd92..861a0f147 100644 --- a/ReactNativeClient/lib/import-enex-md-gen.js +++ b/ReactNativeClient/lib/import-enex-md-gen.js @@ -403,7 +403,7 @@ function addResourceTag(lines, resource, alt = "") { function isBlockTag(n) { - return ["div", "p", "dl", "dd", 'dt', "center", 'address'].indexOf(n) >= 0; + return ["div", "p", "dl", "dd", 'dt', "center", 'address', 'form', 'input', 'section', 'nav', 'header', 'article', 'textarea', 'footer', 'fieldset'].indexOf(n) >= 0; } function isStrongTag(n) { @@ -423,7 +423,7 @@ function isAnchor(n) { } function isIgnoredEndTag(n) { - return ["en-note", "en-todo", "span", "body", "html", "font", "br", 'hr', 'tbody', 'sup', 'img', 'abbr', 'cite', 'thead', 'small', 'tt', 'sub', 'colgroup', 'col', 'ins', 'caption', 'var', 'map', 'area'].indexOf(n) >= 0; + return ["en-note", "en-todo", "span", "body", "html", "font", "br", 'hr', 'tbody', 'sup', 'img', 'abbr', 'cite', 'thead', 'small', 'tt', 'sub', 'colgroup', 'col', 'ins', 'caption', 'var', 'map', 'area', 'label', 'legend'].indexOf(n) >= 0; } function isListTag(n) { @@ -432,7 +432,12 @@ function isListTag(n) { // Elements that don't require any special treatment beside adding a newline character function isNewLineOnlyEndTag(n) { - return ["div", "p", "li", "h1", "h2", "h3", "h4", "h5", 'h6', "dl", "dd", 'dt', "center", 'address'].indexOf(n) >= 0; + return ["div", "p", "li", "h1", "h2", "h3", "h4", "h5", 'h6', "dl", "dd", 'dt', "center", 'address', 'form', 'input', 'section', 'nav', 'header', 'article', 'textarea', 'footer', 'fieldset'].indexOf(n) >= 0; +} + +// Tags that must be ignored - both the tag and its content. +function isIgnoredContentTag(n) { + return ['script', 'style', 'iframe', 'select', 'option', 'button', 'video', 'source'].indexOf(n) >= 0 } function isCodeTag(n) { @@ -480,7 +485,36 @@ function attributeToLowerCase(node) { return output; } -function enexXmlToMdArray(stream, resources) { +function urlWithoutPath(url) { + const parsed = require('url').parse(url, true); + return parsed.protocol + '//' + parsed.host; +} + +function urlProtocol(url) { + const parsed = require('url').parse(url, true); + return parsed.protocol; +} + +const schemeRegex = /[a-zA-Z0-9\+\-\.]+:\/\// +// Make sure baseUrl doesn't end with a slash +function prependBaseUrl(url, baseUrl) { + if (!url) url = ''; + if (!baseUrl) return url; + const matches = schemeRegex.exec(url); + if (matches) return url; // Don't prepend the base URL if the URL already has a scheme + + if (url.length >= 2 && url.indexOf('//') === 0) { // If it starts with // it's a protcol-relative URL + return urlProtocol(baseUrl) + url; + } else if (url && url[0] === '/') { // If it starts with a slash, it's an absolute URL so it should be relative to the domain (and not to the full baseUrl) + return urlWithoutPath(baseUrl) + url; + } else { + return baseUrl + '/' + url; + } +} + +function enexXmlToMdArray(stream, resources, options = {}) { + if (options.baseUrl) options.baseUrl = options.baseUrl.replace(/[\/]+$/, ''); + let remainingResources = resources.slice(); const removeRemainingResource = (id) => { @@ -500,11 +534,12 @@ function enexXmlToMdArray(stream, resources) { inCodeblock: 0, lists: [], anchorAttributes: [], + ignoreContents: [], }; - let options = {}; + let saxStreamOptions = {}; let strict = false; - var saxStream = require('sax').createStream(strict, options) + var saxStream = require('sax').createStream(strict, saxStreamOptions) let section = { type: 'text', @@ -518,6 +553,7 @@ function enexXmlToMdArray(stream, resources) { }) saxStream.on('text', function(text) { + if (state.ignoreContents.length) return; if (['table', 'tr', 'tbody'].indexOf(section.type) >= 0) return; section.lines = collapseWhiteSpaceAndAppend(section.lines, state, text); }) @@ -543,6 +579,8 @@ function enexXmlToMdArray(stream, resources) { // Start of note } else if (isBlockTag(n)) { section.lines.push(BLOCK_OPEN); + } else if (isIgnoredContentTag(n)) { + state.ignoreContents.push(true); } else if (n == 'table') { let newSection = { type: 'table', @@ -590,7 +628,7 @@ function enexXmlToMdArray(stream, resources) { } else if (n == 'li') { section.lines.push(BLOCK_OPEN); if (!state.lists.length) { - reject("Found