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 newTextArea.style.display = 'none';
161 elem.parentNode.replaceChild(newWrap, elem);
163 newWrap.appendChild(newTextArea);
164 newWrap.contentEditable = false;
165 newTextArea.textContent = content;
167 let cm = CodeMirror(function(elt) {
168 newWrap.appendChild(elt);
179 return {wrap: newWrap, editor: cm};
183 * Create a CodeMirror instance to show in the WYSIWYG pop-up editor
184 * @param {HTMLElement} elem
185 * @param {String} modeSuggestion
188 function popupEditor(elem, modeSuggestion) {
189 let content = elem.textContent;
191 return CodeMirror(function(elt) {
192 elem.parentNode.insertBefore(elt, elem);
193 elem.style.display = 'none';
196 mode: getMode(modeSuggestion),
204 * Set the mode of a codemirror instance.
206 * @param modeSuggestion
208 function setMode(cmInstance, modeSuggestion) {
209 cmInstance.setOption('mode', getMode(modeSuggestion));
213 * Set the content of a cm instance.
217 function setContent(cmInstance, codeContent) {
218 cmInstance.setValue(codeContent);
220 cmInstance.refresh();
225 * Get a CodeMirror instace to use for the markdown editor.
226 * @param {HTMLElement} elem
229 function markdownEditor(elem) {
230 let content = elem.textContent;
232 return CodeMirror(function (elt) {
233 elem.parentNode.insertBefore(elt, elem);
234 elem.style.display = 'none';
245 * Get the 'meta' key dependant on the user's system.
248 function getMetaKey() {
249 let mac = CodeMirror.keyMap["default"] == CodeMirror.keyMap.macDefault;
250 return mac ? "Cmd" : "Ctrl";
254 highlight: highlight,
255 highlightElem: highlightElem,
256 wysiwygView: wysiwygView,
257 popupEditor: popupEditor,
259 setContent: setContent,
260 markdownEditor: markdownEditor,
261 getMetaKey: getMetaKey,