Skip to content

Commit 108ece5

Browse files
chore(richtext): astro richtext render logic added in playground
1 parent f6e28f0 commit 108ece5

File tree

10 files changed

+187
-212
lines changed

10 files changed

+187
-212
lines changed

packages/richtext/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"playground": "cd playground && npm run dev",
6969
"playground:vue": "cd playground/vue && npm run dev",
7070
"playground:react": "cd playground/react && npm run dev",
71+
"playground:astro": "pnpm run --filter ./playground/astro dev",
7172
"playground:all": "pnpm -r --parallel --filter='./playground/*' run dev",
7273
"release": "release-it",
7374
"release:dry": "release-it --dry-run"

packages/richtext/playground/astro/astro.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
1111
export default defineConfig({
1212
integrations: [
1313
storyblok({
14-
accessToken: 'xUMqa0Ka06Cfnrjb4M1e5Qtt',
14+
accessToken: 'OurklwV5XsDJTIE1NJaD2wtt',
1515
enableFallbackComponent: true,
1616
}),
1717
],
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
import StoryblokComponent from '@storyblok/astro/StoryblokComponent.astro';
3+
import { type SbBlokData } from '@storyblok/astro';
4+
5+
const body = Astro.props.body as SbBlokData[];
6+
---
7+
8+
{body.map((blok) => <StoryblokComponent blok={blok} />)}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
console.log(Astro.props);
3+
---
4+
5+
<span>This is custom link</span>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
import type { AstroRenderNode, StoryblokComponentMap } from '../utils';
3+
4+
interface Props {
5+
node: AstroRenderNode;
6+
components?: StoryblokComponentMap;
7+
}
8+
9+
const { node, components } = Astro.props;
10+
11+
function isText(n: AstroRenderNode): n is string {
12+
return typeof n === 'string';
13+
}
14+
15+
function isComponent(n: AstroRenderNode): n is Extract<AstroRenderNode, { component: unknown }> {
16+
return typeof n === 'object' && n !== null && 'component' in n;
17+
}
18+
19+
function isTag(n: AstroRenderNode): n is Extract<AstroRenderNode, { tag: unknown }> {
20+
return typeof n === 'object' && n !== null && 'tag' in n;
21+
}
22+
---
23+
24+
{isText(node) && node}
25+
26+
{
27+
isComponent(node) &&
28+
(() => {
29+
if (!components) {
30+
console.warn(`No components provided for rendering component node: ${node.component}`);
31+
return null;
32+
}
33+
const Component = components[node.component];
34+
if (!Component) return null;
35+
36+
return (
37+
<Component {...node.props}>
38+
{node.children?.map((child) => (
39+
<Astro.self node={child} components={components} />
40+
))}
41+
</Component>
42+
);
43+
})()
44+
}
45+
46+
{
47+
isTag(node) &&
48+
(() => {
49+
const Tag = node.tag;
50+
return (
51+
<Tag {...node.attrs}>
52+
{node.children?.map((child) => (
53+
<Astro.self node={child} components={components} />
54+
))}
55+
</Tag>
56+
);
57+
})()
58+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
import type { StoryblokRichTextNode } from '@storyblok/astro';
3+
import { getRichTextSegments, renderSegments, type StoryblokSegmentType } from '@storyblok/richtext';
4+
import { createAstroAdapter, type StoryblokComponentMap } from '../utils';
5+
import RichTextNode from './RichTextNode.astro';
6+
7+
interface Props {
8+
doc: StoryblokRichTextNode;
9+
components?: StoryblokComponentMap;
10+
}
11+
const { doc, components } = Astro.props;
12+
const segments = getRichTextSegments(doc);
13+
const adapter = createAstroAdapter();
14+
const keys = Object.keys(components || {}) as StoryblokSegmentType[];
15+
const content = renderSegments(segments, adapter, keys);
16+
---
17+
18+
{content.map((node) => <RichTextNode node={node} components={components} />)}

packages/richtext/playground/astro/src/components/Welcome.astro

Lines changed: 0 additions & 210 deletions
This file was deleted.

packages/richtext/playground/astro/src/pages/index.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const options: StoryblokRichTextOptions = {
2424
}),
2525
},
2626
};
27-
const renderedRichTextStory = segmentStoryblokRichText(data.story.content.richtext);
27+
const renderedRichTextStory = segmentStoryblokRichText(data.story.content.richText);
2828
2929
import testMarkdown from '/test.md?url&raw';
3030
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
import { useStoryblokApi, type SbBlokData } from '@storyblok/astro';
3+
import StoryblokRichtext from '../components/StoryblokRichtext.astro';
4+
import CustomLink from '../components/CustomLink.astro';
5+
import BlokRender from '../components/BlokRender.astro';
6+
7+
const storyblokApi = useStoryblokApi();
8+
const { data } = await storyblokApi.get('cdn/stories/richtext', {
9+
version: 'draft',
10+
});
11+
const doc = data.story.content.richText;
12+
---
13+
14+
<h1>Hello Astro</h1>
15+
<p>Welcome to your new Astro site.</p>
16+
<StoryblokRichtext
17+
doc={doc}
18+
components={{
19+
link: CustomLink,
20+
blok: BlokRender,
21+
}}
22+
/>

0 commit comments

Comments
 (0)