From 3eabf95fb354d15795910f0377ed12bb64b53608 Mon Sep 17 00:00:00 2001 From: hikerpig Date: Mon, 2 Sep 2019 16:06:52 +0800 Subject: [PATCH] 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 7c747672..c1dc287e 100755 --- a/browser/components/MarkdownPreview.js +++ b/browser/components/MarkdownPreview.js @@ -1040,22 +1040,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