1. highlight-codemirror
A syntax highlighter built to run in node and consume CodeMirror modes.
highlight-codemirror
Package: highlight-codemirror
Created by: ForbesLindesay
Last modified: Sat, 18 Jun 2022 20:43:19 GMT
Version: 4.6.1
License: MIT
Downloads: 6,318
Repository: https://github.com/ForbesLindesay/browserify-middleware

Install

npm install highlight-codemirror
yarn add highlight-codemirror

highlight-codemirror

A syntax highlighter built to run in node and consume CodeMirror modes.

Build Status
Dependency Status
NPM version

API

highlight(source, mode)

The source should be a string of code to highlight. The mode should be either the name of a mode (as a string) or an object (allowing you to specify other options).

 var highlight = require('highlight-codemirror');

var html = highlight('assert(typeof "foo" === "string")', 'javascript');
// => '<span class="cm-variable">assert</span>(<span class="cm-keyword">typeof</span> <span class="cm-string">&quot;foo&quot;</span> <span class="cm-operator">===</span> <span class="cm-string">&quot;string&quot;</span>)'

highlight.loadMode(name);

Loading modes is synchronous, so you may wish to pre-populate the cache bu loading the mode up front. You can also load custom modes by passing an absolute path to a JavaScript file. e.g. the null mode might look like:

/custom-null-mode.js

 var CodeMirror = require('codemirror');

// Minimal default mode.
CodeMirror.defineMode("custom-null", function() {
  return {token: function(stream) {stream.skipToEnd();}};
});

You could then do:

 highlight.loadMode('/custom-null-mode.js');
assert(highlight('This is not really a programming language', 'custom-null') === 'This is not really a programming language');

License

MIT

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code