@mdgate/ipynb 0.1.0

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/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # @mdgate/ipynb
2
+
3
+ Convert Jupyter notebooks to Markdown. Outputs GitHub-Flavored Markdown. Works in Node, Edge, and
4
+ browsers. No native addons.
5
+
6
+ Handles: `.ipynb` (nbformat JSON)
7
+
8
+ ## Usage
9
+
10
+ ```ts
11
+ import { toMarkdown } from '@mdgate/ipynb';
12
+
13
+ const markdown = await toMarkdown(bytes);
14
+ ```
15
+
16
+ Compose with other converters:
17
+
18
+ ```ts
19
+ import { create } from '@mdgate/core';
20
+ import { ipynb } from '@mdgate/ipynb';
21
+
22
+ const convert = create([ipynb()]);
23
+ ```
24
+
25
+ Part of [mdgate converters](https://github.com/mdgate/converters); install
26
+ `@mdgate/converters` for every format at once.
@@ -0,0 +1,2 @@
1
+ export { ipynb } from './ipynb.js';
2
+ export { toMarkdown } from './to-markdown.js';
package/dist/index.js ADDED
@@ -0,0 +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(`
3
+ `)?n.slice(0,n.endsWith(`\r
4
+ `)?-2:-1):n}function m(n,r){let t=r.replace(/\r\n/g,`
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 2;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};
@@ -0,0 +1,2 @@
1
+ import type { Converter } from '@mdgate/core';
2
+ export declare function ipynb(): Converter;
@@ -0,0 +1,6 @@
1
+ import { type ConvertHint } from '@mdgate/core';
2
+ /**
3
+ * Convert a single file's bytes to Markdown with the ipynb converter.
4
+ * `hint.path` is only a sniff hint; it is never read from disk.
5
+ */
6
+ export declare function toMarkdown(bytes: Uint8Array, hint?: ConvertHint): Promise<string>;
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@mdgate/ipynb",
3
+ "version": "0.1.0",
4
+ "description": "mdgate Jupyter notebook converter",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "sideEffects": false,
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "main": "./dist/index.js",
15
+ "types": "./dist/index.d.ts",
16
+ "files": [
17
+ "dist/**/*.js",
18
+ "dist/**/*.d.ts"
19
+ ],
20
+ "scripts": {
21
+ "build": "bun ../../scripts/build-package.ts",
22
+ "prepublishOnly": "bun run build"
23
+ },
24
+ "dependencies": {
25
+ "@mdgate/core": "0.1.3",
26
+ "@mdgate/document": "0.1.0",
27
+ "@mdgate/utils": "0.1.2"
28
+ },
29
+ "publishConfig": {
30
+ "access": "public"
31
+ },
32
+ "engines": {
33
+ "node": ">=20"
34
+ },
35
+ "keywords": [
36
+ "mdgate",
37
+ "markdown",
38
+ "ipynb",
39
+ "jupyter",
40
+ "notebook"
41
+ ]
42
+ }