Skip to content

Commit

Permalink
feat: add rendering of case study asyncapi document (asyncapi#2770)
Browse files Browse the repository at this point in the history
Co-authored-by: Akshat Nema <[email protected]>%0ACo-authored-by: Lukasz Gornicki <[email protected]>
  • Loading branch information
akkshitgupta and derberg committed Jul 24, 2024
1 parent 840e3a8 commit 1ab6e4a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
12 changes: 12 additions & 0 deletions components/helpers/read-yaml-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from 'fs/promises';

export const readYamlFile = async (fileName) => {
try {
const data = await fs.readFile(`./public/${fileName}`, 'utf-8');
const yamlString = `\`\`\`yaml\n${data}\`\`\``;

return yamlString;
} catch (error) {
throw new Error(`Error: something went wrong while reading ${fileName} file: ${error.message}`);
}
};
12 changes: 12 additions & 0 deletions components/helpers/read-yaml-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from 'fs/promises';

export const readYamlFile = async (fileName: string) => {
try {
const data = await fs.readFile(`./public/${fileName}`, 'utf-8');
const yamlString = `\`\`\`yaml\n${data}\`\`\``;

return yamlString;
} catch (error: any) {
throw new Error(`Error: something went wrong while reading ${fileName} file: ${error.message}`);
}
};
6 changes: 6 additions & 0 deletions pages/casestudies/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Heading from '../../components/typography/Heading';
import Paragraph from '../../components/typography/Paragraph';
import CaseStudiesList from '../../config/case-studies.json';
import { generateCaseStudyContent } from '../../utils/staticHelpers';
import { readYamlFile } from '@/components/helpers/read-yaml-file';

interface IndexProps {
casestudy: ICaseStudy;
Expand All @@ -34,6 +35,7 @@ interface IndexProps {
asyncapiBindings: MDXRemoteSerializeResult;
asyncapiTools: MDXRemoteSerializeResult;
additionalResources: MDXRemoteSerializeResult;
fullExample: MDXRemoteSerializeResult;
}

const renderContent = (
Expand Down Expand Up @@ -96,6 +98,7 @@ const renderContent = (
*/
export async function getStaticProps({ params }: { params: { id: string } }) {
const data = CaseStudiesList.filter((p: { id: string }) => p.id === params.id);
const asyncApiDoc = await readYamlFile(data[0].asyncapi.fullExample);

return {
props: {
Expand All @@ -116,6 +119,7 @@ export async function getStaticProps({ params }: { params: { id: string } }) {
asyncapiDocumentation: await serialize(data[0].asyncapi.documentation),
asyncapiBindings: await serialize(data[0].asyncapi.bindings),
asyncapiTools: await serialize(data[0].asyncapi.tools),
fullExample: await serialize(asyncApiDoc),
additionalResources: await serialize(data[0].additionalResources)
}
};
Expand Down Expand Up @@ -153,6 +157,7 @@ const Index: React.FC<IndexProps> = ({
asyncapiDocumentation,
asyncapiBindings,
asyncapiTools,
fullExample,
additionalResources
}) => {
const image = '/img/social/website-card.png';
Expand All @@ -177,6 +182,7 @@ const Index: React.FC<IndexProps> = ({
asyncapiBindings,
asyncapiTools,
additionalResources,
fullExample,
casestudy
});

Expand Down
5 changes: 5 additions & 0 deletions utils/staticHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export const generateCaseStudyContent = (data: any) => {
asyncapiBindings,
asyncapiTools,
additionalResources,
fullExample,
casestudy
} = data;
const { languages } = casestudy.technical;
Expand Down Expand Up @@ -166,6 +167,10 @@ export const generateCaseStudyContent = (data: any) => {
]
}
]
},
{
title: "Production-use AsyncAPI document",
content: fullExample,
}
];
};

0 comments on commit 1ab6e4a

Please sign in to comment.