Skip to content

Commit a73dbea

Browse files
committed
Test and fix frameworks tutorials
1 parent 035796d commit a73dbea

File tree

5 files changed

+170
-59
lines changed

5 files changed

+170
-59
lines changed

docs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# `docs`
22

3-
The content of this directory is the Hugo content source of code-input-js.org. Most code in curly braces {} is relied on by the website setup.
3+
The content of this directory is the Hugo content source of [code-input-js.org](code-input-js.org). Most code in curly braces {} is relied on by the website setup.
44

5-
The homepage can be found at <_index.md>.
5+
The homepage can be found at [_index.md](_index.md).

docs/_index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ more use cases.
3838
</head>
3939
<body>
4040
<!--4. Use the code-input element-->
41-
<code-input language="JavaScript"><textarea code-input-fallback>// Demo editable code
41+
<code-input language="JavaScript"><textarea data-code-input-fallback>// Demo editable code
4242
console.log("Hello, World!");</textarea></code-input>
4343
<!--See details in the Tutorials by Example-->
4444
</body>
@@ -94,7 +94,7 @@ console.log("Hello, World!");</textarea></code-input>
9494
</head>
9595
<body>
9696
<!--The language attribute is case-insensitive but must refer to a language from https://prismjs.com/index.html#supported-languages.-->
97-
<code-input template="syntax-highlighted" language="JavaScript"><textarea code-input-fallback>// Demo editable code
97+
<code-input template="syntax-highlighted" language="JavaScript"><textarea data-code-input-fallback>// Demo editable code
9898
console.log("Hello, World!");</textarea></code-input>
9999
100100
<!--Additional usage details are here: https://code-input-js.org/#pages-->
@@ -151,7 +151,7 @@ console.log("Hello, World!");</textarea></code-input>
151151
</head>
152152
<body>
153153
<!--The language attribute is case-insensitive but must refer to a language imported above.-->
154-
<code-input template="syntax-highlighted" language="JavaScript"><textarea code-input-fallback>// Demo editable code
154+
<code-input template="syntax-highlighted" language="JavaScript"><textarea data-code-input-fallback>// Demo editable code
155155
console.log("Hello, World!");</textarea></code-input>
156156
157157
<!--Additional usage details are here: https://code-input-js.org/#pages-->
@@ -212,7 +212,7 @@ console.log("Hello, World!");</textarea></code-input>
212212
</script>
213213
</head>
214214
<body>
215-
<code-input template="syntax-highlighted"><textarea code-input-fallback>What will you create?
215+
<code-input template="syntax-highlighted"><textarea data-code-input-fallback>What will you create?
216216
Code or something else?</textarea></code-input>
217217
218218
<!--Additional usage details are here: https://code-input-js.org/#pages-->

docs/frameworks/hljs/nuxt/_index.md

Lines changed: 68 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@ title = 'How to use code-input and highlight.js with Nuxt'
44

55
# How to use code-input and highlight.js with Nuxt
66

