@mdgate/visio 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 +26 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/to-markdown.d.ts +6 -0
- package/dist/visio.d.ts +2 -0
- package/package.json +44 -0
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @mdgate/visio
|
|
2
|
+
|
|
3
|
+
Convert Microsoft Visio drawings to Markdown. Outputs GitHub-Flavored Markdown. Works in Node, Edge,
|
|
4
|
+
and browsers. No native addons.
|
|
5
|
+
|
|
6
|
+
Handles: `.vsd`, `.vsdx`, `.vss`, `.vst`, `.vssx`, `.vstx`
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
import { toMarkdown } from '@mdgate/visio';
|
|
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 { visio } from '@mdgate/visio';
|
|
21
|
+
|
|
22
|
+
const convert = create([visio()]);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Part of [mdgate converters](https://github.com/mdgate/converters); install
|
|
26
|
+
`@mdgate/converters` for every format at once.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{create as Fn}from"@mdgate/core";import{CompoundFile as Tn,detectOleDoc as D,detectZipDoc as O,hasOleMagic as W,Package as Un}from"@mdgate/containers";import{ConvertError as x}from"@mdgate/core";import{documentToMarkdown as Ln}from"@mdgate/document";import{fileExtension as _n}from"@mdgate/utils";import{hasOleMagic as vn}from"@mdgate/containers";import{ConvertError as Sn}from"@mdgate/core";import{CompoundFile as Q}from"@mdgate/containers";import{ConvertError as d}from"@mdgate/core";import{emptyDocument as q,plain as X}from"@mdgate/document";import{cleanText as Y,collapseWs as H,isAlphanumeric as J}from"@mdgate/utils";var K=2,w=2;function A(n){let t;try{t=Q.open(n)}catch(i){if(i instanceof d)throw i;let a=i instanceof Error?i.message:String(i);throw d.malformed(`not an OLE2 compound file: ${a}`)}if(t.exists("EncryptionInfo")||t.exists("EncryptedPackage"))throw d.encrypted();let r=!1,o=[];for(let i of t.readRootStorage()){if(nn(i.name,"VisioDocument"))r=!0;if(i.type!==K)continue;try{Z(t.readStream(i.name),o)}catch(a){if(a instanceof d&&a.isFatal())throw a}}if(!r)throw d.malformed("not a Visio document");let s=new Set,e=q();for(let i of o){let a=H(Y(i)).trim();if(!b(a)||s.has(a))continue;s.add(a),e.blocks.push({type:"paragraph",inlines:[X(a)]})}if(e.blocks.length===0)throw d.malformed("no readable text in Visio document");return e}function Z(n,t){z(n,t),j(n,t)}function z(n,t){let r=[],o=()=>{if(r.length>=w)t.push(y(r));r.length=0};for(let s=0;s+1<n.length;s+=2){let e=n[s]|n[s+1]<<8;if(e!==0&&C(e))r.push(e);else o()}o()}function j(n,t){let r=new TextDecoder("utf-8",{fatal:!1}).decode(n),o=-1,s=(e)=>{if(o>=0&&e-o>=w)t.push(r.slice(o,e));o=-1};for(let e=0;e<r.length;e+=1)if(C(r.charCodeAt(e))){if(o<0)o=e}else s(e);s(r.length)}function y(n){if(n.length<=8192)return String.fromCharCode(...n);let r="";for(let o=0;o<n.length;o+=8192)r+=String.fromCharCode(...n.slice(o,o+8192));return r}function C(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 b(n){if(n.length<w)return!1;let t=0;for(let o of n)if(J(o))t+=1;if(t<2)return!1;let r=n.toLowerCase();if(r.includes("xmlns")||r.includes("http://schemas.")||r.includes("https://schemas.")||r.includes("visiodocument"))return!1;return!0}function nn(n,t){return n.length===t.length&&n.toLowerCase()===t.toLowerCase()}import{ns as h,Package as tn,probeOle as rn,readRels as E,relsPartFor as T,relType as on,resolve as en}from"@mdgate/containers";import{ConvertError as l}from"@mdgate/core";import{emptyDocument as an,heading as sn,plain as U}from"@mdgate/document";import{alternateBranch as cn}from"@mdgate/office-common";import{cleanText as L,collapseWs as _}from"@mdgate/utils";var fn="http://schemas.microsoft.com/office/visio/2012/main",un="http://schemas.microsoft.com/visio/2003/core",ln="http://schemas.microsoft.com/visio/2010/relationships/pages",mn="http://schemas.microsoft.com/visio/2010/relationships/page",dn="http://schemas.microsoft.com/visio/2010/relationships/masters",hn="http://schemas.microsoft.com/visio/2010/relationships/master",gn=[fn,un,h.R,h.MC];function I(n){let t;try{t=tn.open(n)}catch(e){throw Rn(e,n)}let r=pn(t);if(r.length===0)throw l.missingPart("visio/pages/pages.xml");let o=an(),s=!1;for(let e=0;e<r.length;e+=1){let i=r[e],a=t.optionalXmlPart(i.path);if(a===void 0)continue;let f=o.blocks.length,c=i.name.length>0?i.name:`Page ${e+1}`;if(o.blocks.push(sn(1,[U(c)])),P(a,o.blocks),o.blocks.length===f+1)o.blocks.pop();else s=!0}if(!s)throw l.malformed("no readable text in Visio document");return o}function pn(n){let t=xn(n)??"visio/document.xml",r=E(n,T(t)),o=N(r,t,ln)??"visio/pages/pages.xml",s=v(n,o,"Page",mn);if(s.length>0)return s;let e=S(n,"visio/pages/","pages.xml");if(e.length>0)return e;let i=N(r,t,dn)??"visio/masters/masters.xml",a=v(n,i,"Master",hn);if(a.length>0)return a;return S(n,"visio/masters/","masters.xml")}function xn(n){let t=E(n,"_rels/.rels"),r=V(t,on.OFFICE_DOCUMENT);if(r!==void 0){let o=p("",r.target);if(o!==void 0)return o}if(n.hasPart("visio/document.xml"))return"visio/document.xml";return}function v(n,t,r,o){let s=n.optionalXmlPart(t);if(s===void 0)return[];let e=E(n,T(t)),i=[],a=new Set;for(let f of s.descendantsAny(r)){let c=wn(f),m=c!==void 0?e.internalTarget(c):void 0,u=m!==void 0?p(t,m):void 0;if(u===void 0||a.has(u))continue;a.add(u),i.push({name:Pn(f),path:u})}if(i.length>0)return i;for(let[,f]of e.entries()){if(f.mode!=="internal"||!M(f.relType,o))continue;let c=p(t,f.target);if(c===void 0||a.has(c))continue;a.add(c),i.push({name:"",path:c})}return i}function S(n,t,r){let o=t.toLowerCase(),s=r.toLowerCase(),e=[];for(let i of n.partNames()){let a=i.toLowerCase();if(!a.startsWith(o)||!a.endsWith(".xml"))continue;if(a.endsWith(".rels")||a.endsWith(`/${s}`)||a===s)continue;e.push(i)}return e.sort((i,a)=>i.localeCompare(a,void 0,{numeric:!0,sensitivity:"base"})),e.map((i,a)=>({name:`Page ${a+1}`,path:i}))}function wn(n){let t=n.attrQualified(h.R,"id")??n.attrAny("id");if(t!==void 0&&t.length>0&&t.toLowerCase().startsWith("rid"))return t;for(let r of n.descendantsAny("Rel")){let o=r.attrQualified(h.R,"id")??r.attrAny("id");if(o!==void 0&&o.length>0)return o}return}function Pn(n){let t=n.attrAny("Name")??n.attrAny("NameU")??"";return _(L(t)).trim()}function N(n,t,r){let o=V(n,r);if(o===void 0)return;return p(t,o.target)}function V(n,t){let r=n.firstOfType(t);if(r!==void 0)return r;let o,s;for(let[e,i]of n.entries()){if(i.mode!=="internal"||!M(i.relType,t))continue;if(o===void 0||e<o)o=e,s=i}return s}function M(n,t){if(n===t)return!0;let r=t.slice(t.lastIndexOf("/"));return r.length>1&&n.endsWith(r)}function P(n,t){if(n.is(h.MC,"AlternateContent")){let r=cn(n,gn);if(r!==void 0)P(r,t);return}if(n.local==="Text"){g(t,n.text());return}if(n.local==="TextBlock"){g(t,n.text());let r=n.attrAny("V")??n.attrAny("Value");if(r!==void 0)g(t,r)}else if(n.local==="Cell"&&Cn(n.attrAny("N")??"","TextBlock")){let r=n.attrAny("V")??n.attrAny("Value");if(r!==void 0)g(t,r);return}for(let r of n.childElems())P(r,t)}function g(n,t){let r=_(L(t)).trim();if(r.length===0||En(r))return;n.push({type:"paragraph",inlines:[U(r)]})}function En(n){return/^-?\d+(\.\d+)?([eE][+-]?\d+)?$/.test(n)}function p(n,t){try{return en(n,t).path}catch{return}}function Rn(n,t){if(n instanceof l){if(n.code==="encrypted"||An(n))return l.encrypted();return n}let r=rn(t);if(r!==void 0)return r;let o=n instanceof Error?n.message:String(n);if($(o))return l.encrypted();return l.malformed(`not a readable zip archive: ${o}`)}function An(n){if(n instanceof l&&n.code==="encrypted")return!0;let t=n instanceof l?n.detail??"":"",r=n instanceof Error?`${n.message} ${t}`:String(n);return $(r)}function $(n){let t=n.toLowerCase();return t.includes("encrypted")||t.includes("password")}function Cn(n,t){return n.length===t.length&&n.toLowerCase()===t.toLowerCase()}function k(n){if(vn(n))return A(n);if(Nn(n))return I(n);throw Sn.unsupported("visio")}function Nn(n){return n.length>=4&&n[0]===80&&n[1]===75&&n[2]===3&&n[3]===4}var In=new Set(["vsd","vsdx","vss","vst","vssx","vstx"]);function R(){return{id:"visio",sniff(n,t){let r=O(n);if(r==="vsdx"||r===void 0&&Mn(n))return 2;if(r!==void 0)return 0;let o=D(n);if(o==="vsd"||o===void 0&&$n(n))return 2;if(o!==void 0)return 0;if(t?.path!==void 0&&In.has(_n(t.path)??""))return 1;return 0},convert(n){return Vn(n),{markdown:Ln(k(n))}}}}function Vn(n){if(On(n))throw x.unsupported("pdf");let t=O(n);if(t!==void 0&&t!=="vsdx")throw x.unsupported(t);let r=D(n);if(r!==void 0&&r!=="vsd")throw x.unsupported(r);if(!B(n)&&!W(n))throw x.unsupported("visio")}function Mn(n){if(!B(n))return!1;try{let t=Un.open(n);if(t.hasPart("visio/document.xml"))return!0;for(let o of t.partNames())if(o.toLowerCase().startsWith("visio/"))return!0;let r=t.optionalPart("[Content_Types].xml");if(r!==void 0&&kn(r,"visio"))return!0;return!1}catch{return!1}}function $n(n){if(!W(n))return!1;try{for(let t of Tn.open(n).readRootStorage())if(Dn(t.name,"VisioDocument"))return!0}catch{return!1}return!1}function kn(n,t){if(t.length===0||n.length<t.length)return!1;let r=t.length,o=t.charCodeAt(0),s=o>=65&&o<=90?o+32:o;for(let e=0;e<=n.length-r;e+=1){let i=n[e];if((i>=65&&i<=90?i+32:i)!==s)continue;let f=!0;for(let c=1;c<r;c+=1){let m=n[e+c],u=t.charCodeAt(c),F=m>=65&&m<=90?m+32:m,G=u>=65&&u<=90?u+32:u;if(F!==G){f=!1;break}}if(f)return!0}return!1}function Dn(n,t){return n.length===t.length&&n.toLowerCase()===t.toLowerCase()}function On(n){let t=Wn(n);return Bn(n,"%PDF-",t)}function B(n){return n.length>=4&&n[0]===80&&n[1]===75&&n[2]===3&&n[3]===4}function Wn(n){let t=0;if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)t=3;while(t<n.length){let r=n[t];if(r!==9&&r!==10&&r!==13&&r!==32)break;t+=1}return t}function Bn(n,t,r){if(r+t.length>n.length)return!1;for(let o=0;o<t.length;o+=1)if(n[r+o]!==t.charCodeAt(o))return!1;return!0}var Gn=Fn([R()]);function Qn(n,t){return Gn(n,t)}export{R as visio,Qn as toMarkdown};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type ConvertHint } from '@mdgate/core';
|
|
2
|
+
/**
|
|
3
|
+
* Convert a single file's bytes to Markdown with the visio 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/dist/visio.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mdgate/visio",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "mdgate Visio converter (vsd/vsdx)",
|
|
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/office-common": "0.1.0",
|
|
29
|
+
"@mdgate/utils": "0.1.2"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=20"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"mdgate",
|
|
39
|
+
"markdown",
|
|
40
|
+
"visio",
|
|
41
|
+
"vsd",
|
|
42
|
+
"vsdx"
|
|
43
|
+
]
|
|
44
|
+
}
|