Skip to content

Commit 8ea920e

Browse files
hikerpigRokt33r
authored andcommitted
fix: Can't open external browser in Markdown Preview with external link containing '#', close #3213
1 parent 3c0f20f commit 8ea920e

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

browser/components/MarkdownPreview.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,17 +1033,21 @@ export default class MarkdownPreview extends React.Component {
10331033
e.stopPropagation()
10341034

10351035
const rawHref = e.target.getAttribute('href')
1036+
if (!rawHref) return // not checked href because parser will create file://... string for [empty link]()
1037+
10361038
const parser = document.createElement('a')
1037-
parser.href = e.target.getAttribute('href')
1039+
parser.href = rawHref
1040+
const isStartWithHash = rawHref[0] === '#'
10381041
const { href, hash } = parser
1039-
const linkHash = hash === '' ? rawHref : hash // needed because we're having special link formats that are removed by parser e.g. :line:10
10401042

1041-
if (!rawHref) return // not checked href because parser will create file://... string for [empty link]()
1043+
const maybeExternalLink = /https?:\/\//.test(rawHref)
1044+
const linkHash = (maybeExternalLink || hash === '') ? rawHref : hash // needed because we're having special link formats that are removed by parser e.g. :line:10
10421045

1043-
const extractId = /(main.html)?#/
1044-
const regexNoteInternalLink = new RegExp(`${extractId.source}(.+)`)
1045-
if (regexNoteInternalLink.test(linkHash)) {
1046-
const targetId = mdurl.encode(linkHash.replace(extractId, ''))
1046+
const extractIdRegex = /file:\/\/.*main.?\w*.html#/ // file://path/to/main(.development.)html
1047+
const regexNoteInternalLink = new RegExp(`${extractIdRegex.source}(.+)`)
1048+
if (isStartWithHash || regexNoteInternalLink.test(linkHash)) {
1049+
const extractedId = isStartWithHash ? linkHash.slice(1) : linkHash.replace(extractIdRegex, '')
1050+
const targetId = mdurl.encode(extractedId)
10471051
const targetElement = this.refs.root.contentWindow.document.getElementById(
10481052
targetId
10491053
)

0 commit comments

Comments
 (0)