Skip to content

Commit 7b64c2a

Browse files
committed
Escape code in unknown formats adequately
1 parent 0a8f804 commit 7b64c2a

2 files changed

Lines changed: 19 additions & 20 deletions

File tree

src/components/CodeViewer.vue

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<pre class="code-viewer" @click.prevent="click"><code v-html="output"></code></pre>
2+
<pre class="code-viewer" @click.prevent="click"><code v-if="rawOutput" v-html="rawOutput"></code><code v-else>{{code}}</code></pre>
33
</template>
44
<script>
55
import hljs from 'highlight.js';
@@ -29,9 +29,9 @@ export default {
2929
type: String,
3030
default: 'text',
3131
},
32-
highlight: {
33-
type: Boolean,
34-
default: true,
32+
rawCode: {
33+
type: String,
34+
required: false,
3535
},
3636
openUrls: {
3737
type: Boolean,
@@ -54,26 +54,25 @@ export default {
5454
knownLang() {
5555
return getHighlightLanguage(this.language);
5656
},
57-
output() {
58-
if (!this.highlighter) {
59-
return null;
60-
}
61-
if (!this.highlight) {
62-
return this.code;
57+
rawOutput() {
58+
if (this.rawCode) {
59+
return this.rawCode;
6360
}
64-
try {
65-
let output = this.highlighter.highlight(this.code, {
61+
if (this.highlighter && this.highlighter.getLanguage(this.language)) {
62+
try {
63+
let output = this.highlighter.highlight(this.code, {
6664
language: this.knownLang,
6765
}).value;
68-
if (this.autolink) {
69-
output = this.autolinkFunction(output, this.knownLang)
66+
if (this.autolink) {
67+
output = this.autolinkFunction(output, this.knownLang)
68+
}
69+
this.$emit('highlight', output);
70+
return output;
71+
} catch (e) {
72+
console.log('Error highlighting code', e);
7073
}
71-
this.$emit('highlight', output);
72-
return output;
73-
} catch (e) {
74-
console.log('Error highlighting code', e);
75-
return this.code;
7674
}
75+
return false;
7776
},
7877
},
7978
methods: {

src/components/bblock/ExampleViewer.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<div style="max-height: 30em; overflow-y: auto">
1414
<code-viewer
1515
:code="currentSnippet.highlighted || currentSnippet.code"
16+
:raw-code="currentSnippet.highlighted"
1617
:language="currentSnippet.language.highlight || currentSnippet.language.id"
17-
:highlight="!currentSnippet.highlighted"
1818
@highlight="currentSnippet.highlighted = $event"
1919
>
2020
</code-viewer>

0 commit comments

Comments
 (0)