amps-in-the-trunk
Advanced tools
Comparing version 1.1.7 to 1.2.0
39
index.js
var cheerio = require('cheerio'), | ||
getImageSize = require('image-size'), | ||
fs = require('fs'), | ||
highlight = require('highlight.js'), | ||
log = console.log.bind(console); | ||
// See README for imageOverrides format | ||
module.exports = function(imageOverrides){ | ||
module.exports = function(imageOverrides, enableCodeHighlighting){ | ||
@@ -17,11 +18,11 @@ imageOverrides = imageOverrides || {}; | ||
try { | ||
status = fs.statSync(imageFile); | ||
status = fs.statSync(imageFile); | ||
} catch (err) { | ||
if ( err.code === 'ENOENT' ) | ||
console.error("Missing image file:", imageFile) | ||
return { | ||
height: null, | ||
width: null, | ||
type: null | ||
} | ||
if ( err.code === 'ENOENT' ) | ||
console.error("Missing image file:", imageFile) | ||
return { | ||
height: null, | ||
width: null, | ||
type: null | ||
} | ||
} | ||
@@ -104,5 +105,24 @@ var cachedDimensions = cache[imageFile] | ||
var highlightCode = function(html){ | ||
var $ = cheerio.load(html); | ||
$('pre code').each(function(index, element) { | ||
var $element = $(element); | ||
log('element', $element.text()) | ||
var elementText = $element.html(); | ||
log('elementText', elementText) | ||
var highlightedText = highlight.highlightAuto(elementText) | ||
log('highlightedText', highlightedText.value) | ||
$element.html(highlightedText.value) | ||
}); | ||
var ampHTML = $.html() | ||
return ampHTML | ||
} | ||
var toAmp = function(html){ | ||
html = imgToAmpImg(html) | ||
html = stripInlineStyles(html) | ||
if ( enableCodeHighlighting ) { | ||
html = highlightCode(html) | ||
} | ||
return html | ||
@@ -115,2 +135,3 @@ } | ||
imgToAmpImg, | ||
highlightCode, | ||
renderAMP, | ||
@@ -117,0 +138,0 @@ getSize |
{ | ||
"name": "amps-in-the-trunk", | ||
"version": "1.1.7", | ||
"version": "1.2.0", | ||
"description": "Turns HTML in Google AMP HTML. Includes express middleware.", | ||
@@ -19,2 +19,3 @@ "main": "index.js", | ||
"dedent-js": "^1.0.1", | ||
"highlight.js": "^9.9.0", | ||
"image-size": "^0.5.0" | ||
@@ -21,0 +22,0 @@ }, |
@@ -31,3 +31,5 @@ # A module to transform HTML directly to Google AMP HTML | ||
// Load the module and pass in any overrides | ||
var ampsInTheTrunk = require('amps-in-the-trunk')() | ||
// imageOverrides optional Object - see below | ||
// enableCodeHighlighting optional Boolean - see below | ||
var ampsInTheTrunk = require('amps-in-the-trunk')(imageOverrides, enableCodeHighlighting) | ||
@@ -46,3 +48,3 @@ // Enable the renderAMP Express middleware | ||
## SVGs aren't sized correctly, or I want a different layout for an image | ||
## The *imageOverrides* option - when SVGs aren't sized correctly, or I want a different layout for an image | ||
@@ -66,2 +68,6 @@ amps-in-the-trunk sets images to `responsive` by default. You can change that with `imageOverrides`. Also most SVGs have incorrect sizing. You could fix the SVGs, or you could override them with `imageOverrides`. | ||
## The enableCodeHighlighting option - highlights `<pre><code>` blok using highlight.js | ||
If you want syntax highlighting, AMPS in the Trunk will use highlight.js to highlight the specific blocks. | ||
## Why another AMP module? | ||
@@ -68,0 +74,0 @@ |
@@ -10,3 +10,3 @@ // Tests. Mocha TDD/assert style. See | ||
dedent = require('dedent-js'), | ||
log = console.log.bind(console) | ||
log = console.log.bind(console); | ||
@@ -39,3 +39,2 @@ suite('amp-img tags', function(){ | ||
}); | ||
}) | ||
@@ -58,2 +57,23 @@ | ||
}) | ||
}); | ||
suite('syntax highlighting', function(){ | ||
test('works', function(){ | ||
var html = dedent(` | ||
<pre> | ||
<code> | ||
console.log('Yolo swag') | ||
</code> | ||
</pre> | ||
`) | ||
var actual = ampsInTheTrunk.highlightCode(html); | ||
var expected = dedent(` | ||
<pre> | ||
<code> | ||
console.log(<span class="hljs-variable">&apos</span>;Yolo swag<span class="hljs-variable">&apos</span>;) | ||
</code> | ||
</pre> | ||
`) | ||
assert.deepEqual(actual, expected) | ||
}) | ||
}) |
1508563
188
81
4
+ Addedhighlight.js@^9.9.0
+ Addedhighlight.js@9.18.5(transitive)