File size: 3,878 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { Docs, docs } from "@/.velite"
import { colorPalettes } from "compositions/lib/color-palettes"

///////////////////////////////////////////////////////////////////////

const excludeSet = new Map([
  ["checkbox", ["checkbox-card"]],
  ["radio", ["radio-card"]],
])

export function findMatchingDocs(component: string) {
  const componentDocs = docs.filter((doc) => {
    if (excludeSet.has(component)) {
      const exclude = excludeSet.get(component)
      if (exclude && exclude.some((pattern) => doc.slug.match(pattern))) {
        return false
      }
    }
    return doc.slug.startsWith(`docs/components/${component}`)
  })
  return componentDocs
}

///////////////////////////////////////////////////////////////////////

export function parseComponent(component: string) {
  const minify = !component.includes("-full.txt")
  component = component.replace(".txt", "").replace("-full", "")
  return { minify, component }
}

///////////////////////////////////////////////////////////////////////

const colorPaletteRegex =
  /import { colorPalettes } from "compositions\/lib\/color-palettes"\n/g
const colorPaletteReplacement = JSON.stringify(colorPalettes)

export function replaceColorPalettes(content: string) {
  const hasColorPaletteImport = content.includes("lib/color-palettes")
  if (hasColorPaletteImport) {
    content = content
      .replace(colorPaletteRegex, "")
      .replace(/colorPalettes/g, colorPaletteReplacement)
  }
  return content
}

///////////////////////////////////////////////////////////////////////

const decorativeBoxRegex =
  /import { DecorativeBox } from "compositions\/lib\/decorative-box"\n/g
const boxImportRegex =
  /import\s+{\s*(?:.*,\s*)?Box(?:\s*,\s*.*)?}\s+from\s+["']@chakra-ui\/react["']/g

export function replaceDecorativeBox(content: string) {
  const hasDecorativeBoxImport = content.includes("lib/decorative-box")
  if (hasDecorativeBoxImport) {
    const isBoxImported = content.match(boxImportRegex)
    content = content.replace(
      decorativeBoxRegex,
      isBoxImported ? "" : 'import { Box } from "@chakra-ui/react"\n',
    )
    content = content.replace(/DecorativeBox/g, "Box")
  }
  return content
}

///////////////////////////////////////////////////////////////////////

export function cleanupContent(content: string) {
  content = replaceColorPalettes(content)
  content = replaceDecorativeBox(content)
  return content
}

export function minifyContent(content: string) {
  const examplesStart = content.indexOf("## Examples")
  const setupStart = content.indexOf("## Setup")
  const usageStart = content.indexOf("## Usage")
  const propsStart = content.indexOf("## Props")

  if (examplesStart !== -1 && propsStart !== -1) {
    content = content.slice(0, examplesStart) + content.slice(propsStart)
  }

  if (setupStart !== -1 && usageStart !== -1) {
    content = content.slice(0, setupStart) + content.slice(usageStart)
  }

  // Remove ::: blocks with any content inside them
  content = content.replace(/\n:::.+?[\r\n]+([\s\S]*?)[\r\n]+:::\n/g, "")

  return content
}

///////////////////////////////////////////////////////////////////////

export function getLlmContent(doc: Docs) {
  // Create the header with page information
  const pageUrl = doc.slug
  const sourceUrl = `https://raw.githubusercontent.com/chakra-ui/chakra-ui/refs/heads/main/apps/www/content/${doc.slug}.mdx`

  const category = doc.slug.split("/").slice(1, -1).join(" > ")
  const header = `# ${category} > ${doc.title}
  
  URL: ${doc.slug}
  Source: ${sourceUrl}
  
  ${doc.description}
          
  ***
  
  title: ${doc.title}
  description: ${doc.description}
  links: \n${Object.entries(doc.links)
    .map(([key, value]) => ` - ${key}: ${value}`)
    .join("\n")}
  ------------------------------------------------------------------------------------------------
  
  `

  return `${header}${doc.llm}`
}