MCPcopy
hub / github.com/ccampbell/rainbow / getLanguageForBlock

Function getLanguageForBlock

src/util.js:17–44  ·  view source on GitHub ↗
(block)

Source from the content-addressed store, hash-verified

15 * @return {string|null}
16 */
17export function getLanguageForBlock(block) {
18
19 // If this doesn't have a language but the parent does then use that.
20 //
21 // This means if for example you have: <pre data-language="php">
22 // with a bunch of <code> blocks inside then you do not have
23 // to specify the language for each block.
24 let language = block.getAttribute('data-language') || block.parentNode.getAttribute('data-language');
25
26 // This adds support for specifying language via a CSS class.
27 //
28 // You can use the Google Code Prettify style: <pre class="lang-php">
29 // or the HTML5 style: <pre><code class="language-php">
30 if (!language) {
31 const pattern = /\blang(?:uage)?-(\w+)/;
32 const match = block.className.match(pattern) || block.parentNode.className.match(pattern);
33
34 if (match) {
35 language = match[1];
36 }
37 }
38
39 if (language) {
40 return language.toLowerCase();
41 }
42
43 return null;
44}
45
46/**
47 * Determines if two different matches have complete overlap with each other

Callers 1

_highlightCodeBlocksFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected