Skip to content

Commit aca5f9a

Browse files
feat: add parameter to enable setting favicon (#368)
Co-authored-by: Matatjahu <[email protected]>
1 parent 36cadc0 commit aca5f9a

File tree

6 files changed

+15053
-127
lines changed

6 files changed

+15053
-127
lines changed

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ HTML template for the [AsyncAPI Generator](https://github.com/asyncapi/generator
1818
- [Usage](#usage)
1919
- [Supported parameters](#supported-parameters)
2020
- [Development](#development)
21-
- [Contributors ✨](#contributors-%E2%9C%A8)
21+
- [Contributors ✨](#contributors-)
2222

2323
<!-- tocstop -->
2424

@@ -41,10 +41,11 @@ npm install -g @asyncapi/generator
4141
| sidebarOrganization | Defines how the sidebar should be organized. Set its value to `byTagsNoRoot` to categorize operations by operations tags. Set its value to `byTags` when you have tags on a root level. These tags are used to model tags navigation and need to have the same tags in operations. | No | undefined | `byTags`, `byTagsNoRoot` | `byTagsNoRoot` |
4242
| baseHref | Sets the base URL for links and forms. | No | `/` | *Any* | `/docs` |
4343
| version | Override the version of your application provided under `info.version` location in the specification file. | No | Version is taken from the spec file. | *Any* ([See Semver versioning](https://semver.org/)) | `1.0.0` |
44-
| singleFile | Set output into one html-file with styles and scripts inside | No | `false` | `true`,`false` | `true` |
44+
| singleFile | Set output into one html-file with styles and scripts inside. | No | `false` | `true`,`false` | `true` |
4545
| outFilename | The filename of the output file. | No | `index.html` | *Any* | `asyncapi.html` |
46-
| pdf | Generates output HTML as PDF | No | `false` | `true`, `false` | `true` |
47-
| pdfTimeout | Timeout (in ms) used to generate the PDF | No | 30000 | >=0 | 1000 |
46+
| pdf | Generates output HTML as PDF. | No | `false` | `true`, `false` | `true` |
47+
| pdfTimeout | Timeout (in ms) used to generate the PDF. | No | 30000 | >=0 | 1000 |
48+
| favicon | Defines the URL/Path used for the favicon. | No | `assets/asyncapi-favicon.ico` | Any valid favicon URL/Path. | `"https://studio.asyncapi.com/favicon.ico"` |
4849
| config | Inline stringified JSON or a path to a JSON file to override default React component config. The config override is merged with the default config using the [JSON Merge Patch](https://tools.ietf.org/html/rfc7386) algorithm. | No | `{ "show": { "sidebar": true }, "sidebar": { "showOperations": "byDefault" } }` | [JSON config for the React component](https://github.com/asyncapi/asyncapi-react/blob/next/docs/configuration/config-modification.md#definition) | `{"show":{"sidebar":false}}` |
4950

5051
> **NOTE**: If you only generate an HTML website, set the environment variable `PUPPETEER_SKIP_CHROMIUM_DOWNLOAD` to `true` and the generator will skip downloading chromium.

assets/asyncapi-favicon.ico

14.7 KB
Binary file not shown.

filters/all.js

+36
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require('fs');
22
const path = require('path');
33
const React = require('react');
44
const ReactDOMServer = require('react-dom/server');
5+
const fetch = require('node-fetch');
56
const { default: AsyncApiComponent, hljs } = require('@asyncapi/react-component');
67
const { AsyncAPIDocument } = require('@asyncapi/parser');
78

@@ -92,6 +93,41 @@ function loadLanguagesConfig() {
9293
}
9394
filter.loadLanguagesConfig = loadLanguagesConfig;
9495

96+
/**
97+
* Generate Base64 value from favicon
98+
*/
99+
async function generateBase64Favicon(params, callback) {
100+
const favicon = params.favicon;
101+
102+
// generate Base64 of AsyncAPI logo
103+
if (!favicon) {
104+
const data = "data:image/x-icon;base64," + fs.readFileSync(path.resolve(__dirname, '../assets/asyncapi-favicon.ico'), "base64");
105+
return callback(null, data);
106+
}
107+
108+
try {
109+
// Attempt to fetch favicon
110+
const response = await fetch(favicon);
111+
if (response.status == 200) {
112+
const buffer = await response.buffer()
113+
const data = "data:image/x-icon;base64," + buffer.toString('base64');
114+
callback(null, data);
115+
}
116+
} catch (fetchErr) {
117+
// Failed to fetch favicon...
118+
try {
119+
// Attempt to read favicon as file
120+
const data = "data:image/x-icon;base64," + fs.readFileSync(favicon, "base64");
121+
callback(null, data);
122+
} catch (err) {
123+
console.error("Failed to fetch/read favicon", fetchErr, err);
124+
callback(err);
125+
throw err;
126+
}
127+
}
128+
}
129+
filter.generateBase64Favicon = generateBase64Favicon;
130+
95131
/**
96132
* More safe function to include content of given file than default Nunjuck's `include`.
97133
* Attaches raw file's content instead of executing it - problem with some attached files in template.

0 commit comments

Comments
 (0)