Skip to content

Commit 36d0bc7

Browse files
feat: highlight markdown code blocks (#210)
1 parent 3274103 commit 36d0bc7

File tree

8 files changed

+114
-311
lines changed

8 files changed

+114
-311
lines changed

filters/all.js

+37-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +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 AsyncApiUI = require('@asyncapi/react-component').default;
5+
const { default: AsyncApiComponent, hljs } = require('@asyncapi/react-component');
66

77
const filter = module.exports;
88

@@ -56,15 +56,32 @@ function replaceCircular(val, cache) {
5656
return val;
5757
}
5858

59+
let initLanguages = false;
5960
/**
60-
* Stringifies the specification with escaping circular refs
61-
* and annotates that specification is parsed.
61+
* Load all language configurations from highlight.js
6262
*/
63-
function stringifySpec(asyncapi) {
64-
asyncapi._json['x-parser-spec-parsed'] = true;
65-
return JSON.stringify(replaceCircular(asyncapi.json()));
63+
function loadLanguagesConfig() {
64+
if (initLanguages === true) {
65+
return;
66+
}
67+
68+
/**
69+
* Retrieve the location of highlight.js.
70+
* It's needed because someone can have installed `highlight.js` as global dependency
71+
* or depper than local `node_modules` of this template.
72+
*/
73+
const hljsPackageDir = path.dirname(require.resolve("highlight.js/package.json"))
74+
const hljsLanguagesPath = path.resolve(hljsPackageDir, 'lib/languages');
75+
const languages = fs.readdirSync(hljsLanguagesPath);
76+
77+
for (let langPath of languages) {
78+
const lang = require(path.resolve(hljsLanguagesPath, langPath));
79+
hljs.registerLanguage(lang.name, lang);
80+
}
81+
82+
initLanguages = true;
6683
}
67-
filter.stringifySpec = stringifySpec;
84+
filter.loadLanguagesConfig = loadLanguagesConfig;
6885

6986
/**
7087
* More safe function to include content of given file than default Nunjuck's `include`.
@@ -76,6 +93,15 @@ function includeFile(pathFile) {
7693
}
7794
filter.includeFile = includeFile;
7895

96+
/**
97+
* Stringifies the specification with escaping circular refs
98+
* and annotates that specification is parsed.
99+
*/
100+
function stringifySpec(asyncapi) {
101+
return JSON.stringify(replaceCircular(asyncapi.json()));
102+
}
103+
filter.stringifySpec = stringifySpec;
104+
79105
/**
80106
* Stringifies prepared configuration for component.
81107
*/
@@ -85,10 +111,12 @@ function stringifyConfiguration(params) {
85111
filter.stringifyConfiguration = stringifyConfiguration;
86112

87113
/**
88-
* Renders AsyncApiUI component by given AsyncAPI spec and with corresponding template configuration.
114+
* Renders AsyncApi component by given AsyncAPI spec and with corresponding template configuration.
89115
*/
90116
function renderSpec(asyncapi, params) {
91-
const component = React.createElement(AsyncApiUI, { schema: asyncapi, config: prepareConfiguration(params) });
117+
loadLanguagesConfig();
118+
119+
const component = React.createElement(AsyncApiComponent, { schema: asyncapi, config: prepareConfiguration(params) });
92120
return ReactDOMServer.renderToString(component);
93121
}
94122
filter.renderSpec = renderSpec;

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@
104104
},
105105
"generator": ">=1.4.0 <2.0.0",
106106
"nonRenderableFiles": [
107-
"js/react.production.min.js",
108-
"js/react-dom.production.min.js",
109-
"js/asyncapi-ui.wp.js"
107+
"js/asyncapi-ui.min.js"
110108
]
111109
},
112110
"jest": {

scripts/copy-sources.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ const copyFile = util.promisify(fs.copyFile);
1111

1212
// source (node_modules): destination (template)
1313
const filesToCopy = {
14-
"react/umd/react.production.min.js": "js/react.production.min.js",
15-
"react-dom/umd/react-dom.production.min.js": "js/react-dom.production.min.js",
16-
"@asyncapi/react-component/browser/without-parser.js": "js/asyncapi-ui.min.js",
14+
"@asyncapi/react-component/browser/standalone/without-parser.js": "js/asyncapi-ui.min.js",
1715
"@asyncapi/react-component/styles/default.min.css": "css/styles.min.css",
1816
};
1917

template/index.html

+1-12
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,17 @@
2020
<div id="root">{{ asyncapi | renderSpec(params) | safe }}</div>
2121

2222
{% if params.singleFile %}
23-
<script type="text/javascript">
24-
{{ "template/js/react.production.min.js" | includeFile | safe }}
25-
</script>
26-
<script type="text/javascript">
27-
{{ "template/js/react-dom.production.min.js" | includeFile | safe }}
28-
</script>
2923
<script type="text/javascript">
3024
{{ "template/js/asyncapi-ui.min.js" | includeFile | safe }}
3125
</script>
3226
{% else %}
33-
<script src="js/react.production.min.js" type="application/javascript"></script>
34-
<script src="js/react-dom.production.min.js" type="application/javascript"></script>
3527
<script src="js/asyncapi-ui.min.js" type="application/javascript"></script>
3628
{% endif %}
3729

3830
<script>
3931
var schema = {{ asyncapi | stringifySpec | safe }};
4032
var config = {{ params | stringifyConfiguration | safe }};
41-
ReactDOM.hydrate(
42-
React.createElement(AsyncApiComponent, { schema, config }),
43-
document.getElementById("root")
44-
);
33+
AsyncApiStandalone.hydrate({ schema, config }, document.getElementById("root"));
4534
</script>
4635
</body>
4736
</html>

template/js/asyncapi-ui.min.js

+34-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)