| @@ -35,6 +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 | |
39 | |
| 39 | const route = useRoute() |
40 | const route = useRoute() |
| 40 | const { frontmatter, isDark, site, theme } = useData() |
41 | const { frontmatter, isDark, site, theme } = useData() |
| @@ -803,6 +804,110 @@ watchEffect(() => { |
| 803 | ) |
804 | ) |
| 804 | }) |
805 | }) |
| 805 | |
806 | |
| |
807 | const { faviconsEnabled } = useFavicons() |
| |
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('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 | |
| |
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) => { |
| |
900 | if (import.meta.env.SSR) return |
| |
901 | if (enabled) nextTick(() => applyFavicons(document)) |
| |
902 | else removeFavicons() |
| |
903 | }, { immediate: true }) |
| |
904 | |
| |
905 | watch( |
| |
906 | () => route.path, |
| |
907 | () => nextTick(() => { if (faviconsEnabled.value) applyFavicons(document) }), |
| |
908 | { flush: 'post' } |
| |
909 | ) |
| |
910 | |
| 806 | type ShadeMap = Record<string, string> |
911 | type ShadeMap = Record<string, string> |
| 807 | type PaletteEntry = ShadeMap | { dark: ShadeMap; light: ShadeMap } |
912 | type 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) |