metalsmith-highlightjs
v0.0.1
Published
A Metalsmith plugin to syntax highlight code blocks.
Maintainers
Readme
metalsmith-highlightjs
A Metalsmith plugin to highlight code blocks with highlight.js.
This plugin replaces every fenced code block in Markdown files with highlighted HTML, optionally with line numbers. It should run before Markdown is rendered to HTML, e.g. before @metalsmith/markdown.
Installation
npm install --save metalsmith-highlightjsJavaScript Usage
import Metalsmith from 'metalsmith';
import highlightjs from 'metalsmith-highlightjs';
import markdown from '@metalsmith/markdown';
Metalsmith(__dirname)
.use(highlightjs({
// options here
}))
.use(markdown())
.build((err) => {
if (err) {
throw err;
}
});Options
markdown (optional)
Type: string | string[] Default: "**/*.md"
A micromatch glob pattern, or array of patterns, to find Markdown files. Negated patterns can be used to exclude files.
highlightjs (optional)
Type: object Default: {}
highlight.js configuration options, passed to hljs.configure(). For example, languages limits which languages auto-detection will choose from:
highlightjs({
highlightjs: {
languages: ['javascript', 'typescript', 'bash'],
},
})aliases (optional)
Type: Record<string, string | string[]> Default: {}
Extra fence languages to register as aliases of a highlight.js language, using hljs.registerAliases(). The keys are highlight.js language names, and the values are one or more aliases:
highlightjs({
aliases: {
bash: ['myshell'],
},
})The build fails if a key isn't a registered highlight.js language.
lineNumbers (optional)
Type: boolean | { includeLanguages?: string[], excludeLanguages?: string[] } Default: false
Whether to add line numbers to code blocks:
false: no code block gets line numberstrue: every code block gets line numbers- an object: a code block gets line numbers if its language is in
includeLanguages(orincludeLanguagesis unset), and it isn't inexcludeLanguages
Languages are matched case-insensitively, either as written on the fence or by highlight.js's name for them, so bash also matches sh code blocks. A code block with no language never matches includeLanguages, so it only gets line numbers when includeLanguages is unset.
For example, to number every code block except for terminal sessions:
highlightjs({
lineNumbers: {
excludeLanguages: ['shell', 'console', 'dos', 'bat', 'cmd', 'powershell', 'ps', 'ps1'],
},
})lineWrap (optional)
Type: boolean Default: false
Whether to wrap long lines instead of scrolling sideways. Words that are too long for a line (e.g. URLs) are broken as well.
theme (optional)
Type: { name: string, filename: string } Default: none
A highlight.js theme CSS to add to the build:
name: the theme's name (e.g.github,github-dark, orbase16/dracula)filename: where to put the theme's minified CSS in the build
highlightjs({
theme: {
name: 'github',
filename: 'css/highlight.css',
},
})The build fails if the theme doesn't exist, or if a file already exists at filename. HTML pages will need to link the CSS file, e.g. <link rel="stylesheet" href="/css/highlight.css">.
