From 866fba34a2f5eca294ec13b498ea72f1915b2421 Mon Sep 17 00:00:00 2001 From: Alex Kocharin Date: Mon, 14 Sep 2020 19:38:30 +0300 Subject: [PATCH] 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 --- lib/renderer.js | 9 ++++++--- test/misc.js | 12 ++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/renderer.js b/lib/renderer.js index 6a9e5244c..d19b95449 100644 --- a/lib/renderer.js +++ b/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); } diff --git a/test/misc.js b/test/misc.js index c933afa9b..e71ad3583 100644 --- a/test/misc.js +++ b/test/misc.js @@ -62,6 +62,18 @@ describe('API', function () { assert.strictEqual(md.render('```\n&\n```'), '
&\n
\n'); }); + it('highlight arguments', function () { + var md = markdownit({ + highlight: function (str, lang, attrs) { + assert.strictEqual(lang, 'a'); + assert.strictEqual(attrs, 'b c d'); + return '
==' + str + '==
'; + } + }); + + assert.strictEqual(md.render('``` a b c d \nhl\n```'), '
==hl\n==
\n'); + }); + it('force hardbreaks', function () { var md = markdownit({ breaks: true });