/
EditOnLink.vue
166 lines (145 loc) · 3.84 KB
/
EditOnLink.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<script lang="ts">
import { joinURL } from 'ufo'
import type { PropType } from 'vue'
import { computed, defineComponent, useSlots } from 'vue'
import { useAppConfig } from '#imports'
export default defineComponent({
props: {
/**
* Repository owner.
*/
owner: {
type: String,
default: () => useAppConfig()?.docus?.github?.owner,
required: false
},
/**
* Repository name.
*/
repo: {
type: String,
default: () => useAppConfig()?.docus?.github?.repo,
required: false
},
/**
* The branch to use for the edit link.
*/
branch: {
type: String,
default: () => useAppConfig()?.docus?.github?.branch,
required: false
},
/**
* A base directory to append to the source path.
*
* Won't be used if `page` is set.
*/
dir: {
type: String,
default: () => useAppConfig()?.docus?.github?.dir,
required: false
},
/**
* Source file path.
*
* Won't be used if `page` is set.
*/
source: {
type: String,
required: false,
default: undefined
},
/**
* Use page from @nuxt/content.
*/
page: {
type: Object as PropType<any>,
required: false,
default: undefined
},
/**
* Content directory (to be used with `page`)
*/
contentDir: {
type: String,
required: false,
default: () => useAppConfig()?.docus?.github?.dir || 'content'
},
/**
* Send to an edit page or not.
*/
edit: {
type: Boolean,
required: false,
default: () => useAppConfig()?.docus?.github?.edit
}
},
setup (props) {
if (!props.owner || !props.repo || !props.branch) {
throw new Error('If you want to use `GithubLink` component, you must specify: `owner`, `repo` and `branch`.')
}
const source = computed(() => {
let { repo, owner, branch, contentDir } = props
let prefix = ''
// Resolve source from content sources
if (useAppConfig()?.public?.content) {
let source
const { sources } = useAppConfig().public.content
for (const key in sources || []) {
if (props.page._id.startsWith(key)) {
source = sources[key]
break
}
}
if (source?.driver === 'github') {
repo = source.repo || props.repo || ''
owner = source.owner || props.owner || ''
branch = source.branch || props.branch || 'main'
contentDir = source.dir || props.contentDir || ''
prefix = source.prefix || ''
}
}
return { repo, owner, branch, contentDir, prefix }
})
const base = computed(() => joinURL('https://github.com', `${source.value.owner}/${source.value.repo}`))
const path = computed(() => {
const dirParts: string[] = []
// @nuxt/content support
// Create the URL from a document data.
if (props?.page?._path) {
// Use content dir
if (source.value.contentDir) { dirParts.push(source.value.contentDir) }
// Get page file from page data
dirParts.push(props.page._file.substring(source.value.prefix.length))
return dirParts
}
// Use props dir
if (props.dir) {
dirParts.push(props.dir)
}
// Use props source
if (props.source) {
dirParts.push(props.source)
}
return dirParts
})
/**
* Create edit link.
*/
const url = computed(() => {
const parts = [base.value]
if (props.edit) { parts.push('edit') } else { parts.push('tree') }
parts.push(source?.value?.branch || '', ...path.value)
return parts.filter(Boolean).join('/')
})
return {
url
}
},
render (ctx: any) {
const { url } = ctx
const slots = useSlots()
return slots?.default?.({ url })
}
})
</script>