-
Notifications
You must be signed in to change notification settings - Fork 51
feat(docs): expand ComponentGrid in llms.txt output #1509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6ed8858
feat(docs): expand ComponentGrid in llms.txt output
SeieunYoo 8035644
fix(docs): address ComponentGrid rule review feedback
SeieunYoo cbab001
refactor(docs): use getLLMMarkdownUrl helper in ComponentGrid rule
SeieunYoo 56a8094
Merge remote-tracking branch 'origin/dev' into joy-yoo_karrot/llms-do…
SeieunYoo 0cb61b8
refactor(docs): use gray-matter in ComponentGrid rule
SeieunYoo 10f2b41
fix(docs): warn when ComponentGrid components directory is missing
SeieunYoo e371456
fix(docs): warn when ComponentGrid transform falls back
SeieunYoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { describe, expect, it } from "bun:test"; | ||
| import { normalizeLLMBodyWithRules } from "../normalize-llm-body"; | ||
| import { componentGridRule } from "./component-grid-rule"; | ||
|
|
||
| describe("componentGridRule", () => { | ||
| it("expands ComponentGrid into a categorized component list", () => { | ||
| const input = "<ComponentGrid />\n"; | ||
|
|
||
| const actual = normalizeLLMBodyWithRules(input, [componentGridRule]); | ||
|
|
||
| expect(actual).toContain("## Buttons"); | ||
| expect(actual).toContain("## Controls"); | ||
| expect(actual).toContain("[Checkbox](/docs/components/checkbox)"); | ||
| }); | ||
|
|
||
| it("excludes deprecated components", () => { | ||
| const input = "<ComponentGrid />\n"; | ||
|
|
||
| const actual = normalizeLLMBodyWithRules(input, [componentGridRule]); | ||
|
|
||
| expect(actual).not.toContain("/docs/components/fab"); | ||
| }); | ||
|
|
||
| it("leaves the node as-is when no components are available", () => { | ||
| const input = "<SomethingElse />\n"; | ||
|
|
||
| const actual = normalizeLLMBodyWithRules(input, [componentGridRule]); | ||
|
|
||
| expect(actual).toContain("<SomethingElse />"); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import type { MdxJsxFlowElement } from "mdast-util-mdx-jsx"; | ||
| import type { Rule } from "./types"; | ||
|
|
||
| interface ComponentEntry { | ||
| category: string; | ||
| title: string; | ||
| description: string; | ||
| url: string; | ||
| } | ||
|
|
||
| function resolveComponentsDir(): string | null { | ||
| const candidates = [ | ||
| path.resolve(process.cwd(), "content/docs/components"), | ||
| path.resolve( | ||
| path.dirname(fileURLToPath(import.meta.url)), | ||
| "../../../content/docs/components", | ||
| ), | ||
| ]; | ||
| for (const candidate of candidates) { | ||
| if (fs.existsSync(candidate)) return candidate; | ||
| } | ||
| return null; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| function parseFrontmatter(source: string): Record<string, string> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 언젠가는 gray-matter로 다 갈아 버리시죠..!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 0cb61b8 이전 PR 에서 gray-matter 도입해서 바로 수정해뒀어요. |
||
| const match = source.match(/^---\r?\n([\s\S]*?)\r?\n---/); | ||
| if (!match) return {}; | ||
| const result: Record<string, string> = {}; | ||
| for (const line of match[1].split(/\r?\n/)) { | ||
| const colon = line.indexOf(":"); | ||
| if (colon === -1) continue; | ||
| const key = line.slice(0, colon).trim(); | ||
| const value = line | ||
| .slice(colon + 1) | ||
| .trim() | ||
| .replace(/^["']|["']$/g, ""); | ||
| if (key) result[key] = value; | ||
| } | ||
| return result; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| function titleCase(value: string): string { | ||
| return value.charAt(0).toUpperCase() + value.slice(1); | ||
| } | ||
|
|
||
| function loadEntries(): ComponentEntry[] { | ||
| const componentsDir = resolveComponentsDir(); | ||
| if (!componentsDir) return []; | ||
|
|
||
| const entries: ComponentEntry[] = []; | ||
| for (const dirent of fs.readdirSync(componentsDir, { withFileTypes: true })) { | ||
| if (!dirent.isDirectory()) continue; | ||
| const match = dirent.name.match(/^\(([^)]+)\)$/); | ||
| if (!match) continue; | ||
|
|
||
| const category = titleCase(match[1]); | ||
| const categoryDir = path.join(componentsDir, dirent.name); | ||
|
|
||
| for (const file of fs.readdirSync(categoryDir)) { | ||
| if (!file.endsWith(".mdx")) continue; | ||
|
|
||
| const source = fs.readFileSync(path.join(categoryDir, file), "utf8"); | ||
| const fm = parseFrontmatter(source); | ||
| if (fm.deprecated) continue; | ||
|
|
||
| const slug = file.slice(0, -".mdx".length); | ||
| entries.push({ | ||
| category, | ||
| title: fm.title ?? slug, | ||
| description: fm.description ?? "", | ||
| url: `/docs/components/${slug}`, | ||
| }); | ||
| } | ||
| } | ||
| return entries; | ||
| } | ||
|
|
||
| let cachedEntries: ComponentEntry[] | null = null; | ||
|
|
||
| function getEntries(): ComponentEntry[] { | ||
| if (cachedEntries === null) cachedEntries = loadEntries(); | ||
| return cachedEntries; | ||
| } | ||
|
|
||
| function buildMarkdown(entries: ComponentEntry[]): string { | ||
| const grouped = new Map<string, ComponentEntry[]>(); | ||
| for (const entry of entries) { | ||
| if (!grouped.has(entry.category)) grouped.set(entry.category, []); | ||
| grouped.get(entry.category)!.push(entry); | ||
| } | ||
| for (const list of grouped.values()) { | ||
| list.sort((a, b) => a.title.localeCompare(b.title)); | ||
| } | ||
|
|
||
| const sections: string[] = []; | ||
| for (const [category, list] of Array.from(grouped.entries()).sort(([a], [b]) => | ||
| a.localeCompare(b), | ||
| )) { | ||
| const lines = [`## ${category}`, ""]; | ||
| for (const entry of list) { | ||
| const suffix = entry.description ? ` — ${entry.description}` : ""; | ||
| lines.push(`- [${entry.title}](${entry.url})${suffix}`); | ||
| } | ||
| sections.push(lines.join("\n")); | ||
| } | ||
| return sections.join("\n\n"); | ||
| } | ||
|
|
||
| export const componentGridRule: Rule = { | ||
| name: "ComponentGrid", | ||
| match: (node): node is MdxJsxFlowElement => | ||
| node.type === "mdxJsxFlowElement" && node.name === "ComponentGrid", | ||
| transform: (node) => { | ||
| try { | ||
| const entries = getEntries(); | ||
| if (entries.length === 0) return [node]; | ||
| return [{ type: "html", value: buildMarkdown(entries) }]; | ||
| } catch { | ||
| return [node]; | ||
| } | ||
| }, | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.