mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 01:36:22 +00:00
fix: Can't open external browser in Markdown Preview with external link containing '#', close #3213
This commit is contained in:
@@ -1014,17 +1014,21 @@ export default class MarkdownPreview extends React.Component {
|
||||
e.stopPropagation()
|
||||
|
||||
const rawHref = e.target.getAttribute('href')
|
||||
const parser = document.createElement('a')
|
||||
parser.href = e.target.getAttribute('href')
|
||||
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 extractId = /(main.html)?#/
|
||||
const regexNoteInternalLink = new RegExp(`${extractId.source}(.+)`)
|
||||
if (regexNoteInternalLink.test(linkHash)) {
|
||||
const targetId = mdurl.encode(linkHash.replace(extractId, ''))
|
||||
const parser = document.createElement('a')
|
||||
parser.href = rawHref
|
||||
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 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
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user