-
Notifications
You must be signed in to change notification settings - Fork 153
/
open-graph.vue
185 lines (169 loc) · 5.07 KB
/
open-graph.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<script setup lang="ts">
import type { NormalizedHeadTag } from '~/../src/types/ui-state'
import { ogTags } from '~/data/open-graph'
definePageMeta({
icon: 'carbon:image-search',
title: 'Open Graph',
layout: 'full',
category: 'analyze',
show: () => {
const client = useClient()
return () => !!client.value
},
})
const counter = ref(0)
const head = useClientHead()
const headTags = computedAsync(async () => {
// eslint-disable-next-line no-unused-expressions
counter.value // for force refresh
const tags = await head.value?.resolveTags()
return tags.map((tag): NormalizedHeadTag => {
const props = tag.props || {}
if (tag.tag === 'htmlAttrs' && props.lang) {
return {
tag: 'html',
name: 'lang',
value: props.lang,
}
}
if (props.charset) {
return {
tag: 'meta',
name: 'charset',
value: props.charset,
}
}
return {
tag: tag.tag,
name: props.property ?? props.name ?? props.rel ?? tag.tag,
value: props.content ?? props.href ?? tag.textContent ?? JSON.stringify(props),
}
})
}, [])
const showPreview = ref(true)
function refresh() {
counter.value += 1
}
const router = useClientRouter()
const route = useClientRoute()
const routeInput = ref('')
const routes = useMergedRouteList()
const openInEditor = useOpenInEditor()
async function navigate() {
if (routeInput.value !== route.value.path)
router.value.push(routeInput.value || '/')
}
const routeInputMatched = computed(() => {
if (routeInput.value === route.value.path)
return []
return router.value.resolve(routeInput.value || '/').matched
})
const routeMatchedFilePath = computed(() => {
const matched = routes.value.find(i => i.path === route.value?.matched?.[0]?.path)
return matched?.file || matched?.meta?.file as string
})
function getDocs(item: NormalizedHeadTag) {
return ogTags.find(i => i.name === item.name)?.docs
}
until(route).toBeTruthy().then((v) => {
routeInput.value = v.path
})
until(router).toBeTruthy().then((v) => {
v.afterEach(() => {
nextTick(() => {
routeInput.value = route.value.path
// try refresh after route change, to get async updates
setTimeout(refresh, 200)
setTimeout(refresh, 800)
})
})
})
</script>
<template>
<div flex="~ col lg:row" h-full w-full of-hidden>
<div flex-auto of-auto>
<NNavbar>
<template #search>
<NTextInput
v-model="routeInput"
placeholder="Route"
icon="carbon-direction-right-01 scale-y--100"
n="primary" flex-auto font-mono
class="px-5 py-2"
:class="route?.path === routeInput ? '' : routeInputMatched.length ? 'text-green' : 'text-orange' "
@keydown.enter="navigate"
/>
</template>
<template #actions>
<div flex-none flex="~ gap2 items-center">
<NButton
v-if="routeMatchedFilePath"
v-tooltip="'Open file in editor'"
text-lg :border="false"
icon="carbon:launch"
title="Open file in editor"
@click="openInEditor(routeMatchedFilePath)"
/>
<NButton
v-tooltip="'Refresh Data'"
text-lg :border="false"
icon="carbon:reset"
title="Refresh Data"
@click="refresh"
/>
<NButton
v-tooltip="'Toggle Preview'"
text-lg :border="false"
:icon="showPreview ? 'carbon:side-panel-open' : 'carbon:open-panel-right'"
title="Toggle Preview"
@click="showPreview = !showPreview"
/>
</div>
</template>
</NNavbar>
<div flex="~ col">
<NSectionBlock
text="Tags"
icon="carbon:tag-group"
>
<!-- TODO: show current route -->
<NCard grid="~ cols-[max-content_1fr]" items-center justify-between of-hidden>
<template v-for="item, index of headTags" :key="index">
<div v-if="index" x-divider />
<div v-if="index" x-divider />
<div mr2 px4 py2>
<NLink
op50
:href="getDocs(item)"
target="_blank"
n="primary"
>
{{ item.name }}
</NLink>
</div>
<NLink
:href="String(item.value).match(/^https?:\/\//) ? item.value : undefined"
target="_blank"
w-full p2 font-mono
n="primary"
>
{{ item.value }}
</NLink>
</template>
</NCard>
</NSectionBlock>
<OpenGraphMissingTabs :tags="headTags" :matched-route-filepath="routeMatchedFilePath" />
</div>
</div>
<SocialPreviewGroup
v-if="showPreview && headTags?.length"
:tags="headTags"
flex-none border-base
lt-lg="border-t"
lg="h-full w-140 border-l"
/>
</div>
<HelpFab>
<DocsOpenGraph />
</HelpFab>
</template>