index.ts 2.0 KB

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