Browse Source

Add lang str remainder to highlight callback

``` 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 https://github.com/markdown-it/markdown-it/issues/626
close https://github.com/markdown-it/markdown-it/pull/706
pull/713/head
Alex Kocharin 4 years ago
parent
commit
866fba34a2
  1. 9
      lib/renderer.js
  2. 12
      test/misc.js

9
lib/renderer.js

@ -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);
}

12
test/misc.js

@ -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 });

Loading…
Cancel
Save