Skip to content

Commit 1afa02b

Browse files
AWolf81Rokt33r
authored andcommitted
fix local link detection by creating a link tag to parse the input string
1 parent 3c39dc3 commit 1afa02b

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

browser/components/MarkdownPreview.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,16 +1015,19 @@ export default class MarkdownPreview extends React.Component {
10151015
e.preventDefault()
10161016
e.stopPropagation()
10171017

1018-
const href = e.target.getAttribute('href')
1019-
const linkHash = href.split('/').pop()
1018+
const rawHref = e.target.getAttribute('href')
1019+
const parser = document.createElement('a')
1020+
parser.href = e.target.getAttribute('href')
1021+
const { href, hash } = parser
1022+
const linkHash = hash === '' ? rawHref : hash // needed because we're having special link formats that are removed by parser e.g. :line:10
10201023

1021-
if (!href) return
1024+
if (!rawHref) return // not checked href because parser will create file://... string for [empty link]()
10221025

1023-
const regexNoteInternalLink = process.env.NODE_ENV === 'production' ? /main.production.html#(.+)/ : /main.development.html#(.+)/
1026+
const regexNoteInternalLink = /.*[main.\w]*.html#/
10241027

1025-
if (regexNoteInternalLink.test(linkHash)) {
1026-
const targetId = mdurl.encode(linkHash.match(regexNoteInternalLink)[1])
1027-
const targetElement = this.refs.root.contentWindow.document.getElementById(
1028+
if (regexNoteInternalLink.test(href)) {
1029+
const targetId = mdurl.encode(linkHash)
1030+
const targetElement = this.refs.root.contentWindow.document.querySelector(
10281031
targetId
10291032
)
10301033

0 commit comments

Comments
 (0)