How to use the highlight function from highlight.js
Find comprehensive JavaScript highlight.js.highlight code examples handpicked from public code repositorys.
12 13 14 15 16 17 18 19 20 21
markedConfig: { renderer: (function() { var r = new marked.Renderer(); r.code = function(code, language){ return '<pre><code class="hljs ' + (language || "") + '">' + hljs.highlight("javascript", code).value + '</code></pre>'; }; return r; })()
75
85
16
+ 5 other calls in file
4 5 6 7 8 9 10 11 12 13 14
const checkCapitalization = require('./capitalization'); const log = require('./log'); const TypeUtils = require('./typeUtils'); marked.setOptions({ highlight: (code, lang) => lang && highlight.highlight(code, { language: lang, ignoreIllegals: true, }).value, });
40
76
6
GitHub: kaleguy/leovue
128 129 130 131 132 133 134 135 136 137
break case 'html': text = text.replace(/(@docs|others)/g, '<span class="directive" title="Leo Directive - does not appear in source file.">$1</span>') break case 'htmlsource': muv = hljs.highlight('html', text) text = muv.value text = text.replace(/(@\w+)/, '<span class="directive" title="Leo Directive - does not appear in source file.">$1</span>') text = text.replace(/@others/g, '<span class="directive" title="Leo Directive: rest of tree goes here.">@others</span>') text = `<pre>${text}</pre>`
28
156
10
+ 2 other calls in file
GitHub: MarkBind/markbind
97 98 99 100 101 102 103 104 105 106
the corresponding opening and closing tags for the html string to be well-formed and maintain the correct state per line. Ref: https://github.com/MarkBind/markbind/pull/1521 */ lines = hljs.highlight(str, { language: lang, ignoreIllegals: true }).value.split('\n'); const tokenStack = []; lines = lines.map((line) => { const prepend = tokenStack.map(tok => `<span class="${tok}">`).join('');
105
95
13
+ 5 other calls in file
70 71 72 73 74 75 76 77 78 79
```javascript require(["highlight.js/build/highlight.pack"], function(hljs){ // If you know the language hljs.highlight(lang, code).value; // Automatic language detection hljs.highlightAuto(code).value; });
16
19
31
GitHub: cortex-js/cortexjs.io
70 71 72 73 74 75 76 77 78 79
typographer: true, quotes: '“”‘’', highlight: (str, lang) => { if (lang && hljs.getLanguage(lang)) { try { return hljs.highlight(str, { language: lang }).value; } catch (err) { console.log(err); } }
9
32
2
+ 33 other calls in file
115 116 117 118 119 120 121 122 123 124
return `<div class="mermaid">${text}</div>`; } let hlHtml = ''; if (lang) { hlHtml = hljs.highlight(lang, text).value } else { hlHtml = hljs.highlightAuto(text).value } return `<code class="hljs code-block">${hlHtml}</code>`
13
33
7
+ 9 other calls in file
GitHub: mrvautin/squido
17 18 19 20 21 22 23 24 25 26
const markdown = new markdownIt({ html: true, highlight: (str, lang) => { if(lang && highlightjs.getLanguage(lang)){ try{ return highlightjs.highlight(str, { language: lang }).value; }catch(ex){ console.log('Failed to highlight code block', ex); } }
7
48
3
+ 25 other calls in file
142 143 144 145 146 147 148 149 150 151
'ebnf': 'ebnf', '': '', })[hjsMediaType]; if(hjsLanguage===undefined) throw new Error('Unknown media type '+JSON.stringify(hjsMediaType)+' at tag '+doc.localName); const textContent = doc.getAttribute('w:space')=='indent' ? stripIndent(doc.textContent).trim() : doc.textContent ; const hjsHTML = hjsLanguage ? hjs.highlight(textContent, {language: hjsLanguage}).value : escapeHTML(textContent); return '<pre'+mapAttributes(doc, function(attr){ if(attr.name=='w:space') return ''; return applyTemplate(attr); }).join('')+'>' + hjsHTML + '</'+doc.localName+'>';
1
8
5
GitHub: nihruk/design-system
21 22 23 24 25 26 27 28 29 30
throwOnUndefined: true, }, ) ) environment._currentPageUrlPath = null; environment.addFilter('highlight', (code, language) => highlight.highlight(code, {language}).value) environment.addFilter('prettier', (code, parser) => prettier.format(code, {parser})) environment.addTest('activeCurrentUrl', urlPath => { if (!environment._currentPageUrlPath) { throw new Error('No page is being templated right now.')
3
3
2
+ 10 other calls in file
59 60 61 62 63 64 65 66 67
let html = marked( md, { highlight: function(code, lang){ let ret; if( lang ){ ret = hljs.highlight(lang, code).value; } else { ret = hljs.highlightAuto(code).value; }
3
3
14
+ 19 other calls in file
GitHub: cnshsliu/smp.nvim
296 297 298 299 300 301 302 303 304 305
renderer: renderer, Lexer: CustomLexer, highlight: function (code, lang) { const hljs = require('highlight.js'); const language = hljs.getLanguage(lang) ? lang : 'plaintext'; return hljs.highlight(code, { language }).value; }, langPrefix: 'hljs language-', // highlight.js css expects a top-level 'hljs' class. pedantic: false, gfm: true,
0
4
1
+ 11 other calls in file
GitHub: epsylon/oasis
422 423 424 425 426 427 428 429 430 431
const articleElement = markdownContent === emptyContent ? article( { class: "content" }, pre({ innerHTML: highlightJs.highlight( JSON.stringify(msg, null, 2), {language: "json", ignoreIllegals: true} ).value, })
1
7
1
GitHub: arangodb/docs-hugo
40 41 42 43 44 45 46 47 48 49 50 51
} // Normalize language language = normalize(language); try { return hljs.highlight(code, {language}).value; } catch(e) { } return code; }
0
2
4
GitHub: RadiumAg/radium-vue
13 14 15 16 17 18 19 20 21
let components = {}; const highlight = (str, lang) => { if (!lang || !hljs.getLanguage(lang)) { return '<pre><code class="hljs">' + str + '</code></pre>'; } const html = hljs.highlight(lang, str, true, undefined).value; return `<pre><code class="hljs language-${lang}">${html}</code></pre>`; }; const md = new Config();
0
2
1
+ 13 other calls in file
GitHub: eluv-io/elv-ramdoc
144 145 146 147 148 149 150 151 152 153 154
const _prettifyCode = pipe( join('\n'), split('\n'), map(_exampleSplitMultilineOutput), join('\n'), s => hljs.highlight(s, {language: 'javascript'}).value ) /** * Replaces '...' with '…' and '->' with '→'.
0
2
0
+ 4 other calls in file
GitHub: niltok/iota
76 77 78 79 80 81 82 83 84 85
codeText += code if (typeof lang == 'undefined' || lang == '') return hljs.highlightAuto(code).value else if (lang == 'nohighlight') return code else return hljs.highlight(code, { language: lang }).value } }))) }
0
2
1
+ 28 other calls in file
10 11 12 13 14 15 16 17 18 19 20
// 配置markdown-it常规代码高亮,相关文档:https://markdown-it.github.io/markdown-it/#MarkdownIt.new const highlight = (str, lang) => { if (!lang || !hljs.getLanguage(lang)) { return '<pre><code class="hljs">' + str + '</code></pre>' } const html = hljs.highlight(lang, str, true, undefined).value return `<pre><code class="hljs language-${lang}">${html}</code></pre>` } config.options
0
1
1
+ 2 other calls in file
182 183 184 185 186 187 188 189 190 191
`No language set in ${filename}, ` + `line ${node.position.start.line}`); } const language = (node.lang || '').split(' ')[0]; const highlighted = getLanguage(language) ? highlight(language, node.value).value : node.value; node.type = 'html'; node.value = '<pre>' + `<code class = 'language-${node.lang}'>` +
2
0
2
GitHub: ancientec/subeditor
6 7 8 9 10 11 12 13 14 15
const { disposeEmitNodes } = require('typescript'); const md = require('markdown-it')({ highlight: function (str, lang ) { if (lang && hljs.getLanguage(lang)) { try { return hljs.highlight(str, {"language": lang, "ignoreIllegals" : true }).value; } catch (__) {} } return ''; }
2
1
1
highlight.js.highlight is the most popular function in highlight.js (665 examples)