Skip to content

Commit

Permalink
Merge pull request #17 from Availity/feature/v3
Browse files Browse the repository at this point in the history
v3
  • Loading branch information
robmcguinness authored Aug 26, 2016
2 parents 8ec68cb + abec551 commit 17b76bf
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 87 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
language: node_js
node_js:
- '0.12'
- '0.10'
- '4'
- '5'
- '6'
before_install:
- npm install -g npm
cache:
directories:
- node_modules
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015 Availity
Copyright (c) 2016 Availity

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
[![Linux Passing](https://img.shields.io/travis/Availity/metalsmith-prism.svg?style=flat-square&label=linux)](https://travis-ci.org/Availity/metalsmith-prism)
[![Windows Passing](https://img.shields.io/appveyor/ci/robmcguinness/metalsmith-prism.svg?style=flat-square&label=windows)](https://ci.appveyor.com/project/robmcguinness/metalsmith-prism)

## Upgrading to version 3

+ Node dependency to `>=4.x.x`
+ Metalsmith to `=>v2.x.x`

## Quickstart

+ Install **metalsmith-prism**
Expand Down
5 changes: 3 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ branches:
environment:
matrix:
# node.js
- nodejs_version: "0.10"
- nodejs_version: "0.12"
- nodejs_version: "4"
- nodejs_version: "5"
- nodejs_version: "6"

# Fix line endings on Windows
init:
- git config --global core.autocrlf true

install:
- ps: Install-Product node $env:nodejs_version
- npm install -g npm
- npm install

# build version format
Expand Down
88 changes: 45 additions & 43 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,85 @@
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 languages = require('prismjs').languages;
const _ = require('lodash');
const Prism = require('prismjs');
const he = require('he');

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

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

module.exports = function(_options) {

var options = _options || {};
module.exports = (options) => {

Prism.languages.json = options.json ? options.json : jsonSyntax;
options = options || {};

return function(files, metalsmith, done) {

setImmediate(done);

function requireLanguage(language) {

if (!Prism.languages[language]) {

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

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

function addLineNumber($el) {
if (!languages[language]) {

var $parent = $el.parent();
if ($parent && $parent.is('pre')) {
$parent.addClass('line-numbers');
try {
require(`prismjs/components/prism-${language}.js`);
} catch (e) {
/* eslint no-console: 0 */
console.warn(`Failed to load prism syntax: ${language}`);
console.warn(e);
}
}

}

_.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) {

if (options.lineNumbers) {
addLineNumber($this);
const $pre = $this.parent('pre');

if ($pre) {
// Copy className to <pre> container
$pre.addClass(className);

if (options.lineNumbers) {
debug('adding line numbers');
$pre.addClass('line-numbers');
}
}

highlighted = true;

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

requireLanguage(language);

var html = (language === 'markup' && !options.decode) ? $this.html() : he.decode($this.html());
if (!languages[language]) {
language = 'markup';
}

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
7 changes: 0 additions & 7 deletions lib/prism-json.js

This file was deleted.

31 changes: 20 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"name": "metalsmith-prism",
"version": "2.2.0",
"version": "3.0.0-beta.1",
"description": "Syntax highlighting for Metalsmith HTML templates using Prism.js",
"main": "lib/index.js",
"engines": {
"node": ">= 4.0.0"
},
"scripts": {
"preversion": "npm test",
"test": "node_modules/.bin/mocha ./tests/index.js",
"preversion": "npm run test",
"test": "mocha ./tests/index.js",
"lint": "eslint ./lib ./tests"
},
"repository": {
Expand All @@ -18,23 +21,29 @@
"syntax",
"highlighting"
],
"files": [
"lib",
"README.md"
],
"author": "Robert McGuinness <rob.mcguinness@availity.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/Availity/metalsmith-prism/issues"
},
"homepage": "https://github.com/Availity/metalsmith-prism",
"dependencies": {
"cheerio": "0.20.0",
"he": "^0.5.0",
"lodash": "4.6.1",
"metalsmith": "^1.7.0",
"prismjs": "1.4.1"
"cheerio": "0.22.0",
"he": "1.1.0",
"lodash": "4.15.0",
"metalsmith": "2.2.0",
"prismjs": "1.5.1"
},
"devDependencies": {
"babel-eslint": "6.1.2",
"chai": "3.5.0",
"eslint": "1.6.0",
"eslint-config-availity": "1.0.1",
"mocha": "2.4.5"
"debug": "^2.2.0",
"eslint": "3.3.1",
"eslint-config-availity": "2.0.0-beta.11",
"mocha": "3.0.2"
}
}
4 changes: 2 additions & 2 deletions tests/fixtures/markup/expected/json.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<code class="language-json">
<span class="token punctuation">{</span>
<span class="token keys">&quot;name&quot;</span><span class="token punctuation">:</span> &quot;metalsmith-prism&quot;<span class="token punctuation">,</span>
<span class="token keys">&quot;description&quot;</span><span class="token punctuation">:</span> &quot;Provides syntax highlighting&quot;
<span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;metalsmith-prism&quot;</span><span class="token punctuation">,</span>
<span class="token property">&quot;description&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Provides syntax highlighting&quot;</span>
<span class="token punctuation">}</span>
</code>
2 changes: 1 addition & 1 deletion tests/fixtures/markup/expected/line-numbers.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<pre class="line-numbers">
<pre class="language-javascript line-numbers">
<code class="language-javascript">
<span class="token keyword">var</span> name <span class="token operator">=</span> <span class="token string">&quot;Rob&quot;</span><span class="token punctuation">;</span>
</code>
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/markup/expected/pre-classname.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<pre class="language-javascript">
<code class="language-javascript">
<span class="token keyword">var</span> name <span class="token operator">=</span> <span class="token string">&quot;Rob&quot;</span><span class="token punctuation">;</span>
</code>
</pre>
57 changes: 39 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 @@ -76,15 +78,34 @@ describe('metalsmith-prism', function() {

});

it('should add language class to <pre> tag', function(done) {

const metal = metalsmith(fixture());

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

if (err) {
return done(err);
}

expect(file('build/line-numbers.html')).to.be.eql(file('expected/pre-classname.html'));

done();
});

});

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 17b76bf

Please sign in to comment.