Skip to content

Commit ebb6c5c

Browse files
committed
feat(github-link): fix github-link component ; add playground page
1 parent 819d99d commit ebb6c5c

File tree

5 files changed

+56
-24
lines changed

5 files changed

+56
-24
lines changed

docs/nuxt.config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { defineNuxtConfig } from 'nuxt'
2-
31
export default defineNuxtConfig({
42
app: {
53
},

playground/components/Navigation.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const nav = [
2929
{
3030
name: 'File Contributors',
3131
path: '/file-contributors'
32+
},
33+
{
34+
name: 'Github Link',
35+
path: '/github-link'
3236
}
3337
]
3438
</script>

playground/nuxt.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { defineNuxtConfig } from 'nuxt'
21
import githubModule from '../src/module'
32

43
export default defineNuxtConfig({

playground/pages/github-link.vue

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script setup lang="ts">
2+
import { ref } from 'vue'
3+
4+
const file = ref('package.json')
5+
</script>
6+
7+
<template>
8+
<div>
9+
<input v-model="file">
10+
<div style="margin: 2rem 0;" />
11+
<GithubLink v-slot="{ url }" :source="file">
12+
<NuxtLink :to="url" target="_blank">
13+
✍️ Edit {{ file }}
14+
</NuxtLink>
15+
</GithubLink>
16+
<div style="margin: 2rem 0;" />
17+
<GithubLink v-slot="{ url }" :edit="false" :source="file">
18+
<NuxtLink :to="url" target="_blank">
19+
🔗 Open {{ file }}
20+
</NuxtLink>
21+
</GithubLink>
22+
</div>
23+
</template>

src/runtime/components/GithubLink.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,35 @@ export default defineComponent({
8080
throw new Error('If you want to use `GithubLink` component, you must specify: `owner`, `repo` and `branch`.')
8181
}
8282

83-
// eslint-disable-next-line vue/no-setup-props-destructure
84-
let { repo, owner, branch, contentDir } = props
85-
let prefix = ''
86-
const { sources } = useRuntimeConfig().content
87-
let source
88-
for (const key in Object.keys(sources)) {
89-
if (props.page._id.startsWith(key)) {
90-
source = sources[key]
91-
break
83+
const source = computed(() => {
84+
let { repo, owner, branch, contentDir } = props
85+
let prefix = ''
86+
87+
// Resolve source from content sources
88+
if (useRuntimeConfig()?.public?.content) {
89+
let source
90+
const { sources } = useRuntimeConfig().public.content
91+
92+
for (const key in sources || []) {
93+
if (props.page._id.startsWith(key)) {
94+
source = sources[key]
95+
break
96+
}
97+
}
98+
99+
if (source?.driver === 'github') {
100+
repo = source.repo || props.repo || ''
101+
owner = source.owner || props.owner || ''
102+
branch = source.branch || props.branch || 'main'
103+
contentDir = source.dir || props.contentDir || ''
104+
prefix = source.prefix || ''
105+
}
92106
}
93-
}
94107

95-
if (source?.driver === 'github') {
96-
repo = source.repo
97-
owner = ''
98-
branch = source.branch || 'main'
99-
contentDir = source.dir || ''
100-
prefix = source.prefix || ''
101-
}
108+
return { repo, owner, branch, contentDir, prefix }
109+
})
102110

103-
const base = computed(() => joinURL('https://github.com', `${owner}/${repo}`))
111+
const base = computed(() => joinURL('https://github.com', `${source.value.owner}/${source.value.repo}`))
104112

105113
const path = computed(() => {
106114
const dirParts: string[] = []
@@ -109,10 +117,10 @@ export default defineComponent({
109117
// Create the URL from a document data.
110118
if (props?.page?._path) {
111119
// Use content dir
112-
if (contentDir) { dirParts.push(contentDir) }
120+
if (source.value.contentDir) { dirParts.push(source.value.contentDir) }
113121

114122
// Get page file from page data
115-
dirParts.push(props.page._file.substring(prefix.length))
123+
dirParts.push(props.page._file.substring(source.value.prefix.length))
116124

117125
return dirParts
118126
}
@@ -138,7 +146,7 @@ export default defineComponent({
138146

139147
if (props.edit) { parts.push('edit') } else { parts.push('tree') }
140148

141-
parts.push(branch, ...path.value)
149+
parts.push(source.value.branch, ...path.value)
142150

143151
return parts.filter(Boolean).join('/')
144152
})

0 commit comments

Comments
 (0)