doc.yeswiki.pro/node_modules/vitepress/dist/client/theme-default/support/utils.js

37 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-05-20 16:37:42 +00:00
import { withBase } from 'vitepress';
import { useData } from '../composables/data';
import { isExternal, PATHNAME_PROTOCOL_RE } from '../../shared';
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.replace(PATHNAME_PROTOCOL_RE, '');
}
const { site } = useData();
const { pathname, search, hash } = new URL(url, 'http://example.com');
const normalizedPath = pathname.endsWith('/') || pathname.endsWith('.html')
? url
: url.replace(/(?:(^\.+)\/)?.*$/, `$1${pathname.replace(/(\.md)?$/, site.value.cleanUrls ? '' : '.html')}${search}${hash}`);
return withBase(normalizedPath);
}