1 require('codemirror/mode/css/css');
2 require('codemirror/mode/clike/clike');
3 require('codemirror/mode/diff/diff');
4 require('codemirror/mode/go/go');
5 require('codemirror/mode/htmlmixed/htmlmixed');
6 require('codemirror/mode/javascript/javascript');
7 require('codemirror/mode/markdown/markdown');
8 require('codemirror/mode/nginx/nginx');
9 require('codemirror/mode/php/php');
10 require('codemirror/mode/powershell/powershell');
11 require('codemirror/mode/python/python');
12 require('codemirror/mode/ruby/ruby');
13 require('codemirror/mode/shell/shell');
14 require('codemirror/mode/sql/sql');
15 require('codemirror/mode/toml/toml');
16 require('codemirror/mode/xml/xml');
17 require('codemirror/mode/yaml/yaml');
19 const Clipboard = require("clipboard");
21 const CodeMirror = require('codemirror');
27 scala: 'text/x-scala',
28 kotlin: 'text/x-kotlin',
29 'c++': 'text/x-c++src',
30 'c#': 'text/x-csharp',
31 csharp: 'text/x-csharp',
35 javascript: 'javascript',
36 json: {name: 'javascript', json: true},
43 powershell: 'powershell',
59 * Highlight pre elements on a page
61 function highlight() {
62 let codeBlocks = document.querySelectorAll('.page-content pre, .comment-box .content pre');
63 for (let i = 0; i < codeBlocks.length; i++) {
64 highlightElem(codeBlocks[i]);
69 * Add code highlighting to a single element.
70 * @param {HTMLElement} elem
72 function highlightElem(elem) {
73 let innerCodeElem = elem.querySelector('code[class^=language-]');
75 if (innerCodeElem !== null) {
76 let langName = innerCodeElem.className.replace('language-', '');
77 mode = getMode(langName);
79 elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
80 let content = elem.textContent.trim();
82 let cm = CodeMirror(function(elt) {
83 elem.parentNode.replaceChild(elt, elem);
96 * Add a button to a CodeMirror instance which copies the contents to the clipboard upon click.
99 function addCopyIcon(cmInstance) {
100 const copyIcon = `<svg viewBox="0 0 24 24" width="16" height="16" xmlns="http://www.w3.org/2000/svg"><path d="M0 0h24v24H0z" fill="none"/><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg>`;
101 const copyButton = document.createElement('div');
102 copyButton.classList.add('CodeMirror-copy');
103 copyButton.innerHTML = copyIcon;
104 cmInstance.display.wrapper.appendChild(copyButton);
106 const clipboard = new Clipboard(copyButton, {
107 text: function(trigger) {
108 return cmInstance.getValue()
112 clipboard.on('success', event => {
113 copyButton.classList.add('success');
115 copyButton.classList.remove('success');
121 * Search for a codemirror code based off a user suggestion
125 function getMode(suggestion) {
126 suggestion = suggestion.trim().replace(/^\./g, '').toLowerCase();
127 return (typeof modeMap[suggestion] !== 'undefined') ? modeMap[suggestion] : '';
131 * Ge the theme to use for CodeMirror instances.
132 * @returns {*|string}
134 function getTheme() {
135 return window.codeTheme || 'base16-light';
139 * Create a CodeMirror instance for showing inside the WYSIWYG editor.
140 * Manages a textarea element to hold code content.
141 * @param {HTMLElement} elem
142 * @returns {{wrap: Element, editor: *}}
144 function wysiwygView(elem) {
145 let doc = elem.ownerDocument;
146 let codeElem = elem.querySelector('code');
148 let lang = (elem.className || '').replace('language-', '');
149 if (lang === '' && codeElem) {
150 lang = (codeElem.className || '').replace('language-', '')
153 elem.innerHTML = elem.innerHTML.replace(/<br\s*[\/]?>/gi ,'\n');
154 let content = elem.textContent;
155 let newWrap = doc.createElement('div');
156 let newTextArea = doc.createElement('textarea');
158 newWrap.className = 'CodeMirrorContainer';
159 newWrap.setAttribute('data-lang', lang);
160 newWrap.setAttribute('dir', 'ltr');
161 newTextArea.style.display = 'none';
162 elem.parentNode.replaceChild(newWrap, elem);
164 newWrap.appendChild(newTextArea);
165 newWrap.contentEditable = false;
166 newTextArea.textContent = content;
168 let cm = CodeMirror(function(elt) {
169 newWrap.appendChild(elt);
180 return {wrap: newWrap, editor: cm};
184 * Create a CodeMirror instance to show in the WYSIWYG pop-up editor
185 * @param {HTMLElement} elem
186 * @param {String} modeSuggestion
189 function popupEditor(elem, modeSuggestion) {
190 let content = elem.textContent;
192 return CodeMirror(function(elt) {
193 elem.parentNode.insertBefore(elt, elem);
194 elem.style.display = 'none';
197 mode: getMode(modeSuggestion),
205 * Set the mode of a codemirror instance.
207 * @param modeSuggestion
209 function setMode(cmInstance, modeSuggestion) {
210 cmInstance.setOption('mode', getMode(modeSuggestion));
214 * Set the content of a cm instance.
218 function setContent(cmInstance, codeContent) {
219 cmInstance.setValue(codeContent);
221 cmInstance.refresh();
226 * Get a CodeMirror instace to use for the markdown editor.
227 * @param {HTMLElement} elem
230 function markdownEditor(elem) {
231 let content = elem.textContent;
233 return CodeMirror(function (elt) {
234 elem.parentNode.insertBefore(elt, elem);
235 elem.style.display = 'none';
246 * Get the 'meta' key dependant on the user's system.
249 function getMetaKey() {
250 let mac = CodeMirror.keyMap["default"] == CodeMirror.keyMap.macDefault;
251 return mac ? "Cmd" : "Ctrl";
255 highlight: highlight,
256 highlightElem: highlightElem,
257 wysiwygView: wysiwygView,
258 popupEditor: popupEditor,
260 setContent: setContent,
261 markdownEditor: markdownEditor,
262 getMetaKey: getMetaKey,