Skip to content
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

Adding: Code hightligher & MarigoldProvider #3150

Merged
merged 17 commits into from
Jul 20, 2023
Merged
22 changes: 22 additions & 0 deletions docs/content/components/accordion/accordion-basic.demo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Accordion, Headline, TextField } from '@marigold/components';

export default () => (
<Accordion defaultExpandedKeys={['1']}>
<Accordion.Item key={1} title="Informations">
{/* just to test scroll */}
<Headline level="3">
Some Lorem ipsum dolor sit amet consectetur adipisicing elit. Autem
voluptatem minima hic ex ullam, asperiores quaerat quas eveniet, magnam
quisquam dolores. Beatae eaque eveniet error ipsa veniam vero culpa
recusandae?
</Headline>
<TextField label="Name" />
</Accordion.Item>
<Accordion.Item key={2} title="Personal Settings">
two
</Accordion.Item>
<Accordion.Item key={3} title="Billing Adress">
<Headline level="3">Some Imformations</Headline>
</Accordion.Item>
</Accordion>
);
2 changes: 2 additions & 0 deletions docs/content/components/accordion/accordion.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ import { Accordion } from '@marigold/components';
### Props Accordion.Item

## Examples

<ComponentDemo file="./accordion-basic.demo.tsx" />
66 changes: 66 additions & 0 deletions docs/contentlayer.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import {

import remarkGfm from 'remark-gfm';
import rehypeSlug from 'rehype-slug';
import rehypePrettyCode from 'rehype-pretty-code';
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
import { getHighlighter, loadTheme } from 'shiki';
import { visit } from 'unist-util-visit';

import path from 'node:path';

import { rehypeComponentDemo } from './lib/mdx/rehype-component-demo';

import { siteConfig } from './lib/config';

// Helpers
Expand Down Expand Up @@ -88,6 +94,66 @@ export default makeSource({
rehypePlugins: [
[rehypeComponentDemo, { contentDirPath }],
rehypeSlug,
// to inject the source code and other stuff inside `pre` element props
() => tree => {
visit(tree, node => {
if (node?.type === 'element' && node?.tagName === 'pre') {
const [codeEl] = node.children;
if (codeEl.tagName !== 'code') {
return;
}

if (codeEl.data?.meta) {
// Extract event from meta and pass it down the tree.
const regex = /event="([^"]*)"/;
const match = codeEl.data?.meta.match(regex);
if (match) {
node.__event__ = match ? match[1] : null;
codeEl.data.meta = codeEl.data.meta.replace(regex, '');
}
}
node.__rawString__ = codeEl.children?.[0].value;
}
});
},
[rehypeAutolinkHeadings, { behavior: 'wrap' }],
OsamaAbdellateef marked this conversation as resolved.
Show resolved Hide resolved
[
rehypePrettyCode,
{
getHighlighter: async () => {
OsamaAbdellateef marked this conversation as resolved.
Show resolved Hide resolved
const theme = await loadTheme(
// loading theme file
path.join(process.cwd(), './light.json')
OsamaAbdellateef marked this conversation as resolved.
Show resolved Hide resolved
);
return await getHighlighter({ theme });
},
onVisitLine(node: any) {
if (node.children.length === 0) {
node.children = [{ type: 'text', value: ' ' }];
OsamaAbdellateef marked this conversation as resolved.
Show resolved Hide resolved
}
},
onVisitHighlightedLine(node: any) {
node.properties.className.push('line--highlighted');
},
onVisitHighlightedWord(node: any) {
node.properties.className = ['word--highlighted'];
},
},
],
() => tree => {
visit(tree, node => {
if (node?.type === 'element' && node?.tagName === 'div') {
if (!('data-rehype-pretty-code-fragment' in node.properties)) {
return;
}
const preElement = node.children.at(-1);
if (preElement.tagName !== 'pre') {
return;
}
preElement.properties['__rawString__'] = node.__rawString__;
}
});
},
[
rehypeAutolinkHeadings,
{
Expand Down
10 changes: 10 additions & 0 deletions docs/lib/registry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import dynamic from 'next/dynamic';
OsamaAbdellateef marked this conversation as resolved.
Show resolved Hide resolved

export const registry: any = {
'button-variant': dynamic(
() => import('@/content/components/button/button-variant.demo')
),
'accordion-basic': dynamic(
() => import('@/content/components/accordion/accordion-basic.demo')
),
};
Loading