diff --git a/docs/src/__registry__/examples.ts b/docs/src/__registry__/examples.ts deleted file mode 100644 index a82a1e0..0000000 --- a/docs/src/__registry__/examples.ts +++ /dev/null @@ -1,28 +0,0 @@ -export const examples: Record> = {} - -const components = import.meta.glob(`../registry/**/example/*.ts`) - -for (const path in components) { - const component = components[path] - const name = path.split('/')[2] - const componentName = path.split('/').pop()?.replace('.ts', '') - if (!componentName || !name) { - continue - } - - if (!examples[name]) { - examples[name] = {} - } - - examples[name][componentName] = { - name: componentName, - type: 'registry:example', - registryDependencies: [name], - files: [path], - component, - source: '', - category: 'undefined', - subcategory: 'undefined', - chunks: [], - } -} diff --git a/docs/src/components/component-preview/ComponentPreview.astro b/docs/src/components/component-preview/ComponentPreview.astro index d4b279f..dae8c19 100644 --- a/docs/src/components/component-preview/ComponentPreview.astro +++ b/docs/src/components/component-preview/ComponentPreview.astro @@ -1,7 +1,4 @@ --- -import { existsSync, readFileSync } from 'node:fs' -import path from 'node:path' -import process from 'node:process' import { cn } from '@/lib/utils' import { styles } from '@/registry/registry-styles' @@ -17,35 +14,10 @@ export interface Props { name: string class?: string align?: string + codeNewYork: string + codeDefault: string } -const { name, class: className, align = 'center', ...rest } = Astro.props - -const sortedStyleByNewYork = styles.toSorted((a, b) => (a.name === 'new-york' ? -1 : b.name === 'new-york' ? 1 : 0)) - -const components = await Promise.all( - [...sortedStyleByNewYork].map(async (style) => { - const filename = path.join(process.cwd(), 'src', 'registry', style.name, 'example', `${name}.ts`) - let pathCode: string - let code: string | null - - if (existsSync(filename)) { - pathCode = `../registry/${style.name}/example/${name}.ts` - code = readFileSync(`${filename}`) - .toString() - .replace(/@\/registry\/.*\/ui/g, '@/components/ui') - .replace(/export\s+default\s+(?:\S.*)?\s*/, '') - } else { - pathCode = '' - code = null - } - - return { - style, - code, - pathCode, - } - }), -) +const { name, class: className, align = 'center', codeDefault, codeNewYork, ...rest } = Astro.props ---
@@ -69,35 +41,34 @@ const components = await Promise.all(
- { - components.map(({ style, code }) => ( - - )) - } + +
{ - components.map(({ style, pathCode }) => ( -
- {pathCode ? ( - - ) : ( -
-

- Component{' '} - {name} - {' not found in registry.'} -

-
- )} + styles.map(({ name: styleName }) => ( +
+
)) } diff --git a/docs/src/components/component-preview/component-preview.ts b/docs/src/components/component-preview/component-preview.ts index a68d567..aa728bd 100644 --- a/docs/src/components/component-preview/component-preview.ts +++ b/docs/src/components/component-preview/component-preview.ts @@ -1,9 +1,10 @@ -import { examples } from '@/__registry__/examples' +import { Index } from '@/__registry__' import { cn } from '@/lib/utils' import { AsyncPipe, NgComponentOutlet } from '@angular/common' - import { Component, computed, input } from '@angular/core' +import type { Style } from '@/registry/registry-styles' + @Component({ standalone: true, selector: 'component-preview', @@ -11,18 +12,27 @@ import { Component, computed, input } from '@angular/core' template: ` @let componentRender = this.component() | async;
- @if(!componentRender || !componentRender.default) { -
Loading...
- } @else { + @if(this.existComponent && (!componentRender || !componentRender.default)) { +
Loading...
+ } + @else if (!this.existComponent) { +
+

+ Component {{nameExample()}} not found in registry. +

+
+ } + @else { }
`, }) export class ComponentPeviewComponent { - styleName = input() + styleName = input() nameExample = input() align = input<'center' | 'start' | 'end'>('center') + existComponent = true computedClass = computed(() => cn('preview [&>div]:flex [&>div]:min-h-[350px] [&>div]:w-full [&>div]:justify-center [&>div]:p-10', { '[&>div]:items-center': this.align() === 'center', @@ -31,9 +41,16 @@ export class ComponentPeviewComponent { })) component = computed(async () => { - if (!this.styleName() || !this.nameExample()) + if (!this.styleName() || !this.nameExample()) { return null + } - return await examples[this.styleName()!][this.nameExample()!].component() + try { + return await Index[this.styleName()!][this.nameExample()!].component() + } + catch { + this.existComponent = false + return null + } }) } diff --git a/docs/src/plugins/rehype-component.ts b/docs/src/plugins/rehype-component.ts index db3fab1..e5d2432 100644 --- a/docs/src/plugins/rehype-component.ts +++ b/docs/src/plugins/rehype-component.ts @@ -8,6 +8,21 @@ import { Index } from '../__registry__' import { styles } from '../registry/registry-styles' import type { UnistNode, UnistTree } from '../../types/unist' +function toCamelCase(str: string): string { + return str + .split(/[-_]/) + .map((word, index) => { + if (index === 0) { + return word + } + return ( + word.charAt(0).toUpperCase() + + word.slice(1) + ) + }) + .join('') +} + export function rehypeComponent() { return async (tree: UnistTree) => { visit(tree, (node: UnistNode) => { @@ -55,6 +70,7 @@ export function rehypeComponent() { '@/components/', ) source = source.replaceAll('export default', 'export') + source = source.replace(/selector: '\[([^\]]+)\]'/, 'selector: \'$1\'') // Add code as children so that rehype can take over at build time. node.children?.push( @@ -117,6 +133,13 @@ export function rehypeComponent() { '@/components/', ) source = source.replaceAll('export default', 'export') + source = source.replace(/selector: '\[([^\]]+)\]'/, 'selector: \'$1\'') + + node.attributes?.push({ + type: 'mdxJsxAttribute', + name: toCamelCase(`code-${style.name}`), + value: source, + }) // Add code as children so that rehype can take over at build time. node.children?.push(