How to use the highlight function from prismjs

Find comprehensive JavaScript prismjs.highlight code examples handpicked from public code repositorys.

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);
fork icon124
star icon659
watch icon33

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;
fork icon81
star icon531
watch icon27

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');
fork icon44
star icon360
watch icon29

+ 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')
}
fork icon30
star icon125
watch icon1

+ 3 other calls in file

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,
fork icon257
star icon0
watch icon43

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 (
fork icon132
star icon0
watch icon48

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(/(&gt;)+/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
fork icon94
star icon0
watch icon40

+ 7 other calls in file

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;
};
fork icon17
star icon0
watch icon0

+ 3 other calls in file

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 `{
fork icon6
star icon84
watch icon85

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
  }
}
fork icon44
star icon713
watch icon8

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)
      },
    }
  },
  // ...
fork icon4
star icon0
watch icon1

+ 3 other calls in file

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);
fork icon3
star icon4
watch icon2

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;
fork icon0
star icon1
watch icon1

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.
fork icon0
star icon0
watch icon1

+ 4 other calls in file