[Edit]
+
0
-
0

CodeMirror 6 - highlight source code under Node.js (run under node)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
// NOTES: // - In this snippet you can find example that shows how to create read-only CodeMirror 6 and run it inder Node.js. // - CodeMirror 6 contributors recommend to use `@lezer/highlight` package to highlight source code outside web browser (outside @codemirror/view). // -- dependencies ------------------------------------------- const {highlightTree, classHighlighter} = require('@lezer/highlight'); // npm install @lezer/highlight const {javascript: JAVASCRIPT_SUPPORT} = require('@codemirror/lang-javascript'); // npm install @codemirror/lang-javascript const {json: JSON_SUPPORT} = require('@codemirror/lang-json'); // npm install @codemirror/lang-json const {css: CSS_SUPPORT} = require('@codemirror/lang-css'); // npm install @codemirror/lang-css const {html: HTML_SUPPORT} = require('@codemirror/lang-html'); // npm install @codemirror/lang-html const {xml: XML_SUPPORT} = require('@codemirror/lang-xml'); // npm install @codemirror/lang-xml const {cpp: CPP_SUPPORT} = require('@codemirror/lang-cpp'); // npm install @codemirror/lang-cpp const {java: JAVA_SUPPORT} = require('@codemirror/lang-java'); // npm install @codemirror/lang-java const {php: PHP_SUPPORT} = require('@codemirror/lang-php'); // npm install @codemirror/lang-php const {python: PYTHON_SUPPORT} = require('@codemirror/lang-python'); // npm install @codemirror/lang-python const {rust: RUST_SUPPORT} = require('@codemirror/lang-rust'); // npm install @codemirror/lang-rust const {sql: SQL_SUPPORT} = require('@codemirror/lang-sql'); // npm install @codemirror/lang-sql const {markdown: MARKDOWN_SUPPORT} = require('@codemirror/lang-markdown'); // npm install @codemirror/lang-markdown // // Hint: check https://codemirror.net website to find language supports (`Language Support` section) // also https://github.com/orgs/codemirror/repositories?q=lang // -- utils -------------------------------------------------- // Source: https://dirask.com/snippets/Node-js-escape-HTML-special-characters-DWBJG1 // const escapeHtml = (html) => { let result = ''; for (const character of html) { switch (character) { // Warning: do not change cases order case '&': result += '&amp;'; break; case '<': result += '&lt;'; break; case '>': result += '&gt;'; break; case '"': result += '&quot;'; break; case '\'': result += '&#039;'; break; default: result += character; } } return result; }; // Source: https://dirask.com/snippets/JavaScript-iterate-through-lines-in-indicated-text-used-newline-symbols-r-n-n-r-n-r-jmLoAp // const iterateLines = (text, callback) => { let pointer = 0; for (let i = 0; i < text.length; i += 1) { switch (text[i]) { case '\r': callback(pointer, i); if (text[i + 1] === '\n') { i += 1; } pointer = i + 1; break; case '\n': callback(pointer, i); if (text[i + 1] === '\r') { i += 1; } pointer = i + 1; break; } } callback(pointer, text.length); }; // -- CodeMirror --------------------------------------------- const LANGUAGE_SUPPORTS = { 'JavaScript': () => JAVASCRIPT_SUPPORT(), 'TypeScript': () => JAVASCRIPT_SUPPORT({ typescript: true }), 'JSX': () => JAVASCRIPT_SUPPORT({ jsx: true }), 'TSX': () => JAVASCRIPT_SUPPORT({ typescript: true, jsx: true }), 'JSON': () => JSON_SUPPORT(), 'CSS': () => CSS_SUPPORT(), 'HTML': () => HTML_SUPPORT(), 'XML': () => XML_SUPPORT(), 'CPP': () => CPP_SUPPORT(), 'Java': () => JAVA_SUPPORT(), 'PHP': () => PHP_SUPPORT(), 'Python': () => PYTHON_SUPPORT(), 'Rust': () => RUST_SUPPORT(), 'SQL': () => SQL_SUPPORT(), 'Markdown': () => MARKDOWN_SUPPORT() // // Hint: check https://codemirror.net website to find language supports (`Language Support` section) }; const createTree = (language, code) => { const creator = LANGUAGE_SUPPORTS[language]; if (creator) { const support = creator(); return support.language.parser.parse(code); } return null; }; const renderGutters = (code) => { let result = ''; let counter = 0; iterateLines(code, () => { if (result) { result += '\n'; } result += String(++counter); }); return result; }; const renderContent = (language, code) => { const tree = createTree(language, code); if (tree) { let result = ''; let index = 0; const construct = (from, to, token) => { if (index < from) { result += escapeHtml(code.substring(index, from)); } result += `<span class="${token}">${escapeHtml(code.substring(from, to))}</span>`; index = to; }; highlightTree(tree, classHighlighter, construct); if (index < code.length) { result += escapeHtml(code.substring(index)); } return result; } else { return escapeHtml(code); } }; const renderCodeMirror = (language, code) => { return ( `<pre class="cm-editor">` + `<div class="cm-gutters">` + renderGutters(code) + `</div>` + `<code class="cm-content">` + renderContent(language, code) + `</code>` + `</pre>` ); }; // ----------------------------------------------------------- // Usage example: // ----------------------------------------------------------- const language = 'JavaScript'; const code = 'for (let i = 0; i < 10; ++i) {\n' + ' console.log(`i: ${i}`);\n' + '}'; const html = renderCodeMirror(language, code); console.log(html); // Output: // // <pre class="cm-editor"><div class="cm-gutters">1\n2\n3</div><code class="cm-content"><span class="tok-keyword">for</span> <span class="tok-punctuation">(</span><span class="tok-keyword">let</span> <span class="tok-variableName tok-definition">i</span> <span class="tok-operator">=</span> <span class="tok-number">0</span><span class="tok-punctuation">;</span> <span class="tok-variableName">i</span> <span class="tok-operator">&lt;</span> <span class="tok-number">10</span><span class="tok-punctuation">;</span> <span class="tok-operator">++</span><span class="tok-variableName">i</span><span class="tok-punctuation">)</span> <span class="tok-punctuation">{</span>\n<span class="tok-variableName">console</span><span class="tok-operator">.</span><span class="tok-propertyName">log</span><span class="tok-punctuation">(</span><span class="tok-string2">`i: </span><span class="tok-punctuation">${</span><span class="tok-variableName">i</span><span class="tok-punctuation">}</span><span class="tok-string2">`</span><span class="tok-punctuation">)</span><span class="tok-punctuation">;</span>\n<span class="tok-punctuation">}</span></code></pre> // Example styles: /* <style> pre.cm-editor { border: 1px solid #ddd; font: 13.5px / 19px monospace; } div.cm-gutters { padding: 10px 6px 10px 12px; position: sticky; left: 0; border-right: 1px solid #ddd; background: #f7f7f7; min-width: 20px; text-align: right; color: #a1a1a1; float: left; } code.cm-content { padding: 10px 16px; background: #fcfcfc; tab-size: 30px; color: #000000; display: block; float: none; overflow-y: hidden; overflow-x: auto; } span.tok-link { color: #0000cc; } span.tok-comment { color: #708090; } span.tok-keyword { color: #770088; } span.tok-atom { color: #221199; } span.tok-bool, span.tok-number { color: #2771bb; } span.tok-string, span.tok-string2 { color: #d03131; } span.tok-punctuation { color: #999977; } </style> */ // Default tokens: // // tok-meta // tok-link // tok-url // tok-invalid // tok-heading // tok-emphasis // tok-strong // tok-deleted // tok-inserted // tok-keyword // tok-operator // tok-punctuation // tok-atom // tok-comment // tok-literal // tok-namespace // tok-bool // tok-number // tok-string // tok-string2 // tok-variableName // tok-variableName tok-local // tok-variableName tok-definition // tok-variableName2 // tok-propertyName // tok-propertyName tok-definition // tok-typeName // tok-className // tok-macroName // tok-labelName // // Source: // https://github.com/lezer-parser/highlight/blob/main/src/highlight.ts