Skip to content

Commit

Permalink
Fix render failed when the head levels are not in order in toc plugin…
Browse files Browse the repository at this point in the history
… and due to incompatibility caused by using the new version mermaid and vega, so it is mandatory to use the old version.
  • Loading branch information
jhuix committed Jul 19, 2024
1 parent 2a4a5d3 commit 24620de
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
14 changes: 7 additions & 7 deletions src/extensions/cdn.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ const cdnSrc = {
VizRender: scheme + 'cdn.jsdelivr.net/npm/viz.js/full.render.js',
Raphael: scheme + 'cdn.jsdelivr.net/npm/raphael/raphael.min.js',
flowchart: scheme + 'cdnjs.cloudflare.com/ajax/libs/flowchart/1.14.1/flowchart.min.js',
mermaid: scheme + 'cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js',
katex: scheme + 'cdn.jsdelivr.net/npm/katex/dist/katex.min.js',
katexCSS: scheme + 'cdn.jsdelivr.net/npm/katex/dist/katex.min.css',
renderMathInElement: scheme + 'cdn.jsdelivr.net/npm/katex/dist/contrib/auto-render.js',
mermaid: scheme + 'cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js',
katex: scheme + 'cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js',
katexCSS: scheme + 'cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css',
renderMathInElement: scheme + 'cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/auto-render.js',
railroad: scheme + 'cdn.jsdelivr.net/npm/railroad-diagrams',
railroadCSS: scheme + 'cdn.jsdelivr.net/npm/railroad-diagrams/railroad-diagrams.css',
Snap: scheme + 'cdn.jsdelivr.net/npm/snapsvg',
Expand All @@ -87,9 +87,9 @@ const cdnSrc = {
lowkey: scheme + 'cdn.jsdelivr.net/npm/wavedrom/skins/lowkey.js',
narrow: scheme + 'cdn.jsdelivr.net/npm/wavedrom/skins/narrow.js'
},
vega: scheme + 'cdn.jsdelivr.net/npm/vega/build/vega.js',
vegaLite: scheme + 'cdn.jsdelivr.net/npm/vega-lite/build/vega-lite.js',
vegaEmbed: scheme + 'cdn.jsdelivr.net/npm/vega-embed/build/vega-embed.js'
vega: scheme + 'cdn.jsdelivr.net/npm/vega@5/build/vega.js',
vegaLite: scheme + 'cdn.jsdelivr.net/npm/vega-lite@4/build/vega-lite.js',
vegaEmbed: scheme + 'cdn.jsdelivr.net/npm/vega-embed@6/build/vega-embed.js'
}
};

Expand Down
27 changes: 18 additions & 9 deletions src/extensions/showdown-toc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
'use strict';

function createHeadingElement(wrapper, element, toc, nexthead) {
if (nexthead) {
toc = toc.appendChild(wrapper.ownerDocument.createElement('ul'));
try {
if (nexthead) {
toc = toc.appendChild(wrapper.ownerDocument.createElement('ul'));
}
const child = toc
.appendChild(wrapper.ownerDocument.createElement('li'))
.appendChild(wrapper.ownerDocument.createElement('a'));
if (element) {
child.appendChild(wrapper.ownerDocument.createTextNode(element.textContent));
child.href = '#' + element.id;
}
} catch {
console.log("append child error:" + wrapper);
}

const child = toc
.appendChild(wrapper.ownerDocument.createElement('li'))
.appendChild(wrapper.ownerDocument.createElement('a'));
child.appendChild(wrapper.ownerDocument.createTextNode(element.textContent));
child.href = '#' + element.id;
return toc;
}

Expand Down Expand Up @@ -43,8 +48,12 @@ function appendTocElement(wrapper, element, currTocNode, headingLevel) {
// you need to create a new level heading with out list.
// Otherwise add a heading of the same heading level.
if (headingLevel > currTocNode.preLevel) {
currTocNode.preLevel ++;
while (headingLevel > currTocNode.preLevel) {
currTocNode.toc = createHeadingElement(wrapper, null, currTocNode.toc, true);
currTocNode.preLevel ++;
}
currTocNode.toc = createHeadingElement(wrapper, element, currTocNode.toc, true);
currTocNode.preLevel = headingLevel;
} else {
while (headingLevel < currTocNode.preLevel) {
currTocNode.toc = currTocNode.toc.parentNode;
Expand Down

0 comments on commit 24620de

Please sign in to comment.