Skip to content

Highlight all Markdown, not just examples. Fixes #610 #613

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 3 commits into from
Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Highlight all Markdown, not just examples. Fixes #610
  • Loading branch information
tmcw committed Nov 21, 2016
commit 12e45263442c81d1ae09f3020e78f1093f763938
22 changes: 22 additions & 0 deletions lib/output/highlighter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var visit = require('unist-util-visit');
var hljs = require('highlight.js');

/**
* Adapted from remark-highlight.js
* https://github.com/ben-eb/remark-highlight.js
* @param {Object} node AST node
* @returns {undefined} modifies the node by reference
* @private
*/
function visitor(node) {
if (node.lang) {
node.type = 'html';
node.value = '<pre class=\'hljs\'>' +
hljs.highlightAuto(node.value, [node.lang]).value + '</pre>';
}
}

module.exports = function (ast) {
visit(ast, 'code', visitor);
return ast;
};
5 changes: 4 additions & 1 deletion lib/output/util/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var remark = require('remark'),
Syntax = require('doctrine').Syntax,
u = require('unist-builder'),
_rerouteLinks = require('./reroute_links'),
highlighter = require('../highlighter'),
formatType = require('./format_type');

/**
Expand Down Expand Up @@ -46,7 +47,9 @@ module.exports = function (getHref) {
*/
formatters.markdown = function (ast) {
if (ast) {
return remark().use(html).stringify(rerouteLinks(ast));
return remark()
.use(html)
.stringify(highlighter(rerouteLinks(ast)));
}
};

Expand Down
1 change: 1 addition & 0 deletions lib/parse_markdown.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var remark = require('remark');
var inlineTokenizer = require('./inline_tokenizer');


/**
* Parse a string of Markdown into a Remark
* abstract syntax tree.
Expand Down
19 changes: 19 additions & 0 deletions test/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,25 @@ test('invalid arguments', function (group) {
group.end();
});

test('--config', options, function (t) {
var dst = path.join(os.tmpdir(), (Date.now() + Math.random()).toString());
fs.mkdirSync(dst);
var outputIndex = path.join(dst, 'index.html');
var expectedOutputPath = path.join(__dirname, 'fixture/html/nested.config-output.html');
documentation(['build -c fixture/html/documentation.yml -f html fixture/html/nested.input.js -o ' +
dst], function (err) {
console.log(err);
t.notOk(err);
var output = fs.readFileSync(outputIndex, 'utf8');
if (process.env.UPDATE) {
fs.writeFileSync(expectedOutputPath, output);
}
var expectedOutput = fs.readFileSync(expectedOutputPath, 'utf8');
t.equal(expectedOutput, output, 'generates correct output');
t.end();
}, false);
});

test('--version', options, function (t) {
documentation(['--version'], {}, function (err, output) {
t.ok(output, 'outputs version');
Expand Down
22 changes: 22 additions & 0 deletions test/fixture/html/documentation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
toc:
- name: Highlighted section
description: |
The public key is a base64 encoded string of a protobuf containing an RSA DER
buffer. This uses a node buffer to pass the base64 encoded public key protobuf
to the multihash for ID generation.

```js
var PeerId = require('peer-id')

PeerId.create({ bits: 1024 }, (err, id) => {
console.log(JSON.stringify(id.toJSON(), null, 2)
})
```

```
{
"id": "Qma9T5YraSnpRDZqRR4krcSJabThc8nwZuJV3LercPHufi",
"privKey": "CAAS4AQwggJcAgEAAoGBAMBgbIqyOL26oV3nGPBYrdpbv..",
"pubKey": "CAASogEwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMBgbIqyOL26oV3nGPBYrdpbvzCY..."
}
```
Loading