mirror of
https://github.com/next-theme/hexo-theme-next.git
synced 2026-01-18 18:33:42 +00:00
Fix mermaid folding
This commit is contained in:
parent
524c3693f2
commit
6742735db4
@ -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),
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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
source/js/third-party/tags/mermaid.js
vendored
10
source/js/third-party/tags/mermaid.js
vendored
@ -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
|
||||
@ -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,
|
||||
|
||||
@ -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) {
|
||||
@ -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');
|
||||
|
||||
@ -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>`);
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user