New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

highlight-js-turbolinks

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

highlight-js-turbolinks

Turbolinks compatible syntax highlighting with language autodetection.

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

This is a fork of Highlight.js, which is compatible with Turbolinks 5.

To make highlight.js compatible with Turbolinks 5, two event listeners have been added to initHighlightingOnLoad, the 'turbolinks:load' and 'turbolinks:render' listeners.

The original event listeners used by initHighlightingOnLoad have been left in place. They are 'DOMContentLoaded' and 'load'. This allows highlight-js-turbolinks to be compatible with applications that use turbolinks, and also compatable with applications that do not. This is done so that if you ever choose to disable or remove turbolinks from your application, you may continue to use highlight-js-turbolinks.

Installing with Yarn and Rails

For Rails 5

Coming soon!

For Rails 5.1 and up

From your Rails root, run

yarn add highlight-js-turbolinks

Then add the following line to app/assets/javascripts/application.js

//= require highlight-js-turbolinks/lib/highlight.js

Then add the following line to app/assets/stylesheets/application.scss

@import 'highlight-js-turbolinks/styles/default.css'

Getting Started

The bare minimum for using highlight.js on a web page is linking to the library along with one of the styles and calling initHighlightingOnLoad:

<link rel="stylesheet" href="/path/to/styles/default.css">
<script src="/path/to/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>

This will find and highlight code inside of <pre><code> tags; it tries to detect the language automatically. If automatic detection doesn’t work for you, you can specify the language in the class attribute:

<pre><code class="html">...</code></pre>

The list of supported language classes is available in the class reference. Classes can also be prefixed with either language- or lang-.

To disable highlighting altogether use the nohighlight class:

<pre><code class="nohighlight">...</code></pre>

Custom Initialization

When you need a bit more control over the initialization of highlight.js, you can use the highlightBlock and configure functions. This allows you to control what to highlight and when.

Here’s an equivalent way to calling initHighlightingOnLoad using jQuery:

$(document).ready(function() {
  $('pre code').each(function(i, block) {
    hljs.highlightBlock(block);
  });
});

You can use any tags instead of <pre><code> to mark up your code. If you don't use a container that preserve line breaks you will need to configure highlight.js to use the <br> tag:

hljs.configure({useBR: true});

$('div.code').each(function(i, block) {
  hljs.highlightBlock(block);
});

For other options refer to the documentation for configure.

Web Workers

You can run highlighting inside a web worker to avoid freezing the browser window while dealing with very big chunks of code.

In your main script:

addEventListener('load', function() {
  var code = document.querySelector('#code');
  var worker = new Worker('worker.js');
  worker.onmessage = function(event) { code.innerHTML = event.data; }
  worker.postMessage(code.textContent);
})

In worker.js:

onmessage = function(event) {
  importScripts('<path>/highlight.pack.js');
  var result = self.hljs.highlightAuto(event.data);
  postMessage(result.value);
}

License

Highlight.js is released under the BSD License. See LICENSE file for details.

The official site for the library is at https://highlightjs.org/.

Further in-depth documentation for the API and other topics is at http://highlightjs.readthedocs.io/.

Authors and contributors are listed in the AUTHORS.en.txt file.

Keywords

FAQs

Package last updated on 23 Jul 2017

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