refactor: adapt docs links for product prefixes (#37565)

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
This commit is contained in:
Stephen Zhouandcopilot-swe-agent[bot] authored and GitHub committed 2026-06-23 03:24:29 +00:00
1 parent f380bbaa10
commit 855bb32306
16 files changed
+692 -112

No files matched your search

+93 -21
View File
@@ -11,7 +11,8 @@ import path from 'node:path'
import { fileURLToPath } from 'node:url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const DOCS_JSON_URL = 'https://raw.githubusercontent.com/langgenius/dify-docs/refs/heads/main/docs.json'
const DEFAULT_DOCS_JSON_URL = 'https://raw.githubusercontent.com/langgenius/dify-docs/refs/heads/main/docs.json'
const DOCS_JSON_URL = process.env.DOCS_JSON_URL || DEFAULT_DOCS_JSON_URL
const OUTPUT_PATH = path.resolve(__dirname, '../types/doc-paths.ts')
type NavItem = string | NavObject | NavItem[]
@@ -21,6 +22,9 @@ type NavObject = {
groups?: NavItem[]
dropdowns?: NavItem[]
languages?: NavItem[]
products?: NavItem[]
tabs?: NavItem[]
menu?: NavItem[]
versions?: NavItem[]
openapi?: string
[key: string]: unknown
@@ -58,7 +62,15 @@ type DocsJson = {
[key: string]: unknown
}
const OPENAPI_BASE_URL = 'https://raw.githubusercontent.com/langgenius/dify-docs/refs/heads/main/'
const OPENAPI_BASE_URL = (process.env.DOCS_OPENAPI_BASE_URL || new URL('.', DOCS_JSON_URL).toString()).replace(/\/?$/, '/')
const DOCS_PRODUCTS = ['cloud', 'self-host'] as const
type DocsProduct = typeof DOCS_PRODUCTS[number]
type ProductAvailability = Record<string, Set<DocsProduct>>
function isDocsProduct(segment: string): segment is DocsProduct {
return DOCS_PRODUCTS.includes(segment as DocsProduct)
}
/**
* Convert summary to URL slug
@@ -112,6 +124,15 @@ function extractOpenAPIPaths(item: NavItem | undefined, paths: Set<string> = new
if (item.languages)
extractOpenAPIPaths(item.languages, paths)
if (item.products)
extractOpenAPIPaths(item.products, paths)
if (item.tabs)
extractOpenAPIPaths(item.tabs, paths)
if (item.menu)
extractOpenAPIPaths(item.menu, paths)
if (item.versions)
extractOpenAPIPaths(item.versions, paths)
}
@@ -199,6 +220,15 @@ function extractPaths(item: NavItem | undefined, paths: Set<string> = new Set())
if (item.languages)
extractPaths(item.languages, paths)
if (item.products)
extractPaths(item.products, paths)
if (item.tabs)
extractPaths(item.tabs, paths)
if (item.menu)
extractPaths(item.menu, paths)
// Handle versions in navigation
if (item.versions)
extractPaths(item.versions, paths)
@@ -207,33 +237,68 @@ function extractPaths(item: NavItem | undefined, paths: Set<string> = new Set())
return paths
}
function addPathToGroup(groups: Record<string, Set<string>>, pathWithoutLang: string): void {
const parts = pathWithoutLang.split('/')
const section = parts[0]
if (!section)
return
if (!groups[section])
groups[section] = new Set()
groups[section]!.add(pathWithoutLang)
}
function getProductPathInfo(pathWithoutLang: string): { product: DocsProduct, pathWithoutProduct: string } | undefined {
const parts = pathWithoutLang.split('/')
const [product, ...rest] = parts
if (!product || !isDocsProduct(product) || rest.length === 0)
return undefined
return {
product,
pathWithoutProduct: rest.join('/'),
}
}
/**
* Group paths by their prefix structure
*/
function groupPathsBySection(paths: Set<string>): Record<string, Set<string>> {
function groupPathsBySection(paths: Set<string>): { groups: Record<string, Set<string>>, productAvailability: ProductAvailability } {
const groups: Record<string, Set<string>> = {}
const productAvailability: ProductAvailability = {}
for (const fullPath of paths) {
// Skip non-doc paths (like .json files for OpenAPI)
if (fullPath.endsWith('.json'))
continue
// Remove language prefix (en/, zh/, ja/)
const withoutLang = fullPath.replace(/^(en|zh|ja)\//, '')
if (!withoutLang || withoutLang === fullPath)
continue
// Get section (first part of path)
const parts = withoutLang.split('/')
const section = parts[0]
// Skip non-doc paths (like .json files for OpenAPI)
if (withoutLang.endsWith('.json') || withoutLang === 'None')
continue
if (!groups[section!])
groups[section!] = new Set()
addPathToGroup(groups, withoutLang)
groups[section!]!.add(withoutLang)
const productPathInfo = getProductPathInfo(withoutLang)
if (productPathInfo) {
const productlessPath = productPathInfo.pathWithoutProduct
const normalizedPath = `/${productlessPath}`
addPathToGroup(groups, productlessPath)
if (!productAvailability[normalizedPath])
productAvailability[normalizedPath] = new Set()
productAvailability[normalizedPath]!.add(productPathInfo.product)
}
}
return groups
return {
groups,
productAvailability,
}
}
/**
@@ -251,6 +316,7 @@ function sectionToTypeName(section: string): string {
*/
function generateTypeDefinitions(
groups: Record<string, Set<string>>,
productAvailability: ProductAvailability,
apiReferencePaths: string[],
apiPathTranslations: Record<string, { zh?: string, ja?: string }>,
): string {
@@ -258,11 +324,12 @@ function generateTypeDefinitions(
'// GENERATE BY script',
'// DON NOT EDIT IT MANUALLY',
'//',
'// Generated from: https://raw.githubusercontent.com/langgenius/dify-docs/refs/heads/main/docs.json',
`// Generated from: ${DOCS_JSON_URL}`,
`// Generated at: ${new Date().toISOString()}`,
'',
'// Language prefixes',
'export type DocLanguage = \'en\' | \'zh\' | \'ja\'',
'export type DocsProduct = \'cloud\' | \'self-host\'',
'',
]
@@ -321,10 +388,14 @@ function generateTypeDefinitions(
lines.push(' | `${DocPathWithoutLangBase}#${string}`')
lines.push('')
// Generate full path type with language prefix
lines.push('// Full documentation path with language prefix')
// eslint-disable-next-line no-template-curly-in-string
lines.push('export type DifyDocPath = `${DocLanguage}/${DocPathWithoutLang}`')
// Generate product availability map for productless runtime links.
lines.push('// Product availability for productless docs paths')
lines.push('export const docPathProductAvailability: Record<string, readonly DocsProduct[]> = {')
for (const path of Object.keys(productAvailability).sort()) {
const products = [...productAvailability[path]!].sort((a, b) => DOCS_PRODUCTS.indexOf(a) - DOCS_PRODUCTS.indexOf(b))
lines.push(` '${path}': [${products.map(product => `'${product}'`).join(', ')}],`)
}
lines.push('}')
lines.push('')
// Generate API reference path translations map
@@ -423,12 +494,13 @@ async function main(): Promise<void> {
console.log(`Generated ${Object.keys(apiPathTranslations).length} API path translations`)
// Group by section
const groups = groupPathsBySection(allPaths)
const { groups, productAvailability } = groupPathsBySection(allPaths)
console.log(`Grouped into ${Object.keys(groups).length} sections:`, Object.keys(groups))
console.log(`Found ${Object.keys(productAvailability).length} product-aware paths`)
// Generate TypeScript
const tsContent = generateTypeDefinitions(groups, uniqueEnApiPaths, apiPathTranslations)
const tsContent = generateTypeDefinitions(groups, productAvailability, uniqueEnApiPaths, apiPathTranslations)
// Write to file
await writeFile(OUTPUT_PATH, tsContent, 'utf-8')