diff --git a/ReactNativeClient/lib/htmlUtils.js b/ReactNativeClient/lib/htmlUtils.js index a7e7a142d..e13db1c07 100644 --- a/ReactNativeClient/lib/htmlUtils.js +++ b/ReactNativeClient/lib/htmlUtils.js @@ -1,5 +1,13 @@ const jsdom = require("jsdom"); const { JSDOM } = jsdom; +const urlUtils = require('lib/urlUtils.js'); + +function headAndBodyHtml_(doc) { + const output = []; + if (doc.head) output.push(doc.head.innerHTML); + if (doc.body) output.push(doc.body.innerHTML); + return output.join('\n'); +} const htmlUtils = { @@ -34,11 +42,22 @@ const htmlUtils = { // This function returns the head and body but without the
and // tag, which for our purpose are not needed and can cause issues when present. - const output = []; - if (doc.head) output.push(doc.head.innerHTML); - if (doc.body) output.push(doc.body.innerHTML); - return output.join('\n'); - }, + return headAndBodyHtml_(doc); + }, + + prependBaseUrl(html, baseUrl) { + const dom = new JSDOM(html); + const doc = dom.window.document; + const anchors = doc.getElementsByTagName('a'); + + for (const anchor of anchors) { + const href = anchor.getAttribute('href'); + const newHref = urlUtils.prependBaseUrl(href, baseUrl); + anchor.setAttribute('href', newHref); + } + + return headAndBodyHtml_(doc); + }, }; diff --git a/ReactNativeClient/lib/services/rest/Api.js b/ReactNativeClient/lib/services/rest/Api.js index d262a30bc..b473753af 100644 --- a/ReactNativeClient/lib/services/rest/Api.js +++ b/ReactNativeClient/lib/services/rest/Api.js @@ -432,6 +432,8 @@ class Api { if (requestNote.id) output.id = requestNote.id; + const baseUrl = requestNote.base_url ? requestNote.base_url : ''; + if (requestNote.body_html) { if (requestNote.convert_to === 'html') { const style = await this.buildNoteStyleSheet_(requestNote.stylesheets); @@ -446,13 +448,14 @@ class Api { // means a code block in Markdown. collapseWhitespace: true, }); + output.body = htmlUtils.prependBaseUrl(output.body, baseUrl); output.markup_language = Note.MARKUP_LANGUAGE_HTML; } else { // Convert to Markdown // Parsing will not work if the HTML is not wrapped in a top level tag, which is not guaranteed // when getting the content from elsewhere. So here wrap it - it won't change anything to the final // rendering but it makes sure everything will be parsed. output.body = await this.htmlToMdParser().parse('