diff --git a/README.md b/README.md index a4e55a60..d041c298 100644 --- a/README.md +++ b/README.md @@ -290,6 +290,7 @@ Option | Description | Default `tab`| Replace tabs | `autoDetect` | Detect language automatically (warning: slow)
_Sublanguage highlight requires `autoDetect` to be enabled and `lang` to be unset_ | false `mark` | Line highlight specific line(s) | +`languageAttr` | Output code language into `data-language` attr | false ### htmlTag(tag, attrs, text, escape) diff --git a/lib/highlight.js b/lib/highlight.js index b267fbaa..eee871b4 100644 --- a/lib/highlight.js +++ b/lib/highlight.js @@ -14,6 +14,7 @@ function highlightUtil(str, options = {}) { firstLine = 1, caption, mark = [], + languageAttr = false, tab } = options; let { wrap = true } = options; @@ -26,7 +27,7 @@ function highlightUtil(str, options = {}) { if (gutter && !wrap) wrap = true; // arbitrate conflict ("gutter:true" takes priority over "wrap:false") - const before = useHljs ? `
` : '
';
+  const before = useHljs ? `
` : '
';
   const after = useHljs ? '
' : '
'; @@ -50,10 +51,10 @@ function highlightUtil(str, options = {}) { if (!wrap) { // if original content has one trailing newline, replace it only once, else remove all trailing newlines content = /\r?\n$/.test(data.value) ? content.replace(/\n$/, '') : content.trimEnd(); - return `
${codeCaption}${content}
`; + return `
${codeCaption}${content}
`; } - let result = `
`; + let result = `
`; result += codeCaption; diff --git a/test/highlight.spec.js b/test/highlight.spec.js index 6e215c0e..0ee4e8df 100644 --- a/test/highlight.spec.js +++ b/test/highlight.spec.js @@ -428,4 +428,34 @@ describe('highlight', () => { result.should.include('class="hljs-keyword"'); validateHtmlAsync(result, done); }); + + it('languageAttr: true', done => { + const str = [ + 'var string = `', + ' Multi', + ' line', + ' string', + '`' + ].join('\n'); + + const result = highlight(str, {languageAttr: true, lang: 'js'}); + result.should.eql([ + '
', + gutter(1, 5), + code('var string = `\n Multi\n line\n string\n`', null), + end + ].join('')); + validateHtmlAsync(result, done); + }); + + it('languageAttr: true (wrap: false)', done => { + const result = highlight(testString, {gutter: false, wrap: false, languageAttr: true}); + console.log(result); + result.should.eql([ + '
',
+      entities.encode(testString),
+      '
' + ].join('')); + validateHtmlAsync(result, done); + }); });