Skip to content

Commit 8d54097

Browse files
Merge pull request #63 from webdevnerdstuff/fix/issue-60
Fix/issue 60
2 parents dde88d8 + f1c5f36 commit 8d54097

File tree

5 files changed

+125
-124
lines changed

5 files changed

+125
-124
lines changed

.release-it.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"git": {
3+
"commitMessage": "chore: release ${version}",
4+
"tagName": "v${version}"
5+
},
6+
"npm": {
7+
"publish": true
8+
},
9+
"github": {
10+
"release": true,
11+
"releaseName": "v${version}"
12+
}
13+
}

dist/vue-code-block.cjs.js

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

dist/vue-code-block.es.js

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

src/plugin/VCodeBlock.vue

+27-14
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

src/plugin/__tests__/__snapshots__/VCodeBlock.test.ts.snap

-32
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,5 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`VCodeBlock Component > Component > should ... 1`] = `
4-
{
5-
"browserWindow": false,
6-
"code": "",
7-
"codeBlockRadius": "0.5rem",
8-
"copyButton": true,
9-
"copyFailedText": "Copy failed!",
10-
"copyIcons": true,
11-
"copySuccessText": "Copied!",
12-
"copyTab": true,
13-
"copyText": "Copy Code",
14-
"cssPath": undefined,
15-
"floatingTabs": true,
16-
"globalOptions": false,
17-
"height": "auto",
18-
"highlightjs": true,
19-
"indent": 2,
20-
"label": "",
21-
"lang": "javascript",
22-
"languages": undefined,
23-
"maxHeight": "auto",
24-
"persistentCopyButton": false,
25-
"prismPlugin": false,
26-
"prismjs": false,
27-
"runTab": false,
28-
"runText": "Run",
29-
"tabGap": "0.25rem",
30-
"tabs": false,
31-
"theme": "neon-bunny",
32-
}
33-
`;
34-
353
exports[`VCodeBlock Component > Component > should mount the component 1`] = `
364
{
375
"browserWindow": false,

0 commit comments

Comments
 (0)