Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
feat: treat bash the same as sh for specifying code fence language (
Browse files Browse the repository at this point in the history
  • Loading branch information
revin authored and bcoe committed Aug 4, 2017
1 parent 3cf4691 commit 8ba4368
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
32 changes: 32 additions & 0 deletions lib/plugin/language-alias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This plugin allows for code fences written in certain languages to be rendered
// as though they'd been written in a different language, e.g.,
//
// ```bash
// $ marky-markdown test.md
// ```
//
// can have the exact same output as
//
// ```sh
// $ marky-markdown test.md
// ```
//
// (doing the normal thing and setting up a syntax highlighter mapping in our
// render module would almost work, except the wrapping <div> still has the
// "bash" class applied)
//

var plugin = module.exports = function (md, opts) {
var previousFenceRule = md.renderer.rules.fence
md.renderer.rules.fence = function (tokens, idx, options, env, slf) {
// code fence language marker is in token.info; do we have an alias?
if (languageAliases.hasOwnProperty(tokens[idx].info)) {
tokens[idx].info = languageAliases[tokens[idx].info]
}
return previousFenceRule.call(this, tokens, idx, options, env, slf)
}
}

var languageAliases = plugin.languageAliases = {
bash: 'sh'
}
6 changes: 5 additions & 1 deletion lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var youtube = require('./plugin/youtube')
var cdnImages = require('./plugin/cdn')
var packagize = require('./plugin/packagize')
var htmlHeading = require('./plugin/html-heading')
var fenceLanguageAliasing = require('./plugin/language-alias')
var relaxedLinkRefs = require('./gfm/relaxed-link-reference')
var githubHeadings = require('./gfm/indented-headings')
var overrideLinkDestinationParser = require('./gfm/override-link-destination-parser')
Expand Down Expand Up @@ -87,7 +88,10 @@ render.getParser = function (options) {
.use(looseLinkParsing)
.use(looseImageParsing)

if (options.highlightSyntax) parser.use(codeWrap)
if (options.highlightSyntax) {
parser.use(codeWrap)
.use(fenceLanguageAliasing)
}
if (options.serveImagesWithCDN) parser.use(cdnImages, {package: options.package})

return githubLinkify(parser)
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ app.listen(3000)
echo hi
```

```bash
echo hi
```

```coffeescript
alert "hi"
```
Expand Down
6 changes: 6 additions & 0 deletions test/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ describe('markdown processing', function () {
assert($('.highlight.sh').length)
})

it('adds sh class to bash blocks', function () {
assert(~fixtures.basic.indexOf('```bash'))
assert($('.highlight.sh').length)
assert.equal($('.highlight.bash').length, 0)
})

it('adds coffeescript class to coffee blocks', function () {
assert(~fixtures.basic.indexOf('```coffee'))
assert($('.highlight.coffeescript').length)
Expand Down

0 comments on commit 8ba4368

Please sign in to comment.