File size: 7,150 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
import rehypeShiki from "@shikijs/rehype"
import {
transformerMetaHighlight,
transformerNotationDiff,
transformerNotationFocus,
transformerNotationHighlight,
transformerNotationWordHighlight,
} from "@shikijs/transformers"
import fs from "node:fs"
import rehypeAutolinkHeadings from "rehype-autolink-headings"
import rehypeSlug from "rehype-slug"
import remarkDirective from "remark-directive"
import remarkGfm from "remark-gfm"
import { defineCollection, defineConfig, s } from "velite"
import { docsConfig } from "./docs.config"
import { remarkCallout } from "./lib/remark-callout"
import { remarkCard } from "./lib/remark-card"
import { remarkCodeTitle } from "./lib/remark-code-title"
import { remarkCodeGroup } from "./lib/remark-codegroup"
import { remarkSteps } from "./lib/remark-steps"
import { transformerMetaWordHighlight } from "./lib/shiki-highlight-word"
import { propsToMdTable } from "./utils/get-component-props.js"
import { replaceTokenDoc } from "./utils/get-theming-doc.js"
const cwd = process.cwd()
const slugify = (str: string) => {
return str
.replace(/.*\/content\//, "")
.replace(/\.mdx$/, "")
.replace(cwd, "")
}
const docs = defineCollection({
name: "Docs",
pattern: ["content/docs/**/*.mdx"],
schema: s
.object({
title: s.string(),
description: s.string(),
metadata: s.metadata(),
content: s.markdown(),
status: s.string().optional(),
toc: s.toc(),
code: s.mdx(),
llm: s.custom().transform((_data, { meta }) => {
const content = replaceExampleTabs((meta.content as string) ?? "")
return replacePropsTable(replaceTokenDoc(content))
}),
hideToc: s.boolean().optional(),
composition: s.boolean().optional(),
links: s
.object({
source: s.string().optional(),
storybook: s.string().optional(),
recipe: s.string().optional(),
ark: s.string().optional(),
recharts: s.string().optional(),
})
.optional(),
})
.transform((data, { meta }) => {
const links = data.links || {}
return {
...data,
slug: slugify(meta.path as string),
links: {
...links,
source: links.source
? `${docsConfig.repoUrl}/tree/${docsConfig.repoBranch}/packages/react/src/${links.source}`
: undefined,
storybook: links.storybook
? `${docsConfig.storybookUrl}/?path=/story/${links.storybook}`
: undefined,
recipe: links.recipe
? `${docsConfig.repoUrl}/tree/${docsConfig.repoBranch}/packages/react/src/theme/recipes/${links.recipe}.ts`
: undefined,
},
category: (meta.path as string)
.replace(/.*\/content\//, "")
.replace(/\/[^/]*$/, "")
.replace(cwd, ""),
}
}),
})
const notes = defineCollection({
name: "Notes",
pattern: "content/notes/**/*.mdx",
schema: s.object({
title: s.string(),
description: s.string(),
metadata: s.metadata(),
content: s.markdown(),
code: s.mdx(),
}),
})
const showcases = defineCollection({
name: "Showcases",
pattern: "content/showcases.json",
schema: s.object({
title: s.string(),
description: s.string().optional(),
url: s.string(),
image: s.string(),
}),
})
const blogs = defineCollection({
name: "Blog",
pattern: "content/blog/**/*.mdx",
schema: s
.object({
title: s.string(),
type: s.enum(["release", "announcement", "article"]),
version: s.string().optional(),
description: s.string(),
metadata: s.metadata(),
content: s.mdx(),
authors: s.array(s.string()),
publishedAt: s.string(),
toc: s.toc(),
draft: s.boolean().optional(),
})
.transform((data, { meta }) => {
return {
...data,
slug: slugify(meta.path as string),
}
}),
})
const guideCollectionId = s.enum([
"overview",
"snippets",
"recipe",
"components",
"next-js",
"styling",
"theming",
])
const guides = defineCollection({
name: "Guides",
pattern: "content/guides/**/*.mdx",
schema: s
.object({
title: s.string(),
description: s.string(),
metadata: s.metadata(),
content: s.mdx(),
publishedAt: s.string(),
collection: guideCollectionId,
})
.transform((data, { meta }) => {
return {
...data,
slug: slugify(meta.path as string),
}
}),
})
const guideCollections = defineCollection({
name: "GuideCollection",
pattern: "content/guides.json",
schema: s.object({
id: guideCollectionId,
title: s.string(),
description: s.string(),
}),
})
export default defineConfig({
root: cwd,
collections: {
docs,
showcases,
notes,
blogs,
guides,
guideCollections,
},
mdx: {
remarkPlugins: [
remarkDirective,
remarkGfm,
remarkCallout,
remarkCodeTitle,
remarkCodeGroup,
remarkSteps,
remarkCard,
],
rehypePlugins: [
rehypeSlug,
[
rehypeShiki,
{
transformers: [
transformerNotationDiff(),
transformerNotationFocus(),
transformerNotationHighlight(),
transformerNotationWordHighlight(),
transformerMetaHighlight(),
transformerMetaWordHighlight(),
],
theme: "dark-plus",
},
],
[
rehypeAutolinkHeadings,
{
behavior: "wrap",
properties: {
className: ["subheading-anchor"],
},
},
],
],
},
})
function replaceExampleTabs(text: string) {
const matches = text.matchAll(/<ExampleTabs name="(.*?)" \/>/g)
if (!matches) return text
for (const match of matches) {
try {
const name = match[1]
let example = fs.readFileSync(
`../compositions/src/examples/${name}.tsx`,
"utf-8",
)
example = example.replaceAll("compositions/ui", "@/components/ui")
text = text.replace(
`<ExampleTabs name="${name}" \/>`,
`\`\`\`tsx\n${example}\n\`\`\``,
)
} catch {
console.log("[velite] no example", match)
}
}
return text
}
function replacePropsTable(text: string) {
try {
const matches = text.matchAll(
/<PropTable\s+component="([^"]+)"\s+part="([^"]+)"(?:\s+omit=\{(\[.*?\])\})?\s*\/>/g,
)
if (!matches) return text
for (const match of matches) {
const component = match[1]
const part = match[2]
const omit = match[3]
const omitArray = omit ? JSON.parse(omit) : undefined
const mdTable = propsToMdTable({ component, part, omit: omitArray })
if (!mdTable) {
console.log("no mdTable", component, part)
continue
}
text = text.replace(
omit
? `<PropTable component="${component}" part="${part}" omit={${omit}} \/>`
: `<PropTable component="${component}" part="${part}" \/>`,
mdTable,
)
}
return text
} catch (error) {
console.error(error)
return text
}
}
|