Commit 6f13e0f9d6

6f13e0f9d672266fe6bc15ff9b5975179b36e043

parent: 0717e805b3

Unsigned

Duck <57977673+woducku@users.noreply.github.com> · 2026-07-18 08:41 UTC

init

Layout: unified · split

docs/.vitepress/theme/Layout.vue +111 −1
@@ -35,6 +35,7 @@ import NavActions from './components/NavActions.vue'
3535import SidebarCard from './components/SidebarCard.vue'
3636import { AccentBgStorageKey, AccentBgStrengthStorageKey, AccentColorStorageKey } from './constants'
3737import { useEffects } from './composables/useEffects'
38import { useFavicons } from './composables/useFavicons'
3839
3940const route = useRoute()
4041const { frontmatter, isDark, site, theme } = useData()
@@ -803,6 +804,110 @@ watchEffect(() => {
803804 )
804805})
805806
807const { faviconsEnabled } = useFavicons()
808
809let faviconObserver: IntersectionObserver | null = null
810
811// favicons for external links
812const FAVICON_BASE_URL = 'https://wfav.pages.dev/'
813let faviconManifestPromise: Promise<Record<string, string>> | null = null
814
815function getFaviconManifest() {
816 if (!faviconManifestPromise) {
817 faviconManifestPromise = fetch(`${FAVICON_BASE_URL}manifest.json`)
818 .then((res) => res.ok ? res.json() : Promise.reject(new Error(`HTTP ${res.status}`)))
819 .then((data) => (data.icons ?? {}) as Record<string, string>)
820 .catch(() => ({}) as Record<string, string>)
821 }
822 return faviconManifestPromise
823}
824
825function showGlobeFallback(el: HTMLElement) {
826 const span = document.createElement('span')
827 span.className = 'wk-favicon wk-favicon-fallback i-lucide:globe'
828 span.setAttribute('aria-hidden', 'true')
829 el.replaceWith(span)
830}
831
832async function loadFavicon(img: HTMLImageElement, domain: string) {
833 const icons = await getFaviconManifest()
834 const path = icons[domain]
835 if (!path) {
836 showGlobeFallback(img)
837 return
838 }
839 img.onerror = () => showGlobeFallback(img)
840 img.src = `${FAVICON_BASE_URL}${path}`
841}
842
843function ensureFaviconObserver() {
844 if (faviconObserver) return faviconObserver
845 faviconObserver = new IntersectionObserver((entries, observer) => {
846 for (const entry of entries) {
847 if (!entry.isIntersecting) continue
848 const img = entry.target as HTMLImageElement
849 const domain = img.dataset.domain
850 if (domain) loadFavicon(img, domain)
851 observer.unobserve(img)
852 }
853 }, { rootMargin: '200px' })
854 return faviconObserver
855}
856
857// Only plain `[text](url)` prose links
858function applyFavicons(root: ParentNode) {
859 const origin = window.location.origin
860 const observer = ensureFaviconObserver()
861
862 root
863 .querySelectorAll<HTMLAnchorElement>('.vp-doc a[href]:not([data-favicon-applied])')
864 .forEach((link) => {
865 const href = link.getAttribute('href') ?? ''
866 if (!/^https?:\/\//i.test(href) || href.startsWith(origin)) return
867 if (link.querySelector('hl, .icon-tip, img, svg')) return
868 if (!link.textContent?.trim()) return
869
870 let hostname: string
871 try {
872 hostname = new URL(href).hostname
873 } catch {
874 return
875 }
876
877 link.dataset.faviconApplied = '1'
878 const img = document.createElement('img')
879 img.className = 'wk-favicon'
880 img.alt = ''
881 img.width = 14
882 img.height = 14
883 img.decoding = 'async'
884 img.dataset.domain = hostname
885 link.prepend(img)
886 observer.observe(img)
887 })
888}
889
890function removeFavicons() {
891 faviconObserver?.disconnect()
892 faviconObserver = null
893 document.querySelectorAll('.wk-favicon').forEach((el) => el.remove())
894 document
895 .querySelectorAll<HTMLElement>('.vp-doc a[data-favicon-applied]')
896 .forEach((link) => { delete link.dataset.faviconApplied })
897}
898
899watch(faviconsEnabled, (enabled) => {
900 if (import.meta.env.SSR) return
901 if (enabled) nextTick(() => applyFavicons(document))
902 else removeFavicons()
903}, { immediate: true })
904
905watch(
906 () => route.path,
907 () => nextTick(() => { if (faviconsEnabled.value) applyFavicons(document) }),
908 { flush: 'post' }
909)
910
806911type ShadeMap = Record<string, string>
807912type PaletteEntry = ShadeMap | { dark: ShadeMap; light: ShadeMap }
808913
@@ -1078,9 +1183,13 @@ function setupManualCopyButtons() {
10781183 if (!(node instanceof HTMLElement)) continue
10791184 if (node.classList.contains('plugin-tabs--content')) {
10801185 initCopyButtonsInRoot(node)
1186 if (faviconsEnabled.value) applyFavicons(node)
10811187 } else {
10821188 node.querySelectorAll<HTMLElement>('.plugin-tabs--content').forEach(
1083 panel => initCopyButtonsInRoot(panel)
1189 (panel) => {
1190 initCopyButtonsInRoot(panel)
1191 if (faviconsEnabled.value) applyFavicons(panel)
1192 }
10841193 )
10851194 }
10861195 }
@@ -1295,6 +1404,7 @@ onUnmounted(() => {
12951404 outlineFollowObserver?.disconnect()
12961405 copyButtonObserver?.disconnect()
12971406 tocMutationObserver?.disconnect()
1407 faviconObserver?.disconnect()
12981408 cancelAnimationFrame(outlineFollowRaf)
12991409 cancelAnimationFrame(tocRaf)
13001410 window.removeEventListener(BOOKMARK_CHANGE_EVENT, syncTocBookmarkStates)
docs/.vitepress/theme/components/settings/Favicons.vue added +60
@@ -0,0 +1,60 @@
1<script setup lang="ts">
2import { NuInputHighlight, NuInputHorizontalRadioGroup } from '@nolebase/ui'
3import { computed, ref } from 'vue'
4
5import MenuHelp from './MenuHelp.vue'
6import MenuTitle from './MenuTitle.vue'
7
8import { useFavicons } from '../../composables/useFavicons'
9
10const menuTitleElementRef = ref<HTMLDivElement>()
11const isMenuHelpPoppedUp = ref(false)
12
13const { favicons } = useFavicons()
14
15const fieldOptions = computed(() => [
16 {
17 value: 'on',
18 title: 'On',
19 ariaLabel: 'On',
20 text: 'ON',
21 name: 'Favicons'
22 },
23 {
24 value: 'off',
25 title: 'Off',
26 ariaLabel: 'Off',
27 text: 'OFF',
28 name: 'Favicons'
29 }
30])
31</script>
32
33<template>
34 <div space-y-2 role="radiogroup">
35 <div ref="menuTitleElementRef" relative flex items-center>
36 <MenuTitle title="Favicons" aria-label="Favicons" flex="1" pr-4>
37 <template #icon>
38 <span i-lucide:globe mr-1 aria-hidden="true" />
39 </template>
40 </MenuTitle>
41 <MenuHelp
42 v-model:is-popped-up="isMenuHelpPoppedUp"
43 :menu-title-element-ref="menuTitleElementRef"
44 >
45 <h4 text-md mb-1 font-semibold>Favicons</h4>
46 <p text="sm" mb-2 max-w-100>
47 Shows favicons beside external links.
48 </p>
49 </MenuHelp>
50 </div>
51 <NuInputHighlight :active="isMenuHelpPoppedUp" class="rounded-md">
52 <NuInputHorizontalRadioGroup
53 v-model="favicons"
54 bg="$vp-nolebase-enhanced-readabilities-menu-background-color"
55 text="sm $vp-nolebase-enhanced-readabilities-menu-text-color"
56 :options="fieldOptions"
57 />
58 </NuInputHighlight>
59 </div>
60</template>
docs/.vitepress/theme/components/settings/Menu.vue +2
@@ -4,6 +4,7 @@ import { useMounted } from '@vueuse/core'
44import AccentBackground from './AccentBackground.vue'
55import AccentColor from './AccentColor.vue'
66import Effects from './Effects.vue'
7import Favicons from './Favicons.vue'
78import SettingsFlyout from './SettingsFlyout.vue'
89
910const mounted = useMounted()
@@ -21,6 +22,7 @@ const mounted = useMounted()
2122 <AccentColor />
2223 <AccentBackground />
2324 <Effects />
25 <Favicons />
2426 </div>
2527 </SettingsFlyout>
2628</template>
docs/.vitepress/theme/components/settings/ScreenMenu.vue +2
@@ -6,6 +6,7 @@ import VPSocialLinks from 'vitepress/dist/client/theme-default/components/VPSoci
66import AccentBackground from './AccentBackground.vue'
77import AccentColor from './AccentColor.vue'
88import Effects from './Effects.vue'
9import Favicons from './Favicons.vue'
910
1011const mounted = useMounted()
1112const { theme } = useData()
@@ -16,6 +17,7 @@ const { theme } = useData()
1617 <AccentColor />
1718 <AccentBackground />
1819 <Effects />
20 <Favicons />
1921 <VPSocialLinks
2022 v-if="theme.socialLinks"
2123 :links="theme.socialLinks"
docs/.vitepress/theme/composables/useFavicons.ts added +11
@@ -0,0 +1,11 @@
1import { useStorage } from '@vueuse/core'
2import { computed } from 'vue'
3
4import { FaviconsStorageKey } from '../constants'
5
6export function useFavicons() {
7 const favicons = useStorage(FaviconsStorageKey, 'off')
8 const faviconsEnabled = computed(() => favicons.value === 'on')
9
10 return { favicons, faviconsEnabled }
11}
docs/.vitepress/theme/constants.ts +1
@@ -1,4 +1,5 @@
11export const EffectsStorageKey = 'preference-effects'
2export const FaviconsStorageKey = 'preference-favicons'
23export const AccentColorStorageKey = 'preference-accent-color'
34export const AccentBgStorageKey = 'preference-accent-bg'
45/** Accent background tint strength, 0–100. */
docs/.vitepress/theme/styles/style.scss +16
@@ -1699,6 +1699,22 @@ html.effects-disabled {
16991699 }
17001700}
17011701
1702// favicons beside external links
1703.wk-favicon {
1704 display: inline-block;
1705 width: 14px;
1706 height: 14px;
1707 margin: 0 0.3em 0 0.1em;
1708 vertical-align: -2px;
1709 border-radius: 3px;
1710 background: var(--vp-c-default-soft);
1711}
1712
1713.wk-favicon-fallback {
1714 background: none;
1715 color: var(--vp-c-text-3);
1716}
1717
17021718/* Icon tooltips (hover on desktop, tap on mobile) */
17031719.icon-tip {
17041720 position: relative;