Skip to content

Commit 8f5af6b

Browse files
add check for valid json, convert lang to text if not valid to prevent errors
1 parent db92268 commit 8f5af6b

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

src/plugin/VCodeBlock.vue

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,13 @@
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+
335339
function 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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy