|
| 1 | +import { promises as fs } from 'fs'; |
| 2 | +import { notFound } from 'next/navigation'; |
| 3 | +import path from 'path'; |
| 4 | + |
| 5 | +const STYLES_DIR = path.resolve( |
| 6 | + process.cwd(), |
| 7 | + '../../packages/raystack/styles' |
| 8 | +); |
| 9 | + |
| 10 | +export const revalidate = false; |
| 11 | + |
| 12 | +function stripVar(value: string) { |
| 13 | + return value.replace(/var\((--[\w-]+)\)/g, '$1'); |
| 14 | +} |
| 15 | + |
| 16 | +function parseCSSFile(content: string) { |
| 17 | + const sections: { |
| 18 | + selector: string; |
| 19 | + comment: string | null; |
| 20 | + tokens: { name: string; value: string }[]; |
| 21 | + }[] = []; |
| 22 | + let currentSelector: string | null = null; |
| 23 | + let currentComment: string | null = null; |
| 24 | + let tokens: { name: string; value: string }[] = []; |
| 25 | + |
| 26 | + for (const line of content.split('\n')) { |
| 27 | + const trimmed = line.trim(); |
| 28 | + |
| 29 | + const selectorMatch = trimmed.match(/^(:root|\[[\w-]+="[\w-]+"\])\s*\{/); |
| 30 | + if (selectorMatch) { |
| 31 | + currentSelector = selectorMatch[1]; |
| 32 | + continue; |
| 33 | + } |
| 34 | + |
| 35 | + if (trimmed === '}') { |
| 36 | + if (currentSelector && tokens.length) { |
| 37 | + sections.push({ |
| 38 | + selector: currentSelector, |
| 39 | + comment: currentComment, |
| 40 | + tokens: [...tokens] |
| 41 | + }); |
| 42 | + } |
| 43 | + currentSelector = null; |
| 44 | + tokens = []; |
| 45 | + currentComment = null; |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + const commentMatch = trimmed.match(/^\/\*\s*(.+?)\s*\*\/$/); |
| 50 | + if (commentMatch && currentSelector) { |
| 51 | + if (tokens.length) { |
| 52 | + sections.push({ |
| 53 | + selector: currentSelector, |
| 54 | + comment: currentComment, |
| 55 | + tokens: [...tokens] |
| 56 | + }); |
| 57 | + tokens = []; |
| 58 | + } |
| 59 | + currentComment = commentMatch[1]; |
| 60 | + if (currentComment.startsWith('Example usage:')) currentComment = null; |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + const varMatch = trimmed.match( |
| 65 | + /^(--[\w-]+)\s*:\s*(.+?)\s*;(?:\s*\/\*\s*(.+?)\s*\*\/)?$/ |
| 66 | + ); |
| 67 | + if (varMatch && currentSelector) { |
| 68 | + tokens.push({ name: varMatch[1], value: stripVar(varMatch[2]) }); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return sections; |
| 73 | +} |
| 74 | + |
| 75 | +function toMarkdown( |
| 76 | + category: string, |
| 77 | + sections: ReturnType<typeof parseCSSFile> |
| 78 | +) { |
| 79 | + const title = category.charAt(0).toUpperCase() + category.slice(1); |
| 80 | + let output = `# ${title} Tokens\n`; |
| 81 | + |
| 82 | + for (const section of sections) { |
| 83 | + const prefix = |
| 84 | + section.selector === ':root' ? title : `${title} (${section.selector})`; |
| 85 | + const heading = section.comment ? `${prefix} - ${section.comment}` : prefix; |
| 86 | + |
| 87 | + output += `\n## ${heading}\n`; |
| 88 | + for (const t of section.tokens) { |
| 89 | + output += `${t.name}: ${t.value}\n`; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return output; |
| 94 | +} |
| 95 | + |
| 96 | +const CATEGORIES = ['colors', 'effects', 'radius', 'spacing', 'typography']; |
| 97 | + |
| 98 | +export async function GET( |
| 99 | + _req: Request, |
| 100 | + { params }: { params: Promise<{ category: string }> } |
| 101 | +) { |
| 102 | + const { category } = await params; |
| 103 | + |
| 104 | + if (!CATEGORIES.includes(category)) notFound(); |
| 105 | + |
| 106 | + const filePath = path.join(STYLES_DIR, `${category}.css`); |
| 107 | + const exists = await fs.stat(filePath).then( |
| 108 | + () => true, |
| 109 | + () => false |
| 110 | + ); |
| 111 | + if (!exists) notFound(); |
| 112 | + |
| 113 | + const content = await fs.readFile(filePath, 'utf-8'); |
| 114 | + |
| 115 | + const sections = parseCSSFile(content); |
| 116 | + return new Response(toMarkdown(category, sections), { |
| 117 | + headers: { 'Content-Type': 'text/markdown' } |
| 118 | + }); |
| 119 | +} |
| 120 | + |
| 121 | +export function generateStaticParams() { |
| 122 | + return CATEGORIES.map(category => ({ category })); |
| 123 | +} |
0 commit comments