Skip to content

Commit 141bd11

Browse files
timaschewulivz
authored andcommitted
feat($theme-default): support external links in sidebar (close: #764)(#1534)
1 parent 1b6c4cd commit 141bd11

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/@vuepress/theme-default/components/SidebarLink.vue

+16-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export default {
2828
const active = item.type === 'auto'
2929
? selfActive || item.children.some(c => isActive($route, item.basePath + '#' + c.slug))
3030
: selfActive
31-
const link = renderLink(h, item.path, item.title || item.path, active)
31+
const link = item.type === 'external'
32+
? renderExternal(h, item.path, item.title || item.path)
33+
: renderLink(h, item.path, item.title || item.path, active)
3234
3335
const configDepth = $page.frontmatter.sidebarDepth
3436
|| sidebarDepth
@@ -75,6 +77,19 @@ function renderChildren (h, children, path, route, maxDepth, depth = 1) {
7577
])
7678
}))
7779
}
80+
81+
function renderExternal (h, to, text) {
82+
return h('a', {
83+
attrs: {
84+
href: to,
85+
target: '_blank',
86+
rel: 'noopener noreferrer'
87+
},
88+
class: {
89+
'sidebar-link': true
90+
}
91+
}, [text, h('OutboundLink')])
92+
}
7893
</script>
7994

8095
<style lang="stylus">

packages/@vuepress/theme-default/util/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ export function isActive (route, path) {
5454
}
5555

5656
export function resolvePage (pages, rawPath, base) {
57+
if (isExternal(rawPath)) {
58+
return {
59+
type: 'external',
60+
path: rawPath
61+
}
62+
}
5763
if (base) {
5864
rawPath = resolvePath(rawPath, base)
5965
}

0 commit comments

Comments
 (0)