-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (63 loc) · 2.59 KB
/
index.js
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
// highlight.js
import hljs from 'highlight.js/lib/highlight';
import xml from 'highlight.js/lib/languages/xml';
import 'highlight.js/styles/github.css';
// CodeMirror
import CodeMirror from 'codemirror/lib/codemirror.js';
import 'codemirror/mode/xml/xml.js';
import 'codemirror/lib/codemirror.css';
// MathML examples
import { multiLineIndented, multiLineNotIndented, singleLine } from './mathml';
// ################################################
// Highlight.js for static content
// ################################################
hljs.registerLanguage('xml', xml);
// htmlentities() is a PHP function which converts special characters (like <)
// into their escaped/encoded values (like <).
// This allows you to show to display the string without the browser reading it as HTML.
// JavaScript doesn't have a native version of it. If you just need the very basics
// so that the browser won't interpret as HTML, this should work fine.
function htmlEntities(str) {
return String(str)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
}
const staticContainer = document.getElementById('static');
const encodedContent = htmlEntities(multiLineIndented);
// Inject MathML into the DOM.
staticContainer.innerHTML = encodedContent;
// Applies highlighting to all <pre><code>..</code></pre> blocks on a page.
// hljs.initHighlighting();
// Alternatively we can target exactly the code block we want.
hljs.highlightBlock(staticContainer);
// ################################################
// CodeMirror for dynamic content
// ################################################
const dynamicTextarea = document.getElementById('dynamic');
const codeMirrorInstance = CodeMirror(
el => dynamicTextarea.parentNode.replaceChild(el, dynamicTextarea),
{
value: singleLine,
mode: 'xml',
lineWrapping: true
}
);
codeMirrorInstance.setSize(null, 100);
// ################################################
// CodeMirror for dynamic content with auto indent
// ################################################
const dynamicTextareaAutoIndent = document.getElementById('dynamic-autoindent');
const codeMirrorAutoIndentInstance = CodeMirror(
el => dynamicTextareaAutoIndent.parentNode.replaceChild(el, dynamicTextareaAutoIndent),
{
value: multiLineNotIndented,
mode: 'xml',
lineNumbers: true,
lineWrapping: true
}
);
codeMirrorAutoIndentInstance.execCommand('selectAll');
codeMirrorAutoIndentInstance.execCommand('indentAuto');
codeMirrorAutoIndentInstance.setCursor({ line: 1, ch: 1 });