|
| 1 | +<script setup lang="ts"> |
| 2 | +import { useData } from "vitepress"; |
| 3 | +import VPLink from "vitepress/dist/client/theme-default/components/VPLink.vue"; |
| 4 | +import { computed } from "vue"; |
| 5 | +import { Fabric } from "../../types"; |
| 6 | +
|
| 7 | +const data = useData(); |
| 8 | +
|
| 9 | +const options = computed(() => data.theme.value.references as Fabric.ReferencesOptions); |
| 10 | +
|
| 11 | +const resources = computed(() => |
| 12 | + Object.entries(data.frontmatter.value.resources ?? {}).map(([href, title]) => { |
| 13 | + const newHref = new URL(href, "https://a.com").href.replace("https://a.com", ""); |
| 14 | + return [newHref, title ?? newHref] as [string, string]; |
| 15 | + }) |
| 16 | +); |
| 17 | +
|
| 18 | +const files = computed(() => (data.frontmatter.value.files ?? []) as string[]); |
| 19 | +
|
| 20 | +const shortestUniquePaths = computed(() => |
| 21 | + files.value.map((file, i) => { |
| 22 | + const parts = file.split("/"); |
| 23 | + for (let len = 1; len <= parts.length; len++) { |
| 24 | + const current = parts.slice(-len).join("/"); |
| 25 | + const isUnique = files.value.every( |
| 26 | + (other, j) => i === j || other.split("/").slice(-len).join("/") !== current |
| 27 | + ); |
| 28 | + if (isUnique) return current; |
| 29 | + } |
| 30 | + return file; |
| 31 | + }) |
| 32 | +); |
| 33 | +
|
| 34 | +const getImageSrc = (href: string) => |
| 35 | + `https://www.google.com/s2/favicons?domain=${new URL(href, "https://docs.fabricmc.net").hostname}&sz=16`; |
| 36 | +
|
| 37 | +const getFileHref = (path: string) => |
| 38 | + path.replace(/^@/, "https://github.com/FabricMC/fabric-docs/blob/-"); |
| 39 | +
|
| 40 | +const getFileTitle = (path: string) => |
| 41 | + path.replace(/^@[/]reference[/][^/]+[/]/, "").replace("com/example/docs", "..."); |
| 42 | +</script> |
| 43 | + |
| 44 | +<template> |
| 45 | + <h2 v-if="resources.length">{{ options.resources }}</h2> |
| 46 | + <VPLink v-for="[href, title] in resources" :key="href" :href> |
| 47 | + <img :src="getImageSrc(href)" alt="" width="16" height="16" /> |
| 48 | + <span>{{ title }}</span> |
| 49 | + </VPLink> |
| 50 | + |
| 51 | + <h2 v-if="files.length">{{ options.files }}</h2> |
| 52 | + <VPLink v-for="(f, i) in files" :key="f" :href="getFileHref(f)" :title="getFileTitle(f)"> |
| 53 | + <code>{{ shortestUniquePaths[i] }}</code> |
| 54 | + </VPLink> |
| 55 | + |
| 56 | + <div /> |
| 57 | +</template> |
| 58 | + |
| 59 | +<style scoped> |
| 60 | +div, |
| 61 | +h2 { |
| 62 | + margin-top: 20px; |
| 63 | + padding-top: 16px; |
| 64 | + border-top: 1px solid var(--vp-c-divider); |
| 65 | + font-size: 12px; |
| 66 | + font-weight: bold; |
| 67 | + color: var(--vp-c-text-2); |
| 68 | + letter-spacing: 0.06em; |
| 69 | + text-transform: uppercase; |
| 70 | +} |
| 71 | +
|
| 72 | +.VPLink { |
| 73 | + display: flex; |
| 74 | + align-items: flex-start; |
| 75 | + line-height: 1.5; |
| 76 | + margin-bottom: 4px; |
| 77 | + gap: 0.3em; |
| 78 | + transition: color 0.15s; |
| 79 | + font-size: 12px; |
| 80 | + color: var(--vp-c-text-2); |
| 81 | +} |
| 82 | +
|
| 83 | +.VPLink:has(code) { |
| 84 | + align-items: center; |
| 85 | +} |
| 86 | +
|
| 87 | +.VPLink:hover { |
| 88 | + color: var(--vp-c-text-1); |
| 89 | +} |
| 90 | +
|
| 91 | +img { |
| 92 | + margin-top: 1px; |
| 93 | +} |
| 94 | +
|
| 95 | +@media (min-width: 1280px) { |
| 96 | + .VPDocFooter > h2, |
| 97 | + .VPDocFooter > .VPLink { |
| 98 | + display: none; |
| 99 | + } |
| 100 | +} |
| 101 | +</style> |
0 commit comments