Skip to content

Commit 11f88e7

Browse files
authored
feat(GithubLink): handle contents from remote github source (#32)
1 parent dc5c6dd commit 11f88e7

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/runtime/components/GithubLink.ts

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

83-
const base = computed(() => joinURL('https://github.com', `${props.owner}/${props.repo}`))
83+
let repo = props.repo
84+
let owner = props.owner
85+
let branch = props.branch
86+
let contentDir = props.contentDir
87+
let prefix = ''
88+
const { sources } = useRuntimeConfig().content
89+
let source
90+
for (const key in Object.keys(sources)) {
91+
if (props.page._id.startsWith(key)) {
92+
source = sources[key]
93+
break
94+
}
95+
}
96+
97+
if (source?.driver === 'github') {
98+
repo = source.repo
99+
owner = ''
100+
branch = source.branch || 'main'
101+
contentDir = source.dir || ''
102+
prefix = source.prefix || ''
103+
}
104+
105+
const base = computed(() => joinURL('https://github.com', `${owner}/${repo}`))
84106

85107
const path = computed(() => {
86-
const dirParts = []
108+
const dirParts: string[] = []
87109

88110
// @nuxt/content support
89111
// Create the URL from a document data.
90112
if (props?.page?._path) {
91113
// Use content dir
92-
if (props.contentDir) { dirParts.push(props.contentDir) }
114+
if (contentDir) { dirParts.push(contentDir) }
93115

94116
// Get page file from page data
95-
dirParts.push(props.page._file)
117+
dirParts.push(props.page._file.substring(prefix.length))
96118

97119
return dirParts
98120
}
@@ -118,7 +140,7 @@ export default defineComponent({
118140

119141
if (props.edit) { parts.push('edit') } else { parts.push('tree') }
120142

121-
parts.push(props.branch, ...path.value)
143+
parts.push(branch, ...path.value)
122144

123145
return parts.filter(Boolean).join('/')
124146
})

0 commit comments

Comments
 (0)