@mdgate/ipynb 0.6.11 → 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.
- package/dist/index.js +8 -8
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import{ConvertError as
|
|
2
|
-
`):"",s=[
|
|
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
|
|
4
|
+
`)?-2:-1):n}function k(n,r){let e=r.replace(/\r\n/g,`
|
|
5
5
|
`).replace(/\r/g,`
|
|
6
|
-
`);if(
|
|
7
|
-
`),i=0;while(i<
|
|
8
|
-
`)});continue}let u=o.match(/^(#{1,6})[ \t]+(.*)$/);if(u!==null){let
|
|
9
|
-
`)),f.blocks.length>0)n.blocks.push({type:"blockQuote",blocks:f.blocks});continue}if(
|
|
10
|
-
`))})}}function
|
|
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.
|
|
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.
|
|
32
|
-
"@mdgate/document": "0.6.
|
|
33
|
-
"@mdgate/utils": "0.6.
|
|
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"
|