7-
> Contributors: 2025 Paul Rosen
7+
> Contributors: 2025 Paul Rosen, 2025 Oliver Geer; **code samples licensed [MIT/Expat](https://spdx.org/licenses/MIT)**
88
9-
<a href="../vue">Vue</a> 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.
9+
[Vue](../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.
1010

1111
## 1. Create a Nuxt app
1212

13-
First, create a Nuxt project. (If you already have a Nuxt project then you can skip this step). On a command line, type this:
13+
First, create a Nuxt project. (If you already have a Nuxt project then you can skip this step). On a command line, use your package manager to do so, for example by typing one of these:
1414
```bash
15+
yarn create nuxt syntax-highlighter
16+
# OR
1517
npm create nuxt@latest syntax-highlighter
1618
```
19+
1720
At the time this tutorial was created, the output was the following:
1821
```plaintext
1922
Need to install the following packages:
@@ -72,22 +75,33 @@ none
7275
And just like the above instructions mention, do the following:
7376
```bash
7477
cd syntax-highlighter
78+
79+
yarn run dev
80+
# OR
7581
npm run dev
82+
# or your package manager's equivalent command
7683
```
7784

78-
You should be able to open your browser to the path that it prints out and see a working Nuxt app. If so, congratulations! Hit Ctrl-C to stop it.
85+
You should be able to open your browser to the path that it prints out and see a working Nuxt app. If so, congratulations! Hit Ctrl-C in the command line to stop it.
7986

8087
## 2. Add dependencies
8188

8289
> This tutorial will use `highlight.js` for the syntax highlighting. If you are using a different method then adjust as needed.
8390
84-
Type this:
91+
Type these 2 commands:
8592
```bash
93+
yarn add @webcoder49/code-input
94+
# OR
8695
npm install @webcoder49/code-input
96+
# or your package manager's equivalent command
97+
98+
yarn add highlight.js
99+
# OR
87100
npm install highlight.js
101+
# or your package manager's equivalent command
88102
```
89103

90-
In the file `vite.config.ts`, after the line `compatibilityDate`, add this so that Vue knows that `code-input` is not a Vue component:
104+
In the file `nuxt.config.ts`, after the line `compatibilityDate`, add this so that Vue knows that `code-input` is not a Vue component:
91105

92106
```javascript
93107
vue: {
@@ -108,41 +122,53 @@ So that the necessary css is loaded for code-input, and an example theme is load
108122
109123
## 3. Initialize the textarea
110124

111-
Create a component with whatever name you want. Perhaps `app/components/RichEditor.vue`. Paste the following into it:
125+
Create a component with whatever name you want. Perhaps `app/components/CodeEditor.vue`. Paste the following into it:
112126

113127
```html
114128
<template>
115-
<div class="rich-editor">
116-
<!-- Use ClientOnly so that no SSR is done on the code-input component -->
117-
<ClientOnly>
118-
<code-input
119-
ref="elem"
120-
:name="name"
121-
:value="value"
122-
spellcheck="false"
123-
@input="emit('input', $event.target.value)"
124-
@code-input_load="loaded"
125-
></code-input>
126-
</ClientOnly>
127-
</div>
129+
<!--Attributes that make sense on a
130+
textarea element are both on the code-
131+
input element for when full
132+
functionality is present, and on the
133+
fallback textarea for when it's not
134+
(e.g. bug or outdated browser).-->
135+
<code-input
136+
ref="elem"
137+
template="code-editor"
138+
:name="name"
139+
:value="value"
140+
:language="language"
141+
@input="emit('input', elem.value)"
142+
@code-input_load="loaded"
143+
>
144+
<textarea
145+
:name="name"
146+
:value="value"
147+
@input="emit('input', elem.value)"
148+
data-code-input-fallback
149+
></textarea>
150+
</code-input>
128151
</template>
129152

130153
<script lang="ts" setup>
131154
// For loading a highlighting engine - this example uses highlight.js
132155
import hljs from 'highlight.js/lib/core';
156+
// You can import and register (in onBeforeMount below) whichever languages you will use,
157+
// or if you will use many import all, following the instructions at https://highlightjs.org/#usage.
133158
import javascript from 'highlight.js/lib/languages/javascript';
134159
135160
// The following are optional.
136161
const emit = defineEmits<{
137162
// If you want a listener when the user changes the contents.
138163
(e: "input", value: string): void;
139164
// If you want to do more initialization after code-input is ready.
140-
(e: "ready", textarea: HTMLElement): void;
165+
(e: "ready", element: HTMLElement): void;
141166
}>();
142167
143168
const props = defineProps<{
144-
value: string; // The starting value for the textarea
145-
name: string; // The name that is used when the textarea is in a form
169+
value: string; // The starting value for the code-input element
170+
name: string; // The name that is used when the code-input element is in a form
171+
language: string; // The programming language name given to highlight.js, which must also be imported above and registered below with highlight.js.
146172
}>();
147173
148174
// This contains the HTMLElement of the code-input component
@@ -152,22 +178,24 @@ const elem = ref()
152178
// This must be onBeforeMount and not onMount!
153179
onBeforeMount(async () => {
154180
// Only if we're in the client
181+
// so that no server-side rendering is done on the code-input component
155182
if (import.meta.browser) {
156183
// Dynamically import code-input so that it is only in the browser
157184
const codeInput = await import("@webcoder49/code-input");
158185
const Template = (await import("@webcoder49/code-input/templates/hljs.mjs")).default;
159186
// Set up highlight.js
160187
hljs.registerLanguage('javascript', javascript);
161188
// Register that engine with code-input
162-
codeInput.registerTemplate("syntax-highlighted", new Template(hljs, []));
189+
// You can also add plugins as shown below, and at https://code-input-js.org/plugins
190+
const Indent = (await import("@webcoder49/code-input/plugins/indent.mjs")).default;
191+
codeInput.registerTemplate("code-editor", new Template(hljs, [new Indent()]));
163192
}
164193
})
165194
166195
function loaded() {
167-
// This is called after the code-input is initialized and it has created a textarea.
168-
// If you have some further initialization for the textarea, then do it in this event.
169-
const ta = elem.value.querySelector('textarea')
170-
emit("ready", ta)
196+
// This is called after the code-input is initialized.
197+
// If you have some further initialization for the code-input element, then do it in this event.
198+
emit("ready", elem)
171199
}
172200
</script>
173201
@@ -178,16 +206,20 @@ function loaded() {
178206
code-input {
179207
resize: both; /* if you want the resizing control that textarea has */
180208
margin: 0; /* you can override other styles */
181-
font-family: "Fira Mono", Monaco, monospace;
209+
font-family: monospace;
210+
211+
background: #f1f1f1; /* As explained below, this is required to set the colour of the code-input element if it
212+
falls back to having no highlighting, and while it loads. */
182213
}
183214
</style>
184215
185216
<style>
186217
/* Notice that these styles aren't scoped */
187218
.hljs {
188-
background: #f1f1f1; /* here's how to change the background color. */
219+
background: #f1f1f1; /* This is a style specific to highlighted code, so needs to use highlight.js' selector.
220+
A side effect of this is that it will not affect unregistered/unloaded code-input elements. */
189221
}
190-
/* If you want to change the selection color */
222+
/* If you want to change the color of selected text during editing */
191223
code-input textarea::selection {
192224
background: #6781ef;
193225
color: #ffffff;
@@ -198,17 +230,17 @@ code-input textarea::selection {
198230
199231
In the generated file `app.vue`, place the following line after the "NuxtRouteAnnouncer" line:
200232
```html
201-
<RichEditor value="function hello() { console.log('world'); }" name="myEditor" />
233+
<CodeEditor value="function hello() { console.log('world'); }" name="myEditor" />
202234
```
203235
204-
And put its import in the `<script>` section:
205-
```html
206-
import RichEditor from "./components/RichEditor.vue";
207-
```
236+
Nuxt will have already imported it by default, so you don't need to import the component in JavaScript.
208237
209238
Restart the server:
210239
```bash
240+
yarn run dev
241+
# OR
211242
npm run dev
243+
# or your package manager's equivalent command
212244
```
213245
214246
If all went well, you should see the following in the browser:

0 commit comments

Comments
 (0)