@mdgate/onenote 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,29 @@
1
+ # @mdgate/onenote
2
+
3
+ Convert Microsoft OneNote section and notebook files to Markdown. Outputs
4
+ GitHub-Flavored Markdown. Works in Node, Edge, and browsers. No native addons.
5
+
6
+ Handles: `.one`, `.onetoc2`, `.onepkg`
7
+
8
+ v1 is best-effort text extraction (page titles and body lines), not a full
9
+ OneNote renderer.
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { toMarkdown } from '@mdgate/onenote';
15
+
16
+ const markdown = await toMarkdown(bytes);
17
+ ```
18
+
19
+ Compose with other converters:
20
+
21
+ ```ts
22
+ import { create } from '@mdgate/core';
23
+ import { onenote } from '@mdgate/onenote';
24
+
25
+ const convert = create([onenote()]);
26
+ ```
27
+
28
+ Part of [mdgate converters](https://github.com/mdgate/converters); install
29
+ `@mdgate/converters` for every format at once.
@@ -0,0 +1,2 @@
1
+ export { onenote } from './onenote.js';
2
+ export { toMarkdown } from './to-markdown.js';
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ import{detectOleDoc as Bn,detectZipDoc as Dn,Package as En}from"@mdgate/containers";import{ConvertError as g}from"@mdgate/core";import{documentToMarkdown as $,emptyDocument as On,heading as jn,plain as C}from"@mdgate/document";import{fileExtension as P}from"@mdgate/utils";import{CompoundFile as F,hasOleMagic as A}from"@mdgate/containers";import{ConvertError as d}from"@mdgate/core";import{emptyDocument as T,heading as dn,plain as h}from"@mdgate/document";import{cleanText as cn,collapseWs as $n,isAlphanumeric as zn}from"@mdgate/utils";import{CompoundFile as t,detectOleDoc as s,hasOleMagic as X}from"@mdgate/containers";var e=[228,82,92,123,140,216,167,77,174,177,83,120,208,41,150,211],nn=[250,55,221,31,190,73,208,17,140,85,0,192,79,217,15,133],rn=[161,47,255,67,217,239,118,76,158,226,16,234,87,34,118,95],on=[63,221,154,16,27,145,245,73,165,208,23,145,237,200,174,216],un=[e,nn,rn],mn=["{7B5C52E4-D88C-4DA7-AEB1-5378D02996D3}","{1FDD37FA-49BE-11D0-8C55-00C04FD90F85}","{43FF2FA1-EFD9-4C76-9EE2-10EA5722765F}"],pn=[79,78,69,32];function z(n){if(s(n)==="one")return!0;return vn(n)}function vn(n){if(!X(n))return!1;try{let r=t.open(n);for(let o of r.readRootStorage())if(o.name.length>=7&&o.name.slice(0,7).toLowerCase()==="onenote")return!0}catch{return!1}return!1}function w(n){return z(n)||fn(n)}function fn(n){if(B(n)||D(n)||E(n))return!0;return O(n)}function L(n){if(B(n))return n.length>=1024?1024:16;if(E(n))return 4;if(O(n))return 38;if(D(n))return n.length>=1024?1024:64;return 0}function B(n){for(let r of un)if(K(n,r,0))return!0;return!1}function D(n){return K(n,on,48)}function E(n){return K(n,pn,0)}function O(n){for(let r of mn)if(wn(n,r,0))return!0;return!1}function j(n){let r=gn(n);return kn(n,"%PDF-",r)}function Z(n){return n.length>=4&&n[0]===80&&n[1]===75&&n[2]===3&&n[3]===4}function R(n){return X(n)}function gn(n){let r=0;if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)r=3;while(r<n.length){let o=n[r];if(o!==9&&o!==10&&o!==13&&o!==32)break;r+=1}return r}function K(n,r,o){if(o+r.length>n.length)return!1;for(let u=0;u<r.length;u+=1)if(n[o+u]!==r[u])return!1;return!0}function kn(n,r,o){if(o+r.length>n.length)return!1;for(let u=0;u<r.length;u+=1)if(n[o+u]!==r.charCodeAt(u))return!1;return!0}function wn(n,r,o){if(o+r.length>n.length)return!1;for(let u=0;u<r.length;u+=1){let p=n[o+u],m=r.charCodeAt(u);if(p===m)continue;let f=p>=65&&p<=90?p+32:p,v=m>=65&&m<=90?m+32:m;if(f!==v)return!1}return!0}var V=4,U=16384,i=2000,Zn=2,Kn=/^\{?[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}?$/i,Jn=["jcid","fcr","objectspace","filedatastore","persistable","onenote_","microsoft.office","http://","https://","xmlns","application/"];function N(n){try{if(A(n))return Qn(n);return Q(M(n))}catch(r){if(r instanceof d&&(r.code==="encrypted"||r.isFatal()))throw r;try{return Q(M(n))}catch(o){if(o instanceof d&&o.isFatal())throw o;return T()}}}function G(n){if(A(n))try{let r=F.open(n);if(r.exists("EncryptionInfo")||r.exists("EncryptedPackage"))return!0}catch{}return W(n,"this section is password protected")||W(n,"this notebook is password protected")||S(n,"this section is password protected")||S(n,"this notebook is password protected")}function Qn(n){let r=F.open(n);if(r.exists("EncryptionInfo")||r.exists("EncryptedPackage"))throw d.encrypted();let o=[];for(let u of r.readRootStorage()){if(u.type!==Zn)continue;try{J(r.readStream(u.name),o)}catch(p){if(p instanceof d&&p.isFatal())throw p}if(o.length>=i)break}if(o.length===0)J(n,o);return Q(o)}function M(n){let r=[];return J(n,r),r}function J(n,r){let o=L(n),u=o>0?n.subarray(o):n;Vn(u,r),qn(u,r)}function Vn(n,r){let o=new TextDecoder("utf-16le");for(let u of[0,1]){let p=-1,m=(v)=>{if(p>=0){let k=v-p>>1;if(k>=V&&k<=U&&r.length<i)I(o.decode(n.subarray(p,v)),r)}p=-1};for(let v=u;v+1<n.length;v+=2){let k=n[v]|n[v+1]<<8;if(k!==0&&_(k)){if(p<0)p=v}else m(v)}let f=n.length-(n.length-u&1);m(f)}}function qn(n,r){let o=new TextDecoder("utf-8",{fatal:!1}).decode(n),u=-1,p=(m)=>{if(u>=0&&m-u>=V&&m-u<=U&&r.length<i)I(o.slice(u,m),r);u=-1};for(let m=0;m<o.length;m+=1){let f=o.charCodeAt(m);if(_(f)){if(u<0)u=m}else p(m)}p(o.length)}function I(n,r){let o=$n(cn(n)).trim();if(!Hn(o))return;if(r.includes(o))return;r.push(o)}function _(n){if(n===9||n===10||n===13||n===32)return!0;if(n<32||n>=127&&n<=159)return!1;return n!==65534&&n!==65535}function Hn(n){if(n.length<V)return!1;if(Kn.test(n))return!1;if(Cn(n.toLowerCase()))return!1;let r=0;for(let o of n)if(zn(o))r+=1;if(r<2)return!1;if(r*4<n.length)return!1;return!0}function Cn(n){for(let r of Jn)if(n.startsWith(r))return!0;return n.includes("xmlns")||n.includes("http://schemas.")||n.includes("https://schemas.")}function Q(n){let r=T(),o=new Set,u=!0,p=!1;for(let m of n){if(o.has(m))continue;o.add(m);let f=Pn(m,u,p);r.blocks.push(f),p=f.type==="paragraph",u=!1}return r}function Pn(n,r,o){if(Yn(n,r,o))return dn(r?1:2,[h(n)]);return{type:"paragraph",inlines:[h(n)]}}function Yn(n,r,o){if(n.includes(`
2
+ `))return!1;if(r)return n.length<=80;if(!o)return!1;return n.length<=40&&!/[.!?]/.test(n)}function S(n,r){if(r.length===0||n.length<r.length)return!1;let o=r.length,u=r.charCodeAt(0),p=u>=65&&u<=90?u+32:u;for(let m=0;m<=n.length-o;m+=1){let f=n[m];if((f>=65&&f<=90?f+32:f)!==p)continue;if(Xn(n,m,r))return!0}return!1}function W(n,r){let o=r.length;if(o===0||n.length<o*2)return!1;let u=r.charCodeAt(0),p=u>=65&&u<=90?u+32:u;for(let m of[0,1])for(let f=m;f+o*2<=n.length;f+=2){let v=n[f]|n[f+1]<<8;if((v>=65&&v<=90?v+32:v)!==p)continue;if(Ln(n,f,r))return!0}return!1}function Xn(n,r,o){for(let u=1;u<o.length;u+=1){let p=n[r+u],m=o.charCodeAt(u),f=p>=65&&p<=90?p+32:p,v=m>=65&&m<=90?m+32:m;if(f!==v)return!1}return!0}function Ln(n,r,o){for(let u=1;u<o.length;u+=1){let p=n[r+u*2]|n[r+u*2+1]<<8,m=o.charCodeAt(u),f=p>=65&&p<=90?p+32:p,v=m>=65&&m<=90?m+32:m;if(f!==v)return!1}return!0}var l=new Set(["one","onetoc2","onepkg"]);function Y(){return{id:"onenote",sniff(n,r){if(w(n))return 2;if(r?.path!==void 0&&l.has(P(r.path)??""))return 1;return 0},convert(n,r){if(Rn(n,r?.path),G(n))throw g.encrypted();if(Z(n))return hn(n,r);return{markdown:$(N(n))}}}}function Rn(n,r){if(j(n))throw g.unsupported("pdf");let o=Bn(n);if(o!==void 0&&o!=="one"&&!z(n))throw g.unsupported(o);if(R(n)&&!w(n)&&!x(r))throw g.unsupported(o??"ole");let u=Dn(n);if(u!==void 0)throw g.unsupported(u);if(Z(n))return;if(w(n)||x(r))return;throw g.unsupported("onenote")}function x(n){if(n===void 0)return!1;return l.has(P(n)??"")}function hn(n,r){let o;try{o=En.open(n)}catch(m){throw Nn(m)}let u=Sn(o),p=u.length>0?u:Wn(o);if(r?.convert!==void 0&&u.length>0)return Mn(o,u,r.convert);return{markdown:$(An(p))}}async function Mn(n,r,o){let u=[];for(let p of r){let m=Tn(p),f;try{f=n.part(p)}catch(v){if(v instanceof g&&v.isFatal())throw v;if(H(v))throw g.encrypted();u.push(c(m,q(a(v))));continue}if(f===void 0){u.push(c(m,q(`missing required part: ${p}`)));continue}try{let v=await o(f,{path:p});u.push(c(m,v.trimEnd()))}catch(v){if(v instanceof g&&v.isFatal())throw v;if(H(v))throw g.encrypted();u.push(c(m,q(a(v))))}}return{markdown:Un(u)}}function Sn(n){let r=[];for(let o of n.partNames()){if(!Fn(o))continue;r.push(o)}return r}function Wn(n){let r=[];for(let o of n.partNames()){if(o.endsWith("/"))continue;if(y(o))continue;r.push(o)}return r}function Fn(n){if(n.endsWith("/")||y(n))return!1;let r=P(n);return r==="one"||r==="onetoc2"}function y(n){for(let r of n.split("/"))if(r==="__MACOSX")return!0;return!1}function An(n){if(n.length===0)return On();return{blocks:[{type:"list",list:{marker:"bullet",start:1,items:n.map((r)=>({blocks:[{type:"paragraph",inlines:[C(r)]}],checked:void 0,markerLabel:void 0}))}}],notes:[],assets:[]}}function Tn(n){return $({blocks:[jn(2,[C(n)])],notes:[],assets:[]}).trimEnd()}function q(n){return $({blocks:[{type:"paragraph",inlines:[C(`could not convert: ${n}`)]}],notes:[],assets:[]}).trimEnd()}function c(n,r){return r.length===0?n:`${n}
3
+
4
+ ${r}`}function Un(n){let r=n.filter((o)=>o.length>0);return r.length===0?"":`${r.join(`
5
+
6
+ `)}
7
+ `}function a(n){return n instanceof Error?n.message:String(n)}function Nn(n){if(n instanceof g){if(n.code==="encrypted"||H(n))return g.encrypted();if(n.isFatal())return n;return g.unsupported("zip")}let r=n instanceof Error?n.message:String(n);if(b(r))return g.encrypted();return g.unsupported("zip")}function H(n){if(n instanceof g&&n.code==="encrypted")return!0;let r=n instanceof g?n.detail??"":"",o=n instanceof Error?`${n.message} ${r}`:String(n);return b(o)}function b(n){let r=n.toLowerCase();return r.includes("encrypted")||r.includes("password")}import{create as Gn}from"@mdgate/core";var In=Gn([Y()]);function _n(n,r){return In(n,r)}export{_n as toMarkdown,Y as onenote};
@@ -0,0 +1,2 @@
1
+ import type { Converter } from '@mdgate/core';
2
+ export declare function onenote(): Converter;
@@ -0,0 +1,6 @@
1
+ import { type ConvertHint } from '@mdgate/core';
2
+ /**
3
+ * Convert a single file's bytes to Markdown with the onenote 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,44 @@
1
+ {
2
+ "name": "@mdgate/onenote",
3
+ "version": "0.1.0",
4
+ "description": "mdgate OneNote converter (.one / .onetoc2 / .onepkg)",
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/containers": "0.1.0",
26
+ "@mdgate/core": "0.1.3",
27
+ "@mdgate/document": "0.1.0",
28
+ "@mdgate/utils": "0.1.2"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "engines": {
34
+ "node": ">=20"
35
+ },
36
+ "keywords": [
37
+ "mdgate",
38
+ "markdown",
39
+ "onenote",
40
+ "one",
41
+ "onetoc2",
42
+ "onepkg"
43
+ ]
44
+ }