@mdgate/ipynb 0.6.12 → 0.6.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +8 -8
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- import{ConvertError as k}from"@mdgate/core";import{documentToMarkdown as F}from"@mdgate/document";import{fileExtension as H}from"@mdgate/utils";import{ConvertError as p}from"@mdgate/core";import{emptyDocument as _,heading as v,PLAIN as R,plain as g}from"@mdgate/document";var S={bold:!1,italic:!1,strike:!1,code:!0};function D(n){let r=L(n,!1);if(r===void 0)return!1;try{return O(JSON.parse(r))}catch{return!1}}function C(n){let r=L(n,!0);if(r===void 0)throw p.malformed("invalid UTF-8");let t;try{t=JSON.parse(r)}catch{throw p.malformed("invalid JSON")}if(!O(t))throw p.malformed("not a Jupyter notebook");return $(t)}function L(n,r){let t=0;if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)t=3;try{return new TextDecoder("utf-8",{fatal:r}).decode(n.subarray(t))}catch{return}}function c(n){return typeof n==="object"&&n!==null&&!Array.isArray(n)}function O(n){return c(n)&&typeof n.nbformat==="number"&&Array.isArray(n.cells)}function $(n){let r=E(n),t=_(),e=n.cells;if(!Array.isArray(e))return t;for(let i of e){if(!c(i))continue;let o=typeof i.cell_type==="string"?i.cell_type:"";if(o==="markdown"||o==="raw")m(t,a(i.source));else if(o==="code"){if(W(t,a(i.source),r),Array.isArray(i.outputs))j(t,i.outputs)}else if(o==="heading"){let s=typeof i.level==="number"?i.level:1,u=a(i.source).trim();if(u.length>0)t.blocks.push(v(s,d(u)))}}return t}function E(n){let r=c(n.metadata)?n.metadata:void 0;if(r!==void 0){let t=c(r.language_info)?r.language_info:void 0;if(t!==void 0&&typeof t.name==="string"&&t.name.trim().length>0)return t.name.trim();let e=c(r.kernelspec)?r.kernelspec:void 0;if(e!==void 0&&typeof e.language==="string"&&e.language.trim().length>0)return e.language.trim()}return"python"}function a(n){if(typeof n==="string")return n;if(!Array.isArray(n))return"";let r="";for(let t of n)if(typeof t==="string")r+=t;return r}function W(n,r,t){let e=U(r);if(e.length===0)return;n.blocks.push({type:"codeBlock",lang:t,text:e})}function j(n,r){for(let t of r){if(!c(t))continue;let e=typeof t.output_type==="string"?t.output_type:"";if(e==="stream"){h(n,a(t.text));continue}if(e==="error"){let o=Array.isArray(t.traceback)?t.traceback.filter((u)=>typeof u==="string").join(`
2
- `):"",s=[t.ename,t.evalue].filter((u)=>typeof u==="string"&&u.length>0).join(": ");h(n,o.length>0?o:s);continue}if(e!=="execute_result"&&e!=="display_data"&&e!=="update_display_data")continue;let i=c(t.data)?t.data:void 0;if(i===void 0)continue;if(i["text/markdown"]!==void 0)m(n,a(i["text/markdown"]));if(i["text/plain"]!==void 0)h(n,a(i["text/plain"]));if(i["image/png"]!==void 0)n.blocks.push({type:"paragraph",inlines:[{type:"image",alt:"image/png",source:{type:"external",url:"image.png"}}]})}}function h(n,r){let t=U(T(r));if(t.length===0)return;n.blocks.push({type:"codeBlock",lang:void 0,text:t})}function T(n){let r="";for(let t=0;t<n.length;t+=1){if(n.charCodeAt(t)===27&&n[t+1]==="["){t+=2;while(t<n.length){let e=n.charCodeAt(t);if(e>=64&&e<=126)break;t+=1}continue}r+=n[t]}return r}function U(n){return n.endsWith(`
1
+ import{ConvertError as y}from"@mdgate/core";import{documentToMarkdown as M}from"@mdgate/document";import{fileExtension as P}from"@mdgate/utils";import{ConvertError as p}from"@mdgate/core";import{emptyDocument as v,heading as C,PLAIN as N,plain as g}from"@mdgate/document";var $={bold:!1,italic:!1,strike:!1,code:!0};function D(n){let r=O(n,!1);if(r===void 0)return!1;try{return R(JSON.parse(r))}catch{return!1}}function L(n){let r=O(n,!0);if(r===void 0)throw p.malformed("invalid UTF-8");let e;try{e=JSON.parse(r)}catch{throw p.malformed("invalid JSON")}if(!R(e))throw p.malformed("not a Jupyter notebook");return W(e)}function O(n,r){let e=0;if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)e=3;try{return new TextDecoder("utf-8",{fatal:r}).decode(n.subarray(e))}catch{return}}function l(n){return typeof n==="object"&&n!==null&&!Array.isArray(n)}function R(n){if(!l(n)||typeof n.nbformat!=="number")return!1;if(Array.isArray(n.cells))return!0;return Array.isArray(n.worksheets)}function E(n){if(Array.isArray(n.cells))return n.cells;if(!Array.isArray(n.worksheets))return[];let r=[];for(let e of n.worksheets){if(!l(e)||!Array.isArray(e.cells))continue;for(let t of e.cells)r.push(t)}return r}function h(n){if(n.source!==void 0)return a(n.source);return a(n.input)}function W(n){let r=j(n),e=v();for(let t of E(n)){if(!l(t))continue;let i=typeof t.cell_type==="string"?t.cell_type:"";if(i==="markdown"||i==="raw")k(e,h(t));else if(i==="code"){if(T(e,h(t),r),Array.isArray(t.outputs))J(e,t.outputs)}else if(i==="heading"){let o=typeof t.level==="number"?t.level:1,s=h(t).trim();if(s.length>0)e.blocks.push(C(o,d(s)))}}return e}function j(n){let r=l(n.metadata)?n.metadata:void 0;if(r!==void 0){let e=l(r.language_info)?r.language_info:void 0;if(e!==void 0&&typeof e.name==="string"&&e.name.trim().length>0)return e.name.trim();let t=l(r.kernelspec)?r.kernelspec:void 0;if(t!==void 0&&typeof t.language==="string"&&t.language.trim().length>0)return t.language.trim()}return"python"}function a(n){if(typeof n==="string")return n;if(!Array.isArray(n))return"";let r="";for(let e of n)if(typeof e==="string")r+=e;return r}function T(n,r,e){let t=S(r);if(t.length===0)return;n.blocks.push({type:"codeBlock",lang:e,text:t})}function J(n,r){for(let e of r){if(!l(e))continue;let t=typeof e.output_type==="string"?e.output_type:"";if(t==="stream"){m(n,a(e.text));continue}if(t==="error"){let o=Array.isArray(e.traceback)?e.traceback.filter((u)=>typeof u==="string").join(`
2
+ `):"",s=[e.ename,e.evalue].filter((u)=>typeof u==="string"&&u.length>0).join(": ");m(n,o.length>0?o:s);continue}if(t!=="execute_result"&&t!=="display_data"&&t!=="update_display_data")continue;let i=l(e.data)?e.data:void 0;if(i===void 0)continue;if(i["text/markdown"]!==void 0)k(n,a(i["text/markdown"]));if(i["text/plain"]!==void 0)m(n,a(i["text/plain"]));if(i["image/png"]!==void 0)n.blocks.push({type:"paragraph",inlines:[{type:"image",alt:"image/png",source:{type:"external",url:"image.png"}}]})}}function m(n,r){let e=S(B(r));if(e.length===0)return;n.blocks.push({type:"codeBlock",lang:void 0,text:e})}function B(n){let r="";for(let e=0;e<n.length;e+=1){if(n.charCodeAt(e)===27&&n[e+1]==="["){e+=2;while(e<n.length){let t=n.charCodeAt(e);if(t>=64&&t<=126)break;e+=1}continue}r+=n[e]}return r}function S(n){return n.endsWith(`
3
3
  `)?n.slice(0,n.endsWith(`\r
4
- `)?-2:-1):n}function m(n,r){let t=r.replace(/\r\n/g,`
4
+ `)?-2:-1):n}function k(n,r){let e=r.replace(/\r\n/g,`
5
5
  `).replace(/\r/g,`
6
- `);if(t.trim().length===0)return;let e=t.split(`
7
- `),i=0;while(i<e.length){let o=e[i];if(o.trim().length===0){i+=1;continue}let s=o.match(/^(```+|~~~+)(.*)$/);if(s!==null){let l=s[1],f=s[2].trim(),x=[];i+=1;while(i<e.length&&!e[i].startsWith(l))x.push(e[i]),i+=1;if(i<e.length)i+=1;n.blocks.push({type:"codeBlock",lang:f.length>0?f:void 0,text:x.join(`
8
- `)});continue}let u=o.match(/^(#{1,6})[ \t]+(.*)$/);if(u!==null){let l=u[2].replace(/[ \t]+#*[ \t]*$/,"").trim();if(l.length>0)n.blocks.push(v(u[1].length,d(l)));i+=1;continue}if(I(o)){n.blocks.push({type:"rule"}),i+=1;continue}if(/^>[ \t]?/.test(o)){let l=[];while(i<e.length&&/^>[ \t]?/.test(e[i]))l.push(e[i].replace(/^>[ \t]?/,"")),i+=1;let f=_();if(m(f,l.join(`
9
- `)),f.blocks.length>0)n.blocks.push({type:"blockQuote",blocks:f.blocks});continue}if(N(o)){i=B(n,e,i);continue}let w=[o];i+=1;while(i<e.length&&e[i].trim().length>0&&!J(e[i]))w.push(e[i]),i+=1;n.blocks.push({type:"paragraph",inlines:d(w.join(`
10
- `))})}}function I(n){let r=n.trim();return/^(\*[ \t]*){3,}$/.test(r)||/^(-[ \t]*){3,}$/.test(r)||/^(_[ \t]*){3,}$/.test(r)}function N(n){return/^\s*(?:[-*+]|\d+[.)])\s+\S/.test(n)}function J(n){return/^(```+|~~~+)/.test(n)||/^#{1,6}[ \t]+/.test(n)||I(n)||/^>[ \t]?/.test(n)||N(n)}function B(n,r,t){let e=/^\s*\d+[.)]\s+\S/.test(r[t]),i=e?/^\s*\d+[.)]\s+(.*)$/:/^\s*[-*+]\s+(.*)$/,o=[],s=t;while(s<r.length){let u=r[s].match(i);if(u===null)break;o.push({blocks:[{type:"paragraph",inlines:d(u[1])}],checked:void 0,markerLabel:void 0}),s+=1}if(o.length>0)n.blocks.push({type:"list",list:{marker:e?"decimal":"bullet",start:1,items:o}});return s}function d(n){let r=[],t="",e=0,i=()=>{if(t.length===0)return;r.push(g(t)),t=""};while(e<n.length){if(n.startsWith("![",e)){let o=A(n,e+1);if(o!==void 0){i(),r.push({type:"image",alt:o.label,source:{type:"external",url:o.url}}),e=o.end;continue}}if(n[e]==="["){let o=A(n,e);if(o!==void 0){i(),r.push({type:"link",content:[g(o.label)],target:{type:"external",url:o.url}}),e=o.end;continue}}if(n[e]==="`"){let o=n.indexOf("`",e+1);if(o>e+1){i(),r.push({type:"text",text:n.slice(e+1,o),style:S}),e=o+1;continue}}if(n.startsWith("**",e)||n.startsWith("__",e)){let o=n.slice(e,e+2),s=n.indexOf(o,e+2);if(s>e+2){i(),r.push({type:"text",text:n.slice(e+2,s),style:{...R,bold:!0}}),e=s+2;continue}}t+=n[e],e+=1}return i(),r.length>0?r:[g("")]}function A(n,r){if(n[r]!=="[")return;let t=n.indexOf("]",r+1);if(t<0||n[t+1]!=="(")return;let e=n.indexOf(")",t+2);if(e<0)return;return{label:n.slice(r+1,t),url:n.slice(t+2,e),end:e+1}}function b(){return{id:"ipynb",sniff(n,r){if(D(n))return 3;if(r?.path!==void 0&&H(r.path)==="ipynb")return 1;return 0},convert(n){return M(n),{markdown:F(C(n))}}}}function M(n){if(y(n,[37,80,68,70,45]))throw k.unsupported("pdf");if(y(n,[208,207,17,224,161,177,26,225]))throw k.unsupported("ole");if(y(n,[80,75,3,4]))throw k.unsupported("zip")}function y(n,r){if(n.length<r.length)return!1;for(let t=0;t<r.length;t+=1)if(n[t]!==r[t])return!1;return!0}import{create as P}from"@mdgate/core";var q=P([b()]);function z(n,r){return q(n,r)}export{z as toMarkdown,b as ipynb};
6
+ `);if(e.trim().length===0)return;let t=e.split(`
7
+ `),i=0;while(i<t.length){let o=t[i];if(o.trim().length===0){i+=1;continue}let s=o.match(/^(```+|~~~+)(.*)$/);if(s!==null){let c=s[1],f=s[2].trim(),A=[];i+=1;while(i<t.length&&!t[i].startsWith(c))A.push(t[i]),i+=1;if(i<t.length)i+=1;n.blocks.push({type:"codeBlock",lang:f.length>0?f:void 0,text:A.join(`
8
+ `)});continue}let u=o.match(/^(#{1,6})[ \t]+(.*)$/);if(u!==null){let c=u[2].replace(/[ \t]+#*[ \t]*$/,"").trim();if(c.length>0)n.blocks.push(C(u[1].length,d(c)));i+=1;continue}if(U(o)){n.blocks.push({type:"rule"}),i+=1;continue}if(/^>[ \t]?/.test(o)){let c=[];while(i<t.length&&/^>[ \t]?/.test(t[i]))c.push(t[i].replace(/^>[ \t]?/,"")),i+=1;let f=v();if(k(f,c.join(`
9
+ `)),f.blocks.length>0)n.blocks.push({type:"blockQuote",blocks:f.blocks});continue}if(I(o)){i=H(n,t,i);continue}let x=[o];i+=1;while(i<t.length&&t[i].trim().length>0&&!F(t[i]))x.push(t[i]),i+=1;n.blocks.push({type:"paragraph",inlines:d(x.join(`
10
+ `))})}}function U(n){let r=n.trim();return/^(\*[ \t]*){3,}$/.test(r)||/^(-[ \t]*){3,}$/.test(r)||/^(_[ \t]*){3,}$/.test(r)}function I(n){return/^\s*(?:[-*+]|\d+[.)])\s+\S/.test(n)}function F(n){return/^(```+|~~~+)/.test(n)||/^#{1,6}[ \t]+/.test(n)||U(n)||/^>[ \t]?/.test(n)||I(n)}function H(n,r,e){let t=/^\s*\d+[.)]\s+\S/.test(r[e]),i=t?/^\s*\d+[.)]\s+(.*)$/:/^\s*[-*+]\s+(.*)$/,o=[],s=e;while(s<r.length){let u=r[s].match(i);if(u===null)break;o.push({blocks:[{type:"paragraph",inlines:d(u[1])}],checked:void 0,markerLabel:void 0}),s+=1}if(o.length>0)n.blocks.push({type:"list",list:{marker:t?"decimal":"bullet",start:1,items:o}});return s}function d(n){let r=[],e="",t=0,i=()=>{if(e.length===0)return;r.push(g(e)),e=""};while(t<n.length){if(n.startsWith("![",t)){let o=_(n,t+1);if(o!==void 0){i(),r.push({type:"image",alt:o.label,source:{type:"external",url:o.url}}),t=o.end;continue}}if(n[t]==="["){let o=_(n,t);if(o!==void 0){i(),r.push({type:"link",content:[g(o.label)],target:{type:"external",url:o.url}}),t=o.end;continue}}if(n[t]==="`"){let o=n.indexOf("`",t+1);if(o>t+1){i(),r.push({type:"text",text:n.slice(t+1,o),style:$}),t=o+1;continue}}if(n.startsWith("**",t)||n.startsWith("__",t)){let o=n.slice(t,t+2),s=n.indexOf(o,t+2);if(s>t+2){i(),r.push({type:"text",text:n.slice(t+2,s),style:{...N,bold:!0}}),t=s+2;continue}}e+=n[t],t+=1}return i(),r.length>0?r:[g("")]}function _(n,r){if(n[r]!=="[")return;let e=n.indexOf("]",r+1);if(e<0||n[e+1]!=="(")return;let t=n.indexOf(")",e+2);if(t<0)return;return{label:n.slice(r+1,e),url:n.slice(e+2,t),end:t+1}}function w(){return{id:"ipynb",sniff(n,r){if(D(n))return 3;if(r?.path!==void 0&&P(r.path)==="ipynb")return 1;return 0},convert(n){return q(n),{markdown:M(L(n))}}}}function q(n){if(b(n,[37,80,68,70,45]))throw y.unsupported("pdf");if(b(n,[208,207,17,224,161,177,26,225]))throw y.unsupported("ole");if(b(n,[80,75,3,4]))throw y.unsupported("zip")}function b(n,r){if(n.length<r.length)return!1;for(let e=0;e<r.length;e+=1)if(n[e]!==r[e])return!1;return!0}import{create as z}from"@mdgate/core";var Q=z([w()]);function Y(n,r){return Q(n,r)}export{Y as toMarkdown,w as ipynb};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdgate/ipynb",
3
- "version": "0.6.12",
3
+ "version": "0.6.13",
4
4
  "description": "mdgate Jupyter notebook converter",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -28,9 +28,9 @@
28
28
  "prepublishOnly": "bun run build"
29
29
  },
30
30
  "dependencies": {
31
- "@mdgate/core": "0.6.12",
32
- "@mdgate/document": "0.6.12",
33
- "@mdgate/utils": "0.6.12"
31
+ "@mdgate/core": "0.6.13",
32
+ "@mdgate/document": "0.6.13",
33
+ "@mdgate/utils": "0.6.13"
34
34
  },
35
35
  "publishConfig": {
36
36
  "access": "public"