notebook-mdx
Version:
Jupyter Notebook support for MDX via Remark and Rehype plugins
2 lines • 1.35 kB
JavaScript
import{readFileSync as e}from"fs";import{dirname as t,resolve as n}from"path";import{visit as r}from"unist-util-visit";const i=e=>e===void 0?void 0:e===`true`||e===``,a=(a={})=>{let{componentName:o=`NotebookLoader`}=a;return(s,c)=>{r(s,r=>{if(![`containerDirective`,`leafDirective`,`textDirective`].includes(r.type)||r.name!==`notebook`)return;r.type===`textDirective`&&c.fail("Unexpected `:notebook` text directive, use two colons for a leaf directive or three colons for a container directive",r);let s=r.attributes?.file;s||c.fail("Unexpected missing `file` attribute on `notebook` directive",r);try{let l=a.baseDir||(c.path?t(c.path):process.cwd()),u=n(l,s),d=c.data;d.dependencies||=[],d.dependencies.includes(u)||d.dependencies.push(u),c.history.includes(u)||c.history.push(u);let f=e(u,`utf-8`),p=JSON.parse(f),{file:m,...h}=r.attributes||{},g={notebookDataJson:JSON.stringify(p),...h};[`hideCode`,`showOutputs`,`showCellNumbers`,`showLanguageIndicators`,`interactive`].forEach(e=>{let t=i(h[e]);t!==void 0&&(g[e]=t)});let _=r.data||={};_.hName=o,_.hProperties=g,r.type===`containerDirective`&&r.children?.length>0&&(g.caption=r.children,r.children=[])}catch(e){c.fail(`Failed to process notebook file "${s}": ${e instanceof Error?e.message:String(e)}`,r)}})}};export{a as remarkNotebookDirective};
//# sourceMappingURL=server-DlxJH4C0.js.map