Commit 4c25a2847f
Unsigned
Layout: unified · split
docs/.vitepress/theme/Layout.vue +1 −91
| @@ -35,7 +35,7 @@ import NavActions from './components/NavActions.vue' | |||
| 35 | import SidebarCard from './components/SidebarCard.vue' | 35 | import SidebarCard from './components/SidebarCard.vue' |
| 36 | import { AccentBgStorageKey, AccentBgStrengthStorageKey, AccentColorStorageKey } from './constants' | 36 | import { AccentBgStorageKey, AccentBgStrengthStorageKey, AccentColorStorageKey } from './constants' |
| 37 | import { useEffects } from './composables/useEffects' | 37 | import { useEffects } from './composables/useEffects' |
| 38 | import { useFavicons } from './composables/useFavicons' | 38 | import { applyFavicons, removeFavicons, useFavicons } from './composables/useFavicons' |
| 39 | 39 | ||
| 40 | const route = useRoute() | 40 | const route = useRoute() |
| 41 | const { frontmatter, isDark, site, theme } = useData() | 41 | const { frontmatter, isDark, site, theme } = useData() |
| @@ -806,96 +806,6 @@ watchEffect(() => { | |||
| 806 | 806 | ||
| 807 | const { faviconsEnabled } = useFavicons() | 807 | const { faviconsEnabled } = useFavicons() |
| 808 | 808 | ||
| 809 | let faviconObserver: IntersectionObserver | null = null | ||
| 810 | |||
| 811 | // favicons for external links | ||
| 812 | const FAVICON_BASE_URL = 'https://wfav.pages.dev/' | ||
| 813 | let faviconManifestPromise: Promise<Record<string, string>> | null = null | ||
| 814 | |||
| 815 | function 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 | |||
| 825 | function 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 | |||
| 832 | async 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 | |||
| 843 | function 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 | ||
| 858 | function 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('.highlight-pill, .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 | |||
| 890 | function 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 | |||
| 899 | watch(faviconsEnabled, (enabled) => { | 809 | watch(faviconsEnabled, (enabled) => { |
| 900 | if (import.meta.env.SSR) return | 810 | if (import.meta.env.SSR) return |
| 901 | if (enabled) nextTick(() => applyFavicons(document)) | 811 | if (enabled) nextTick(() => applyFavicons(document)) |
docs/.vitepress/theme/components/Search.vue +7
| @@ -63,6 +63,7 @@ import { LRUCache } from '../composables/search/lru-cache' | |||
| 63 | import { createSearchTranslate } from '../composables/search/translation' | 63 | import { createSearchTranslate } from '../composables/search/translation' |
| 64 | import { useData } from '../composables/search/use-data' | 64 | import { useData } from '../composables/search/use-data' |
| 65 | import { useEffects } from '../composables/useEffects' | 65 | import { useEffects } from '../composables/useEffects' |
| 66 | import { applyFavicons, useFavicons } from '../composables/useFavicons' | ||
| 66 | import { registerGlobalComponents } from '../globalComponents' | 67 | import { registerGlobalComponents } from '../globalComponents' |
| 67 | import { enhanceAppWithTabs } from './tabs' | 68 | import { enhanceAppWithTabs } from './tabs' |
| 68 | 69 | ||
| @@ -140,6 +141,7 @@ const filterText = disableQueryPersistence.value | |||
| 140 | : useSessionStorage('vitepress:local-search-filter', '') | 141 | : useSessionStorage('vitepress:local-search-filter', '') |
| 141 | 142 | ||
| 142 | const { effectsEnabled: searchAnimationsEnabled } = useEffects() | 143 | const { effectsEnabled: searchAnimationsEnabled } = useEffects() |
| 144 | const { faviconsEnabled } = useFavicons() | ||
| 143 | const showKeyboardShortcuts = computed(() => !filterText.value) | 145 | const showKeyboardShortcuts = computed(() => !filterText.value) |
| 144 | 146 | ||
| 145 | const SearchMotionDiv = motion.div | 147 | const SearchMotionDiv = motion.div |
| @@ -1673,6 +1675,11 @@ watch(filteredUrlResults, (r) => { | |||
| 1673 | } | 1675 | } |
| 1674 | }) | 1676 | }) |
| 1675 | 1677 | ||
| 1678 | watch([pagedResults, filteredUrlResults, faviconsEnabled], () => { | ||
| 1679 | if (!faviconsEnabled.value) return | ||
| 1680 | nextTick(() => { if (resultsEl.value) applyFavicons(resultsEl.value) }) | ||
| 1681 | }) | ||
| 1682 | |||
| 1676 | function scrollToSelectedResult() { | 1683 | function scrollToSelectedResult() { |
| 1677 | nextTick(() => { | 1684 | nextTick(() => { |
| 1678 | const selectedEl = document.querySelector('.result.selected') | 1685 | const selectedEl = document.querySelector('.result.selected') |
docs/.vitepress/theme/composables/useFavicons.ts +88
| @@ -9,3 +9,91 @@ export function useFavicons() { | |||
| 9 | 9 | ||
| 10 | return { favicons, faviconsEnabled } | 10 | return { favicons, faviconsEnabled } |
| 11 | } | 11 | } |
| 12 | |||
| 13 | const FAVICON_BASE_URL = 'https://wfav.pages.dev/' | ||
| 14 | let faviconManifestPromise: Promise<Record<string, string>> | null = null | ||
| 15 | let faviconObserver: IntersectionObserver | null = null | ||
| 16 | |||
| 17 | function getFaviconManifest() { | ||
| 18 | if (!faviconManifestPromise) { | ||
| 19 | faviconManifestPromise = fetch(`${FAVICON_BASE_URL}manifest.json`) | ||
| 20 | .then((res) => res.ok ? res.json() : Promise.reject(new Error(`HTTP ${res.status}`))) | ||
| 21 | .then((data) => (data.icons ?? {}) as Record<string, string>) | ||
| 22 | .catch(() => ({}) as Record<string, string>) | ||
| 23 | } | ||
| 24 | return faviconManifestPromise | ||
| 25 | } | ||
| 26 | |||
| 27 | function showGlobeFallback(el: HTMLElement) { | ||
| 28 | const span = document.createElement('span') | ||
| 29 | span.className = 'wk-favicon wk-favicon-fallback i-lucide:globe' | ||
| 30 | span.setAttribute('aria-hidden', 'true') | ||
| 31 | el.replaceWith(span) | ||
| 32 | } | ||
| 33 | |||
| 34 | async function loadFavicon(img: HTMLImageElement, domain: string) { | ||
| 35 | const icons = await getFaviconManifest() | ||
| 36 | const path = icons[domain] | ||
| 37 | if (!path) { | ||
| 38 | showGlobeFallback(img) | ||
| 39 | return | ||
| 40 | } | ||
| 41 | img.onerror = () => showGlobeFallback(img) | ||
| 42 | img.src = `${FAVICON_BASE_URL}${path}` | ||
| 43 | } | ||
| 44 | |||
| 45 | function ensureFaviconObserver() { | ||
| 46 | if (faviconObserver) return faviconObserver | ||
| 47 | faviconObserver = new IntersectionObserver((entries, observer) => { | ||
| 48 | for (const entry of entries) { | ||
| 49 | if (!entry.isIntersecting) continue | ||
| 50 | const img = entry.target as HTMLImageElement | ||
| 51 | const domain = img.dataset.domain | ||
| 52 | if (domain) loadFavicon(img, domain) | ||
| 53 | observer.unobserve(img) | ||
| 54 | } | ||
| 55 | }, { rootMargin: '200px' }) | ||
| 56 | return faviconObserver | ||
| 57 | } | ||
| 58 | |||
| 59 | // Only plain `[text](url)` prose links | ||
| 60 | export function applyFavicons(root: ParentNode) { | ||
| 61 | const origin = window.location.origin | ||
| 62 | const observer = ensureFaviconObserver() | ||
| 63 | |||
| 64 | root | ||
| 65 | .querySelectorAll<HTMLAnchorElement>('.vp-doc a[href]:not([data-favicon-applied])') | ||
| 66 | .forEach((link) => { | ||
| 67 | const href = link.getAttribute('href') ?? '' | ||
| 68 | if (!/^https?:\/\//i.test(href) || href.startsWith(origin)) return | ||
| 69 | if (link.querySelector('.highlight-pill, .icon-tip, img, svg')) return | ||
| 70 | if (!link.textContent?.trim()) return | ||
| 71 | |||
| 72 | let hostname: string | ||
| 73 | try { | ||
| 74 | hostname = new URL(href).hostname | ||
| 75 | } catch { | ||
| 76 | return | ||
| 77 | } | ||
| 78 | |||
| 79 | link.dataset.faviconApplied = '1' | ||
| 80 | const img = document.createElement('img') | ||
| 81 | img.className = 'wk-favicon' | ||
| 82 | img.alt = '' | ||
| 83 | img.width = 14 | ||
| 84 | img.height = 14 | ||
| 85 | img.decoding = 'async' | ||
| 86 | img.dataset.domain = hostname | ||
| 87 | link.prepend(img) | ||
| 88 | observer.observe(img) | ||
| 89 | }) | ||
| 90 | } | ||
| 91 | |||
| 92 | export function removeFavicons() { | ||
| 93 | faviconObserver?.disconnect() | ||
| 94 | faviconObserver = null | ||
| 95 | document.querySelectorAll('.wk-favicon').forEach((el) => el.remove()) | ||
| 96 | document | ||
| 97 | .querySelectorAll<HTMLElement>('.vp-doc a[data-favicon-applied]') | ||
| 98 | .forEach((link) => { delete link.dataset.faviconApplied }) | ||
| 99 | } | ||