mirror of
https://github.com/langgenius/dify.git
synced 2026-02-12 22:01:20 -05:00
87 lines
2.9 KiB
TypeScript
87 lines
2.9 KiB
TypeScript
import type { Locale } from '@/i18n-config/language'
|
|
import type { DocPathWithoutLang } from '@/types/doc-paths'
|
|
import { useTranslation } from '#i18n'
|
|
import { useCallback, useMemo } from 'react'
|
|
import { getDocLanguage, getLanguage, getPricingPageLanguage } from '@/i18n-config/language'
|
|
import { apiReferencePathTranslations } from '@/types/doc-paths'
|
|
|
|
export const useLocale = () => {
|
|
const { i18n } = useTranslation()
|
|
return i18n.language as Locale
|
|
}
|
|
|
|
export const useGetLanguage = () => {
|
|
const locale = useLocale()
|
|
|
|
return getLanguage(locale)
|
|
}
|
|
export const useGetPricingPageLanguage = () => {
|
|
const locale = useLocale()
|
|
|
|
return getPricingPageLanguage(locale)
|
|
}
|
|
|
|
export const defaultDocBaseUrl = 'https://docs.bash-is-all-you-need.dify.dev'
|
|
export type DocPathMap = Partial<Record<Locale, DocPathWithoutLang>>
|
|
export type DocAnchorMap = Partial<Record<Locale, string>>
|
|
export type DocLinkOptions = {
|
|
pathMap?: DocPathMap
|
|
anchorMap?: DocAnchorMap
|
|
}
|
|
export type BuildDocLink = (path?: DocPathWithoutLang, options?: DocLinkOptions) => string
|
|
|
|
const splitPathWithHash = (path: string) => {
|
|
const [pathname, ...hashParts] = path.split('#')
|
|
return {
|
|
pathname,
|
|
hash: hashParts.join('#'),
|
|
}
|
|
}
|
|
|
|
const normalizeAnchor = (anchor: string) => {
|
|
const normalizedAnchor = anchor.startsWith('#') ? anchor.slice(1) : anchor
|
|
if (!normalizedAnchor)
|
|
return ''
|
|
|
|
const isAsciiOnly = Array.from(normalizedAnchor).every(char => char.codePointAt(0)! <= 0x7F)
|
|
if (isAsciiOnly)
|
|
return normalizedAnchor
|
|
|
|
return encodeURIComponent(normalizedAnchor)
|
|
}
|
|
|
|
export const useDocLink = (baseUrl?: string): BuildDocLink => {
|
|
const baseDocUrl = useMemo(() => {
|
|
const resolvedBaseUrl = baseUrl || defaultDocBaseUrl
|
|
return resolvedBaseUrl.endsWith('/') ? resolvedBaseUrl.slice(0, -1) : resolvedBaseUrl
|
|
}, [baseUrl])
|
|
const locale = useLocale()
|
|
return useCallback(
|
|
(path?: DocPathWithoutLang, options?: DocLinkOptions): string => {
|
|
const docLanguage = getDocLanguage(locale)
|
|
const pathUrl = path || ''
|
|
const { pathMap, anchorMap } = options || {}
|
|
const targetPath = (pathMap) ? pathMap[locale] || pathUrl : pathUrl
|
|
const { pathname: pathWithoutHash, hash: pathAnchor } = splitPathWithHash(targetPath)
|
|
let targetPathWithoutHash = pathWithoutHash
|
|
let languagePrefix = `/${docLanguage}`
|
|
|
|
if (targetPathWithoutHash.startsWith('/api-reference/')) {
|
|
languagePrefix = ''
|
|
if (docLanguage !== 'en') {
|
|
const translatedPath = apiReferencePathTranslations[targetPathWithoutHash]?.[docLanguage]
|
|
if (translatedPath) {
|
|
targetPathWithoutHash = translatedPath
|
|
}
|
|
}
|
|
}
|
|
|
|
const anchor = normalizeAnchor(anchorMap?.[locale] || pathAnchor)
|
|
const anchorSuffix = anchor ? `#${anchor}` : ''
|
|
|
|
return `${baseDocUrl}${languagePrefix}${targetPathWithoutHash}${anchorSuffix}`
|
|
},
|
|
[baseDocUrl, locale],
|
|
)
|
|
}
|