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'
35import SidebarCard from './components/SidebarCard.vue' 35import SidebarCard from './components/SidebarCard.vue'
36import { AccentBgStorageKey, AccentBgStrengthStorageKey, AccentColorStorageKey } from './constants' 36import { AccentBgStorageKey, AccentBgStrengthStorageKey, AccentColorStorageKey } from './constants'
37import { useEffects } from './composables/useEffects' 37import { useEffects } from './composables/useEffects'
38import { useFavicons } from './composables/useFavicons'
38 39
39const route = useRoute() 40const route = useRoute()
40const { frontmatter, isDark, site, theme } = useData() 41const { frontmatter, isDark, site, theme } = useData()
@@ -803,6 +804,110 @@ watchEffect(() => {
803 ) 804 )
804}) 805})
805 806
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
806type ShadeMap = Record<string, string> 911type ShadeMap = Record<string, string>
807type PaletteEntry = ShadeMap | { dark: ShadeMap; light: ShadeMap } 912type PaletteEntry = ShadeMap | { dark: ShadeMap; light: ShadeMap }
808 913
@@ -1078,9 +1183,13 @@ function setupManualCopyButtons() {
1078 if (!(node instanceof HTMLElement)) continue 1183 if (!(node instanceof HTMLElement)) continue
1079 if (node.classList.contains('plugin-tabs--content')) { 1184 if (node.classList.contains('plugin-tabs--content')) {
1080 initCopyButtonsInRoot(node) 1185 initCopyButtonsInRoot(node)
1186 if (faviconsEnabled.value) applyFavicons(node)
1081 } else { 1187 } else {
1082 node.querySelectorAll<HTMLElement>('.plugin-tabs--content').forEach( 1188 node.querySelectorAll<HTMLElement>('.plugin-tabs--content').forEach(
1083 panel => initCopyButtonsInRoot(panel) 1189 (panel) => {
1190 initCopyButtonsInRoot(panel)
1191 if (faviconsEnabled.value) applyFavicons(panel)
1192 }
1084 ) 1193 )
1085 } 1194 }
1086 } 1195 }
@@ -1295,6 +1404,7 @@ onUnmounted(() => {
1295 outlineFollowObserver?.disconnect() 1404 outlineFollowObserver?.disconnect()
1296 copyButtonObserver?.disconnect() 1405 copyButtonObserver?.disconnect()
1297 tocMutationObserver?.disconnect() 1406 tocMutationObserver?.disconnect()
1407 faviconObserver?.disconnect()
1298 cancelAnimationFrame(outlineFollowRaf) 1408 cancelAnimationFrame(outlineFollowRaf)
1299 cancelAnimationFrame(tocRaf) 1409 cancelAnimationFrame(tocRaf)
1300 window.removeEventListener(BOOKMARK_CHANGE_EVENT, syncTocBookmarkStates) 1410 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'
4import AccentBackground from './AccentBackground.vue' 4import AccentBackground from './AccentBackground.vue'
5import AccentColor from './AccentColor.vue' 5import AccentColor from './AccentColor.vue'
6import Effects from './Effects.vue' 6import Effects from './Effects.vue'
7import Favicons from './Favicons.vue'
7import SettingsFlyout from './SettingsFlyout.vue' 8import SettingsFlyout from './SettingsFlyout.vue'
8 9
9const mounted = useMounted() 10const mounted = useMounted()
@@ -21,6 +22,7 @@ const mounted = useMounted()
21 <AccentColor /> 22 <AccentColor />
22 <AccentBackground /> 23 <AccentBackground />
23 <Effects /> 24 <Effects />
25 <Favicons />
24 </div> 26 </div>
25 </SettingsFlyout> 27 </SettingsFlyout>
26</template> 28</template>
docs/.vitepress/theme/components/settings/ScreenMenu.vue +2
@@ -6,6 +6,7 @@ import VPSocialLinks from 'vitepress/dist/client/theme-default/components/VPSoci
6import AccentBackground from './AccentBackground.vue' 6import AccentBackground from './AccentBackground.vue'
7import AccentColor from './AccentColor.vue' 7import AccentColor from './AccentColor.vue'
8import Effects from './Effects.vue' 8import Effects from './Effects.vue'
9import Favicons from './Favicons.vue'
9 10
10const mounted = useMounted() 11const mounted = useMounted()
11const { theme } = useData() 12const { theme } = useData()
@@ -16,6 +17,7 @@ const { theme } = useData()
16 <AccentColor /> 17 <AccentColor />
17 <AccentBackground /> 18 <AccentBackground />
18 <Effects /> 19 <Effects />
20 <Favicons />
19 <VPSocialLinks 21 <VPSocialLinks
20 v-if="theme.socialLinks" 22 v-if="theme.socialLinks"
21 :links="theme.socialLinks" 23 :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 @@
1export const EffectsStorageKey = 'preference-effects' 1export const EffectsStorageKey = 'preference-effects'
2export const FaviconsStorageKey = 'preference-favicons'
2export const AccentColorStorageKey = 'preference-accent-color' 3export const AccentColorStorageKey = 'preference-accent-color'
3export const AccentBgStorageKey = 'preference-accent-bg' 4export const AccentBgStorageKey = 'preference-accent-bg'
4/** Accent background tint strength, 0–100. */ 5/** Accent background tint strength, 0–100. */
docs/.vitepress/theme/styles/style.scss +16
@@ -1699,6 +1699,22 @@ html.effects-disabled {
1699 } 1699 }
1700} 1700}
1701 1701
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
1702/* Icon tooltips (hover on desktop, tap on mobile) */ 1718/* Icon tooltips (hover on desktop, tap on mobile) */
1703.icon-tip { 1719.icon-tip {
1704 position: relative; 1720 position: relative;