Socket
Socket
Sign inDemoInstall

@financial-times/o-syntax-highlight

Package Overview
Dependencies
Maintainers
16
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@financial-times/o-syntax-highlight

syntax highlighter for FT products


Version published
Weekly downloads
103
decreased by-29.45%
Maintainers
16
Weekly downloads
 
Created
Source

o-syntax-highlight Circle CI MIT licensed

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

Usage

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.

o-syntax-highlight uses the following colours, on a grey-5 background (#f2f2f2). It is compliant with the contrast for WCAG AA. In order to meet the criteria for AAA at 14px+, the colours would be far too dark to distinguish syntax highlighting effectively.

ColorHexRatio14px+18px+ or 14px bold
black-crimson-25#a50f2d6.93AAAAA
black-jade-30#006b365.94AAAAA
black-lemon-55#736a0c4.94AAAAA
crimson-jade-80#a31f0f6.79AAAAA
grey-60#6666665.12AAAAA
grey-70#4d4d4d7.55AAAAAA
oxford#0f54996.82AAAAA
oxford-jade-60#097a7f4.57AAAAA
oxford-lemon-80#3f72804.77AAAAA
oxford-crimson-50#6e2a4d8.92AAAAAA

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>
		<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>
		<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>
	<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:

const oSyntaxHighlight = require('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:

const oSyntaxHighlight = require('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'

Migration

StateMajor VersionLast Minor ReleaseMigration guide
✨ active2N/Amigrate to v2
⚠ maintained11.6.4N/A

Contact

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

Licence

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

Keywords

FAQs

Package last updated on 19 Jul 2019

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