mirror of
https://github.com/BoostIo/Boostnote
synced 2025-12-13 09:46:22 +00:00
fix: Can't open external browser in Markdown Preview with external link containing '#', close #3213
This commit is contained in:
@@ -1033,17 +1033,21 @@ export default class MarkdownPreview extends React.Component {
|
|||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
|
|
||||||
const rawHref = e.target.getAttribute('href')
|
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]()
|
if (!rawHref) return // not checked href because parser will create file://... string for [empty link]()
|
||||||
|
|
||||||
const extractId = /(main.html)?#/
|
const parser = document.createElement('a')
|
||||||
const regexNoteInternalLink = new RegExp(`${extractId.source}(.+)`)
|
parser.href = rawHref
|
||||||
if (regexNoteInternalLink.test(linkHash)) {
|
const isStartWithHash = rawHref[0] === '#'
|
||||||
const targetId = mdurl.encode(linkHash.replace(extractId, ''))
|
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(
|
const targetElement = this.refs.root.contentWindow.document.getElementById(
|
||||||
targetId
|
targetId
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user