2021-09-17 00:18:50 +08:00
|
|
|
import {
|
|
|
|
endingSlashRE,
|
2022-03-25 15:35:56 +08:00
|
|
|
isActive,
|
2021-09-17 00:18:50 +08:00
|
|
|
isExternal,
|
2022-03-25 15:35:56 +08:00
|
|
|
normalize,
|
2021-09-17 00:18:50 +08:00
|
|
|
} from 'vitepress/dist/client/theme-default/utils'
|
2021-09-24 01:19:18 +08:00
|
|
|
|
2021-09-17 00:18:50 +08:00
|
|
|
import type { Route } from 'vitepress'
|
|
|
|
|
2022-02-22 16:42:54 +08:00
|
|
|
export * from './colors'
|
|
|
|
|
2021-12-30 19:00:11 +08:00
|
|
|
export {
|
|
|
|
isArray,
|
|
|
|
isNullish,
|
|
|
|
isExternal,
|
|
|
|
isActive,
|
|
|
|
normalize,
|
|
|
|
joinUrl,
|
|
|
|
ensureEndingSlash,
|
|
|
|
ensureStartingSlash,
|
|
|
|
removeExtention,
|
|
|
|
} from 'vitepress/dist/client/theme-default/utils'
|
2021-09-17 00:18:50 +08:00
|
|
|
|
2021-12-21 15:51:33 +08:00
|
|
|
export function utoa(data: string): string {
|
|
|
|
return btoa(unescape(encodeURIComponent(data)))
|
|
|
|
}
|
|
|
|
|
2021-09-17 00:18:50 +08:00
|
|
|
export const throttleAndDebounce = (fn: () => any, delay: number) => {
|
|
|
|
let timeout: ReturnType<typeof setTimeout>
|
|
|
|
let called = false
|
|
|
|
return () => {
|
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout)
|
|
|
|
}
|
|
|
|
if (!called) {
|
|
|
|
fn()
|
|
|
|
called = true
|
|
|
|
setTimeout(() => {
|
|
|
|
called = false
|
|
|
|
}, delay)
|
|
|
|
} else {
|
|
|
|
timeout = setTimeout(fn, delay)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// When match === true, meaning `path` is a string for build regex
|
|
|
|
export const isActiveLink = (
|
|
|
|
route: Route,
|
|
|
|
pathPattern: string,
|
|
|
|
match?: boolean
|
|
|
|
) => {
|
|
|
|
if (!match) return isActive(route, pathPattern)
|
|
|
|
const regex = new RegExp(pathPattern)
|
|
|
|
|
|
|
|
return regex.test(normalize(`/${route.data.relativePath}`))
|
|
|
|
}
|
|
|
|
|
2021-09-18 00:34:56 +08:00
|
|
|
export function createGitHubUrl(
|
|
|
|
docsRepo: string,
|
|
|
|
docsDir: string,
|
|
|
|
docsBranch: string,
|
|
|
|
path: string,
|
|
|
|
folder = 'examples/',
|
|
|
|
ext = '.vue'
|
|
|
|
) {
|
2021-09-17 00:18:50 +08:00
|
|
|
const base = isExternal(docsRepo)
|
|
|
|
? docsRepo
|
|
|
|
: `https://github.com/${docsRepo}`
|
2021-09-19 00:12:50 +08:00
|
|
|
return `${base.replace(endingSlashRE, '')}/edit/${docsBranch}/${
|
|
|
|
docsDir ? `${docsDir.replace(endingSlashRE, '')}/` : ''
|
|
|
|
}${folder || ''}${path}${ext || ''}`
|
2021-09-17 00:18:50 +08:00
|
|
|
}
|
|
|
|
|
2021-09-24 01:19:18 +08:00
|
|
|
export function createCrowdinUrl(targetLang: string) {
|
|
|
|
let translateLang = ''
|
|
|
|
// for zh-CN zh-HK zh-TW, maybe later we will have cases like Chinese lang
|
|
|
|
// for now we just keep it as simple as possible.
|
|
|
|
if (targetLang.startsWith('zh-')) {
|
|
|
|
translateLang = targetLang.split('-').join('').toLocaleLowerCase()
|
|
|
|
} else {
|
2021-12-30 19:00:11 +08:00
|
|
|
translateLang = targetLang.split('-').shift()!.toLocaleLowerCase()
|
2021-09-24 01:19:18 +08:00
|
|
|
}
|
|
|
|
return `https://crowdin.com/translate/element-plus/all/en-${translateLang}`
|
|
|
|
}
|