Skip to content

Commit

Permalink
conver to es6
Browse files Browse the repository at this point in the history
  • Loading branch information
robmcguinness committed Mar 20, 2016
1 parent 674cdcc commit 7409e5e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 44 deletions.
56 changes: 31 additions & 25 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
var cheerio = require('cheerio');
var extname = require('path').extname;
var _ = require('lodash');
var Prism = require('prismjs');
var he = require('he');
var vm = require('vm');
var fs = require('fs');
'use strict';

var jsonSyntax = require('./prism-json');
const cheerio = require('cheerio');
const _debug = require('debug');
const extname = require('path').extname;
const _ = require('lodash');
const Prism = require('prismjs');
const he = require('he');
const vm = require('vm');
const fs = require('fs');

var isHTMLFile = function(filePath) {
const debug = _debug('metalsmith-prism');

const jsonSyntax = require('./prism-json');

const isHTMLFile = (filePath) => {
return /\.html|\.htm/.test(extname(filePath));
};

module.exports = function(_options) {
module.exports = (options) => {

var options = _options || {};
options = options || {};

Prism.languages.json = options.json ? options.json : jsonSyntax;

Expand All @@ -26,42 +31,43 @@ module.exports = function(_options) {

if (!Prism.languages[language]) {

var path = require.resolve('prismjs/components/prism-' + language);
var code = fs.readFileSync(path, 'utf8').toString();
const path = require.resolve('prismjs/components/prism-' + language);
const code = fs.readFileSync(path, 'utf8').toString();

// make Prism and self object available in the plugins local scope
vm.runInNewContext(code, {
self: {},
Prism: Prism
Prism
});
}
}

function addLineNumber($el) {

var $parent = $el.parent();
const $parent = $el.parent();
if ($parent && $parent.is('pre')) {
debug('adding line numbers');
$parent.addClass('line-numbers');
}

}

_.each(files, function(file, name) {
_.each(files, (file, name) => {

// gulpsmith || vanilla Metalsmith support
if (!isHTMLFile(file.path || name)) {
return;
}

var contents = file.contents.toString();
var $ = cheerio.load(contents);
var highlighted = false;
const contents = file.contents.toString();
const $ = cheerio.load(contents);
let highlighted = false;

$('code').each(function() {

var $this = $(this);
var className = $this.attr('class') || '';
var targets = className.split('language-');
const $this = $(this);
const className = $this.attr('class') || '';
const targets = className.split('language-');

if (targets.length > 1) {

Expand All @@ -71,13 +77,13 @@ module.exports = function(_options) {

highlighted = true;

var language = targets[1];
const language = targets[1];

requireLanguage(language);

var html = (language === 'markup' && !options.decode) ? $this.html() : he.decode($this.html());
const html = (language === 'markup' && !options.decode) ? $this.html() : he.decode($this.html());

var highlightedCode = Prism.highlight(html, Prism.languages[language]);
const highlightedCode = Prism.highlight(html, Prism.languages[language]);
$this.html(highlightedCode);

}
Expand Down
2 changes: 2 additions & 0 deletions lib/prism-json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// json syntax highlighting
'use strict';

module.exports = {
'keys': /".+"(?=:)/g,
'boolean': /\b(true|false)/g,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"chai": "3.5.0",
"debug": "^2.2.0",
"eslint": "^2.4.0",
"eslint-config-availity": "^2.0.0-beta.6",
"eslint-config-availity": "^2.0.0-beta.7",
"mocha": "2.4.5"
}
}
38 changes: 20 additions & 18 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
/* global describe, it */

var chai = require('chai');
var metalsmith = require('metalsmith');
var metalsmithPrism = require('../lib');
var fs = require('fs');
var path = require('path');
var expect = chai.expect;
'use strict';

var fixture = path.resolve.bind(path, __dirname, 'fixtures/markup');
const chai = require('chai');
const metalsmith = require('metalsmith');
const metalsmithPrism = require('../lib');
const fs = require('fs');
const path = require('path');
const expect = chai.expect;

const fixture = path.resolve.bind(path, __dirname, 'fixtures/markup');

function file(_path) {
return fs.readFileSync(fixture(_path), 'utf8');
}

describe('metalsmith-prism', function() {
describe('metalsmith-prism', () => {

it('should highlight code blocks for json, markup, ruby and bash', function(done) {
it('should highlight code blocks for json, markup, ruby and bash', done => {

var metal = metalsmith(fixture());
const metal = metalsmith(fixture());

metal
.use(metalsmithPrism())
.build(function(err) {
.build( err => {

if (err) {
return done(err);
Expand All @@ -37,13 +39,13 @@ describe('metalsmith-prism', function() {

});

it('should NOT highlight unknown language code blocks', function(done) {
it('should NOT highlight unknown language code blocks', done => {

var metal = metalsmith(fixture());
const metal = metalsmith(fixture());

metal
.use(metalsmithPrism())
.build(function(err) {
.build( err => {

if (err) {
return done(err);
Expand All @@ -57,13 +59,13 @@ describe('metalsmith-prism', function() {

it('should decode markup blocks when options#decode is true', function(done) {

var metal = metalsmith(fixture());
const metal = metalsmith(fixture());

metal
.use(metalsmithPrism({
decode: true
}))
.build(function(err) {
.build( err => {

if (err) {
return done(err);
Expand All @@ -78,13 +80,13 @@ describe('metalsmith-prism', function() {

it('should add line numbers class to <pre> tag when options#lineNumbers is true', function(done) {

var metal = metalsmith(fixture());
const metal = metalsmith(fixture());

metal
.use(metalsmithPrism({
lineNumbers: true
}))
.build(function(err) {
.build( err => {

if (err) {
return done(err);
Expand Down

0 comments on commit 7409e5e

Please sign in to comment.