-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes GH-14. Reviewed-by: Titus Wormer <[email protected]>
- Loading branch information
1 parent
c244fde
commit bf464d7
Showing
6 changed files
with
265 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
'use strict' | ||
|
||
var toText = require('hast-util-to-text') | ||
var visit = require('unist-util-visit') | ||
|
||
module.exports = createPlugin | ||
|
||
function createPlugin(lowlight) { | ||
return function (options) { | ||
var settings = options || {} | ||
var name = 'hljs' | ||
var pos | ||
|
||
if (settings.aliases) { | ||
lowlight.registerAlias(settings.aliases) | ||
} | ||
|
||
if (settings.languages) { | ||
// eslint-disable-next-line guard-for-in | ||
for (let name in settings.languages) { | ||
lowlight.registerLanguage(name, settings.languages[name]) | ||
} | ||
} | ||
|
||
if (settings.prefix) { | ||
pos = settings.prefix.indexOf('-') | ||
name = pos > -1 ? settings.prefix.slice(0, pos) : settings.prefix | ||
} | ||
|
||
return transformer | ||
|
||
function transformer(tree) { | ||
visit(tree, 'element', visitor) | ||
} | ||
|
||
function visitor(node, index, parent) { | ||
var props | ||
var result | ||
var lang | ||
|
||
if (!parent || parent.tagName !== 'pre' || node.tagName !== 'code') { | ||
return | ||
} | ||
|
||
lang = language(node) | ||
|
||
if ( | ||
lang === false || | ||
(!lang && settings.subset === false) || | ||
(settings.plainText && settings.plainText.indexOf(lang) > -1) | ||
) { | ||
return | ||
} | ||
|
||
props = node.properties | ||
|
||
if (!props.className) { | ||
props.className = [] | ||
} | ||
|
||
if (props.className.indexOf(name) < 0) { | ||
props.className.unshift(name) | ||
} | ||
|
||
try { | ||
result = lang | ||
? lowlight.highlight(lang, toText(parent), options) | ||
: lowlight.highlightAuto(toText(parent), options) | ||
} catch (error) { | ||
if ( | ||
!settings.ignoreMissing || | ||
!/Unknown language/.test(error.message) | ||
) { | ||
throw error | ||
} | ||
|
||
result = {} | ||
} | ||
|
||
if (!lang && result.language) { | ||
props.className.push('language-' + result.language) | ||
} | ||
|
||
if (result.value) { | ||
node.children = result.value | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Get the programming language of `node`. | ||
function language(node) { | ||
var className = node.properties.className || [] | ||
var index = -1 | ||
var value | ||
|
||
while (++index < className.length) { | ||
value = className[index] | ||
|
||
if (value === 'no-highlight' || value === 'nohighlight') { | ||
return false | ||
} | ||
|
||
if (value.slice(0, 5) === 'lang-') { | ||
return value.slice(5) | ||
} | ||
|
||
if (value.slice(0, 9) === 'language-') { | ||
return value.slice(9) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,5 @@ | ||
'use strict' | ||
|
||
var toText = require('hast-util-to-text') | ||
var lowlight = require('lowlight') | ||
var visit = require('unist-util-visit') | ||
|
||
module.exports = attacher | ||
|
||
function attacher(options) { | ||
var settings = options || {} | ||
var name = 'hljs' | ||
var pos | ||
|
||
if (settings.aliases) { | ||
lowlight.registerAlias(settings.aliases) | ||
} | ||
|
||
if (settings.languages) { | ||
// eslint-disable-next-line guard-for-in | ||
for (let name in settings.languages) { | ||
lowlight.registerLanguage(name, settings.languages[name]) | ||
} | ||
} | ||
|
||
if (settings.prefix) { | ||
pos = settings.prefix.indexOf('-') | ||
name = pos > -1 ? settings.prefix.slice(0, pos) : settings.prefix | ||
} | ||
|
||
return transformer | ||
|
||
function transformer(tree) { | ||
visit(tree, 'element', visitor) | ||
} | ||
|
||
function visitor(node, index, parent) { | ||
var props | ||
var result | ||
var lang | ||
|
||
if (!parent || parent.tagName !== 'pre' || node.tagName !== 'code') { | ||
return | ||
} | ||
|
||
lang = language(node) | ||
|
||
if ( | ||
lang === false || | ||
(!lang && settings.subset === false) || | ||
(settings.plainText && settings.plainText.indexOf(lang) > -1) | ||
) { | ||
return | ||
} | ||
|
||
props = node.properties | ||
|
||
if (!props.className) { | ||
props.className = [] | ||
} | ||
|
||
if (props.className.indexOf(name) < 0) { | ||
props.className.unshift(name) | ||
} | ||
|
||
try { | ||
result = lang | ||
? lowlight.highlight(lang, toText(parent), options) | ||
: lowlight.highlightAuto(toText(parent), options) | ||
} catch (error) { | ||
if (!settings.ignoreMissing || !/Unknown language/.test(error.message)) { | ||
throw error | ||
} | ||
|
||
result = {} | ||
} | ||
|
||
if (!lang && result.language) { | ||
props.className.push('language-' + result.language) | ||
} | ||
|
||
if (result.value) { | ||
node.children = result.value | ||
} | ||
} | ||
} | ||
|
||
// Get the programming language of `node`. | ||
function language(node) { | ||
var className = node.properties.className || [] | ||
var index = -1 | ||
var value | ||
|
||
while (++index < className.length) { | ||
value = className[index] | ||
|
||
if (value === 'no-highlight' || value === 'nohighlight') { | ||
return false | ||
} | ||
|
||
if (value.slice(0, 5) === 'lang-') { | ||
return value.slice(5) | ||
} | ||
var createPlugin = require('./core') | ||
|
||
if (value.slice(0, 9) === 'language-') { | ||
return value.slice(9) | ||
} | ||
} | ||
} | ||
module.exports = createPlugin(lowlight) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict' | ||
var lowlight = require('lowlight/lib/core') | ||
var createPlugin = require('./core') | ||
|
||
module.exports = createPlugin(lowlight) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,9 @@ | |
"Titus Wormer <[email protected]> (https://wooorm.com)" | ||
], | ||
"files": [ | ||
"index.js" | ||
"core.js", | ||
"index.js", | ||
"light.js" | ||
], | ||
"dependencies": { | ||
"hast-util-to-text": "^2.0.0", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.