File tree Expand file tree Collapse file tree 1 file changed +27
-14
lines changed
Expand file tree Collapse file tree 1 file changed +27
-14
lines changed Original file line number Diff line number Diff line change 9696 :class =" `language-${settings.lang}`"
9797 :style =" preTagStyles"
9898 >
99- <code
100- v-if =" prismPlugin"
101- :class =" `language-${settings.lang} ${settings.browserWindow ? 'v-code-block--code-browser' : ''} ${settings.highlightjs ? 'hljs' : ''}`"
102- :style =" codeTagStyles"
103- v-text =" computedCode"
104- ></code >
105- <code
106- v-else
107- :class =" `language-${settings.lang} ${settings.browserWindow ? 'v-code-block--code-browser' : ''} ${settings.highlightjs ? 'hljs' : ''}`"
108- :style =" codeTagStyles"
109- v-html =" renderedCode"
110- ></code >
111- </pre >
99+ <code v-if =" prismPlugin"
100+ :class =" `language-${settings.lang} ${settings.browserWindow ? 'v-code-block--code-browser' : ''} ${settings.highlightjs ? 'hljs' : ''}`"
101+ :style =" codeTagStyles" v-text =" computedCode" ></code >
102+ <code v-else
103+ :class =" `language-${settings.lang} ${settings.browserWindow ? 'v-code-block--code-browser' : ''} ${settings.highlightjs ? 'hljs' : ''}`"
104+ :style =" codeTagStyles" v-html =" renderedCode" ></code >
105+ </pre >
112106 </div >
113107 </div >
114108</template >
@@ -332,10 +326,29 @@ function checkLibrary(): void {
332326 }
333327}
334328
329+ function isValidJSON(str : string ): boolean {
330+ try {
331+ JSON .parse (str );
332+ return true ;
333+ }
334+ catch (e ) {
335+ return false ;
336+ }
337+ }
338+
335339function convertCode(): void {
336340 if (settings .value .lang === ' json' ) {
337341 const propsCode = settings .value .code .toString ();
338- convertedCode .value = JSON .stringify (JSON .parse (propsCode ), null , settings .value .indent );
342+
343+ // Check if the code is valid JSON //
344+ if (isValidJSON (propsCode )) {
345+ convertedCode .value = JSON .stringify (JSON .parse (propsCode ), null , settings .value .indent );
346+ return ;
347+ }
348+
349+ // Change lang to text if not valid JSON to prevent errors //
350+ settings .value .lang = ' text' ;
351+ convertedCode .value = propsCode ;
339352 return ;
340353 }
341354
You can’t perform that action at this time.
0 commit comments