Skip to content

Commit 9bc4be8

Browse files
committed
Add help section
Partially solves issue #1. Signed-off-by: Felipe Contreras <[email protected]>
1 parent 6188d3c commit 9bc4be8

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
<body>
1313
<div id="input" class="editor language-mathjs"></div>
1414
<div id="output" class="editor language-mathjs"></div>
15+
<div id="help" style="display: none">
16+
<h1 id="help_name"></h1>
17+
<p id="help_description"></p>
18+
<h2>Syntax</h2>
19+
<pre><code id="help_syntax" class="language-mathjs"></code></pre>
20+
<h2>Examples</h2>
21+
<pre><code id="help_examples" class="language-mathjs"></code></pre>
22+
<h2>See also</h2>
23+
<p id="help_seealso"></p>
24+
</div>
1525
</body>
1626

1727
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.3.1/highlight.min.js"></script>

script.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
const wait = 100;
22

3+
function showDoc(doc) {
4+
if (!doc) {
5+
help.style.display = 'none';
6+
return;
7+
}
8+
9+
help_name.textContent = doc.name;
10+
help_description.textContent = doc.description;
11+
help_syntax.textContent = doc.syntax.join("\n");
12+
hljs.highlightElement(help_syntax);
13+
help_examples.textContent = doc.examples.join("\n");
14+
hljs.highlightElement(help_examples);
15+
help_seealso.textContent = doc.seealso.join(", ");
16+
help.style.display = 'block';
17+
}
18+
319
function doMath(input) {
420
let output = [];
521
let scope = {};
22+
let doc;
623

724
for (const line of input.split('\n')) {
825
let output_line = '';
@@ -11,7 +28,9 @@ function doMath(input) {
1128
output_line = line;
1229
} else {
1330
try {
14-
output_line = math.format(math.evaluate(line, scope), 14);
31+
const r = math.evaluate(line, scope);
32+
if (r.doc) doc = r.doc;
33+
else output_line = math.format(r, 14);
1534
} catch(e) {
1635
output_line = e.toString();
1736
}
@@ -21,6 +40,7 @@ function doMath(input) {
2140
}
2241

2342
results.updateCode(output.join('\n'));
43+
showDoc(doc);
2444
}
2545

2646
var timer;

style.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ body {
2121
background-color: hsl(240, 6.25%, 12.5%);
2222
}
2323

24+
#help {
25+
font-family: sans-serif;
26+
flex: 1;
27+
padding: 8px;
28+
}
29+
30+
h1, h2 {
31+
font-size: 1.2em;
32+
margin-top: 1em;
33+
}
34+
35+
h1 {
36+
font-size: 1.4em;
37+
}
38+
2439
.hljs-class {
2540
font-weight: bold;
2641
}

0 commit comments

Comments
 (0)