1
0
mirror of https://github.com/BoostIo/Boostnote synced 2025-12-14 02:06:29 +00:00

optimize implementation for a0c15182

This commit is contained in:
hikerpig
2019-09-02 16:06:52 +08:00
committed by Junyoung Choi
parent 8ea920ef91
commit 3eabf95fb3

View File

@@ -1040,22 +1040,24 @@ export default class MarkdownPreview extends React.Component {
const isStartWithHash = rawHref[0] === '#' const isStartWithHash = rawHref[0] === '#'
const { href, hash } = parser const { href, hash } = parser
const maybeExternalLink = /https?:\/\//.test(rawHref) const linkHash = hash === '' ? rawHref : hash // needed because we're having special link formats that are removed by parser e.g. :line:10
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 extractIdRegex = /file:\/\/.*main.?\w*.html#/ // file://path/to/main(.development.)html
const regexNoteInternalLink = new RegExp(`${extractIdRegex.source}(.+)`) const regexNoteInternalLink = new RegExp(`${extractIdRegex.source}(.+)`)
if (isStartWithHash || regexNoteInternalLink.test(linkHash)) { if (isStartWithHash || regexNoteInternalLink.test(rawHref)) {
const extractedId = isStartWithHash ? linkHash.slice(1) : linkHash.replace(extractIdRegex, '') const posOfHash = linkHash.indexOf('#')
const targetId = mdurl.encode(extractedId) if (posOfHash > -1) {
const targetElement = this.refs.root.contentWindow.document.getElementById( const extractedId = linkHash.slice(posOfHash + 1)
targetId const targetId = mdurl.encode(extractedId)
) const targetElement = this.refs.root.contentWindow.document.getElementById(
targetId
)
if (targetElement != null) { if (targetElement != null) {
this.getWindow().scrollTo(0, targetElement.offsetTop) this.getWindow().scrollTo(0, targetElement.offsetTop)
}
return
} }
return
} }
// this will match the new uuid v4 hash and the old hash // this will match the new uuid v4 hash and the old hash