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
content:"No-JavaScript fallback. For highlighting and plugins: as a user use a newer browser/enable JavaScript support; as a developer use codeInput.registerTemplate.";
141
+
content:"No highlighting. JavaScript support is disabled or insufficient, or codeInput.registerTemplate has not been called.";
Copy file name to clipboardExpand all lines: code-input.d.ts
+21-6Lines changed: 21 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -46,6 +46,13 @@ export abstract class Plugin {
46
46
* modifications to the `codeInput.Plugin.attributeChanged` method.
47
47
*/
48
48
observedAttributes: Array<string>
49
+
50
+
/**
51
+
* Replace the values in destination with those from source where the keys match, in-place.
52
+
* @param {Object} destination Where to place the translated strings, already filled with the keys pointing to English strings.
53
+
* @param {Object} source The same keys, or some of them, mapped to translated strings. Keys not present here will retain the values they are maapped to in destination.
// ESM-SUPPORT-START-NAMESPACE-1 Do not (re)move this - it's needed for ESM generation
@@ -128,8 +135,8 @@ export namespace plugins {
128
135
classFindAndReplaceextendsPlugin{
129
136
/**
130
137
* Create a find-and-replace command plugin to pass into a template
131
-
* @param {boolean} useCtrlF Should Ctrl+F be overriden for find-and-replace find functionality? If not, you can trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, false)`.
132
-
* @param {boolean} useCtrlH Should Ctrl+H be overriden for find-and-replace replace functionality? If not, you can trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, true)`.
138
+
* @param {boolean} useCtrlF Should Ctrl+F be overriden for find-and-replace find functionality? Either way, you can also trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, false)`.
139
+
* @param {boolean} useCtrlH Should Ctrl+H be overriden for find-and-replace replace functionality? Either way, you can also trigger it yourself using (instance of this plugin)`.showPrompt(code-input element, true)`.
133
140
* @param {Object} instructionTranslations: user interface string keys mapped to translated versions for localisation. Look at the find-and-replace.js source code for the English text.
* Create a go-to-line command plugin to pass into a template
174
-
* @param {boolean} useCtrlG Should Ctrl+G be overriden for go-to-line functionality? If not, you can trigger it yourself using (instance of this plugin)`.showPrompt(code-input element)`.
181
+
* @param {boolean} useCtrlG Should Ctrl+G be overriden for go-to-line functionality? Either way, you can trigger it yourself using (instance of this plugin)`.showPrompt(code-input element)`.
175
182
* @param {Object} instructionTranslations: user interface string keys mapped to translated versions for localisation. Look at the go-to-line.js source code for the English text.
176
183
*/
177
184
constructor(useCtrlG?: boolean,
@@ -269,14 +276,22 @@ export namespace plugins {
269
276
/**
270
277
* Render special characters and control characters as a symbol with their hex code.
271
278
* Files: special-chars.js, special-chars.css
279
+
*
280
+
* WARNING:
281
+
*
282
+
* This plugin is currently unstable when used with other plugins,
283
+
* Unicode characters, or highlight.js. I hope to fix much of this by
284
+
* major version 3, and if you could help that would be amazing!
285
+
*
286
+
* See https://github.com/WebCoder49/code-input/issues?q=is%3Aissue%20state%3Aopen%20specialchars
272
287
*/
273
288
classSpecialCharsextendsPlugin{
274
289
/**
275
290
* Create a special characters plugin instance.
276
291
* Default = covers many non-renderable ASCII characters.
277
-
* @param {Boolean} colorInSpecialChars Whether or not to give special characters custom background colors based on their hex code
278
-
* @param {Boolean} inheritTextColor If `inheritTextColor` is false, forces the color of the hex code to inherit from syntax highlighting. Otherwise, the base color of the `pre code` element is used to give contrast to the small characters.
279
-
* @param {RegExp} specialCharRegExp The regular expression which matches special characters
292
+
* @param {Boolean} colorInSpecialChars Whether or not to give special characters custom background colors based on their hex code. Defaults to false.
293
+
* @param {Boolean} inheritTextColor If true, forces the color of the hex code to inherit from syntax highlighting. If false, the base color of the `pre code` element is used to give contrast to the small characters. Defaults to false.
294
+
* @param {RegExp} specialCharRegExp The regular expression which matches special characters. Defaults to many non-renderable ASCII characters (which characters are renderable depends on the browser and OS).
Copy file name to clipboardExpand all lines: code-input.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -367,9 +367,9 @@ var codeInput = {
367
367
}
368
368
369
369
/**
370
-
* Replace the keys in destination with any source
370
+
* Replace the values in destination with those from source where the keys match, in-place.
371
371
* @param {Object} destination Where to place the translated strings, already filled with the keys pointing to English strings.
372
-
* @param {Object} source The same keys, or some of them, mapped to translated strings.
372
+
* @param {Object} source The same keys, or some of them, mapped to translated strings. Keys not present here will retain the values they are maapped to in destination.
#### Editor with Custom Highlighting Algorithm (use **with vanilla HTML here** or [with ECMAScript Modules/Vue/Nuxt](frameworks/custom)) {#playground-preset-custom}
163
+
#### Editor with Custom Highlighting Algorithm (use **with vanilla HTML here** or [with ECMAScript Modules/Vue/Nuxt](modules-and-frameworks/custom)) {#playground-preset-custom}
164
164
165
165
```
166
166
<!DOCTYPE html>
@@ -279,7 +279,7 @@ Widely Usable and Progressively Enhanced
279
279
</dt>
280
280
<dd>
281
281
282
-
Works on any modern browser independent of whether a framework is used, with the standardised web component API. Integrates well into modular setups and web frameworks with TypeScript definitions, an ECMAScript Module build and tutorials. Falls back to a `textarea` element when there is insufficient JavaScript support. The fallback even works on Lynx.
282
+
Works on any modern browser independent of whether a framework is used, with the standardised web component API. Integrates well into modular setups and web frameworks with TypeScript definitions, an ECMAScript Module build and [tutorials](modules-and-frameworks). Falls back to a `textarea` element when there is insufficient JavaScript support. The fallback even works on Lynx.
283
283
284
284
</dd>
285
285
</dl>
@@ -297,4 +297,4 @@ something like [CodeMirror](https://codemirror.net/),
**If you don't need web framework integration, get started with the commented tutorials by example on this page, for [Prism.js](#playground-preset-prism), [highlight.js](#playground-preset-hljs), or [another highlighter](#playground-preset-custom). If you're using a web framework, start [here](frameworks).**
300
+
**If you don't need web framework integration, get started with the commented tutorials by example on this page, for [Prism.js](#playground-preset-prism), [highlight.js](#playground-preset-hljs), or [another highlighter](#playground-preset-custom). If you're using ECMAScript modules or a web framework, start [here](modules-and-frameworks).**
code-input.js defaults to using English text, but when want the interface in a different language you can provide text in a different language.
12
+
13
+
**The core of code-input.js** contains one message shown only when CSS is supported but JavaScript has not been used to register the element. Translate it by writing the following CSS rule, replacing the text with its localised version.
14
+
```css
15
+
code-input:not(.code-input_registered)::after {
16
+
content: "No highlighting. JavaScript support is disabled or insufficient, or codeInput.registerTemplate has not been called."!important;
17
+
}
18
+
```
19
+
It is only present for debugging and explanatory purposes when highlighting cannot be seen, and should not contain important or specific information about the editor state; if you need such information, especially for screen reader users, add separate text to your application which disappears after registering the code-input without errors.
20
+
21
+
**Plugins** sometimes come with user interface features (e.g. the find-and-replace dialog) which contain text to be translated. The text is provided as an extra argument to the plugin constructor containing translated strings or functions to produce them for each translation key, with the keys and their English values found in either the `code-input.d.ts` or the plugin's source code file. Here's an example:
22
+
```javascript
23
+
// CC-BY; Attribution: Translated by Oliver Geer with some help from English Wiktionary
24
+
let findAndReplaceTranslations = {
25
+
start:"Buscar términos en su código.",
26
+
none:"No hay sucesos",
27
+
oneFound:"1 suceso encontrado.",
28
+
matchIndex: (index, count) =>`${index} de ${count} sucesos.`,
29
+
error: (message) =>`Error: ${message}`,
30
+
infiniteLoopError:"Causa un ciclo infinito",
31
+
closeDialog:"Cerrar el Diálogo y Regresar al Editor",
32
+
findPlaceholder:"Buscar",
33
+
findCaseSensitive:"Prestar atención a las minúsculas/mayúsculas",
34
+
findRegExp:"Utilizar expresión regular de JavaScript",
Copy file name to clipboardExpand all lines: docs/modules-and-frameworks/custom/_index.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,4 +6,4 @@ title = 'Using code-input.js with custom highlighting algorihms in projects whic
6
6
7
7
> Contributors: 2025 Oliver Geer
8
8
9
-
We plan to get documentation for this soon (contributions via a pull request are welcome!), but for now please use [the corresponding article for highlight.js](../hljs) and [the demo frameworkless code for code-input and custom highlighting algorithms](../../#playground-preset-custom).
9
+
We plan to get documentation for this soon (contributions via a pull request are welcome!), but for now please use [the corresponding article for highlight.js](../hljs) and [the demo vanilla code for code-input and custom highlighting algorithms](../../#playground-preset-custom).
title = 'How to use code-input and highlight.js with ECMAScript Modules (not framework-specific)'
3
+
+++
4
+
5
+
# How to use code-input and highlight.js with ECMAScript Modules (not framework-specific)
6
+
7
+
> Contributors: 2025 Oliver Geer
8
+
9
+
These instructions will work anywhere [ECMAScript modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) are supported. If you want a tutorial specific to a web framework, please [look here](..).
10
+
11
+
This is a client-side library so will not work in Node.js or similar environments. It is assumed that you have already researched and run an example frontend ECMAScript module (ESM) project using either a bundler like [esbuild](https://esbuild.github.io/) or [webpack](https://webpack.js.org), or newer browsers' direct ESM support (less browser compatibility but easier when debugging); setting them up is not described here. You can [learn about ESM from the very basics here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules).
12
+
13
+
## Building
14
+
15
+
The ECMAScript modules in version 2 of the code-input.js library need to be "built" from the source files, because the source files are not in the ESM format.
16
+
17
+
For builds from package managers like Yarn and NPM, the ESM files have already been built and are in the `esm` directory.
18
+
19
+
When the code is downloaded or cloned from `git` source, the ESM directory contains files to do the building but not the built modules yet; follow the instructions in `esm/README.md` to run the build.
20
+
21
+
## Usage
22
+
23
+
Here's how to use code-input and highlight.js with ECMAScript modules.
24
+
25
+
```javascript
26
+
// Access the registerTemplate function and CodeInput (extends HTMLElement),
27
+
// Template (custom highlighter) and Plugin (abstract class) classes inside the
Note the `.mjs` extensions on code-input.js import paths. They are needed, and also make the next part easier:
57
+
58
+
## Import Paths
59
+
60
+
The import paths above assume a package manager and `package.json` export paths are being used.
61
+
62
+
In some setups, this will not work. You have two options (replace `node_modules/@webcoder49/code-input` with the relative path to the library, if it is different):
63
+
* use relative paths instead: replace `"@webcoder49/code-input/path/to/file.mjs"` with `"node_modules/@webcoder49/code-input/esm/path/to/file.mjs"` (note the `esm` directory). Also replace `"@webcoder49/code-input/path/to/file.mjs"` with `"node_modules/@highlightjs/cdn-assets/es/path/to/file.mjs"` (note the `es` directory) *If you're not using an import map yet, I recommend this option because import maps are not supported on as many browsers.*
64
+
* If you're using an [import map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#importing_modules_using_import_maps), add the following to it:
0 commit comments