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

feat: switch-to-jsx-renderer #316

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions components/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
* Below you can see how to create reusable chunks/components/helpers.
* Check the files in the `template` folder to see how to import and use them within a template.
*/

import {
Indent,
IndentationTypes,
withIndendation,
} from "@asyncapi/generator-react-sdk";

/*
* Each component has a `childrenContent` property.
* It is the processed children content of a component into a pure string. You can use it for compositions in your component.
*
* Example:
* function CustomComponent({ childrenContent }) {
* return `some text at the beginning: ${childrenContent}`
* }
*
* function RootComponent() {
* return (
* <CustomComponent>
* some text at the end.
* </CustomComponent>
* );
* }
*
* then output from RootComponent will be `some text at the beginning: some text at the end.`.
*/
export function HTML({ childrenContent }) {
return `
<!DOCTYPE html>
<html lang="en">
${childrenContent}
</html>
`;
}

/*
* If you need indent content inside template you can use `withIndendation` function or wrap content between `Indent` component.
* The mentioned helper and component can be imported from `@asyncapi/generator-react-sdk` package.
*
* `withIndendation` function performs action on pure string, but `Indent` can wraps part of template.
* You can see usage both cases below.
*
* Also you can see how to create components using composition.
* You can use another component with the given parameters for the given use-case.
*/
export function Head({ title, cssLinks = [],base }) {
const links = cssLinks
.map((link) => `<link rel="stylesheet" href="${link}">\n`)
.join("");
ritik307 marked this conversation as resolved.
Show resolved Hide resolved

const content = `
<head>
<meta charset="utf-8">
<base href=${base}>
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
<title>${title}</title>
${withIndendation(links, 2, IndentationTypes.SPACES)}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
</head>
`;

return (
<Indent size={2} type={IndentationTypes.SPACES}>
{content}
</Indent>
);
}

export function Body({ childrenContent }) {
const content = `
<body>
${withIndendation(childrenContent, 2, IndentationTypes.SPACES)}
</body>
`;

return (
<Indent size={2} type={IndentationTypes.SPACES}>
{content}
</Indent>
);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"access": "public"
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
},
"dependencies": {
"@asyncapi/generator-react-sdk": "^0.2.23",
"@asyncapi/parser": "^1.13.0",
"@asyncapi/react-component": "^1.0.0-next.32",
"highlight.js": "10.7.3",
Expand Down
58 changes: 58 additions & 0 deletions template/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { File } from "@asyncapi/generator-react-sdk";

// Import custom components from file
import { HTML, Head, Body } from "../components/common";

export default function Index({ asyncapi, params }) {
const getCssLinks = () => {
const cssLinks = [];
if (params.singleFile) {
cssLinks.push("template/css/asyncapi-ui.css");
cssLinks.push("template/css/asyncapi-ui.css");
} else {
cssLinks.push("css/asyncapi-ui.css");
cssLinks.push("css/asyncapi-ui.css");
}
return cssLinks;
};
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
return (
<File name="index.html">
<HTML>
<Head
title={
asyncapi.info().title() +
asyncapi.info().version() +
"documentation"
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
}
cssLinks={getCssLinks()}
base={params.baseHref ? params.baseHref : ""}
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
/>
<Body>
<div id="root">{asyncapi}</div>
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
<Scripts params={params} />
</Body>
</HTML>
</File>
);
}
function Scripts({ params }) {
if (params.singleFile) {
return `
<script src="template/js/asyncapi-ui.min.js" type="text/javascript" ></script>
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
<script>
var schema = ${asyncapi};
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
var config = ${params};
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
AsyncApiStandalone.hydrate({ schema, config }, document.getElementById("root"));
</script>
`;
} else {
return `
<script src="js/asyncapi-ui.min.js" type="application/javascript"></script>
<script>
var schema = ${asyncapi};
var config = ${params};
ritik307 marked this conversation as resolved.
Show resolved Hide resolved
AsyncApiStandalone.hydrate({ schema, config }, document.getElementById("root"));
</script>
`;
}
}