Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add syntax highlighting to .md processing #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/template/processors/md.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
var marked = require("marked").setOptions({ langPrefix: 'language-', headerPrefix: '' })
var highlight = require('highlight.js')
var marked = require("marked").setOptions({
langPrefix: 'language-',
headerPrefix: '' ,
highlight: applySyntaxHighlighting
})
var TerraformError = require("../../error").TerraformError
var marked = require("marked")
var renderer = new marked.Renderer()

// This overrides Marked’s default headings with IDs,
Expand All @@ -26,5 +30,13 @@ module.exports = function(fileContents, options){
return new TerraformError(error)
}
}
}

function applySyntaxHighlighting (code, lang) {
if (lang !== undefined && typeof lang === "string" && highlight.getLanguage(lang) !== undefined)
return highlight.highlight(lang, code).value

var result = highlight.highlightAuto(code);
console.log("Code block language was not specified in markdown. Auto detected language: " + result.language)
return result.value
}
77 changes: 58 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,71 @@
},
"author": "Brock Whitten <[email protected]>",
"contributors": [
{ "name": "Brock Whitten", "email": "[email protected]" },
{ "name": "Brian Donovan", "email": "[email protected]" },
{ "name": "Kenneth Ormandy", "email": "[email protected]" },
{ "name": "Zhang Yichao", "email": "[email protected]" },
{ "name": "Carlos Rodriguez" },
{ "name": "Zeke Sikelianos", "email": "[email protected]" },
{ "name": "Guilherme Rodrigues", "email": "[email protected]" },
{ "name": "Radu Brehar", "email": "[email protected]" },
{ "name": "Glen Maddern", "email": "[email protected]" },
{ "name": "Jed Foster", "email": "[email protected]" },
{ "name": "Sehrope Sarkuni", "email": "[email protected]" },
{ "name": "Keiichiro Matsumoto", "email": "[email protected]" },
{ "name": "Najam Khn", "email": "[email protected]" }
{
"name": "Brock Whitten",
"email": "[email protected]"
},
{
"name": "Brian Donovan",
"email": "[email protected]"
},
{
"name": "Kenneth Ormandy",
"email": "[email protected]"
},
{
"name": "Zhang Yichao",
"email": "[email protected]"
},
{
"name": "Carlos Rodriguez"
},
{
"name": "Zeke Sikelianos",
"email": "[email protected]"
},
{
"name": "Guilherme Rodrigues",
"email": "[email protected]"
},
{
"name": "Radu Brehar",
"email": "[email protected]"
},
{
"name": "Glen Maddern",
"email": "[email protected]"
},
{
"name": "Jed Foster",
"email": "[email protected]"
},
{
"name": "Sehrope Sarkuni",
"email": "[email protected]"
},
{
"name": "Keiichiro Matsumoto",
"email": "[email protected]"
},
{
"name": "Najam Khn",
"email": "[email protected]"
}
],
"license": "MIT",
"dependencies": {
"lru-cache": "2.6.1",
"jade": "git://github.com/harp/jade#v1.9.3-bc.2",
"autoprefixer": "5.1.0",
"coffee-script": "1.9.2",
"ejs": "1.0.0",
"node-sass": "3.0.0-beta.5",
"marked": "0.3.3",
"highlight.js": "^8.6.0",
"jade": "git://github.com/harp/jade#v1.9.3-bc.2",
"less": "2.5.0",
"stylus": "0.47.3",
"lru-cache": "2.6.1",
"marked": "0.3.3",
"minify": "git://github.com/kennethormandy/minify#v0.3.0",
"autoprefixer": "5.1.0"
"node-sass": "3.0.0-beta.5",
"stylus": "0.47.3"
},
"devDependencies": {
"mocha": "1.8.2",
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/templates/code.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

```json
{
"hello-saturn": {
"title": "Hello, Saturn",
"date": "1610-01-01"
}
}
```

```
<h1>
Header
</h1>

<div class="main">
<!-- Stuff goes here... -->
</div>
```
18 changes: 18 additions & 0 deletions test/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ describe("templates", function(){
})
})

it("should highlight code with specified language", function(done){
poly.render("code.md", function(error, body){
body.should.include('<code class="language-json">')
body.should.include('<span class="hljs-attribute">')
body.should.include('<span class="hljs-value">')
done()
})
})

it("should auto highlight code blocks when language is not provided", function(done){
poly.render("code.md", function(error, body){
body.should.include('<code>')
body.should.include('<span class="hljs-tag">')
body.should.include('<span class="hljs-comment">')
done()
})
})

it("should minify beyond preprocessor", function(done){
poly.render("stuff.md", function(error, body){
should.not.exist(error)
Expand Down