index.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { endingSlashRE, isActive, isExternal, normalize } from 'vitepress/dist/client/theme-default/utils'
  2. import type { Route } from 'vitepress'
  3. export * from './colors'
  4. export { isArray, isNullish, isExternal, isActive, normalize, joinUrl, ensureEndingSlash, ensureStartingSlash, removeExtention } from 'vitepress/dist/client/theme-default/utils'
  5. export function utoa(data: string): string {
  6. return btoa(unescape(encodeURIComponent(data)))
  7. }
  8. export const throttleAndDebounce = (fn: () => any, delay: number) => {
  9. let timeout: ReturnType<typeof setTimeout>
  10. let called = false
  11. return () => {
  12. if (timeout) {
  13. clearTimeout(timeout)
  14. }
  15. if (!called) {
  16. fn()
  17. called = true
  18. setTimeout(() => {
  19. called = false
  20. }, delay)
  21. } else {
  22. timeout = setTimeout(fn, delay)
  23. }
  24. }
  25. }
  26. // When match === true, meaning `path` is a string for build regex
  27. export const isActiveLink = (route: Route, pathPattern: string, match?: boolean) => {
  28. if (!match) return isActive(route, pathPattern)
  29. const regex = new RegExp(pathPattern)
  30. return regex.test(normalize(`/${route.data.relativePath}`))
  31. }
  32. export function createGitHubUrl(docsRepo: string, docsDir: string, docsBranch: string, path: string, folder = 'examples/', ext = '.vue') {
  33. const base = isExternal(docsRepo) ? docsRepo : `https://github.com/${docsRepo}`
  34. return `${base.replace(endingSlashRE, '')}/edit/${docsBranch}/${docsDir ? `${docsDir.replace(endingSlashRE, '')}/` : ''}${folder || ''}${path}${ext || ''}`
  35. }
  36. export function createCrowdinUrl(targetLang: string) {
  37. let translateLang = ''
  38. // for zh-CN zh-HK zh-TW, maybe later we will have cases like Chinese lang
  39. // for now we just keep it as simple as possible.
  40. if (targetLang.startsWith('zh-')) {
  41. translateLang = targetLang.split('-').join('').toLocaleLowerCase()
  42. } else {
  43. translateLang = targetLang.split('-').shift()!.toLocaleLowerCase()
  44. }
  45. return `https://crowdin.com/translate/element-plus/all/en-${translateLang}`
  46. }