2021-09-17 00:18:50 +08:00
|
|
|
import {
|
|
|
|
isActive,
|
|
|
|
normalize,
|
|
|
|
endingSlashRE,
|
|
|
|
isExternal,
|
|
|
|
} from 'vitepress/dist/client/theme-default/utils'
|
2021-09-27 17:22:54 +08:00
|
|
|
import { inBrowser } from 'vitepress'
|
2021-09-24 01:19:18 +08:00
|
|
|
|
2021-09-17 00:18:50 +08:00
|
|
|
import type { Route } from 'vitepress'
|
|
|
|
|
|
|
|
export * from 'vitepress/dist/client/theme-default/utils'
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
export const isServer = typeof window === 'undefined'
|
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 {
|
|
|
|
translateLang = targetLang.split('-').shift().toLocaleLowerCase()
|
|
|
|
}
|
|
|
|
return `https://crowdin.com/translate/element-plus/all/en-${translateLang}`
|
|
|
|
}
|
2021-09-27 17:22:54 +08:00
|
|
|
|
|
|
|
export function insertLinkIcon(contentRef: any) {
|
|
|
|
if (!inBrowser) return
|
|
|
|
const links = Array.from(
|
|
|
|
contentRef.value?.$el.querySelectorAll('a:not(.header-anchor)') ?? []
|
|
|
|
)
|
|
|
|
|
|
|
|
links.forEach((link: any) => {
|
2021-10-25 16:57:22 +08:00
|
|
|
link.classList.add('vp-link')
|
2021-09-27 17:22:54 +08:00
|
|
|
if (
|
|
|
|
!link.href.startsWith(window.origin) &&
|
|
|
|
!link.innerHTML.includes('<img')
|
|
|
|
) {
|
|
|
|
link.innerHTML = `
|
|
|
|
${link.innerHTML}
|
|
|
|
<svg class="link-icon" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" width="16" height="16">
|
|
|
|
<path
|
|
|
|
d="
|
|
|
|
M853.333333 469.333333a42.666667 42.666667 0 0 0-42.666666
|
|
|
|
42.666667v256a42.666667 42.666667 0 0 1-42.666667 42.666667H256a42.666667
|
|
|
|
42.666667 0 0 1-42.666667-42.666667V256a42.666667
|
|
|
|
42.666667 0 0 1 42.666667-42.666667h256a42.666667 42.666667
|
|
|
|
0 0 0 0-85.333333H256a128 128 0 0 0-128 128v512a128 128
|
|
|
|
0 0 0 128 128h512a128 128 0 0 0 128-128v-256a42.666667
|
|
|
|
42.666667 0 0 0-42.666667-42.666667z
|
|
|
|
"
|
|
|
|
fill="currentColor">
|
|
|
|
</path>
|
|
|
|
<path
|
|
|
|
d="
|
|
|
|
M682.666667 213.333333h67.413333l-268.373333 267.946667a42.666667
|
|
|
|
42.666667 0 0 0 0 60.586667 42.666667 42.666667
|
|
|
|
0 0 0 60.586666 0L810.666667 273.92V341.333333a42.666667
|
|
|
|
42.666667 0 0 0 42.666666 42.666667 42.666667 42.666667 0 0 0
|
|
|
|
42.666667-42.666667V170.666667a42.666667 42.666667 0 0
|
|
|
|
0-42.666667-42.666667h-170.666666a42.666667
|
|
|
|
42.666667 0 0 0 0 85.333333z
|
|
|
|
"
|
|
|
|
fill="currentColor"
|
|
|
|
>
|
|
|
|
</path>
|
|
|
|
</svg>
|
|
|
|
`
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|