Comparing version 1.11.0 to 1.12.0
139
lib/core.js
@@ -6,5 +6,4 @@ 'use strict' | ||
/* The lowlight interface, which has to be compatible | ||
* with highlight.js, as this object is passed to | ||
* highlight.js syntaxes. */ | ||
// The lowlight interface, which has to be compatible with highlight.js, as | ||
// this object is passed to highlight.js syntaxes. | ||
@@ -15,3 +14,3 @@ function High() {} | ||
/* Expose. */ | ||
// Expose. | ||
var low = new High() // Ha! | ||
@@ -24,2 +23,3 @@ | ||
low.registerLanguage = registerLanguage | ||
low.listLanguages = listLanguages | ||
low.registerAlias = registerAlias | ||
@@ -36,9 +36,5 @@ low.getLanguage = getLanguage | ||
var space = ' ' | ||
var pipe = '|' | ||
var verticalBar = '|' | ||
var T_ELEMENT = 'element' | ||
var T_TEXT = 'text' | ||
var T_SPAN = 'span' | ||
/* Maps of syntaxes. */ | ||
// Maps of syntaxes. | ||
var languageNames = [] | ||
@@ -48,12 +44,11 @@ var languages = {} | ||
/* Highlighting with language detection. Accepts a string | ||
* with the code to highlight. Returns an object with the | ||
* following properties: | ||
* | ||
* - language (detected language) | ||
* - relevance (int) | ||
* - value (a HAST tree with highlighting markup) | ||
* - secondBest (object with the same structure for | ||
* second-best heuristically detected language, may | ||
* be absent) */ | ||
// Highlighting with language detection. | ||
// Accepts a string with the code to highlight. | ||
// Returns an object with the following properties: | ||
// | ||
// * `language` — Detected language | ||
// * `relevance` — Integer | ||
// * `value` — HAST tree with highlighting markup | ||
// * `secondBest` — Object with the same structure for second-best | ||
// heuristically detected language, may be absent. | ||
function autoHighlight(value, options) { | ||
@@ -109,3 +104,3 @@ var settings = options || {} | ||
/* Highlighting `value` in the language `language`. */ | ||
// Highlighting `value` in the language `language`. | ||
function highlight(language, value, options) { | ||
@@ -122,3 +117,3 @@ var settings = options || {} | ||
/* Register a language. */ | ||
// Register a language. | ||
function registerLanguage(name, syntax) { | ||
@@ -136,3 +131,8 @@ var lang = syntax(low) | ||
/* Register more aliases for an already registered language. */ | ||
// Get a list of all registered languages. | ||
function listLanguages() { | ||
return languageNames.concat() | ||
} | ||
// Register more aliases for an already registered language. | ||
function registerAlias(name, alias) { | ||
@@ -162,5 +162,5 @@ var map = name | ||
/* Core highlighting function. Accepts a language name, or | ||
* an alias, and a string with the code to highlight. | ||
* Returns an object with the following properties: */ | ||
// Core highlighting function. | ||
// Accepts a language name, or an alias, and a string with the code to | ||
// highlight. | ||
function coreHighlight(name, value, ignore, prefix, continuation) { | ||
@@ -239,3 +239,3 @@ var continuations = {} | ||
/* Process a lexeme. Returns next position. */ | ||
// Process a lexeme. Returns next position. | ||
function processLexeme(buffer, lexeme) { | ||
@@ -275,3 +275,3 @@ var newMode | ||
/* Close open modes. */ | ||
// Close open modes. | ||
do { | ||
@@ -307,7 +307,5 @@ if (top.className) { | ||
/* Parser should not reach this point as all | ||
* types of lexemes should be caught earlier, | ||
* but if it does due to some bug make sure it | ||
* advances at least one character forward to | ||
* prevent infinite looping. */ | ||
// Parser should not reach this point as all types of lexemes should be | ||
// caught earlier, but if it does due to some bug make sure it advances | ||
// at least one character forward to prevent infinite looping. | ||
modeBuffer += lexeme | ||
@@ -318,3 +316,3 @@ | ||
/* Start a new mode with a `lexeme` to process. */ | ||
// Start a new mode with a `lexeme` to process. | ||
function startNewMode(mode, lexeme) { | ||
@@ -337,3 +335,3 @@ var node | ||
/* Enter a new mode. */ | ||
// Enter a new mode. | ||
if (node) { | ||
@@ -348,3 +346,3 @@ currentChildren.push(node) | ||
/* Process the buffer. */ | ||
// Process the buffer. | ||
function processBuffer() { | ||
@@ -356,3 +354,3 @@ var result = top.subLanguage ? processSubLanguage() : processKeywords() | ||
/* Process a sublanguage (returns a list of nodes). */ | ||
// Process a sublanguage (returns a list of nodes). | ||
function processSubLanguage() { | ||
@@ -382,8 +380,6 @@ var explicit = typeof top.subLanguage === 'string' | ||
/* Counting embedded language score towards the | ||
* host language may be disabled with zeroing the | ||
* containing mode relevance. Usecase in point is | ||
* Markdown that allows XML everywhere and makes | ||
* every XML snippet to have a much larger Markdown | ||
* score. */ | ||
// Counting embedded language score towards the host language may be | ||
// disabled with zeroing the containing mode relevance. | ||
// Usecase in point is Markdown that allows XML everywhere and makes every | ||
// XML snippet to have a much larger Markdown score. | ||
if (top.relevance > 0) { | ||
@@ -400,3 +396,3 @@ relevance += subvalue.relevance | ||
/* Process keywords. Returns nodes. */ | ||
// Process keywords. Returns nodes. | ||
function processKeywords() { | ||
@@ -445,3 +441,3 @@ var nodes = [] | ||
/* Add siblings. */ | ||
// Add siblings. | ||
function addSiblings(siblings, nodes) { | ||
@@ -455,3 +451,3 @@ var length = siblings.length | ||
if (sibling.type === T_TEXT) { | ||
if (sibling.type === 'text') { | ||
addText(sibling.value, nodes) | ||
@@ -464,3 +460,3 @@ } else { | ||
/* Add a text. */ | ||
// Add a text. | ||
function addText(value, nodes) { | ||
@@ -472,3 +468,3 @@ var tail | ||
if (tail && tail.type === T_TEXT) { | ||
if (tail && tail.type === 'text') { | ||
tail.value += value | ||
@@ -483,12 +479,12 @@ } else { | ||
/* Build a text. */ | ||
// Build a text. | ||
function buildText(value) { | ||
return {type: T_TEXT, value: value} | ||
return {type: 'text', value: value} | ||
} | ||
/* Build a span. */ | ||
// Build a span. | ||
function build(name, contents, noPrefix) { | ||
return { | ||
type: T_ELEMENT, | ||
tagName: T_SPAN, | ||
type: 'element', | ||
tagName: 'span', | ||
properties: { | ||
@@ -501,3 +497,3 @@ className: [(noPrefix ? '' : prefix) + name] | ||
/* Check if the first word in `keywords` is a keyword. */ | ||
// Check if the first word in `keywords` is a keyword. | ||
function keywordMatch(mode, keywords) { | ||
@@ -513,3 +509,3 @@ var keyword = keywords[0] | ||
/* Check if `lexeme` is illegal according to `mode`. */ | ||
// Check if `lexeme` is illegal according to `mode`. | ||
function isIllegal(lexeme, mode) { | ||
@@ -519,3 +515,3 @@ return !ignore && test(mode.illegalRe, lexeme) | ||
/* Check if `lexeme` ends `mode`. */ | ||
// Check if `lexeme` ends `mode`. | ||
function endOfMode(mode, lexeme) { | ||
@@ -535,3 +531,3 @@ if (test(mode.endRe, lexeme)) { | ||
/* Check a sub-mode. */ | ||
// Check a sub-mode. | ||
function subMode(lexeme, mode) { | ||
@@ -549,3 +545,3 @@ var values = mode.contains | ||
/* Exit the current context. */ | ||
// Exit the current context. | ||
function pop() { | ||
@@ -581,7 +577,7 @@ /* istanbul ignore next - removed in hljs 9.3 */ | ||
/* Compile a language. */ | ||
// Compile a language. | ||
function compileLanguage(language) { | ||
compileMode(language) | ||
/* Compile a language mode, optionally with a parent. */ | ||
// Compile a language mode, optionally with a parent. | ||
function compileMode(mode, parent) { | ||
@@ -616,3 +612,3 @@ var compiledKeywords = {} | ||
mode.begin = | ||
'\\b(' + mode.beginKeywords.split(space).join(pipe) + ')\\b' | ||
'\\b(' + mode.beginKeywords.split(space).join(verticalBar) + ')\\b' | ||
} | ||
@@ -637,3 +633,4 @@ | ||
if (mode.endsWithParent && parent.terminatorEnd) { | ||
mode.terminatorEnd += (mode.end ? pipe : '') + parent.terminatorEnd | ||
mode.terminatorEnd += | ||
(mode.end ? verticalBar : '') + parent.terminatorEnd | ||
} | ||
@@ -678,3 +675,3 @@ } | ||
? {exec: execNoop} | ||
: langRe(terminators.join(pipe), true) | ||
: langRe(terminators.join(verticalBar), true) | ||
@@ -685,3 +682,3 @@ function map(c) { | ||
/* Flatten a classname. */ | ||
// Flatten a classname. | ||
function flatten(className, value) { | ||
@@ -702,3 +699,3 @@ var pairs | ||
while (++index < length) { | ||
pair = pairs[index].split(pipe) | ||
pair = pairs[index].split(verticalBar) | ||
@@ -710,3 +707,3 @@ compiledKeywords[pair[0]] = [className, pair[1] ? Number(pair[1]) : 1] | ||
/* Create a regex for `value`. */ | ||
// Create a regex for `value`. | ||
function langRe(value, global) { | ||
@@ -719,3 +716,3 @@ return new RegExp( | ||
/* Get the source of an expression or string. */ | ||
// Get the source of an expression or string. | ||
function source(re) { | ||
@@ -726,3 +723,3 @@ return (re && re.source) || re | ||
/* Normalize a syntax result. */ | ||
// Normalize a syntax result. | ||
function normalize(result) { | ||
@@ -736,3 +733,3 @@ return { | ||
/* Check if `expression` matches `lexeme`. */ | ||
// Check if `expression` matches `lexeme`. | ||
function test(expression, lexeme) { | ||
@@ -743,3 +740,3 @@ var match = expression && expression.exec(lexeme) | ||
/* No-op exec. */ | ||
// No-op exec. | ||
function execNoop() { | ||
@@ -749,3 +746,3 @@ return null | ||
/* Get a language by `name`. */ | ||
// Get a language by `name`. | ||
function getLanguage(name) { | ||
@@ -752,0 +749,0 @@ name = name.toLowerCase() |
{ | ||
"name": "lowlight", | ||
"version": "1.11.0", | ||
"version": "1.12.0", | ||
"description": "Virtual syntax highlighting for virtual DOMs and non-HTML things", | ||
@@ -29,3 +29,3 @@ "license": "MIT", | ||
"fault": "^1.0.2", | ||
"highlight.js": "~9.13.0" | ||
"highlight.js": "~9.15.0" | ||
}, | ||
@@ -37,3 +37,3 @@ "devDependencies": { | ||
"prettier": "^1.12.0", | ||
"rehype": "^6.0.0", | ||
"rehype": "^7.0.0", | ||
"remark-cli": "^6.0.0", | ||
@@ -44,7 +44,7 @@ "remark-preset-wooorm": "^4.0.0", | ||
"unist-util-remove-position": "^1.1.2", | ||
"xo": "^0.23.0" | ||
"xo": "^0.24.0" | ||
}, | ||
"scripts": { | ||
"generate": "node script/build-registry", | ||
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix", | ||
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix", | ||
"build-bundle": "browserify index.js -s lowlight > lowlight.js", | ||
@@ -51,0 +51,0 @@ "build-mangle": "browserify index.js -s lowlight -p tinyify > lowlight.min.js", |
164
readme.md
@@ -17,7 +17,8 @@ # lowlight [![Build][build-badge]][build] [![Coverage][coverage-badge]][coverage] [![Downloads][downloads-badge]][downloads] [![Size][size-badge]][size] | ||
* [API](#api) | ||
* [low.registerLanguage(name, syntax)](#lowregisterlanguagename-syntax) | ||
* [low.registerAlias(name\[, alias\])](#lowregisteraliasname-alias) | ||
* [low.highlight(language, value\[, options\])](#lowhighlightlanguage-value-options) | ||
* [low.highlightAuto(value\[, options\])](#lowhighlightautovalue-options) | ||
* [Result](#result) | ||
* [low.registerLanguage(name, syntax)](#lowregisterlanguagename-syntax) | ||
* [low.registerAlias(name\[, alias\])](#lowregisteraliasname-alias) | ||
* [low.listLanguages()](#lowlistlanguages) | ||
* [Browser](#browser) | ||
@@ -43,6 +44,6 @@ * [Related](#related) | ||
```javascript | ||
var low = require('lowlight'); | ||
var ast = low.highlight('js', '"use strict";').value; | ||
var low = require('lowlight') | ||
var tree = low.highlight('js', '"use strict";').value | ||
console.log(ast); | ||
console.log(tree) | ||
``` | ||
@@ -63,6 +64,8 @@ | ||
```js | ||
var rehype = require('rehype'); | ||
var html = rehype().stringify({type: 'root', children: ast}).toString(); | ||
var rehype = require('rehype') | ||
var html = rehype() | ||
.stringify({type: 'root', children: tree}) | ||
.toString() | ||
console.log(html); | ||
console.log(html) | ||
``` | ||
@@ -81,56 +84,2 @@ | ||
### `low.registerLanguage(name, syntax)` | ||
Register a [syntax][] as `name` (`string`). Useful in the browser or with | ||
custom grammars. | ||
###### Example | ||
```js | ||
var low = require('lowlight/lib/core'); | ||
var xml = require('highlight.js/lib/languages/xml'); | ||
low.registerLanguage('xml', xml); | ||
console.log(low.highlight('html', '<em>Emphasis</em>')); | ||
``` | ||
Yields: | ||
```js | ||
{ relevance: 2, language: 'html', value: [Array] } | ||
``` | ||
### `low.registerAlias(name[, alias])` | ||
Register a new `alias` for the `name` language. | ||
###### Signatures | ||
* `registerAlias(name, alias|list)` | ||
* `registerAlias(aliases)` | ||
###### Parameters | ||
* `name` (`string`) — [Name][names] of a registered language | ||
* `alias` (`string`) — New alias for the registered language | ||
* `list` (`Array.<alias>`) — List of aliases | ||
* `aliases` (`Object.<alias|list>`) — Map where each key is a `name` and each | ||
value an `alias` or a `list` | ||
###### Example | ||
```js | ||
var low = require('lowlight/lib/core'); | ||
var md = require('highlight.js/lib/languages/markdown'); | ||
low.registerLanguage('markdown', md); | ||
// low.highlight('mdown', '<em>Emphasis</em>') | ||
// ^ would throw: Error: Unknown language: `mdown` is not registered | ||
low.registerAlias({markdown: ['mdown', 'mkdn', 'mdwn', 'ron']}) | ||
low.highlight('mdown', '<em>Emphasis</em>') | ||
// ^ Works! | ||
``` | ||
### `low.highlight(language, value[, options])` | ||
@@ -151,5 +100,5 @@ | ||
```js | ||
var low = require('lowlight'); | ||
var low = require('lowlight') | ||
console.log(low.highlight('css', 'em { color: red }')); | ||
console.log(low.highlight('css', 'em { color: red }')) | ||
``` | ||
@@ -180,5 +129,5 @@ | ||
```js | ||
var low = require('lowlight'); | ||
var low = require('lowlight') | ||
console.log(low.highlightAuto('"hello, " + name + "!"')); | ||
console.log(low.highlightAuto('"hello, " + name + "!"')) | ||
``` | ||
@@ -210,2 +159,77 @@ | ||
### `low.registerLanguage(name, syntax)` | ||
Register a [syntax][] as `name` (`string`). Useful in the browser or with | ||
custom grammars. | ||
###### Example | ||
```js | ||
var low = require('lowlight/lib/core') | ||
var xml = require('highlight.js/lib/languages/xml') | ||
low.registerLanguage('xml', xml) | ||
console.log(low.highlight('html', '<em>Emphasis</em>')) | ||
``` | ||
Yields: | ||
```js | ||
{ relevance: 2, language: 'html', value: [Array] } | ||
``` | ||
### `low.registerAlias(name[, alias])` | ||
Register a new `alias` for the `name` language. | ||
###### Signatures | ||
* `registerAlias(name, alias|list)` | ||
* `registerAlias(aliases)` | ||
###### Parameters | ||
* `name` (`string`) — [Name][names] of a registered language | ||
* `alias` (`string`) — New alias for the registered language | ||
* `list` (`Array.<alias>`) — List of aliases | ||
* `aliases` (`Object.<alias|list>`) — Map where each key is a `name` and each | ||
value an `alias` or a `list` | ||
###### Example | ||
```js | ||
var low = require('lowlight/lib/core') | ||
var md = require('highlight.js/lib/languages/markdown') | ||
low.registerLanguage('markdown', md) | ||
// low.highlight('mdown', '<em>Emphasis</em>') | ||
// ^ would throw: Error: Unknown language: `mdown` is not registered | ||
low.registerAlias({markdown: ['mdown', 'mkdn', 'mdwn', 'ron']}) | ||
low.highlight('mdown', '<em>Emphasis</em>') | ||
// ^ Works! | ||
``` | ||
### `low.listLanguages()` | ||
List all registered languages. | ||
###### Returns | ||
`Array.<string>`. | ||
###### Example | ||
```js | ||
var low = require('lowlight/lib/core') | ||
var md = require('highlight.js/lib/languages/markdown') | ||
console.log(low.listLanguages()) // => [] | ||
low.registerLanguage('markdown', md) | ||
console.log(low.listLanguages()) // => ['markdown'] | ||
``` | ||
## Browser | ||
@@ -220,8 +244,8 @@ | ||
```js | ||
var low = require('lowlight/lib/core'); | ||
var js = require('highlight.js/lib/languages/javascript'); | ||
var low = require('lowlight/lib/core') | ||
var js = require('highlight.js/lib/languages/javascript') | ||
low.registerLanguage('javascript', js); | ||
low.registerLanguage('javascript', js) | ||
low.highlight('js', '"use strict";'); | ||
low.highlight('js', '"use strict";') | ||
// See `Usage` for the results. | ||
@@ -228,0 +252,0 @@ ``` |
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
41080
321
837
+ Addedhighlight.js@9.15.10(transitive)
- Removedhighlight.js@9.13.1(transitive)
Updatedhighlight.js@~9.15.0