Skip to content

Commit 2e7bb7e

Browse files
authored
Merge pull request #43 from theicfire/chase/space-to-tabs
Convert spaces to tabs
2 parents 2a2cfa6 + bf04ff0 commit 2e7bb7e

File tree

6 files changed

+230
-261
lines changed

6 files changed

+230
-261
lines changed

build.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var Metalsmith = require('metalsmith'),
2-
metallic = require('metalsmith-metallic'),
32
drafts = require('metalsmith-drafts'),
43
layouts = require('metalsmith-layouts'),
54
markdown = require('metalsmith-markdown'),
@@ -10,6 +9,40 @@ var Metalsmith = require('metalsmith'),
109
var serve = require('metalsmith-serve');
1110
var watch = require('metalsmith-watch');
1211
var sass = require('metalsmith-sass');
12+
const hljs = require('highlight.js');
13+
const marked = require('marked');
14+
15+
var renderer = new marked.Renderer();
16+
17+
renderer.code = function (code, infostring, escaped) {
18+
/** Copied from the default implementation, with the following changes.
19+
* 1. Find a series of 4 spaces and convert it into tabs
20+
* 2. Add "hljs" to the code class. I don't get why, but metalsmith-metallic was doing this, so I copied the pattern.
21+
* 3. Remove this.options.langPrefix, because metalsmith-metallic didn't have this.
22+
*/
23+
code = code.replace(/ {4}/g, '\t'); // Convert spaces back into tabs
24+
var lang = (infostring || '').match(/\S*/)[0];
25+
if (this.options.highlight) {
26+
var out = this.options.highlight(code, lang);
27+
if (out != null && out !== code) {
28+
escaped = true;
29+
code = out;
30+
}
31+
}
32+
33+
if (!lang) {
34+
return '<pre><code>' + (escaped ? code : escape(code, true)) + '</code></pre>';
35+
}
36+
37+
return (
38+
'<pre><code class="hljs ' +
39+
// this.options.langPrefix +
40+
escape(lang, true) +
41+
'">' +
42+
(escaped ? code : escape(code, true)) +
43+
'</code></pre>\n'
44+
);
45+
};
1346

1447
build(function () {
1548
console.log('Done building.');
@@ -29,14 +62,16 @@ function build(callback) {
2962
// Use the drafts plugin
3063
.use(drafts())
3164

32-
// Use metallic plugin to add syntax highlighting
33-
.use(metallic())
34-
3565
// Use Github Flavored Markdown for content
3666
.use(
3767
markdown({
3868
gfm: true,
3969
tables: true,
70+
highlight: function (code, lang) {
71+
// TODO what if there is no lang? highlightAuto..
72+
return hljs.highlight(lang, code).value;
73+
},
74+
renderer,
4075
}),
4176
)
4277

0 commit comments

Comments
 (0)