You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed in the future ideas section of the readme it mentioned "generate SVG server-side during build". I've been playing around with 11ty and I'm not sure if I'm going to continue with it but thought I'd share how I got server side rendering working in a prototype.
It uses the mermaid-cli. The CLI in turn uses a puppeteer instance as mermaid requires a browser to render.
import{renderMermaid}from"@mermaid-js/mermaid-cli";importpuppeteerfrom"puppeteer";exportconstmermaidPlugin=(eleventyConfig,options)=>{letbrowser;eleventyConfig.on("eleventy.before",async()=>{if(!browser){browser=awaitpuppeteer.launch({headless: "new"});};});eleventyConfig.on("eleventy.after",async()=>{if(browser){browser.close();browser=undefined;}});eleventyConfig.htmlTransformer.addPosthtmlPlugin("html",()=>{returnasync(tree)=>{consttransformTag=async(node)=>{letcontent;if(Array.isArray(node.content)){content=node.content.join("");}else{content=node.content;}const{ data }=awaitrenderMermaid(browser,content,"svg",{mermaidConfig: options.mermaidConfig,});consthtmlString=data.toString();node.tag=false;node.content=tree.parser(htmlString);};constpromises=[];tree.match({tag: "pre",attrs: {class: "mermaid"}},node=>{promises.push(transformTag(node));returnnode;});awaitPromise.all(promises);returntree;};});};
It's probably very unperformant but appears to work. Usage would be like this:
It asumes/requires you have set up the syntax highlighter to ignore mermaid code blocks and instead return a <pre class="mermaid"> tag, like you already do here.
The text was updated successfully, but these errors were encountered:
Wow, thanks a lot!
This is a great starting point to get this feature going, if I find the time I'll give it a try!
Some questions I have (for myself):
Can puppeteer and mermaid-cli be hidden behind a feature flag?
Don't want to pull in big dependencies unless people wanna use them.
Split plugin into browser and ssr?
Maybe it makes sense to create a new plugin which implements the SSR. This plugin could re-use the existing plugin as dependency, people who want SSR can then use the SSR plugin.
Hi
I noticed in the future ideas section of the readme it mentioned "generate SVG server-side during build". I've been playing around with 11ty and I'm not sure if I'm going to continue with it but thought I'd share how I got server side rendering working in a prototype.
It uses the
mermaid-cli
. The CLI in turn uses a puppeteer instance as mermaid requires a browser to render.It's probably very unperformant but appears to work. Usage would be like this:
It asumes/requires you have set up the syntax highlighter to ignore mermaid code blocks and instead return a
<pre class="mermaid">
tag, like you already do here.The text was updated successfully, but these errors were encountered: