markdown-it-highlightjs
Advanced tools
Comparing version 3.6.0 to 4.0.0
73
core.js
@@ -1,72 +0,1 @@ | ||
function maybe (f) { | ||
try { | ||
return f() | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
// Allow registration of other languages. | ||
const registerLangs = (hljs, register) => register && | ||
Object.entries(register).map(([lang, pack]) => { hljs.registerLanguage(lang, pack) }) | ||
// Highlight with given language. | ||
const highlight = (hljs, code, lang) => | ||
maybe(() => hljs.highlight(code, { language: lang || 'plaintext', ignoreIllegals: true }).value) || '' | ||
// Highlight with given language or automatically. | ||
const highlightAuto = (hljs, code, lang) => | ||
lang | ||
? highlight(hljs, code, lang) | ||
: maybe(() => hljs.highlightAuto(code).value) || '' | ||
// Wrap a render function to add `hljs` class to code blocks. | ||
const wrap = render => | ||
function (...args) { | ||
return render.apply(this, args) | ||
.replace('<code class="', '<code class="hljs ') | ||
.replace('<code>', '<code class="hljs">') | ||
} | ||
function inlineCodeRenderer (md, 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="${options.langPrefix}${md.utils.escapeHtml(lang)}"` : '' | ||
return `<code${cls}>${highlighted}</code>` | ||
} | ||
module.exports = (md, opts) => { | ||
if (!opts || !opts.hljs) { | ||
throw new Error('Please pass a highlight.js instance for the required `hljs` option.') | ||
} | ||
registerLangs(opts.hljs, opts.register) | ||
md.options.highlight = (opts.auto ? highlightAuto : highlight).bind(null, opts.hljs) | ||
md.renderer.rules.fence = wrap(md.renderer.rules.fence) | ||
if (opts.code) { | ||
md.renderer.rules.code_block = wrap(md.renderer.rules.code_block) | ||
} | ||
if (opts.inline) { | ||
md.renderer.rules.code_inline = inlineCodeRenderer.bind(null, md) | ||
} | ||
} | ||
module.exports = require('./dist/core').default |
18
index.js
@@ -1,17 +0,1 @@ | ||
const highlightjs = (md, opts) => { | ||
opts = Object.assign({}, highlightjs.defaults, opts) | ||
if (!opts.hljs) { | ||
opts.hljs = require('highlight.js') | ||
} | ||
return require('./core')(md, opts) | ||
} | ||
highlightjs.defaults = { | ||
auto: true, | ||
code: true, | ||
inline: false | ||
} | ||
module.exports = highlightjs | ||
module.exports = require('./dist').default |
{ | ||
"name": "markdown-it-highlightjs", | ||
"version": "3.6.0", | ||
"version": "4.0.0", | ||
"description": "Preset to use highlight.js with markdown-it.", | ||
@@ -8,31 +8,30 @@ "keywords": [ | ||
], | ||
"homepage": "https://github.com/valeriangalliat/markdown-it-highlightjs", | ||
"license": "Unlicense", | ||
"author": { | ||
"name": "Valérian Galliat", | ||
"url": "http://val.codejam.info/" | ||
}, | ||
"files": [ | ||
"README.md", | ||
"UNLICENSE", | ||
"core.js", | ||
"index.js" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/valeriangalliat/markdown-it-highlightjs.git" | ||
}, | ||
"author": "Val (https://val.codejam.info)", | ||
"types": "types/index.d.ts", | ||
"repository": "valeriangalliat/markdown-it-highlightjs", | ||
"scripts": { | ||
"lint": "standard", | ||
"prepublish": "npm test", | ||
"test": "npm run lint && node test" | ||
"build": "esbuild src/*.ts --format=cjs --outdir=dist", | ||
"lint": "ts-standard src/*.ts test/*.js", | ||
"prepack": "npm run prepare && npm run lint && npm run type-check && npm test", | ||
"prepare": "npm run build && npm run type-declarations", | ||
"test": "npm run build && mocha", | ||
"type-check": "tsc -noEmit", | ||
"type-declarations": "tsc --project tsconfig.build.json --declaration --emitDeclarationOnly --outDir types" | ||
}, | ||
"dependencies": { | ||
"highlight.js": "^11.3.1", | ||
"lodash.flow": "^3.5.0" | ||
"highlight.js": "^11.5.1" | ||
}, | ||
"devDependencies": { | ||
"markdown-it": "^10.0.0", | ||
"standard": "^14.3.4" | ||
"@tsconfig/recommended": "^1.0.1", | ||
"@types/markdown-it": "^12.2.3", | ||
"@types/node": "^17.0.23", | ||
"chai": "^4.3.6", | ||
"esbuild": "^0.14.34", | ||
"markdown-it": "^12.3.2", | ||
"markdown-it-attrs": "^4.1.3", | ||
"mocha": "^9.2.2", | ||
"ts-standard": "^11.0.0", | ||
"typescript": "^4.6.3" | ||
} | ||
} |
@@ -8,4 +8,3 @@ # markdown-it-highlightjs [![npm version](https://img.shields.io/npm/v/markdown-it-highlightjs.svg?style=flat-square)](https://www.npmjs.org/package/markdown-it-highlightjs) | ||
Usage | ||
----- | ||
## Usage | ||
@@ -21,11 +20,12 @@ ```js | ||
Name | Type | Description | Default | ||
-----------|------|----------------------------------------------------------------------------|-------- | ||
`auto` | boolean | Whether to automatically detect language if not specified. | `true` | ||
`code` | boolean | Whether to add the `hljs` class to raw code blocks (not fenced blocks). | `true` | ||
`register` | object | Register other languages which are not included in the standard pack. | `null` | ||
`inline` | boolean | Whether to highlight inline code. | `false` | ||
`hljs` | object | Provide the instance of [highlight.js] to use for highlighting | `require('highlight.js')` | ||
| Name | Type | Description | Default | | ||
|------------------|---------|---------------------------------------------------------------------------------------------------------------------------|---------------------------| | ||
| `auto` | boolean | Whether to automatically detect language if not specified. | `true` | | ||
| `code` | boolean | Whether to add the `hljs` class to raw code blocks (not fenced blocks). | `true` | | ||
| `register` | object | Register other languages which are not included in the standard pack. | `null` | | ||
| `inline` | boolean | Whether to highlight inline code. | `false` | | ||
| `hljs` | object | Provide the instance of [highlight.js] to use for highlighting | `require('highlight.js')` | | ||
| `ignoreIllegals` | boolean | Forces highlighting to finish even in case of detecting illegal syntax for the language instead of throwing an exception. | `true` | | ||
### Register languages | ||
## Register languages | ||
@@ -41,3 +41,3 @@ ```js | ||
### Inline code highlighting | ||
## Inline code highlighting | ||
@@ -51,3 +51,4 @@ You can enable inline code highlighting by setting `inline` to true: | ||
You can specify the language for inline code using [Pandoc syntax](https://pandoc.org/MANUAL.html#extension-inline_code_attributes): | ||
You can specify the language for inline code using | ||
[Pandoc syntax](https://pandoc.org/MANUAL.html#extension-inline_code_attributes): | ||
@@ -58,3 +59,3 @@ ```markdown | ||
or [Kramdown IAL syntax](https://kramdown.gettalong.org/syntax.html#inline-attribute-lists): | ||
Or [kramdown IAL syntax](https://kramdown.gettalong.org/syntax.html#inline-attribute-lists): | ||
@@ -65,9 +66,22 @@ ```markdown | ||
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). | ||
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). | ||
### Provide the [highlight.js] instance | ||
### Usage with markdown-it-attrs | ||
You can specify the `hljs` option to override the default [highlight.js] instance with your own: | ||
If you use markdown-it-attrs, make sure to include it *after* | ||
markdown-it-highlightjs if you want inline code highlighting to work: | ||
```js | ||
const md = require('markdown-it')() | ||
.use(require('markdown-it-highlightjs'), { inline: true }) | ||
.use(require('markdown-it-attrs')) | ||
``` | ||
## Provide the [highlight.js] instance | ||
You can specify the `hljs` option to override the default [highlight.js] | ||
instance with your own: | ||
```js | ||
const hljs = require('highlight.js/lib/core') | ||
@@ -84,10 +98,13 @@ | ||
### Core plugin | ||
## Core plugin | ||
You may import the core `markdown-it-highlightjs` plugin directly, without any default options. You must specify an instance of [highlight.js] for the `hljs` option. | ||
You may import the core `markdown-it-highlightjs` plugin directly, | ||
without any default options. You must specify an instance of | ||
[highlight.js] for the `hljs` option. | ||
```js | ||
const hljs = require('highlight.js/lib/core') | ||
const md = require('markdown-it')() | ||
.use(require('markdown-it-highlightjs/core'), { hljs }) | ||
``` |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
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
20662
1
12
287
104
10
1
1
1
- Removedlodash.flow@^3.5.0
- Removedlodash.flow@3.5.0(transitive)
Updatedhighlight.js@^11.5.1