Skip to content

Commit 3eabf95

Browse files
hikerpigRokt33r
authored andcommitted
optimize implementation for a0c1518
1 parent 8ea920e commit 3eabf95

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

browser/components/MarkdownPreview.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,22 +1040,24 @@ export default class MarkdownPreview extends React.Component {
10401040
const isStartWithHash = rawHref[0] === '#'
10411041
const { href, hash } = parser
10421042

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
1043+
const linkHash = hash === '' ? rawHref : hash // needed because we're having special link formats that are removed by parser e.g. :line:10
10451044

10461045
const extractIdRegex = /file:\/\/.*main.?\w*.html#/ // file://path/to/main(.development.)html
10471046
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)
1051-
const targetElement = this.refs.root.contentWindow.document.getElementById(
1052-
targetId
1053-
)
1047+
if (isStartWithHash || regexNoteInternalLink.test(rawHref)) {
1048+
const posOfHash = linkHash.indexOf('#')
1049+
if (posOfHash > -1) {
1050+
const extractedId = linkHash.slice(posOfHash + 1)
1051+
const targetId = mdurl.encode(extractedId)
1052+
const targetElement = this.refs.root.contentWindow.document.getElementById(
1053+
targetId
1054+
)
10541055

1055-
if (targetElement != null) {
1056-
this.getWindow().scrollTo(0, targetElement.offsetTop)
1056+
if (targetElement != null) {
1057+
this.getWindow().scrollTo(0, targetElement.offsetTop)
1058+
}
1059+
return
10571060
}
1058-
return
10591061
}
10601062

10611063
// this will match the new uuid v4 hash and the old hash

0 commit comments

Comments
 (0)