@@ -2,7 +2,7 @@ const fs = require('fs');
2
2
const path = require ( 'path' ) ;
3
3
const React = require ( 'react' ) ;
4
4
const ReactDOMServer = require ( 'react-dom/server' ) ;
5
- const AsyncApiUI = require ( '@asyncapi/react-component' ) . default ;
5
+ const { default : AsyncApiComponent , hljs } = require ( '@asyncapi/react-component' ) ;
6
6
7
7
const filter = module . exports ;
8
8
@@ -56,15 +56,32 @@ function replaceCircular(val, cache) {
56
56
return val ;
57
57
}
58
58
59
+ let initLanguages = false ;
59
60
/**
60
- * Stringifies the specification with escaping circular refs
61
- * and annotates that specification is parsed.
61
+ * Load all language configurations from highlight.js
62
62
*/
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 ;
66
83
}
67
- filter . stringifySpec = stringifySpec ;
84
+ filter . loadLanguagesConfig = loadLanguagesConfig ;
68
85
69
86
/**
70
87
* More safe function to include content of given file than default Nunjuck's `include`.
@@ -76,6 +93,15 @@ function includeFile(pathFile) {
76
93
}
77
94
filter . includeFile = includeFile ;
78
95
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
+
79
105
/**
80
106
* Stringifies prepared configuration for component.
81
107
*/
@@ -85,10 +111,12 @@ function stringifyConfiguration(params) {
85
111
filter . stringifyConfiguration = stringifyConfiguration ;
86
112
87
113
/**
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.
89
115
*/
90
116
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 ) } ) ;
92
120
return ReactDOMServer . renderToString ( component ) ;
93
121
}
94
122
filter . renderSpec = renderSpec ;
0 commit comments