Initial rearrangment of vuetom site source
This commit is contained in:
42
packages/vuetom/blog/support/sidebar.ts
Normal file
42
packages/vuetom/blog/support/sidebar.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { DefaultTheme } from 'vitepress/theme'
|
||||
import { ensureStartingSlash } from './utils.js'
|
||||
|
||||
/**
|
||||
* Get the `Sidebar` from sidebar option. This method will ensure to get correct
|
||||
* sidebar config from `MultiSideBarConfig` with various path combinations such
|
||||
* as matching `guide/` and `/guide/`. If no matching config was found, it will
|
||||
* return empty array.
|
||||
*/
|
||||
export function getSidebar(
|
||||
sidebar: DefaultTheme.Sidebar,
|
||||
path: string
|
||||
): DefaultTheme.SidebarGroup[] {
|
||||
if (Array.isArray(sidebar)) {
|
||||
return sidebar
|
||||
}
|
||||
|
||||
path = ensureStartingSlash(path)
|
||||
|
||||
for (const dir in sidebar) {
|
||||
// make sure the multi sidebar key starts with slash too
|
||||
if (path.startsWith(ensureStartingSlash(dir))) {
|
||||
return sidebar[dir]
|
||||
}
|
||||
}
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
export function getFlatSideBarLinks(
|
||||
sidebar: DefaultTheme.SidebarGroup[]
|
||||
): DefaultTheme.SidebarItem[] {
|
||||
const links: DefaultTheme.SidebarItem[] = []
|
||||
|
||||
for (const group of sidebar) {
|
||||
for (const link of group.items) {
|
||||
links.push(link)
|
||||
}
|
||||
}
|
||||
|
||||
return links
|
||||
}
|
90
packages/vuetom/blog/support/utils.ts
Normal file
90
packages/vuetom/blog/support/utils.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
/** @format */
|
||||
|
||||
import { ref } from 'vue'
|
||||
import { withBase } from 'vitepress'
|
||||
import { EXTERNAL_URL_RE } from '../shared.js'
|
||||
|
||||
export const HASH_RE = /#.*$/
|
||||
export const EXT_RE = /(index)?\.(md|html)$/
|
||||
|
||||
const inBrowser = typeof window !== 'undefined'
|
||||
const hashRef = ref(inBrowser ? location.hash : '')
|
||||
|
||||
export function isExternal(path: string): boolean {
|
||||
return EXTERNAL_URL_RE.test(path)
|
||||
}
|
||||
|
||||
export function throttleAndDebounce(fn: () => void, delay: number): () => void {
|
||||
let timeout: any
|
||||
let called = false
|
||||
|
||||
return () => {
|
||||
if (timeout) {
|
||||
clearTimeout(timeout)
|
||||
}
|
||||
|
||||
if (!called) {
|
||||
fn()
|
||||
called = true
|
||||
setTimeout(() => {
|
||||
called = false
|
||||
}, delay)
|
||||
} else {
|
||||
timeout = setTimeout(fn, delay)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function isActive(
|
||||
currentPath: string,
|
||||
matchPath?: string,
|
||||
asRegex: boolean = false
|
||||
): boolean {
|
||||
if (matchPath === undefined) {
|
||||
return false
|
||||
}
|
||||
|
||||
currentPath = normalize(`/${currentPath}`)
|
||||
|
||||
if (asRegex) {
|
||||
return new RegExp(matchPath).test(currentPath)
|
||||
}
|
||||
|
||||
if (normalize(matchPath) !== currentPath) {
|
||||
return false
|
||||
}
|
||||
|
||||
const hashMatch = matchPath.match(HASH_RE)
|
||||
|
||||
if (hashMatch) {
|
||||
return hashRef.value === hashMatch[0]
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
export function ensureStartingSlash(path: string): string {
|
||||
return /^\//.test(path) ? path : `/${path}`
|
||||
}
|
||||
|
||||
export function normalize(path: string): string {
|
||||
return decodeURI(path).replace(HASH_RE, '').replace(EXT_RE, '')
|
||||
}
|
||||
|
||||
export function normalizeLink(url: string): string {
|
||||
if (isExternal(url)) {
|
||||
return url
|
||||
}
|
||||
|
||||
const { pathname, search, hash } = new URL(url, 'http://example.com')
|
||||
|
||||
const normalizedPath = pathname.endsWith('/') || pathname.endsWith('.html')
|
||||
? url
|
||||
: `${pathname.replace(/(\.md)?$/, '.html')}${search}${hash}`
|
||||
|
||||
return withBase(normalizedPath)
|
||||
}
|
||||
|
||||
export function fmt(inputTime: string) {
|
||||
return inputTime.replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
|
||||
}
|
Reference in New Issue
Block a user