Skip to content

Commit

Permalink
Add lang str remainder to highlight callback
Browse files Browse the repository at this point in the history
    ``` javascript {line-numbers=5 highlight=14-17}
    test
    ```

This markup now calls `highlight` like this:

    require('markdown-it')({
      highlight(code, lang, attrs) {
        assert(code === 'test')
        assert(lang === 'javascript')
        assert(attrs === '{line-numbers=5 highlight=14-17}')
      }
    })

close #626
close #706
  • Loading branch information
rlidwka committed Sep 15, 2020
1 parent 3021a52 commit 866fba3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/renderer.js
Expand Up @@ -40,14 +40,17 @@ default_rules.fence = function (tokens, idx, options, env, slf) {
var token = tokens[idx],
info = token.info ? unescapeAll(token.info).trim() : '',
langName = '',
highlighted, i, tmpAttrs, tmpToken;
langAttrs = '',
highlighted, i, arr, tmpAttrs, tmpToken;

if (info) {
langName = info.split(/\s+/g)[0];
arr = info.split(/(\s+)/g);
langName = arr[0];
langAttrs = arr.slice(2).join('');
}

if (options.highlight) {
highlighted = options.highlight(token.content, langName) || escapeHtml(token.content);
highlighted = options.highlight(token.content, langName, langAttrs) || escapeHtml(token.content);
} else {
highlighted = escapeHtml(token.content);
}
Expand Down
12 changes: 12 additions & 0 deletions test/misc.js
Expand Up @@ -62,6 +62,18 @@ describe('API', function () {
assert.strictEqual(md.render('```\n&\n```'), '<pre><code>&amp;\n</code></pre>\n');
});

it('highlight arguments', function () {
var md = markdownit({
highlight: function (str, lang, attrs) {
assert.strictEqual(lang, 'a');
assert.strictEqual(attrs, 'b c d');
return '<pre><code>==' + str + '==</code></pre>';
}
});

assert.strictEqual(md.render('``` a b c d \nhl\n```'), '<pre><code>==hl\n==</code></pre>\n');
});

it('force hardbreaks', function () {
var md = markdownit({ breaks: true });

Expand Down

0 comments on commit 866fba3

Please sign in to comment.