@@ -234,28 +234,39 @@ document.addEventListener('DOMContentLoaded', function () {
234234 * JSON syntax highlighter
235235 */
236236const json_syntax_highlight = ( json ) => {
237+ if ( typeof json !== 'string' ) {
238+ json = JSON . stringify ( json , null , 2 ) ;
239+ }
240+
241+ json = json . replace ( / & / g, '&' )
242+ . replace ( / < / g, '<' )
243+ . replace ( / > / g, '>' ) ;
244+
237245 return json . replace (
238- / ( " ( \\ u [ a - z A - Z 0 - 9 ] { 4 } | \\ [ ^ u ] | [ ^ \\ " ] ) * " ( \s * : ) ? | \b ( t r u e | f a l s e | n u l l ) \b | - ? \d + (?: \. \d * ) ? (?: [ e E ] [ + - ] ? \d + ) ? | [ [ \] { } : , s ] ) / g,
246+ / ( " ( \\ u [ a - z A - Z 0 - 9 ] { 4 } | \\ [ ^ u ] | [ ^ \\ " ] ) * " ( \s * : ) ? | \b ( t r u e | f a l s e | n u l l ) \b | - ? \d + (?: \. \d * ) ? (?: [ e E ] [ + - ] ? \d + ) ? | [ { } \ [\] , : ] ) / g,
239247 match => {
240- if ( match . startsWith ( "\"" ) ) {
241- if ( / " ( \w + ) " : / . test ( match ) ) {
242- return `<span class="json-key">${ match . replace ( '":' , '"' ) } </span><span class="json-colon">:</span>` ;
248+ let cls = '' ;
249+ if ( / ^ " / . test ( match ) ) {
250+ if ( / : $ / . test ( match ) ) {
251+ cls = 'json-key' ;
243252 } else {
244- return `<span class=" json-string"> ${ match } </span>` ;
253+ cls = ' json-string' ;
245254 }
246- } else if ( / [ [ \] { } ] / . test ( match ) ) {
247- return `<span class="json-bracket">${ match } </span>` ;
248255 } else if ( / t r u e | f a l s e / . test ( match ) ) {
249- return `<span class=" json-boolean"> ${ match } </span>` ;
256+ cls = ' json-boolean' ;
250257 } else if ( / n u l l / . test ( match ) ) {
251- return `<span class=" json-null"> ${ match } </span>` ;
258+ cls = ' json-null' ;
252259 } else if ( / ^ - ? \d + (?: \. \d + ) ? (?: [ e E ] [ + - ] ? \d + ) ? $ / . test ( match ) ) {
253- return `<span class="json-number">${ match } </span>` ;
260+ cls = 'json-number' ;
261+ } else if ( / [ \[ \] { } ] / . test ( match ) ) {
262+ cls = 'json-bracket' ;
254263 } else if ( match === ',' ) {
255- return `<span class=" json-comma"> ${ match } </span>` ;
256- } else {
257- return match ;
264+ cls = ' json-comma' ;
265+ } else if ( match === ':' ) {
266+ cls = 'json-colon' ;
258267 }
268+
269+ return cls ? `<span class="${ cls } ">${ match } </span>` : match ;
259270 }
260271 ) ;
261272} ;
0 commit comments