Skip to content

Commit

Permalink
add stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
helios2003 committed Jul 1, 2024
1 parent a428b15 commit 4d5c517
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 19 deletions.
83 changes: 64 additions & 19 deletions apps/studio-next/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,74 @@
import dynamic from 'next/dynamic';
import parseURL from '@/helpers/parser';
import { DocumentInfo } from '@/types';
const StudioWrapper = dynamic(() => import('@/components/StudioWrapper'), {ssr: false})
import { Metadata } from 'next';
import ogImage from '@/img/meta-studio-og-image.jpeg';

export const metadata: Metadata = {
metadataBase: new URL('https://studio-next.netlify.app'),
openGraph: {
type: 'website',
title: 'AsyncAPI Studio',
description: 'Studio for AsyncAPI specification, where you can validate, view preview documentation, and generate templates from AsyncAPI document.',
url: 'https://studio-next.netlify.app',
images: [
{
url: ogImage.src,
width: 800,
height: 600,
alt: 'AsyncAPI default image',
},
]
},
twitter: {
site: '@AsyncAPISpec',
type Props = {
searchParams: { [key: string]: string | undefined };
};

export async function generateMetadata({
searchParams,
}: Props): Promise<Metadata> {
const base64Doc = searchParams["base64"];
let metadata: Metadata = {
metadataBase: new URL("https://studio-studio-next.vercel.app"),
openGraph: {
type: "website",
title: "AsyncAPI Studio",
description:
"Studio for AsyncAPI specification, where you can validate, view preview documentation, and generate templates from AsyncAPI document.",
url: "https://studio-studio-next.vercel.app",
images: [
{
url: ogImage.src,
alt: "AsyncAPI default image",
},
],
},
twitter: {
site: "@AsyncAPISpec",
},
};
if (base64Doc) {
try {
const ogInfo: DocumentInfo = await parseURL(base64Doc);
metadata = {
...metadata,
openGraph: {
...metadata.openGraph,
title: ogInfo.title,
description: ogInfo.description,
images: [
{
url: `https://ogp-studio.vercel.app/api/og?title=${ogInfo.title}&description=${ogInfo.description}&numServers=${ogInfo.numServers}&numChannels=${ogInfo.numChannels}`,
alt: "AsyncAPI default image",
},
],
},
twitter: {
...metadata.twitter,
title: ogInfo.title,
description: ogInfo.description,
images: [
{
url: `https://ogp-studio.vercel.app/api/og?title=${ogInfo.title}&description=${ogInfo.description}&numServers=${ogInfo.numServers}&numChannels=${ogInfo.numChannels}`,
alt: "AsyncAPI default image",
},
],
},
};
} catch (error) {
console.error("Error parsing URL:", error);
}
}

return metadata;
}
export default async function Home() {
export default async function Home({ searchParams }: Props) {
await generateMetadata({ searchParams });
return (
<StudioWrapper />
)
Expand Down
39 changes: 39 additions & 0 deletions apps/studio-next/src/helpers/parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Parser } from '@asyncapi/parser';
import { DocumentInfo } from '@/types';

export default async function parseURL(base64Document: string): Promise<DocumentInfo> {
const parser = new Parser();

const decodedDocument = Buffer.from(base64Document, "base64").toString("utf-8");
const { document, diagnostics } = await parser.parse(decodedDocument);

// if (diagnostics.length !== 0) {
// console.log(diagnostics);
// }

let title = document?.info().title();
if (title !== undefined) {
title = title.length <= 20 ? title : title.slice(0, 20) + "...";
}
const version = document?.info().version();

let description = document?.info().description();
if (description !== undefined) {
description = description.length <= 100 ? description : description.slice(0, 100) + "...";
}

const servers = document?.allServers();
const channels = document?.allChannels();

const numServers = servers?.length;
const numChannels = channels?.length;

const response = {
title,
version,
description,
numServers,
numChannels
};
return response;
}
8 changes: 8 additions & 0 deletions apps/studio-next/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import type specs from '@asyncapi/specs';

export type SpecVersions = keyof typeof specs.schemas;

export interface DocumentInfo {
title? : string,
version? : string,
description? : string,
numServers? : number,
numChannels? : number
}

0 comments on commit 4d5c517

Please sign in to comment.