Skip to content

Commit

Permalink
Fix mermaid folding
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed May 8, 2024
1 parent 524c369 commit 6742735
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
1 change: 0 additions & 1 deletion scripts/events/lib/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module.exports = hexo => {
const { config } = hexo;
const theme = hexo.theme.config;
config.highlight.hljs = false;
config.prismjs = config.prismjs || {};
theme.highlight = {
enable: config.syntax_highlighter === 'highlight.js' || config.highlight.enable,
light : highlightTheme(theme.codeblock.theme.light),
Expand Down
1 change: 1 addition & 0 deletions scripts/helpers/next-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ hexo.extend.helper.register('next_config', function() {
version : this.next_version,
exturl : theme.exturl,
sidebar : theme.sidebar,
hljswrap : theme.highlight.enable && config.highlight.wrap,
copycode : theme.codeblock.copy_button,
fold : theme.codeblock.fold,
bookmark : theme.bookmark,
Expand Down
7 changes: 6 additions & 1 deletion scripts/tags/mermaid.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
const { escapeHTML } = require('hexo-util');

module.exports = function(args, content) {
return `<pre class="mermaid">
// Support mermaid inside backtick code block
// Keep the same HTML structure
// Fix issue #347 #797
return `<pre>
<code class="mermaid">
${args.join(' ')}
${escapeHTML(content)}
</code>
</pre>`;
};
10 changes: 2 additions & 8 deletions source/js/third-party/tags/mermaid.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global NexT, CONFIG, mermaid */

document.addEventListener('page:loaded', () => {
const mermaidElements = document.querySelectorAll('.mermaid');
const mermaidElements = document.querySelectorAll('pre > .mermaid');
if (mermaidElements.length) {
NexT.utils.getScript(CONFIG.mermaid.js, {
condition: window.mermaid
Expand All @@ -11,13 +11,7 @@ document.addEventListener('page:loaded', () => {
newElement.innerHTML = element.innerHTML;
newElement.className = element.className;
const parent = element.parentNode;
// Fix issue #347
// Support mermaid inside backtick code block
if (parent.matches('pre')) {
parent.parentNode.replaceChild(newElement, parent);
} else {
parent.replaceChild(newElement, element);
}
parent.parentNode.replaceChild(newElement, parent);
});
mermaid.initialize({
theme : CONFIG.darkmode && window.matchMedia('(prefers-color-scheme: dark)').matches ? CONFIG.mermaid.theme.dark : CONFIG.mermaid.theme.light,
Expand Down
16 changes: 9 additions & 7 deletions source/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ NexT.utils = {

registerCodeblock(element) {
const inited = !!element;
let figure = (inited ? element : document).querySelectorAll('figure.highlight');
let isHljsWithWrap = true;
if (figure.length === 0) {
figure = document.querySelectorAll('pre:not(.mermaid)');
isHljsWithWrap = false;
let figure;
if (CONFIG.hljswrap) {
figure = (inited ? element : document).querySelectorAll('figure.highlight');
} else {
figure = document.querySelectorAll('pre');
}
figure.forEach(element => {
if (!inited) {
Expand All @@ -61,10 +61,12 @@ NexT.utils = {
});
}
const height = parseInt(window.getComputedStyle(element).height.replace('px', ''), 10);
const needFold = CONFIG.fold.enable && (height > CONFIG.fold.height);
// Skip pre > .mermaid for folding but keep the copy button
// Note that it only works before mermaid.js loaded (race condition)
const needFold = CONFIG.fold.enable && (height > CONFIG.fold.height) && !element.querySelector('.mermaid');
if (!needFold && !CONFIG.copycode.enable) return;
let target;
if (isHljsWithWrap && CONFIG.copycode.style === 'mac') {
if (CONFIG.hljswrap && CONFIG.copycode.style === 'mac') {
target = element;
} else {
let box = element.querySelector('.code-container');
Expand Down
4 changes: 3 additions & 1 deletion test/tags/mermaid.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ describe('mermaid', () => {
const mermaid = require('../../scripts/tags/mermaid');

it('default', () => {
mermaid(['graph', 'TD'], result).should.eql(`<pre class="mermaid">
mermaid(['graph', 'TD'], result).should.eql(`<pre>
<code class="mermaid">
graph TD
${escapeHTML(result)}
</code>
</pre>`);
});
});

0 comments on commit 6742735

Please sign in to comment.