Skip to content

Commit

Permalink
Merge pull request #1163 from PrefectHQ/nicholas/bugfix-markdown-link…
Browse files Browse the repository at this point in the history
…-base-url-2024-03-19

Bugfix: Markdown absolute links being treated as relative
  • Loading branch information
znicholasbrown authored Mar 19, 2024
2 parents 202f8f4 + 250f3d0 commit 5e69268
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/components/MarkdownRenderer/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from '@/types/markdownRenderer'
import { ColumnClassesMethod } from '@/types/tables'
import { randomId } from '@/utilities'
import { isRouteExternal } from '@/utilities/router'
import { isRouteExternal, isRouteRelative, stripBaseUrl } from '@/utilities/router'
import { unescapeHtml } from '@/utilities/strings'

const baseElement = 'div'
Expand All @@ -39,7 +39,19 @@ const mapChildTokens = (tokens: Token[], options: ParserOptions): VNodeChildren
const getVNode = (token: Token, options: ParserOptions): VNode | VNode[] => {
const { headingClasses = defaultHeadingClasses, baseLinkUrl = '' } = options

const normalizeHref = (href: string): string => isRouteExternal(href) ? href : `${baseLinkUrl}${href}`
const normalizeHref = (href: string): string => {
if (isRouteExternal(href)) {
return href
}

if (isRouteRelative(href)) {
return `${baseLinkUrl}${href}`
}

// This is used to strip the base URL from the href; without this
// vue-router will append the entire URL to the current route
return stripBaseUrl(href)
}

let children: VNodeChildren = []

Expand Down
14 changes: 14 additions & 0 deletions src/utilities/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,18 @@ export function isRouteExternal(route: RouteLocationRaw): boolean {
} catch (error) {
return false
}
}

export function isRouteRelative(route: string): boolean {
return route.startsWith('/')
}

export function stripBaseUrl(href: string): string {
try {
const strippedBaseUrl = new URL(href).pathname
return strippedBaseUrl
} catch {
// fail silently
return href
}
}

0 comments on commit 5e69268

Please sign in to comment.