If you paste content having diagrams into Notion, every block defaults to Split mode (showing both the raw script and the diagram). Switching each block manually to Preview one by one gets repetitive fast.
I put together a small, zero-dependency Chrome bookmarklet that sweeps the active Notion page, triggers Notion's native Preview toggle on every Mermaid block (so the diagram-only view saves permanently across desktop and mobile), restores your scroll position, and shows a quick summary when finished.
How to Set It Up
Open your Chrome Bookmarks Bar (Ctrl + Shift + B on Windows/Linux, Cmd + Shift + B on Mac).
Right-click the Bookmarks Bar and select Add page... (or Add bookmark...).
Set Name to Mermaid → Preview.
Paste the single-line code below into the URL field and click Save.
Open any Notion page with Mermaid blocks and click the bookmark.
Bookmarklet Code
javascript:(async()=>{const S=m=>new Promise(r=>setTimeout(r,m)),T=e=>(e?.innerText||e?.textContent||"").trim().split(/\r?\n/)[0].trim(),L=e=>((e?.getAttribute("aria-label")||"")+" "+(e?.getAttribute("title")||"")).trim(),V=e=>{if(!e)return false;const s=getComputedStyle(e),r=e.getBoundingClientRect();return s.display!=="none"&&s.visibility!=="hidden"&&parseFloat(s.opacity||"1")>0&&r.width>0&&r.height>0},C=e=>{const r=e.getBoundingClientRect(),o={bubbles:true,cancelable:true,view:window,clientX:r.left+r.width/2,clientY:r.top+r.height/2,button:0,buttons:1};["pointerdown","mousedown"].forEach(n=>e.dispatchEvent(new(n[0]==="p"?PointerEvent:MouseEvent)(n,o)));["pointerup","mouseup","click"].forEach(n=>e.dispatchEvent(new(n[0]==="p"?PointerEvent:MouseEvent)(n,{...o,buttons:0})))},H=b=>{[b,...b.querySelectorAll(":scope > div, svg, pre, code")].slice(0,6).forEach(t=>{["pointerover","pointerenter"].forEach(n=>t.dispatchEvent(new PointerEvent(n,{bubbles:true,cancelable:true,view:window})));["mouseover","mouseenter","mousemove"].forEach(n=>t.dispatchEvent(new MouseEvent(n,{bubbles:true,cancelable:true,view:window})))})},N=m=>{let e=document.getElementById("nm-pv-toast");if(!e){e=document.createElement("div");e.id="nm-pv-toast";Object.assign(e.style,{position:"fixed",bottom:"20px",right:"20px",zIndex:"999999",padding:"10px 14px",borderRadius:"8px",background:"#1f1f1f",color:"#fff",fontSize:"13px",fontFamily:"system-ui,sans-serif",boxShadow:"0 4px 14px rgba(0,0,0,0.35)"});document.body.appendChild(e)}e.textContent=m},F=(mc,sw,ok,nf)=>{["nm-pv-toast","nm-pv-modal","nm-pv-confetti"].forEach(id=>document.getElementById(id)?.remove());const cv=document.createElement("canvas");cv.id="nm-pv-confetti";Object.assign(cv.style,{position:"fixed",inset:"0",width:"100vw",height:"100vh",pointerEvents:"none",zIndex:"9999999"});const dpr=window.devicePixelRatio||1;cv.width=innerWidth*dpr;cv.height=innerHeight*dpr;document.body.appendChild(cv);const ctx=cv.getContext("2d");ctx.scale(dpr,dpr);const cols=["#ff5c8a","#ffd166","#06d6a0","#4cc9f0","#c77dff","#ff9f1c","#ffffff"],cx=innerWidth/2,cy=innerHeight/2,P=Array.from({length:110},()=>{const a=Math.random()*Math.PI*2,sp=4+Math.random()*11;return{x:cx,y:cy,vx:Math.cos(a)*sp,vy:Math.sin(a)*sp-4.5,w:5+Math.random()*6,h:3+Math.random()*5,c:cols[Math.floor(Math.random()*cols.length)],r:Math.random()*6.28,vr:(Math.random()-0.5)*0.3,g:0.22+Math.random()*0.14}});let f=0;const tk=()=>{f++;ctx.clearRect(0,0,innerWidth,innerHeight);ctx.globalAlpha=f>105?Math.max(0,1-(f-105)/40):1;for(const p of P){p.x+=p.vx;p.y+=p.vy;p.vy+=p.g;p.vx*=0.985;p.r+=p.vr;ctx.save();ctx.translate(p.x,p.y);ctx.rotate(p.r);ctx.fillStyle=p.c;ctx.fillRect(-p.w/2,-p.h/2,p.w,p.h);ctx.restore()}if(f<145&&document.body.contains(cv))requestAnimationFrame(tk);else cv.remove()};requestAnimationFrame(tk);const m=document.createElement("div");m.id="nm-pv-modal";Object.assign(m.style,{position:"fixed",inset:"0",margin:"auto",width:"228px",height:"228px",zIndex:"9999998",background:"#1e1e1e",color:"#fff",border:"1px solid rgba(255,255,255,0.14)",borderRadius:"18px",boxShadow:"0 22px 55px rgba(0,0,0,0.6)",display:"flex",flexDirection:"column",alignItems:"center",justifyContent:"center",gap:"8px",padding:"18px",boxSizing:"border-box",fontFamily:"system-ui,-apple-system,sans-serif",textAlign:"center",cursor:"pointer",transform:"scale(0.82)",opacity:"0",transition:"transform 0.24s cubic-bezier(0.22,1.3,0.36,1),opacity 0.2s ease"});const mk=(t,st,tx)=>{const d=document.createElement(t);Object.assign(d.style,st);d.textContent=tx;return d};m.append(mk("div",{width:"48px",height:"48px",borderRadius:"999px",background:"rgba(6,214,160,0.16)",color:"#06d6a0",display:"flex",alignItems:"center",justifyContent:"center",fontSize:"24px",fontWeight:"700",marginBottom:"2px"},"\u2713"),mk("div",{fontSize:"16px",fontWeight:"700",letterSpacing:"-0.2px"},"Preview Mode Set!"),mk("div",{fontSize:"12.5px",lineHeight:"1.55",color:"rgba(255,255,255,0.8)",whiteSpace:"pre-line"},"Mermaid: "+mc+"\nSwitched: "+sw+"\nAlready Preview: "+ok+(nf?"\nMissed: "+nf:"")),mk("div",{fontSize:"11px",color:"rgba(255,255,255,0.38)",marginTop:"2px"},"Click to close"));const close=()=>{clearTimeout(tm);m.style.transform="scale(0.9)";m.style.opacity="0";setTimeout(()=>m.remove(),200)};m.onclick=close;document.body.appendChild(m);requestAnimationFrame(()=>{m.style.transform="scale(1)";m.style.opacity="1"});const tm=setTimeout(close,3800)},E=()=>document.dispatchEvent(new KeyboardEvent("keydown",{key:"Escape",code:"Escape",keyCode:27,bubbles:true}));const sc=[...document.querySelectorAll(".notion-scroller.vertical,.notion-frame .notion-scroller")].sort((a,b)=>(b.scrollHeight-b.clientHeight)-(a.scrollHeight-a.clientHeight))[0]||document.scrollingElement;const orig=sc.scrollTop,done=new Set();let sw=0,ok=0,nf=0,mc=0;const findPrev=(b,tr)=>{const root=document.querySelector(".notion-overlay-container")||document.body;const all=[...root.querySelectorAll('[role="button"],button,[role="menuitem"],[role="option"],[role="tab"],[role="radio"]')].filter(x=>!b.contains(x)&&V(x));const byTxt=all.find(x=>/^preview(\s+only)?$/i.test(T(x))||/^preview(\s+only)?$/i.test(L(x)));if(byTxt)return byTxt;const near=all.map(x=>({x,r:x.getBoundingClientRect()})).filter(({r})=>r.width>=14&&r.width<=64&&r.height>=14&&r.height<=64&&Math.abs((r.left+r.width/2)-(tr.left+tr.width/2))<160&&r.top>=tr.top-100&&r.top<=tr.bottom+140);const rows=[];for(const it of near){const row=rows.find(g=>Math.abs(g[0].r.top-it.r.top)<12);if(row)row.push(it);else rows.push([it])}const triple=rows.find(g=>g.length===3);if(triple){triple.sort((a,b)=>a.r.left-b.r.left);return triple[1].x}return null};const scan=async()=>{const set=new Set();document.querySelectorAll('.notion-code-block,svg[id^="mermaid"],[id^="mermaid-"]').forEach(el=>{const blk=el.closest("[data-block-id]")||el.closest(".notion-code-block")||el;if(blk)set.add(blk)});document.querySelectorAll("[data-block-id]").forEach(blk=>{if(blk.querySelector("[data-block-id]"))return;if(/^(flowchart|graph\s+(TD|TB|LR|RL|BT)|sequenceDiagram|classDiagram|stateDiagram|erDiagram|gantt|pie|mindmap|timeline|gitGraph)\b/.test((blk.innerText||"").trim()))set.add(blk)});for(const b of set){const id=b.getAttribute("data-block-id")||b;if(done.has(id))continue;b.scrollIntoView({block:"center",behavior:"instant"});await S(160);H(b);await S(240);let btns=[...b.querySelectorAll('[role="button"],button')];let mIdx=btns.findIndex(x=>/^mermaid$/i.test(T(x)));if(mIdx===-1){H(b);await S(240);btns=[...b.querySelectorAll('[role="button"],button')];mIdx=btns.findIndex(x=>/^mermaid$/i.test(T(x)))}if(mIdx===-1){done.add(id);continue}done.add(id);mc++;const hasSvg=!!b.querySelector('svg[id^="mermaid"],[id^="mermaid-"],.mermaid');const codeEl=b.querySelector('[contenteditable],pre,code');if(hasSvg&&(!codeEl||!V(codeEl))){ok++;continue}N("Switching Mermaid "+mc+"...");const tg=btns[mIdx+1];if(!tg){nf++;continue}const tr=tg.getBoundingClientRect();C(tg);let opt=null;for(let k=0;k<20;k++){await S(55);opt=findPrev(b,tr);if(opt)break}if(opt){C(opt);sw++;await S(180);E();await S(100)}else{nf++;E();await S(100)}}};N("Scanning page for Mermaid blocks...");const step=Math.max(280,Math.floor(sc.clientHeight*0.65));for(let pos=0,p=0;p<60;p++){sc.scrollTop=pos;await S(320);const atEnd=sc.scrollTop+sc.clientHeight>=sc.scrollHeight-10;await scan();if(atEnd)break;pos+=step}sc.scrollTop=Math.min(orig,sc.scrollHeight);F(mc,sw,ok,nf)})();
Details
- Handles lazy-loaded pages: Scrolls top-to-bottom so off-screen Mermaid blocks mount in the DOM before toggling them, then restores your original scroll position.
- Persistent state: Clicks Notion's native 3-icon view popover and selects the middle Preview icon so the change saves to your Notion workspace.
- Safe & local: Ignores non-Mermaid code blocks, skips diagrams already in Preview mode, and runs 100% locally in your browser with no external dependencies.