Skip to content

Commit

Permalink
fix(ui): ssr treats all anchor links as external (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkeye64 committed Dec 31, 2024
1 parent 06de7ed commit f4cf928
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions ui/src/util/extendLink.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import slugify from './slugify'

export default function extendLink (md, { noopener = true, noreferrer = true }) {
export default function extendLink(md, { noopener = true, noreferrer = true }) {
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
const token = tokens[ idx ]
const token = tokens[idx]

const hrefIndex = token.attrIndex('href')

if (token.attrs[ hrefIndex ][ 1 ][ 0 ] === '#') {
if (token.attrs[hrefIndex][1][0] === '#') {
if (typeof location !== 'undefined') {
token.attrs[ hrefIndex ][ 1 ] = location.pathname + token.attrs[ hrefIndex ][ 1 ]
token.attrs[hrefIndex][1] = location.pathname + token.attrs[hrefIndex][1]
}
}

if (token.attrs[ hrefIndex ][ 1 ] === '') {
if (token.attrs[hrefIndex][1] === '') {
token.attrSet('class', 'q-markdown--link q-markdown--link-local')
if (tokens[ idx + 1 ] && tokens[ idx + 1 ].type === 'text' && tokens[ idx + 1 ].content) {
token.attrSet('id', slugify(tokens[ idx + 1 ].content))
if (tokens[idx + 1] && tokens[idx + 1].type === 'text' && tokens[idx + 1].content) {
token.attrSet('id', slugify(tokens[idx + 1].content))
}
}
else if (token.attrs[ hrefIndex ][ 1 ][ 0 ] === '/'
|| token.attrs[ hrefIndex ][ 1 ].startsWith('..')) {
} else if (
token.attrs[hrefIndex][1][0] === '/' ||
token.attrs[hrefIndex][1][0] === '#' ||
token.attrs[hrefIndex][1].startsWith('..')
) {
token.attrSet('class', 'q-markdown--link q-markdown--link-local')
}
else {
} else {
token.attrSet('class', 'q-markdown--link q-markdown--link-external')
token.attrSet('target', '_blank')
if (noopener === true || noreferrer === true) {
Expand Down

0 comments on commit f4cf928

Please sign in to comment.