17

For example, if I have a markdown file with the following:

###This is my markdown file
here is some text.
here is my code block:

``` js
var accountdown = require('accountdown');
var level = require('level');
var db = level('/tmp/users.db');

var users = accountdown(db, {
    login: { basic: require('accountdown-basic') }
});

var user = process.argv[2];
var pass = process.argv[3];
var bio = process.argv[4];

var opts = {
    login: { basic: { username: user, password: pass } },
    value: { bio: bio }
};
users.create(user, opts, function (err) {
    if (err) console.error(err);
});
```

How can I enable highlighting in the code block?

Perhaps it's unrelated, but org-mode can highlight source code blocks as follows:

(org-babel-do-load-languages
      'org-babel-load-languages
      '((python . t)
        (js . t)
        (R . t)))
3
  • The best you can do is using gfm-mode for README or more generally, all Markdown files, it will highlight the code blocks in one uniform color. The feature of highlighting them à la org-mode hasn't been implemented yet.
    – wasamasa
    Commented Jun 16, 2015 at 5:46
  • Thanks, that is disappointing but good to know. The feature works great in org-mode, hopefully someone can implement it in gfm-mode
    – modulitos
    Commented Jun 16, 2015 at 9:27
  • 1
    FWIW, I haven't turned this into an answer as one could still rip out the code responsible for this from org-mode and adapt it for your problem to turn it into an answer.
    – wasamasa
    Commented Jun 16, 2015 at 9:29

1 Answer 1

24

Native syntax highlighting of code blocks is now part of the current development version of Markdown mode. Specifically, it works for those GFM or tilde-fenced code blocks for which the language name has been specified, like the JavaScript code block in your question. You can toggle this mode with C-c C-x C-f (markdown-toggle-fontify-code-blocks-natively). You can set the default behavior by customizing the variable markdown-fontify-code-blocks-natively. For example:

(setq markdown-fontify-code-blocks-natively t)
1
  • 4
    Why the hell is this not on by default?
    – psygo
    Commented May 18, 2021 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.