Skip to content

Commit

Permalink
fix(ui/marked): optimize same origin link judgment logic
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Apr 30, 2024
1 parent fa6648e commit e132c31
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions ui/artalk/src/lib/marked-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@ export function getRenderer() {
return renderer
}

export const markedLinkRenderer =
const markedLinkRenderer =
(renderer: any, orgLinkRenderer: Function) =>
(href: string, title: string, text: string): string => {
const localLink = href?.startsWith(`${window.location.protocol}//${window.location.hostname}`)
const getLinkOrigin = (link: string) => {
try {
return new URL(link).origin
} catch {
return ''
}
}
const isSameOriginLink = getLinkOrigin(href) === window.location.origin
const html = orgLinkRenderer.call(renderer, href, title, text)
return html.replace(
/^<a /,
`<a target="_blank" ${!localLink ? `rel="noreferrer noopener nofollow"` : ''} `,
`<a target="_blank" ${!isSameOriginLink ? `rel="noreferrer noopener nofollow"` : ''} `,
)
}

export const markedCodeRenderer =
const markedCodeRenderer =
() =>
(block: string, lang: string | undefined): string => {
// Colorize the block only if the language is known to highlight.js
Expand Down

0 comments on commit e132c31

Please sign in to comment.