summaryrefslogtreecommitdiffstats
path: root/web_src/js
diff options
context:
space:
mode:
authorsilverwind <me@silverwind.io>2020-07-27 08:24:09 +0200
committerGitHub <noreply@github.com>2020-07-27 02:24:09 -0400
commit4315e313d12bbf0655710b6ab0aad121e0f7dba2 (patch)
treedfcc816a013288f5112f3be4f38ac183557220fb /web_src/js
parent46ef562a161a2685d429ffd7dc594383de533c07 (diff)
downloadgitea-4315e313d12bbf0655710b6ab0aad121e0f7dba2.tar.gz
gitea-4315e313d12bbf0655710b6ab0aad121e0f7dba2.zip
Add mermaid JS renderer (#12334)
* Add mermaid JS renderer For feature parity with GitLab. Tested in files, issues, wiki, editor. arc-green only does an inversion because the renderer seems to like to render white backgrounds on boxes. Ref: https://github.com/go-gitea/gitea/issues/3340 Fixes: https://github.com/go-gitea/gitea/issues/12307 * add feature entry, switch to neutral theme, remove border * add bindFunctions support * remove unnecessary border-radius Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'web_src/js')
-rw-r--r--web_src/js/index.js6
-rw-r--r--web_src/js/markdown/content.js5
-rw-r--r--web_src/js/markdown/mermaid.js23
-rw-r--r--web_src/js/utils.js11
4 files changed, 45 insertions, 0 deletions
diff --git a/web_src/js/index.js b/web_src/js/index.js
index 03f0c6e4cc..cb1d39e62d 100644
--- a/web_src/js/index.js
+++ b/web_src/js/index.js
@@ -13,6 +13,7 @@ import initClipboard from './features/clipboard.js';
import initUserHeatmap from './features/userheatmap.js';
import initServiceWorker from './features/serviceworker.js';
import initMarkdownAnchors from './markdown/anchors.js';
+import renderMarkdownContent from './markdown/content.js';
import attachTribute from './features/tribute.js';
import createDropzone from './features/dropzone.js';
import initTableSort from './features/tablesort.js';
@@ -46,6 +47,7 @@ function initCommentPreviewTab($form) {
}, (data) => {
const $previewPanel = $form.find(`.tab[data-tab="${$tabMenu.data('preview')}"]`);
$previewPanel.html(data);
+ renderMarkdownContent();
});
});
@@ -75,6 +77,7 @@ function initEditPreviewTab($form) {
}, (data) => {
const $previewPanel = $form.find(`.tab[data-tab="${$tabMenu.data('preview')}"]`);
$previewPanel.html(data);
+ renderMarkdownContent();
});
});
}
@@ -1007,6 +1010,7 @@ async function initRepository() {
}
dz.emit('submit');
dz.emit('reload');
+ renderMarkdownContent();
});
});
} else {
@@ -1347,6 +1351,7 @@ function initWikiForm() {
wiki: true
}, (data) => {
preview.innerHTML = `<div class="markdown ui segment">${data}</div>`;
+ renderMarkdownContent();
});
};
if (!simplemde.isSideBySideActive()) {
@@ -2484,6 +2489,7 @@ $(document).ready(async () => {
initUserHeatmap(),
initServiceWorker(),
initNotificationCount(),
+ renderMarkdownContent(),
]);
});
diff --git a/web_src/js/markdown/content.js b/web_src/js/markdown/content.js
new file mode 100644
index 0000000000..f41800ee30
--- /dev/null
+++ b/web_src/js/markdown/content.js
@@ -0,0 +1,5 @@
+import {renderMermaid} from './mermaid.js';
+
+export default async function renderMarkdownContent() {
+ await renderMermaid(document.querySelectorAll('.language-mermaid'));
+}
diff --git a/web_src/js/markdown/mermaid.js b/web_src/js/markdown/mermaid.js
new file mode 100644
index 0000000000..1fda101dc0
--- /dev/null
+++ b/web_src/js/markdown/mermaid.js
@@ -0,0 +1,23 @@
+import {random} from '../utils.js';
+
+export async function renderMermaid(els) {
+ if (!els || !els.length) return;
+
+ const {mermaidAPI} = await import(/* webpackChunkName: "mermaid" */'mermaid');
+
+ mermaidAPI.initialize({
+ startOnLoad: false,
+ theme: 'neutral',
+ securityLevel: 'strict',
+ });
+
+ for (const el of els) {
+ mermaidAPI.render(`mermaid-${random(12)}`, el.textContent, (svg, bindFunctions) => {
+ const div = document.createElement('div');
+ div.classList.add('mermaid-chart');
+ div.innerHTML = svg;
+ if (typeof bindFunctions === 'function') bindFunctions(div);
+ el.closest('pre').replaceWith(div);
+ });
+ }
+}
diff --git a/web_src/js/utils.js b/web_src/js/utils.js
index 229f4d6304..d5f921f8dc 100644
--- a/web_src/js/utils.js
+++ b/web_src/js/utils.js
@@ -23,3 +23,14 @@ export function isDarkTheme() {
export function uniq(arr) {
return Array.from(new Set(arr));
}
+
+const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
+
+// generate a random string
+export function random(length) {
+ let str = '';
+ for (let i = 0; i < length; i++) {
+ str += chars.charAt(Math.floor(Math.random() * chars.length));
+ }
+ return str;
+}