How to use the highlight function from prismjs
Find comprehensive JavaScript prismjs.highlight code examples handpicked from public code repositorys.
GitHub: YMFE/ydoc
16 17 18 19 20 21 22 23 24 25
// html 的高亮使用 haml 语法 if (lang.toLocaleLowerCase() === 'html') { lang = 'haml'; } loadLanguages([lang]); return prism.highlight(str, prism.languages[lang], lang); } return he.encode(str); } catch (err) { return he.encode(str);
124
659
33
GitHub: Houfeng/mditor
84 85 86 87 88 89 90 91 92 93
if (Parser.highlights[lang]) { let result = Parser.highlights[lang].parse(code, lang, callback); if (!callback) return result; } else if (Prism.languages[lang]) { let result = Prism.highlight(code, Prism.languages[lang]); if (callback) return callback(null, result); else return result;
81
531
27
32 33 34 35 36 37 38 39 40 41
// Highlight all pre tags const Prism = require('prismjs'); const pres = document.getElementsByTagName('pre'); for(let i = 0; i < pres.length; i++) { pres[i].firstChild.innerHTML = Prism.highlight(pres[i].firstChild.textContent, Prism.languages.javascript) } const tocToggle = document.getElementById('tocToggle'); const toc = document.getElementById('tocContainer');
44
360
29
+ 3 other calls in file
46 47 48 49 50 51 52 53 54 55
} catch (e) { console.warn(`[vuepress] Syntax highlight for language "${lang}" is not supported.`) } } if (prism.languages[lang]) { const code = prism.highlight(str, prism.languages[lang], lang) return wrap(code, rawLang) } return wrap(str, 'text') }
30
125
1
+ 3 other calls in file
GitHub: scullyio/scully
46 47 48 49 50 51 52 53 54 55
Note that this is a sample the actual syntax might be slightly different --------------------------------------------------------------------------------------- `); return code; } return Prism.highlight(code, Prism.languages[lang]); }, pedantic: false, gfm: true, breaks: false,
257
0
43
GitHub: cerebral/cerebral
7 8 9 10 11 12 13 14 15 16
prismJsxTs(Prism) const compile = marksy({ createElement: React.createElement, highlight(language, code) { return Prism.highlight(code, Prism.languages[language]) }, components: { Views() { return (
132
0
48
GitHub: ffoodd/a11y.css
87 88 89 90 91 92 93 94 95 96
// CSS code blocks const stylesRegex = /((<pre><code class="css language-css">)(.[\s\S]+?)(\/code><\/pre>))/gm const cssRegex = /((?<=<code class="css language-css">)(.[\s\S]+?)(?=<\/code>))/gm let cssContent = processedContent.match(cssRegex) cssContent = String(cssContent).replace(/(>)+/g, '>') let processedCSS = prism.highlight(String(cssContent), prism.languages.css, 'css-extras') processedContent = processedContent.replace(stylesRegex, `<div class="pre"><pre><code class="css language-css" data-language="CSS">${processedCSS}</code></pre></div>`) // SCSS code blocks const scssBlockRegex = /((<pre><code class="scss language-scss">)(.[\s\S]+?)(\/code><\/pre>))/gm
94
0
40
+ 7 other calls in file
GitHub: ritsok/clay-starter
5 6 7 8 9 10 11 12 13
module.exports.save = (uri, data) => { // Adds manual spaces, Kiln codemirror doesn't recognizes tab spaces data.code = data.code.replace(/\t/g, ' '); // Returns a highlighted HTML string data.html = Prism.highlight(data.code, Prism.languages[data.language], data.language); return data; };
17
0
0
+ 3 other calls in file
GitHub: mapbox/mr-ui
37 38 39 40 41 42 43 44 45 46
.replace(/from '\.\.\/(\.\.\/)?/g, `from '@mapbox/mr-ui/`) // Remove block comments (especially the description). .replace(/\/\*[\s\S]*?\*\/[\s]*/, '') .trim(); const highlightedCode = Prism.highlight(code, Prism.languages.jsx, 'jsx'); const renderedDescription = encodeJsx( jsxtremeMarkdown.toJsx(descriptionMatch[1].trim()) ); return `{
6
84
85
228 229 230 231 232 233 234 235 236 237
} else { lang = aliases[lang] || lang require(`prismjs/components/prism-${lang}.js`) if (Prism.languages[lang]) { return Prism.highlight(str, Prism.languages[lang]) } else { return str } }
44
713
8
101 102 103 104 105 106 107 108 109 110
module.exports = { markdown: { markdownit: { highlight: (code, lang) => { lang = lang || 'markup' return Prism.highlight(code, Prism.languages[lang], lang) }, } }, // ...
4
0
1
+ 3 other calls in file
GitHub: ytkj/prism-loader
54 55 56 57 58 59 60 61 62 63
Prism.hooks.run('complete', env); return; } Prism.hooks.run('before-highlight', env); let highlightedCode = Prism.highlight(code, grammar); env.highlightedCode = highlightedCode; Prism.hooks.run('before-insert', env); $element.text(highlightedCode);
3
4
2
302 303 304 305 306 307 308 309 310 311
display, }; }); const renderCode = vue.computed(() => { convertCode(); const html = Prism.highlight(convertedCode.value, Prism.languages[props.lang], props.lang); return html; }); const tabClasses = vue.computed(() => { const theme = useTheme.value === '' || useTheme.value === 'prism' ? 'default' : useTheme.value;
0
1
1
10 11 12 13 14 15 16 17 18
let html; if (lang === 'text') { html = str; } else { html = Prism.highlight(str, Prism.languages[lang], '' + lang + ''); } const lines = html.split('\n').slice(0, -1); // The last line is empty.
0
0
1
+ 4 other calls in file
prismjs.highlight is the most popular function in prismjs (55 examples)