Skip to content
This repository was archived by the owner on Feb 20, 2025. It is now read-only.

Commit 713e301

Browse files
Steve-MclSteve McLaughlinMclaughlin
authored
improve mermaid (#284)
* improve mermaid - update mermaid to v8.3.1 - live preview - charts update as you type - reduced number of renders when code changed (timed onchange) - markdown code fence for mermaid support added - e.g ``` mermaid - minor version bump * Default mermaid_auto_update to true * fix fenced mermaid code not rendering * remove temp debugger and console.log instructions Co-authored-by: Steve McLaughlin <[email protected]> Co-authored-by: Mclaughlin <[email protected]>
1 parent 9824e3e commit 713e301

File tree

11 files changed

+111
-302
lines changed

11 files changed

+111
-302
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ Most of the configuration can be done on the `/settings` page but there are some
8989
|`suggest_allowed`|If enabled non authenticated users can submit article suggestions for approval|
9090
|`show_author_email`|Controls whether the authors email address is displayed in the meta. Requires "Show article meta data" to be true.|
9191
|`mermaid`|Whether to allow Mermaid charts within articles|
92+
|`mermaid_auto_update`|Whether to auto update Mermaid charts in the preview window as you edit (default is `true`)|
93+
|`mermaid_options`|Additional options for Mermaid charts see [here](https://mermaidjs.github.io/#/mermaidAPI?id=configuration) (Default options - [link](https://github.com/knsv/mermaid/blob/master/docs/mermaidAPI.md#mermaidapi-configuration-defaults)) |
9294
|`mathjax`|Whether to allow MathJax inputs within articles|
9395
|`app_context`|Allows for the website to be run from a non root path. Eg: http://127.0.0.1:4444/openkb/|
9496
|`links_blank_page`|Controls whether links within articles open a new page (tab)|

bin/uglify.js

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const fs = require('fs');
77
uglifyFile('public/stylesheets/style.css', 'css');
88
uglifyFile('public/stylesheets/github.css', 'css');
99
uglifyFile('public/stylesheets/jpushmenu.css', 'css');
10-
uglifyFile('public/stylesheets/mermaid.css', 'css');
1110
uglifyFile('public/stylesheets/bootstrap-tokenfield.css', 'css');
1211

1312
// js files

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openkb",
3-
"version": "1.0.23",
3+
"version": "1.0.24",
44
"description": "openKB is an Open Source Nodejs Markdown based knowledge base/FAQ/Wiki app with powerful lunr search",
55
"private": false,
66
"scripts": {

public/javascripts/mermaid.min.js

+19-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/javascripts/openKB.js

+64-9
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,20 @@ $(document).ready(function(){
1010
}
1111

1212
// setup mermaid charting
13-
if(typeof mermaid !== 'undefined'){
14-
mermaid.initialize({startOnLoad: true});
13+
if(typeof mermaid !== 'undefined' && config.mermaid){
14+
//defaults - can be overridden in config.json by specifying mermaid_options
15+
//TODO: Consider adding mermaid_options to settings page?
16+
var mermaid_opts = {
17+
"theme" : "forest",
18+
"flowchart": { "curve": "linear" },
19+
"gantt": { "axisFormat": "%Y/%m/%d" },
20+
"sequence": { "actorMargin": 20 },
21+
"securityLevel": "loose"
22+
};
23+
// Merge mermaid_options into mermaid_opts, recursively
24+
$.extend( true, mermaid_opts, config.mermaid_options || {} );
25+
mermaid_opts.startOnLoad = true;
26+
mermaid.initialize(mermaid_opts);
1527
}
1628

1729
// add the table class to all tables
@@ -117,16 +129,26 @@ $(document).ready(function(){
117129
inlineAttachment.editors.codemirror4.attach(simplemde.codemirror, {uploadUrl: $('#app_context').val() + '/file/upload_file'});
118130

119131
// do initial convert on load
120-
convertTextAreaToMarkdown();
121-
122-
// attach to editor changes and update preview
123-
simplemde.codemirror.on('change', function(){
124-
convertTextAreaToMarkdown();
125-
});
132+
convertTextAreaToMarkdown(true); //true means this is first call - do all rendering
126133

127134
// auto scrolls the simpleMDE preview pane
128135
var preview = document.getElementById('preview');
129136
if(preview !== null){
137+
138+
//timed re-render (virtual speedup) - i.e. only call convertTextAreaToMarkdown() after xxxms of inactivity to reduce redraws
139+
var timer = null;
140+
//TODO: Consider adding the renderDelayTime to settings
141+
var renderDelayTime = 500;//only re-render when user stops changing text
142+
143+
// attach to editor changes and update preview
144+
simplemde.codemirror.on('change', function(){
145+
if(timer != null)
146+
clearTimeout(timer);
147+
timer = setTimeout(function(){
148+
convertTextAreaToMarkdown(false);//pass false to indicate this call is due to a code change
149+
}, renderDelayTime);
150+
});
151+
130152
// Syncs scroll editor -> preview
131153
var cScroll = false;
132154
var pScroll = false;
@@ -244,11 +266,39 @@ $(document).ready(function(){
244266
});
245267

246268
// convert editor markdown to HTML and display in #preview div
247-
function convertTextAreaToMarkdown(){
269+
//firstRender indicates this is a first call (i.e. not a re-render request due to a code editor change)
270+
function convertTextAreaToMarkdown(firstRender){
248271
var classy = window.markdownItClassy;
249272

250273
var mark_it_down = window.markdownit({html: true, linkify: true, typographer: true, breaks: true});
251274
mark_it_down.use(classy);
275+
276+
if(typeof mermaid !== 'undefined' && config.mermaid){
277+
278+
var mermaidChart = function(code) {
279+
try {
280+
mermaid.parse(code)
281+
return '<div class="mermaid">'+code+'</div>';
282+
} catch ({ str, hash }) {
283+
return '<pre><code>'+code+'</code></pre>';
284+
}
285+
}
286+
287+
var defFenceRules = mark_it_down.renderer.rules.fence.bind(mark_it_down.renderer.rules)
288+
mark_it_down.renderer.rules.fence = function(tokens, idx, options, env, slf) {
289+
var token = tokens[idx]
290+
var code = token.content.trim()
291+
if (token.info === 'mermaid') {
292+
return mermaidChart(code)
293+
}
294+
var firstLine = code.split(/\n/)[0].trim()
295+
if (firstLine === 'gantt' || firstLine === 'sequenceDiagram' || firstLine.match(/^graph (?:TB|BT|RL|LR|TD);?$/)) {
296+
return mermaidChart(code)
297+
}
298+
return defFenceRules(tokens, idx, options, env, slf)
299+
}
300+
}
301+
252302
var html = mark_it_down.render(simplemde.value());
253303

254304
// add responsive images and tables
@@ -269,6 +319,11 @@ $(document).ready(function(){
269319
$('pre code').each(function(i, block){
270320
hljs.highlightBlock(block);
271321
});
322+
323+
if(!firstRender && typeof mermaid !== 'undefined' && (config.mermaid && config.mermaid_auto_update)) {
324+
mermaid.init();//when this is not first render AND mermaid_auto_update==true, re-init mermaid charts (render code changes)
325+
}
326+
272327
}
273328

274329
// user up vote clicked

public/stylesheets/mermaid.css

-258
This file was deleted.

public/stylesheets/mermaid.min.css

-1
This file was deleted.

0 commit comments

Comments
 (0)