You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/vue-tutorial.md
+213-6Lines changed: 213 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,10 @@
1
-
# Vue/Nuxt Tutorial
2
-
1
+
# Vue/Nuxt Tutorial - For Highlight.js Integration
2
+
3
3
Vue and Nuxt have some similarities, but there is one big difference in how they can use this library. In Nuxt there is server side rendering (SSR) that will attempt to create the final HTML before sending the page to the browser. This cannot use any browser-specific things so the `code-input` component must be excluded from rendering until hydration in the browser.
4
4
5
+
> [!NOTE]
6
+
> The following demo is for integrating with `highlight.js` but if you follow the instructions in [the main README](../README.md) you can substitute highlight.js for Prism.js or a custom template.
7
+
5
8
## Vue
6
9
7
10
### 1. Create a Vue app
@@ -56,11 +59,11 @@ npm install
56
59
npm run dev
57
60
```
58
61
59
-
You should be able to open your browser to the path that it prints out and see a started Vue app. If so, congratulations! Hit Ctrl-C to stop it.
62
+
You should be able to open your browser to the path that it prints out and see a working Vue app. If so, congratulations! Hit Ctrl-C to stop it.
60
63
61
64
### 2. Add dependencies
62
65
63
-
This tutorial will use `highlight.js` for the syntax highlighting. If you are using a different method then adjust as needed.
66
+
> This tutorial will use `highlight.js` for the syntax highlighting. If you are using a different method then adjust as needed.
64
67
65
68
Type this:
66
69
```bash
@@ -79,6 +82,8 @@ vue({
79
82
})
80
83
```
81
84
85
+
So that Vue knows that `code-input` is not a Vue component.
86
+
82
87
### 3. Initialize the textarea
83
88
84
89
Create a component with whatever name you want. Perhaps `RichEditor.vue`. Paste the following into it:
@@ -131,7 +136,7 @@ In the generated file `HelloWorld.vue`, place the following line after the "gree
131
136
<RichEditor />
132
137
```
133
138
134
-
And put its input in the `<script>` section:
139
+
And put its import in the `<script>` section:
135
140
```vue
136
141
import RichEditor from "@/components/RichEditor.vue";
137
142
```
@@ -147,4 +152,206 @@ If all went well, you should see the following in the browser:
147
152
148
153
## Nuxt
149
154
150
-
TODO
155
+
### 1. Create a Nuxt app
156
+
157
+
First, create a Nuxt project. (If you already have a Vue project then you can skip this step). On a command line, type this:
158
+
```bash
159
+
npm create nuxt@latest syntax-highlighter
160
+
```
161
+
At the time this tutorial was created, the output was the following:
So that the necessary css is loaded for code-input, and an example theme is loaded. You might want to replace the second file with your own theme, but you need the first file.
250
+
251
+
### 3. Initialize the textarea
252
+
253
+
Create a component with whatever name you want. Perhaps `app/components/RichEditor.vue`. Paste the following into it:
254
+
255
+
```vue
256
+
<template>
257
+
<div class="rich-editor">
258
+
<!-- Use ClientOnly so that no SSR is done on the code-input component -->
259
+
<ClientOnly>
260
+
<code-input
261
+
ref="elem"
262
+
:name="name"
263
+
:value="value"
264
+
spellcheck="false"
265
+
@input="emit('input', $event.target.value)"
266
+
@code-input_load="loaded"
267
+
></code-input>
268
+
</ClientOnly>
269
+
</div>
270
+
</template>
271
+
272
+
<script lang="ts" setup>
273
+
// For loading a highlighting engine - this example uses highlight.js
274
+
import hljs from 'highlight.js/lib/core';
275
+
import javascript from 'highlight.js/lib/languages/javascript';
276
+
277
+
// The following are optional.
278
+
const emit = defineEmits<{
279
+
// If you want a listener when the user changes the contents.
280
+
(e: "input", value: string): void;
281
+
// If you want to do more initialization after code-input is ready.
282
+
(e: "ready", textarea: HTMLElement): void;
283
+
}>();
284
+
285
+
const props = defineProps<{
286
+
value: string; // The starting value for the textarea
287
+
name: string; // The name that is used when the textarea is in a form
288
+
}>();
289
+
290
+
// This contains the HTMLElement of the code-input component
291
+
const elem = ref()
292
+
293
+
// Before it appears on the page, code-input needs to be initialized
294
+
onBeforeMount(async () => {
295
+
// Only if we're in the client
296
+
if (import.meta.browser) {
297
+
// Dynamically import code-input so that it is only in the browser
0 commit comments