Skip to content

Commit

Permalink
docs: add rehype-components
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-ub committed Oct 27, 2024
1 parent 50d742e commit 062239d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
14 changes: 14 additions & 0 deletions docs/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import angular from '@analogjs/astro-angular'
import mdx from '@astrojs/mdx'
import sitemap from '@astrojs/sitemap'
Expand All @@ -9,6 +12,8 @@ import { rehypePrettyCode } from 'rehype-pretty-code'
import rehypeSlug from 'rehype-slug'
import { codeImport } from 'remark-code-import'
import { visit } from 'unist-util-visit'

import { rehypeComponent } from './plugins/rehype-component'
import { rehypeNpmCommand } from './plugins/rehype-npm-command'
import { siteConfig } from './src/config/site'

Expand All @@ -35,6 +40,9 @@ const optionsRehypePrettyCode = {
},
}

const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

// https://astro.build/config
export default defineConfig({
site: siteConfig.url,
Expand Down Expand Up @@ -66,6 +74,7 @@ export default defineConfig({
remarkPlugins: [codeImport],
rehypePlugins: [
rehypeSlug,
rehypeComponent,
() => (tree) => {
visit(tree, (node) => {
if (node?.type === 'element' && node?.tagName === 'pre') {
Expand Down Expand Up @@ -159,5 +168,10 @@ export default defineConfig({
'@ng-icons/lucide',
],
},
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
},
},
},
})
35 changes: 14 additions & 21 deletions docs/plugins/rehype-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import process from 'node:process'
import { u } from 'unist-builder'
import { visit } from 'unist-util-visit'

// import { Index } from '../src/__registry__'
import { Index } from '../src/__registry__'
import { styles } from '../src/registry/registry-styles'
import type { UnistNode, UnistTree } from '../types/unist'

Expand All @@ -21,9 +21,9 @@ export function rehypeComponent() {

if (node.name === 'ComponentSource') {
const name = getNodeAttributeByName(node, 'name')?.value as string
// const fileName = getNodeAttributeByName(node, 'fileName')?.value as
// | string
// | undefined
const fileName = getNodeAttributeByName(node, 'fileName')?.value as
| string
| undefined

if (!name && !srcPath) {
return null
Expand All @@ -37,20 +37,14 @@ export function rehypeComponent() {
src = srcPath
}
else {
src = ''
// const component = Index[style.name][name]
// src = fileName
// ? component.files.find((file: string) => {
// return (
// file.endsWith(`${fileName}.tsx`)
// || file.endsWith(`${fileName}.ts`)
// )
// }) || component.files[0]
// : component.files[0]
const component = Index[style.name][name]
src = fileName
? component.files.find((file: string) => file.endsWith(`${fileName}.ts`)) || component.files[0]
: component.files[0]
}

// Read the source file.
const filePath = path.join(process.cwd(), src)
const filePath = path.join(process.cwd(), 'src', src)
let source = fs.readFileSync(filePath, 'utf8')

// Replace imports.
Expand Down Expand Up @@ -81,7 +75,7 @@ export function rehypeComponent() {
u('element', {
tagName: 'code',
properties: {
className: ['language-tsx'],
className: ['language-angular-ts'],
},
children: [
{
Expand Down Expand Up @@ -109,12 +103,11 @@ export function rehypeComponent() {

try {
for (const style of styles) {
// const component = Index[style.name][name]
// const src = component.files[0]
const src = ''
const component = Index[style.name][name]
const src = component.files[0]

// Read the source file.
const filePath = path.join(process.cwd(), src)
const filePath = path.join(process.cwd(), 'src', src)
let source = fs.readFileSync(filePath, 'utf8')

// Replace imports.
Expand All @@ -135,7 +128,7 @@ export function rehypeComponent() {
u('element', {
tagName: 'code',
properties: {
className: ['language-tsx'],
className: ['language-angular-ts'],
},
children: [
{
Expand Down
6 changes: 3 additions & 3 deletions docs/src/__registry__/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import process from 'node:process'
import { registry } from '@/registry'
import { type Style, styles } from '@/registry/registry-styles'
import { type Registry, registrySchema } from '@/registry/schema'
import { registry } from '../registry'
import { type Style, styles } from '../registry/registry-styles'
import { type Registry, registrySchema } from '../registry/schema'

export const Index: Record<Style['name'], any> = {
'default': {},
Expand Down
14 changes: 7 additions & 7 deletions docs/src/registry/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { blocks } from '@/registry/registry-blocks'
import { charts } from '@/registry/registry-charts'
import { examples } from '@/registry/registry-examples'
import { lib } from '@/registry/registry-lib'
import { themes } from '@/registry/registry-themes'
import { ui } from '@/registry/registry-ui'
import type { Registry } from '@/registry/schema'
import { blocks } from './registry-blocks'
import { charts } from './registry-charts'
import { examples } from './registry-examples'
import { lib } from './registry-lib'
import { themes } from './registry-themes'
import { ui } from './registry-ui'
import type { Registry } from './schema'

export const registry: Registry = [
...ui,
Expand Down

0 comments on commit 062239d

Please sign in to comment.