Skip to content

Convert spaces to tabs #43

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

Merged
merged 2 commits into from
Dec 24, 2021
Merged
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
43 changes: 39 additions & 4 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var Metalsmith = require('metalsmith'),
metallic = require('metalsmith-metallic'),
drafts = require('metalsmith-drafts'),
layouts = require('metalsmith-layouts'),
markdown = require('metalsmith-markdown'),
Expand All @@ -10,6 +9,40 @@ var Metalsmith = require('metalsmith'),
var serve = require('metalsmith-serve');
var watch = require('metalsmith-watch');
var sass = require('metalsmith-sass');
const hljs = require('highlight.js');
const marked = require('marked');

var renderer = new marked.Renderer();

renderer.code = function (code, infostring, escaped) {
/** Copied from the default implementation, with the following changes.
* 1. Find a series of 4 spaces and convert it into tabs
* 2. Add "hljs" to the code class. I don't get why, but metalsmith-metallic was doing this, so I copied the pattern.
* 3. Remove this.options.langPrefix, because metalsmith-metallic didn't have this.
*/
code = code.replace(/ {4}/g, '\t'); // Convert spaces back into tabs
var lang = (infostring || '').match(/\S*/)[0];
if (this.options.highlight) {
var out = this.options.highlight(code, lang);
if (out != null && out !== code) {
escaped = true;
code = out;
}
}

if (!lang) {
return '<pre><code>' + (escaped ? code : escape(code, true)) + '</code></pre>';
}

return (
'<pre><code class="hljs ' +
// this.options.langPrefix +
escape(lang, true) +
'">' +
(escaped ? code : escape(code, true)) +
'</code></pre>\n'
);
};

build(function () {
console.log('Done building.');
Expand All @@ -29,14 +62,16 @@ function build(callback) {
// Use the drafts plugin
.use(drafts())

// Use metallic plugin to add syntax highlighting
.use(metallic())

// Use Github Flavored Markdown for content
.use(
markdown({
gfm: true,
tables: true,
highlight: function (code, lang) {
// TODO what if there is no lang? highlightAuto..
return hljs.highlight(lang, code).value;
},
renderer,
}),
)

Expand Down
Loading