markdown-it-highlightjs
Advanced tools
Comparing version 3.1.0 to 3.2.0
@@ -7,2 +7,10 @@ # [Changelog](http://keepachangelog.com/) | ||
## [3.2.0] - 2020-06-01 | ||
* Support inline code highlighting. ([#10]) | ||
## [3.1.0] - 2020-03-05 | ||
* Update dependencies. ([#6]) | ||
* Fix "unknown language" error. ([#8]) | ||
* Allow to register extra languages. ([#9]) | ||
## [3.0.0] - 2017-02-26 | ||
@@ -34,3 +42,5 @@ * Update highlight.js. ([#2]) | ||
[unreleased]: https://github.com/valeriangalliat/markdown-it-highlightjs/compare/v3.0.0...HEAD | ||
[unreleased]: https://github.com/valeriangalliat/markdown-it-highlightjs/compare/v3.2.0...HEAD | ||
[3.2.0]: https://github.com/valeriangalliat/markdown-it-highlightjs/compare/v3.1.0...v3.2.0 | ||
[3.1.0]: https://github.com/valeriangalliat/markdown-it-highlightjs/compare/v3.0.0...v3.1.0 | ||
[3.0.0]: https://github.com/valeriangalliat/markdown-it-highlightjs/compare/v2.0.0...v3.0.0 | ||
@@ -43,1 +53,5 @@ [2.0.0]: https://github.com/valeriangalliat/markdown-it-highlightjs/compare/v1.1.2...v2.0.0 | ||
[#2]: https://github.com/valeriangalliat/markdown-it-highlightjs/pull/2 | ||
[#6]: https://github.com/valeriangalliat/markdown-it-highlightjs/pull/6 | ||
[#8]: https://github.com/valeriangalliat/markdown-it-highlightjs/pull/8 | ||
[#9]: https://github.com/valeriangalliat/markdown-it-highlightjs/pull/9 | ||
[#10]: https://github.com/valeriangalliat/markdown-it-highlightjs/pull/10 |
33
index.js
const hljs = require('highlight.js') | ||
const maybe = f => { | ||
function maybe (f) { | ||
try { | ||
@@ -33,2 +33,26 @@ return f() | ||
function inlineCodeRenderer (tokens, idx, options) { | ||
const code = tokens[idx] | ||
const next = tokens[idx + 1] | ||
let lang | ||
if (next && next.type === 'text') { | ||
// Match kramdown- or pandoc-style language specifier. | ||
// e.g. `code`{:.ruby} or `code`{.haskell} | ||
const match = /^{:?\.([^}]+)}/.exec(next.content) | ||
if (match) { | ||
lang = match[1] | ||
// Remove the language specification from text following the code. | ||
next.content = next.content.slice(match[0].length) | ||
} | ||
} | ||
const highlighted = options.highlight(code.content, lang) | ||
const cls = lang ? ` class="language-${lang}"` : '' | ||
return `<code${cls}>${highlighted}</code>` | ||
} | ||
const highlightjs = (md, opts) => { | ||
@@ -44,2 +68,6 @@ opts = Object.assign({}, highlightjs.defaults, opts) | ||
} | ||
if (opts.inline) { | ||
md.renderer.rules.code_inline = inlineCodeRenderer | ||
} | ||
} | ||
@@ -49,5 +77,6 @@ | ||
auto: true, | ||
code: true | ||
code: true, | ||
inline: false | ||
} | ||
module.exports = highlightjs |
{ | ||
"name": "markdown-it-highlightjs", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Preset to use highlight.js with markdown-it.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -25,2 +25,3 @@ # markdown-it-highlightjs [![npm version](http://img.shields.io/npm/v/markdown-it-highlightjs.svg?style=flat-square)](https://www.npmjs.org/package/markdown-it-highlightjs) | ||
`register` | object | Register other languages which are not included in the standard pack. | `null` | ||
`inline` | boolean | Whether to highlight inline code. | `false` | ||
@@ -37,1 +38,24 @@ ### Register languages | ||
``` | ||
### Inline code highlighting | ||
You can enable inline code highlighting by setting `inline` to true: | ||
```js | ||
const md = require('markdown-it')() | ||
.use(require('markdown-it-highlightjs'), { inline: true }) | ||
``` | ||
You can specify the language for inline code using [Pandoc syntax](https://pandoc.org/MANUAL.html#extension-inline_code_attributes): | ||
```markdown | ||
`x=4`{.js} | ||
``` | ||
or [Kramdown IAL syntax](https://kramdown.gettalong.org/syntax.html#inline-attribute-lists): | ||
```markdown | ||
`x=4`{:.js} | ||
``` | ||
If you do not specify a language, then highlight.js will attempt to guess the language if `auto` is true (which it is by default). |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8290
62
60