Skip to content

thiagooak/code-highlighter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm jsDelivr license

A lightweight <code-highlighter> custom element that provides syntax highlighting without DOM mutation, using the CSS Custom Highlight API

Features

  • No DOM mutation for highlighting (no <span> injection)
  • Uses Prism.js's tokenizer, but lets you decide how to import it
  • Works as a read-only viewer or simple editor
  • Minimal, only 50 lines of JavaScript

Limitations

Usage

Full Example Page

Read-only

<code-highlighter
  language="clojure"
  spellcheck="false"
>
(defn greet [name] (str "Hello, " name))
</code-highlighter>

Simple Editor

<code-highlighter
  language="clojure"
  contenteditable="plaintext-only"
  spellcheck="false"
  oninput="this.highlight()"
>
(defn greet [name] (str "Hello, " name))
</code-highlighter>

Note: Consider debouncing calls to highlight() to avoid re-tokenizing on every keystroke

API

highlight()

Recompute syntax highlighting

<code-highlighter oninput="this.highlight()"></code-highlighter>

Installation

  1. Add Prism core and any languages you need
  <script src="https://cdn.jsdelivr.net/npm/prismjs@1.30.0/components/prism-core.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/prismjs@1.30.0/components/prism-clojure.min.js"></script>
  1. Add <code-highlighter>
<script type="module"
        src="https://cdn.jsdelivr.net/npm/@thiago.oak/code-highlighter@latest/code-highlighter.js"></script>
  1. Add a theme
<link rel="stylesheet"
      href="https://cdn.jsdelivr.net/npm/@thiago.oak/code-highlighter@latest/prettylights.css"/>

Styling

Highlight colors are defined with ::highlight() selectors. Your theme sets them

Credits

About

syntax highlighter

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors