Skip to content

Commit 0310dc6

Browse files
authored
allow script and script setup in same example (#104)
* allow script and script setup in same example * fix build * use highlighter from vsg * fix editor line numbers * build editor errors
1 parent 3aa4320 commit 0310dc6

9 files changed

+117
-164
lines changed

package-lock.json

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

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"debounce": "^1.2.1",
2424
"hash-sum": "^2.0.0",
2525
"prismjs": "^1.29.0",
26-
"vue-inbrowser-compiler-sucrase": "^4.62.1",
26+
"vue-inbrowser-compiler-sucrase": "^4.64.1",
27+
"vue-inbrowser-prismjs-highlighter": "^4.69.1",
2728
"vue-prism-editor": "^2.0.0-alpha.2"
2829
},
2930
"devDependencies": {

src/Editor.vue

+69-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<template>
2-
<PrismEditor v-model="stableCode" @update:modelValue="updatePreview" :highlight="highlighter" v-bind="editorProps" />
2+
<PrismEditor :class="{ 'VueLive-LineNumbers': editorProps.lineNumbers }" v-model="stableCode"
3+
@update:modelValue="updatePreview" :highlight="highlighter" v-bind="editorProps" :lineNumbers="false" />
34
</template>
45

56
<script lang="ts">
67
import { defineComponent, type PropType } from "vue";
78
import { PrismEditor } from "vue-prism-editor";
9+
import makeHighlight, { CONFIGURED_LANGS, type CONFIGURED_LANGS_TYPE } from "vue-inbrowser-prismjs-highlighter";
810
import debounce from "debounce";
911
1012
import "vue-prism-editor/dist/prismeditor.min.css";
1113
12-
import makeHighlight, { CONFIGURED_LANGS, type CONFIGURED_LANGS_TYPE } from "./utils/highlight";
1314
1415
const UPDATE_DELAY = 300;
1516
@@ -37,7 +38,7 @@ export default defineComponent({
3738
prismLang: {
3839
type: String as PropType<CONFIGURED_LANGS_TYPE>,
3940
default: "html",
40-
validator: (val: string) => CONFIGURED_LANGS.includes(val as CONFIGURED_LANGS_TYPE),
41+
validator: (val: CONFIGURED_LANGS_TYPE) => CONFIGURED_LANGS.includes(val),
4142
},
4243
jsx: {
4344
type: Boolean,
@@ -57,10 +58,7 @@ export default defineComponent({
5758
* editor repainted every keystroke
5859
*/
5960
stableCode: this.code,
60-
highlight: (() => (code: string) => code) as (
61-
lang: CONFIGURED_LANGS_TYPE,
62-
jsxInExamples: boolean
63-
) => (code: string, errorLoc: any) => string,
61+
highlight: (() => (code: string) => code) as Awaited<ReturnType<typeof makeHighlight>>,
6462
};
6563
},
6664
async beforeMount() {
@@ -69,16 +67,51 @@ export default defineComponent({
6967
* load javascript first then load jsx
7068
* order is not guaranteed to work in ESmodules imports
7169
*/
72-
this.highlight = await makeHighlight();
70+
this.highlight = await makeHighlight('VueLive-squiggles');
7371
},
7472
methods: {
7573
highlighter(code: string) {
7674
return this.highlight(this.prismLang, this.jsx)(
7775
code,
78-
this.squiggles && this.error && this.error.loc
76+
this.squiggles && this.adaptedErrorLoc ? this.adaptedErrorLoc : undefined
7977
);
8078
},
8179
},
80+
computed: {
81+
adaptedErrorLoc() {
82+
// for now, we only support error in vsg format.
83+
// TODO: figure out how SourceMaps work and use them to support vue-sfc format
84+
if (this.prismLang !== 'vsg') {
85+
return undefined
86+
}
87+
88+
const scriptEnd = /\n[\t ]*</.exec(this.stableCode)
89+
const scriptCode = scriptEnd ? this.stableCode.slice(0, scriptEnd.index + 1) : ''
90+
const linesInScriptCode = (/^[\t ]*</.test(scriptCode) ? 0 : (scriptCode.match(/\n/g)?.length || 0)) + 1
91+
92+
return this.error && this.error.loc ?
93+
this.error.loc.start ?
94+
{
95+
start: {
96+
...this.error.loc.start,
97+
line: this.error.loc.start.line + linesInScriptCode,
98+
},
99+
end: {
100+
...this.error.loc.end,
101+
line: this.error.loc.end.line + linesInScriptCode,
102+
},
103+
} : this.error.loc.line ? {
104+
start: {
105+
...this.error.loc,
106+
line: this.error.loc.line + linesInScriptCode,
107+
},
108+
end: {
109+
...this.error.loc,
110+
line: this.error.loc.line + linesInScriptCode,
111+
},
112+
} : undefined : undefined
113+
},
114+
},
82115
watch: {
83116
code(value) {
84117
this.updatePreview(value);
@@ -103,4 +136,31 @@ export default defineComponent({
103136
.VueLive-squiggles {
104137
border-bottom: 2px dotted red;
105138
}
139+
140+
.VueLive-LineNumbers.prism-editor-wrapper pre.prism-editor__editor,
141+
.VueLive-LineNumbers.prism-editor-wrapper textarea.prism-editor__textarea {
142+
padding-left: 2.5rem;
143+
}
144+
145+
146+
.VueLive-LineNumbers pre.prism-editor__editor {
147+
counter-reset: step;
148+
counter-increment: step 0;
149+
}
150+
151+
.VueLive-LineNumbers pre .line {
152+
position: relative;
153+
}
154+
155+
.VueLive-LineNumbers pre .line::before {
156+
content: counter(step);
157+
counter-increment: step;
158+
white-space: nowrap;
159+
width: 2rem;
160+
position: absolute;
161+
left: -2.5rem;
162+
display: inline-block;
163+
text-align: right;
164+
color: rgba(255, 255, 255, .4)
165+
}
106166
</style>

src/VueLive.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import hash from "hash-sum";
2525
import Preview from "./Preview.vue";
2626
import Editor from "./Editor.vue";
2727
import VueLiveDefaultLayout from "./VueLiveDefaultLayout.vue";
28-
import type { CONFIGURED_LANGS_TYPE } from "./utils/highlight";
28+
import type { CONFIGURED_LANGS_TYPE } from "vue-inbrowser-prismjs-highlighter";
2929
3030
const LANG_TO_PRISM = {
3131
vue: "html",

src/utils/getScript.ts

-26
This file was deleted.

0 commit comments

Comments
 (0)