Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

compose-code-highlighter

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compose-code-highlighter

Highlight static code snippets with CodeMirror

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

Compose Code Highlighter

Easily highlight static code snippets using the wonderful CodeMirror. This wraps the CodeMirror run mode feature and adds language aliases, and DOM interaction.

Build Status

Setup:

When requiring this module, be sure to also require the CodeMirror language modes for the languages you'll be working with. For example if you are going to use Ruby, C, and CSS, you might add this:

require('codemirror')
require('codemirror/mode/ruby/ruby')
require('codemirror/mode/clike/clike')
require('codemirror/mode/css/css')

See CodeMirror's mode documentation for a complete list of supported languages.

Usage

Code snippets can be any element (not just a <pre> block) but should either have a data-lang attribute to set the language or should have a classname matching matching lang-[language].

For example:

<pre data-lang='css'>
body {
  background: #c0ffee;
}
</pre>

<div class='lang-ruby'>
puts 'hello world'
</div>

Then to highlight all code snippets on the page:

Highlighter.highlight()

Alias languages

If your language doesn't seem to be highlighting properly, you can specify the mimetype that CodeMirror is using to identify your language. For Scss, you'd use data-lang="text/x-scss". That's kind of verbose so this library adds some aliases so you can go on using data-lang="scss" and it is converted before invoking CodeMirror.

aliases: {
  'bash'  : 'text/x-sh',
  'c'     : 'text/x-csrc',
  'html'  : 'text/html',
  'js'    : 'text/javascript',
  'json'  : 'application/json',
  'java'  : 'text/x-java',
  'markup': 'text/html',
  'sass'  : 'text/x-sass',
  'scss'  : 'text/x-scss',
  'sh'    : 'text/x-sh'
},

If you want to add aliases you can do so like this:

var Highlighter = require('compose-code-highlighter')
Highlighter.addAlias({
  'less': 'text/x-less'
})

I'm happy to accept pullrequests for adding aliases to the default list.

Keywords

FAQs

Package last updated on 02 Aug 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc