npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@financial-times/o-syntax-highlight

v4.2.5

Published

A syntax highlighter for Origami-supported documentation that wraps PrismJs

Downloads

267

Readme

o-syntax-highlight

A syntax highlighter for Origami-supported documentation that wraps PrismJs.

Usage

Check out how to include Origami components in your project to get started with o-syntax-highlight.

Overview

This component provides accessible syntax highlighting for bash, diff, Javascript, JSON, YAML/YML, HTML, CSS and Sass/SCSS. If there are any languages you would like to highlight that we don't currently support, please open an issue and we will provide it.

Markup

o-syntax-highlight works with a single class. As long as this class is present in a wrapper that holds the code you wish to highlight, it will tokenise and colorise the syntax according to your preference. The semantically correct way of declaring a code block is to place a <code> inside a <pre>. That is what o-syntax-highlight looks for when it is tokenising the code block.

<pre> is whitespace sensitive, so it is important to follow the markup to get the correct spacing for your syntax.

Every language has its own set of styles — in order to highlight html for example, you would need:

<div class="demo" data-o-component='o-syntax-highlight'>
	<pre tabindex="0">
		<code class="o-syntax-highlight--json">
<!-- everything in this element will be highlighted, including this comment! -->
"object": {
	"string": "string",
	"array": [
		"name",
		"names"
	],
	"object" : {
		"nested": "html"
	}
	"numbers": 1
}</code>
		</pre>
	</div>

The same is true for all other available languages:

  • Javascript: o-syntax-highlight--js or o-syntax-highlight--javascript
  • HTML: o-syntax-highlight--html
  • CSS: o-syntax-highlight--css
  • SCSS/SASS: o-syntax-highlight--scss or o-syntax-highlight--sass
  • JSON: o-syntax-highlight--json
  • YAML: o-syntax-highlight--yaml
  • DIFF: o-syntax-highlight--diff

It is worth pointing out that the wrapper can hold any html elements. So long as all of the code blocks within the wrapper are written as described above, o-syntax-highlight will ignore everything else.

<div class="demo" data-o-component='o-syntax-highlight'>
	<div class="example">
		<p></p>
		<p></p>
	</div>

	<pre tabindex="0">
		<code class="o-syntax-highlight--json">
<!-- everything in this element will be highlighted, including this comment! --></code>
	</pre>

	<button type="button" name="button">Button</button>
</div>

However, when highlighting HTML, there is a caveat. Because of the way in which the <code> tag works, if you want to highlight markup which has already been declared, you will have to replace and < with &lt; in order for the markup to be read as a string instead of actual HTML.

For example:

<pre tabindex="0">
	<code class="o-syntax-highlight--html">
&lt;html>
	&lt;head>
		&lt;!-- links and scripts -->
	&lt;/head>
	&lt;body>
		&lt;div class="some-class"&lt;/div>
	&lt;/body>
&lt;/html></code>
</pre>

JavaScript

No code will run automatically unless you are using the Build Service. You must either construct an o-syntax-highlight object or fire the o.DOMContentLoaded event, which oComponent listens for.

Construction

In order to use o-syntax-highlight with already declared markup, you can use:

import oSyntaxHighlight from '@financial-times/o-syntax-highlight';
oSyntaxHighlight.init();

If you are initialising the component imperatively, you will need to supply a string of code that you want to highlight, and an options object that defines the language of that code. To tokenise the string you will need be explicit about the element that will hold the highlighted syntax:

import oSyntaxHighlight from '@financial-times/o-syntax-highlight';
const highlighter = new oSyntaxHighlight('code to highlight', { language: 'html'});

myElement.innerHTML = highlighter.tokenise();

Sass

You can include highlighting for all languages by calling:

@include oSyntaxHighlight();

You can also be more specific about which languages you would like styling output for by using an $opts map:

	@include oSyntaxHighlight($opts: (
		'languages': (
			'html',
			'json'
		)
	));

o-syntax-highlight accepts the following options:

  • 'bash'
  • 'css'
  • 'diff'
  • 'html'
  • 'js' or 'javascript'
  • 'json'
  • 'scss' or 'sass'
  • 'yaml'

Troubleshooting

How do I use Marked to convert Markdown files to HTML with o-syntax-highlight code blocks?

When using Marked it is possible to override its default renderer so you can create custom output for code blocks. To get o-syntax-highlight compatible output see the example in this Github issue.

Migration guide

State | Major Version | Last Minor Release | Migration guide | :---: | :---: | :---: | :---: ✨ active | 3 | N/A | migrate to v4 | ⚠ maintained | 3 | 3.0 | migrate to v3 | ╳ deprecated | 2 | 2.1 | migrate to v2 | ╳ deprecated | 1 | 1.6.4 | N/A |

Contact

If you have any questions or comments about this component, or need help using it, please either raise an issue, visit #origami-support or email Origami Support.

Licence

This software is published by the Financial Times under the MIT licence.