-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: headless implement docs content (#2)
* setup * basic usage * code highlight * example component * fix * fix typecheck * improve example like real usage * fix reactive
- Loading branch information
Showing
16 changed files
with
1,894 additions
and
93 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -22,3 +22,7 @@ dist-ssr | |
*.njsproj | ||
*.sln | ||
*.sw? | ||
|
||
.velite | ||
|
||
public/static |
This file contains 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,71 @@ | ||
import { Suspense, lazy } from 'react' | ||
import { codeToHtml } from 'shiki' | ||
|
||
const exampleComponents = import.meta.glob('../examples/**/*.tsx') as { | ||
[key: string]: () => Promise<{ | ||
default: React.ComponentType | ||
}> | ||
} | ||
|
||
const exampleCodes = import.meta.glob('../examples/**/*.tsx', { | ||
query: '?raw', | ||
import: 'default', | ||
}) as { | ||
[key: string]: () => Promise<string> | ||
} | ||
|
||
export function ComponentExample({ name }: { name: string }) { | ||
const path = `../examples/${name}.tsx` | ||
|
||
return ( | ||
<div> | ||
<ComponentPreview path={path} /> | ||
<ComponentCode path={path} /> | ||
</div> | ||
) | ||
} | ||
|
||
function ComponentPreview(props: { path: string }) { | ||
const loadComponent = exampleComponents[props.path] | ||
|
||
if (!loadComponent) { | ||
throw new Error(`Component does not exists on [${props.path}]`) | ||
} | ||
|
||
const Component = lazy(loadComponent) | ||
|
||
return ( | ||
// TODO | ||
<Suspense fallback="loading..."> | ||
<Component /> | ||
</Suspense> | ||
) | ||
} | ||
|
||
function ComponentCode(props: { path: string }) { | ||
const loadCode = exampleCodes[props.path] | ||
|
||
if (!loadCode) { | ||
throw new Error(`Component does not exists on [${props.path}]`) | ||
} | ||
|
||
const Code = lazy(async () => { | ||
const code = await loadCode() | ||
const html = await codeToHtml(code, { | ||
lang: 'tsx', | ||
theme: 'github-dark', | ||
}) | ||
return { | ||
default: () => { | ||
return <div dangerouslySetInnerHTML={{ __html: html }} /> | ||
}, | ||
} | ||
}) | ||
|
||
return ( | ||
// TODO | ||
<Suspense fallback="loading..."> | ||
<Code /> | ||
</Suspense> | ||
) | ||
} |
This file contains 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,22 @@ | ||
import { ComponentExample } from './component-example' | ||
import '@web/styles/mdx.css' | ||
import * as runtime from 'react/jsx-runtime' | ||
|
||
const mdxComponents = { | ||
ComponentExample, | ||
} | ||
|
||
const useMDXComponent = (code: string) => { | ||
const fn = new Function(code) | ||
return fn({ ...runtime }).default | ||
} | ||
|
||
interface MdxProps { | ||
code: string | ||
components?: Record<string, React.ComponentType> | ||
} | ||
|
||
export const MDXContent = ({ code, components }: MdxProps) => { | ||
const Component = useMDXComponent(code) | ||
return <Component components={{ ...mdxComponents, ...components }} /> | ||
} |
This file contains 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,13 @@ | ||
--- | ||
title: Button component | ||
description: Button | ||
publishAt: 1992-02-25 13:22 | ||
--- | ||
|
||
The content of button component | ||
|
||
```js | ||
import { jsx } from 'react/jsx-runtime' | ||
``` | ||
|
||
<ComponentExample name="ui/button" /> |
This file contains 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,5 @@ | ||
import { Button } from '@dinui/react/button' | ||
|
||
export default function ButtonExample() { | ||
return <Button>hehe hoho</Button> | ||
} |
This file contains 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
This file contains 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
This file contains 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,25 @@ | ||
import { docs } from '@web/.velite' | ||
import { MDXContent } from '@web/components/mdx-content' | ||
import { useLoaderData, type LoaderFunctionArgs } from 'react-router-dom' | ||
|
||
export function loader({ params }: LoaderFunctionArgs) { | ||
const doc = docs.find((d) => `${params['category']}/${params['name']}` === d.relativePath) | ||
|
||
if (!doc) { | ||
throw new Error(`Doc does not exist: ${params['*']}`) | ||
} | ||
|
||
return { | ||
doc, | ||
} | ||
} | ||
|
||
export function Component() { | ||
const { doc } = useLoaderData() as Awaited<ReturnType<typeof loader>> | ||
|
||
return ( | ||
<div className="py-8 container"> | ||
<MDXContent code={doc.code} /> | ||
</div> | ||
) | ||
} |
This file contains 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
This file contains 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,35 @@ | ||
[data-rehype-pretty-code-figure] pre { | ||
@apply px-0; | ||
} | ||
|
||
[data-rehype-pretty-code-figure] code { | ||
@apply text-sm !leading-loose md:text-base border-0 p-0; | ||
} | ||
|
||
[data-rehype-pretty-code-figure] code[data-line-numbers] { | ||
counter-reset: line; | ||
} | ||
|
||
[data-rehype-pretty-code-figure] code[data-line-numbers] > [data-line]::before { | ||
counter-increment: line; | ||
content: counter(line); | ||
@apply mr-4 inline-block w-4 text-right text-gray-500; | ||
} | ||
|
||
[data-rehype-pretty-code-figure] [data-line] { | ||
@apply border-l-2 border-l-transparent px-3; | ||
} | ||
|
||
[data-rehype-pretty-code-figure] [data-highlighted-line] { | ||
background: rgba(200, 200, 255, 0.1); | ||
@apply border-l-blue-400; | ||
} | ||
|
||
[data-rehype-pretty-code-figure] [data-highlighted-chars] { | ||
@apply rounded bg-zinc-600/50; | ||
box-shadow: 0 0 0 4px rgb(82 82 91 / 0.5); | ||
} | ||
|
||
[data-rehype-pretty-code-figure] [data-chars-id] { | ||
@apply border-b-2 p-1 shadow-none; | ||
} |
This file contains 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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
/** @type {import('tailwindcss').Config} */ | ||
import type { Config } from 'tailwindcss' | ||
|
||
export default { | ||
content: ['./layouts/**.tsx', './pages/**.tsx', './components/**.tsx'], | ||
content: ['./layouts/**.tsx', './pages/**.tsx', './components/**.tsx', './examples/**.tsx'], | ||
theme: { | ||
extend: {}, | ||
}, | ||
plugins: [], | ||
} | ||
} satisfies Config |
This file contains 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
This file contains 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,50 @@ | ||
import rehypeAutolinkHeadings from 'rehype-autolink-headings' | ||
import rehypePrettyCode from 'rehype-pretty-code' | ||
import rehypeSlug from 'rehype-slug' | ||
import { defineCollection, defineConfig, s } from 'velite' | ||
|
||
const docs = defineCollection({ | ||
name: 'Post', | ||
pattern: 'docs/**/*.mdx', | ||
schema: s | ||
.object({ | ||
title: s.string().min(1).max(99), | ||
description: s.string().min(1), | ||
publishAt: s.isodate(), | ||
|
||
toc: s.toc(), | ||
code: s.mdx(), | ||
path: s.path(), | ||
}) | ||
.transform((data) => { | ||
const relativePath = data.path.replace('docs/', '') | ||
const pathName = '/' + data.path | ||
|
||
return { | ||
...data, | ||
relativePath, | ||
pathName, | ||
} | ||
}), | ||
}) | ||
|
||
export default defineConfig({ | ||
collections: { | ||
docs, | ||
}, | ||
mdx: { | ||
rehypePlugins: [ | ||
rehypeSlug, | ||
[rehypePrettyCode, { theme: 'github-dark' }], | ||
[ | ||
rehypeAutolinkHeadings, | ||
{ | ||
properties: { | ||
className: ['subheading-anchor'], | ||
ariaLabel: 'Link to section', | ||
}, | ||
}, | ||
], | ||
], | ||
}, | ||
}) |
This file contains 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
This file contains 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.