Skip to content

Commit

Permalink
Add support for mermaid.js diagrams (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
dohliam committed Oct 17, 2021
1 parent 4b2fa91 commit 7b208ca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions mermaid/info.json
@@ -0,0 +1,10 @@
{
"name": "Mermaid Diagrams",
"identifier": "mermaid",
"script": "mermaid.qml",
"authors": ["@dohliam"],
"platforms": ["linux", "macos", "windows"],
"version": "0.0.1",
"minAppVersion": "20.6.0",
"description" : "Support for rendering mermaid diagrams."
}
30 changes: 30 additions & 0 deletions mermaid/mermaid.qml
@@ -0,0 +1,30 @@
import QtQml 2.0
import QOwnNotesTypes 1.0

QtObject {
/**
* This function is called when the markdown html of a note is generated
*
* It allows you to modify this html
* This is for example called before by the note preview
*
* The method can be used in multiple scripts to modify the html of the preview
*
* @param {NoteApi} note - the note object
* @param {string} html - the html that is about to being rendered
* @param {string} forExport - the html is used for an export, false for the preview
* @return {string} the modified html or an empty string if nothing should be modified
*/

function preNoteToMarkdownHtmlHook(note, markdown, forExport) {
var re = /```mermaid\n([\s\S]*?)\n```/gim;
markdown = markdown.replace(re, function(_, diag){
var normalize = diag.replace(/&gt;/gi, ">").replace(/&lt;/gi, "<");
var encodedData = Qt.btoa(normalize);
var ink = '![](https://mermaid.ink/img/' + encodedData + ')';
return ink;
});
return markdown;
}

}

0 comments on commit 7b208ca

Please sign in to comment.