Skip to content

Commit eda3a4b

Browse files
authored
Merge pull request #52 from rackerlabs/hljs-syntax-highlighting
feat(syntaxHighlight): Initial Helix theme for Highlight.js
2 parents ec8fe70 + f483b3d commit eda3a4b

File tree

16 files changed

+471
-298
lines changed

16 files changed

+471
-298
lines changed

_config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ CONFIG.docs = {
7777
CONFIG.copy = {
7878
dirs: [
7979
'images',
80-
'vendor',
8180
]
8281
};
8382

lib/nunjucks/code.tag.js

Lines changed: 27 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@
55
const hljs = require('highlight.js');
66
const nunjucks = require('nunjucks');
77

8+
function highlight (code, lang) {
9+
if (hljs.getLanguage(lang)) {
10+
return hljs.highlight(lang, code).value;
11+
} else {
12+
return hljs.highlightAuto(code).value;
13+
}
14+
}//highlight()
15+
16+
function stripIndent (str) {
17+
const match = str.match(/^[ \t]*(?=\S)/gm);
18+
19+
if (!match) {
20+
return str;
21+
}
22+
23+
const indent = Math.min.apply(Math, match.map(x => x.length));
24+
const re = new RegExp(`^[ \\t]{${indent}}`, 'gm');
25+
26+
return indent > 0 ? str.replace(re, '') : str;
27+
}//stripIndent()
28+
829
function CodeTag () {
930
this.tags = ['code'];
1031

@@ -20,70 +41,17 @@ function CodeTag () {
2041
// parse the body and possibly the error block, which is optional
2142
var body = parser.parseUntilBlocks('endcode');
2243

23-
// I found Nunjucks to be incredibly convoluted on how to just get some data into the BlockTag function,
24-
// this finally worked by faking another template node.
25-
var tabStart = new nodes.NodeList(0, 0, [
26-
new nodes.Output(0, 0, [
27-
new nodes.TemplateData(0, 0, (tok.colno - 1))
28-
])
29-
]);
30-
3144
parser.advanceAfterBlockEnd();
3245

3346
// See above for notes about CallExtension
34-
return new nodes.CallExtension(this, 'render', args, [body, tabStart]);
47+
return new nodes.CallExtension(this, 'render', args, [body]);
3548
};//parse()
3649

37-
// code rendering for the block. Pretty simple, just get the body text and pass
38-
// it through the code renderer.
39-
this.render = function (context, blockBody, blockTabStart) {
40-
let body = blockBody();
41-
let spacesRegex = /^[\s]+/;
42-
let tabStart = blockTabStart(); // The column postion of the {% code %} tag.
43-
44-
// TODO: Update to automatically normalize indentation to least indentation needed.
45-
// - This still adds unnecessary indentation to beginning of output.
46-
if (tabStart > 0) { // If the {% code %} tag is tabbed in, normalize the content to the same depth.
47-
body = body.split(/\r?\n/); // Split into lines.
48-
body = body.map( line => {
49-
let startSpaces = line.match(spacesRegex);
50-
// If the content is not at the same or greater tab depth, do nothing..
51-
if (startSpaces && startSpaces[0].length >= tabStart) {
52-
return line.slice(tabStart); // Subtract the column position from the start of the string.
53-
} else {
54-
return line;
55-
}
56-
});
57-
58-
// Remove blank start line
59-
if (body[0] === '') {
60-
body = body.slice(1);
61-
}
62-
// Remove blank end line
63-
if (body[body.length - 1] === '') {
64-
body = body.slice(0, body.length - 1);
65-
}
66-
67-
body = body.join('\n'); // Rejoin into one string.
68-
}
69-
70-
// remove line break from start and end of string
71-
// <http://stackoverflow.com/a/14572494>
72-
body = body.replace(/^\s+|\s+$/, '');
73-
74-
// get the first line of the body to see if we have a file type specified
75-
var value = hljs.highlightAuto(body).value;
76-
if (body.split('\n').length > 1) {
77-
var lang = hljs.getLanguage(body.split("\n")[0]);
78-
if (lang) {
79-
lang = body.split('\n')[0];
80-
body = body.split('\n').slice(1).join('\n');
81-
value = hljs.highlight(lang, body).value;
82-
}
83-
}
84-
85-
let template = `<pre class="highlight"><code class="hljs">${value}</code></pre>`;
86-
50+
// Code rendering for the block.
51+
this.render = function (context, lang, blockBody) {
52+
let body = stripIndent(blockBody());
53+
let highlightedBody = highlight(body, lang);
54+
let template = `<pre><code class="${lang}">${highlightedBody}</code></pre>`;
8755
return new nunjucks.runtime.SafeString(template);
8856
};//render();
8957
}//CodeTag

source/_less/scaffold.less

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,3 @@
22
html {
33
font-family: "Roboto", sans-serif;
44
}
5-
6-
pre,
7-
code {
8-
font-family: "Roboto Mono", monospace;
9-
}
10-
11-
//* ===== VERTICAL SPACING ===== */
12-
address,
13-
article,
14-
aside,
15-
details,
16-
fieldset,
17-
figure,
18-
footer,
19-
form,
20-
h1,
21-
h2,
22-
h3,
23-
h4,
24-
h5,
25-
h6,
26-
header,
27-
hgroup,
28-
main,
29-
nav,
30-
section,
31-
table {
32-
// Apply top margin to:
33-
// - "sectioning" elements (https://goo.gl/7uHQM0)
34-
// - <details>
35-
// - <figure>
36-
// - <table>
37-
// - <form>
38-
// - <fieldset>
39-
// - <main>
40-
//margin-bottom: 1.5em; /* ~20px */
41-
42-
// Ensure that the first child element
43-
// doesn't extend the top margin.
44-
//& > *:first-child {
45-
// margin-top: 0;
46-
//}
47-
}
48-
49-
hr {
50-
//margin-top: 1.5em;
51-
}

source/_templates/partials/head.njk

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<head>
2-
<title>{% if page.title %}{{page.title}} | {% endif %}{{site.title}}</title>
2+
<title>{% if page.title %}{{page.title}} - {% endif %}{{site.title}}</title>
33
<meta charset="utf-8">
44
<meta name="viewport" content="width=device-width, initial-scale=1">
55
<base href="{{site.baseHref}}">
@@ -13,7 +13,6 @@
1313
crossorigin="anonymous">
1414

1515
<link rel="stylesheet" href="bootstrap.helix.css">
16-
<link rel="stylesheet" href="vendor/hljs.agate.css">
1716
<link rel="stylesheet" href="helix-ui.css">
1817
<link rel="stylesheet" href="docs.css">
1918

source/components/alert/index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ <h2 id="default">Default</h2>
2727
</div>
2828
</div>
2929
</div>
30-
{% code %}html
30+
{% code 'html' %}
3131
<div role="status" class="hxAlerts" aria-live="polite">
3232
<div class="hxAlert">
3333
<div class="hxAlert__icon">
@@ -66,7 +66,7 @@ <h2 id="error">Error</h2>
6666
</div>
6767
</div>
6868
</div>
69-
{% code %}html
69+
{% code 'html' %}
7070
<div role="status" class="hxAlerts" aria-live="polite">
7171
<div class="hxAlert hxAlert--error">
7272
<div class="hxAlert__icon">
@@ -105,7 +105,7 @@ <h2 id="warning">Warning</h2>
105105
</div>
106106
</div>
107107
</div>
108-
{% code %}html
108+
{% code 'html' %}
109109
<div role="status" class="hxAlerts" aria-live="polite">
110110
<div class="hxAlert hxAlert--warning">
111111
<div class="hxAlert__icon">
@@ -144,7 +144,7 @@ <h2 id="success">Success</h2>
144144
</div>
145145
</div>
146146
</div>
147-
{% code %}html
147+
{% code 'html' %}
148148
<div role="status" class="hxAlerts" aria-live="polite">
149149
<div class="hxAlert hxAlert--success">
150150
<div class="hxAlert__icon">

source/components/breadcrumb/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</nav>
2828
</div>
2929

30-
{% code %}html
30+
{% code 'html' %}
3131
<nav class="hxBreadcrumb">
3232
<a href="#">Home</a>
3333
</nav>

0 commit comments

Comments
 (0)