Skip to content

Commit 7f400da

Browse files
authored
Highlight all Markdown, not just examples. Fixes #610 (#613)
* Highlight all Markdown, not just examples. Fixes #610 * Fix html output fixture * Remove unrelated change
1 parent 70c081f commit 7f400da

File tree

7 files changed

+1375
-3
lines changed

7 files changed

+1375
-3
lines changed

lib/output/highlighter.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var visit = require('unist-util-visit');
2+
var hljs = require('highlight.js');
3+
4+
/**
5+
* Adapted from remark-highlight.js
6+
* https://github.com/ben-eb/remark-highlight.js
7+
* @param {Object} node AST node
8+
* @returns {undefined} modifies the node by reference
9+
* @private
10+
*/
11+
function visitor(node) {
12+
if (node.lang) {
13+
node.type = 'html';
14+
node.value = '<pre class=\'hljs\'>' +
15+
hljs.highlightAuto(node.value, [node.lang]).value + '</pre>';
16+
}
17+
}
18+
19+
module.exports = function (ast) {
20+
visit(ast, 'code', visitor);
21+
return ast;
22+
};

lib/output/util/formatters.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var remark = require('remark'),
33
Syntax = require('doctrine').Syntax,
44
u = require('unist-builder'),
55
_rerouteLinks = require('./reroute_links'),
6+
highlighter = require('../highlighter'),
67
formatType = require('./format_type');
78

89
/**
@@ -46,7 +47,9 @@ module.exports = function (getHref) {
4647
*/
4748
formatters.markdown = function (ast) {
4849
if (ast) {
49-
return remark().use(html).stringify(rerouteLinks(ast));
50+
return remark()
51+
.use(html)
52+
.stringify(highlighter(rerouteLinks(ast)));
5053
}
5154
};
5255

test/bin.js

+18
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,24 @@ test('invalid arguments', function (group) {
182182
group.end();
183183
});
184184

185+
test('--config', options, function (t) {
186+
var dst = path.join(os.tmpdir(), (Date.now() + Math.random()).toString());
187+
fs.mkdirSync(dst);
188+
var outputIndex = path.join(dst, 'index.html');
189+
var expectedOutputPath = path.join(__dirname, 'fixture/html/nested.config-output.html');
190+
documentation(['build -c fixture/html/documentation.yml -f html fixture/html/nested.input.js -o ' +
191+
dst], function (err) {
192+
t.notOk(err);
193+
var output = fs.readFileSync(outputIndex, 'utf8');
194+
if (process.env.UPDATE) {
195+
fs.writeFileSync(expectedOutputPath, output);
196+
}
197+
var expectedOutput = fs.readFileSync(expectedOutputPath, 'utf8');
198+
t.equal(expectedOutput, output, 'generates correct output');
199+
t.end();
200+
}, false);
201+
});
202+
185203
test('--version', options, function (t) {
186204
documentation(['--version'], {}, function (err, output) {
187205
t.ok(output, 'outputs version');

test/fixture/html/documentation.yml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
toc:
2+
- name: Highlighted section
3+
description: |
4+
The public key is a base64 encoded string of a protobuf containing an RSA DER
5+
buffer. This uses a node buffer to pass the base64 encoded public key protobuf
6+
to the multihash for ID generation.
7+
8+
```js
9+
var PeerId = require('peer-id')
10+
11+
PeerId.create({ bits: 1024 }, (err, id) => {
12+
console.log(JSON.stringify(id.toJSON(), null, 2)
13+
})
14+
```
15+
16+
```
17+
{
18+
"id": "Qma9T5YraSnpRDZqRR4krcSJabThc8nwZuJV3LercPHufi",
19+
"privKey": "CAAS4AQwggJcAgEAAoGBAMBgbIqyOL26oV3nGPBYrdpbv..",
20+
"pubKey": "CAASogEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMBgbIqyOL26oV3nGPBYrdpbvzCY..."
21+
}
22+
```

0 commit comments

Comments
 (0)