UNPKG

@neosjs/vitepress-theme

Version:

NeosJS VitePress theme

60 lines (59 loc) 1.98 kB
import { withBase } from "vitepress"; import { useData } from "../composables/data.mjs"; import { inBrowser, isExternal, sanitizeFileName } from "../shared.mjs"; export function throttleAndDebounce(fn, delay) { let timeoutId; let called = false; return () => { if (timeoutId) clearTimeout(timeoutId); if (!called) { fn(); (called = true) && setTimeout(() => called = false, delay); } else { timeoutId = setTimeout(fn, delay); } }; } export function ensureStartingSlash(path) { return /^\//.test(path) ? path : `/${path}`; } export function normalizeLink(url) { if (isExternal(url)) return url; const { site } = useData(); const { pathname, search, hash } = new URL(url, "http://a.com"); const normalizedPath = pathname.endsWith("/") || pathname.endsWith(".html") ? url : url.replace( /(?:(^\.+)\/)?.*$/, `$1${pathname.replace( /(\.md)?$/, site.value.cleanUrls ? "" : ".html" )}${search}${hash}` ); return withBase(normalizedPath); } export function pathToFile(path) { let pagePath = path.replace(/\.html$/, ""); pagePath = decodeURIComponent(pagePath); pagePath = pagePath.replace(/\/$/, "/index"); if (import.meta.env.DEV) { pagePath += `.md?t=${Date.now()}`; } else { if (inBrowser) { const base = import.meta.env.BASE_URL; pagePath = `${sanitizeFileName( pagePath.slice(base.length).replace(/\//g, "_") || "index" )}.md`; let pageHash = __VP_HASH_MAP__[pagePath.toLowerCase()]; if (!pageHash) { pagePath = pagePath.endsWith("_index.md") ? `${pagePath.slice(0, -9)}.md` : `${pagePath.slice(0, -3)}_index.md`; pageHash = __VP_HASH_MAP__[pagePath.toLowerCase()]; } if (!pageHash) return null; pagePath = `${base}${__ASSETS_DIR__}/${pagePath}.${pageHash}.js`; } else { pagePath = `./${sanitizeFileName( pagePath.slice(1).replace(/\//g, "_") )}.md.js`; } } return pagePath; }