From a0c151821c4c31d0c44c1c135bec045ae12b0f5b Mon Sep 17 00:00:00 2001 From: hikerpig Date: Mon, 2 Sep 2019 14:56:36 +0800 Subject: [PATCH 1/2] fix: Can't open external browser in Markdown Preview with external link containing '#', close #3213 --- browser/components/MarkdownPreview.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/browser/components/MarkdownPreview.js b/browser/components/MarkdownPreview.js index 15941cf87..7d049673c 100755 --- a/browser/components/MarkdownPreview.js +++ b/browser/components/MarkdownPreview.js @@ -1014,17 +1014,21 @@ export default class MarkdownPreview extends React.Component { e.stopPropagation() const rawHref = e.target.getAttribute('href') + if (!rawHref) return // not checked href because parser will create file://... string for [empty link]() + const parser = document.createElement('a') - parser.href = e.target.getAttribute('href') + parser.href = rawHref + const isStartWithHash = rawHref[0] === '#' const { href, hash } = parser - const linkHash = hash === '' ? rawHref : hash // needed because we're having special link formats that are removed by parser e.g. :line:10 - if (!rawHref) return // not checked href because parser will create file://... string for [empty link]() + const maybeExternalLink = /https?:\/\//.test(rawHref) + const linkHash = (maybeExternalLink || hash === '') ? rawHref : hash // needed because we're having special link formats that are removed by parser e.g. :line:10 - const extractId = /(main.html)?#/ - const regexNoteInternalLink = new RegExp(`${extractId.source}(.+)`) - if (regexNoteInternalLink.test(linkHash)) { - const targetId = mdurl.encode(linkHash.replace(extractId, '')) + const extractIdRegex = /file:\/\/.*main.?\w*.html#/ // file://path/to/main(.development.)html + const regexNoteInternalLink = new RegExp(`${extractIdRegex.source}(.+)`) + if (isStartWithHash || regexNoteInternalLink.test(linkHash)) { + const extractedId = isStartWithHash ? linkHash.slice(1) : linkHash.replace(extractIdRegex, '') + const targetId = mdurl.encode(extractedId) const targetElement = this.refs.root.contentWindow.document.getElementById( targetId ) From 3f2824ca674812f79b1889f8fa6736cac95232f8 Mon Sep 17 00:00:00 2001 From: hikerpig Date: Mon, 2 Sep 2019 16:06:52 +0800 Subject: [PATCH 2/2] optimize implementation for a0c15182 --- browser/components/MarkdownPreview.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/browser/components/MarkdownPreview.js b/browser/components/MarkdownPreview.js index 7d049673c..b3a16151a 100755 --- a/browser/components/MarkdownPreview.js +++ b/browser/components/MarkdownPreview.js @@ -1021,22 +1021,24 @@ export default class MarkdownPreview extends React.Component { const isStartWithHash = rawHref[0] === '#' const { href, hash } = parser - const maybeExternalLink = /https?:\/\//.test(rawHref) - const linkHash = (maybeExternalLink || hash === '') ? rawHref : hash // needed because we're having special link formats that are removed by parser e.g. :line:10 + const linkHash = hash === '' ? rawHref : hash // needed because we're having special link formats that are removed by parser e.g. :line:10 const extractIdRegex = /file:\/\/.*main.?\w*.html#/ // file://path/to/main(.development.)html const regexNoteInternalLink = new RegExp(`${extractIdRegex.source}(.+)`) - if (isStartWithHash || regexNoteInternalLink.test(linkHash)) { - const extractedId = isStartWithHash ? linkHash.slice(1) : linkHash.replace(extractIdRegex, '') - const targetId = mdurl.encode(extractedId) - const targetElement = this.refs.root.contentWindow.document.getElementById( - targetId - ) + if (isStartWithHash || regexNoteInternalLink.test(rawHref)) { + const posOfHash = linkHash.indexOf('#') + if (posOfHash > -1) { + const extractedId = linkHash.slice(posOfHash + 1) + const targetId = mdurl.encode(extractedId) + const targetElement = this.refs.root.contentWindow.document.getElementById( + targetId + ) - if (targetElement != null) { - this.getWindow().scrollTo(0, targetElement.offsetTop) + if (targetElement != null) { + this.getWindow().scrollTo(0, targetElement.offsetTop) + } + return } - return } // this will match the new uuid v4 hash and the old hash