@mdgate/image 0.6.2 → 0.6.3

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @mdgate/image
2
2
 
3
- Converters for JPEG, PNG, WebP, GIF, TIFF, HEIC, BMP, and SVG.
3
+ Converters for JPEG, PNG, WebP, GIF, TIFF, HEIC, BMP, SVG, and gzip SVG (`.svgz`).
4
4
 
5
5
  Raster formats need an `(input) => markdown` function — `@mdgate/ai`, Apple OCR, or your own. SVG is converted locally (`svg()`, or `image()` without calling that function). `all()` registers `svg()`, not `image()`.
6
6
 
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import{ConvertError as G}from"@mdgate/core";import{ConvertError as x}from"@mdgate/core";var k={jpg:"image/jpeg",jpeg:"image/jpeg",png:"image/png",webp:"image/webp",gif:"image/gif",tif:"image/tiff",tiff:"image/tiff",heic:"image/heic",heif:"image/heic",bmp:"image/bmp",svg:"image/svg+xml"},F=[208,207,17,224,161,177,26,225];function u(n){if(n.length>=3&&n[0]===255&&n[1]===216&&n[2]===255)return"image/jpeg";if(n.length>=8&&n[0]===137&&n[1]===80&&n[2]===78&&n[3]===71&&n[4]===13&&n[5]===10&&n[6]===26&&n[7]===10)return"image/png";if(n.length>=12&&n[0]===82&&n[1]===73&&n[2]===70&&n[3]===70&&n[8]===87&&n[9]===69&&n[10]===66&&n[11]===80)return"image/webp";if(T(n))return"image/gif";if(z(n))return"image/bmp";if(H(n))return"image/tiff";if(S(n))return"image/heic";if(E(n))return"image/svg+xml";return}function m(n){let e=n.split(/[/\\]/).pop()??"",r=e.lastIndexOf(".");if(r<=0||r===e.length-1)return;return k[e.slice(r+1).toLowerCase()]}function c(n,e){return u(n)??(e?.path!==void 0?m(e.path):void 0)}function p(n){if(j(n))throw x.unsupported("pdf");if(B(n,F))throw x.unsupported("ole");if(n.length>=4&&n[0]===80&&n[1]===75&&n[2]===3&&n[3]===4)throw x.unsupported("zip")}function T(n){if(n.length<6)return!1;if(n[0]!==71||n[1]!==73||n[2]!==70||n[3]!==56)return!1;return(n[4]===55||n[4]===57)&&n[5]===97}function z(n){return n.length>=2&&n[0]===66&&n[1]===77}function H(n){if(n.length<4)return!1;if(n[0]===73&&n[1]===73&&n[2]===42&&n[3]===0)return!0;return n[0]===77&&n[1]===77&&n[2]===0&&n[3]===42}function S(n){if(n.length<12)return!1;if(n[4]!==102||n[5]!==116||n[6]!==121||n[7]!==112)return!1;let e=(n[0]<<24|n[1]<<16|n[2]<<8|n[3])>>>0,r=Math.min(n.length,e>=16?e:n.length,256);if(v(n,8))return!0;for(let t=16;t+4<=r;t+=4)if(v(n,t))return!0;return!1}function v(n,e){let r=n[e],t=n[e+1],o=n[e+2],i=n[e+3];if(r===104&&t===101&&o===105&&(i===99||i===102))return!0;return r===109&&t===105&&o===102&&i===49}function E(n){let e=C(n);if(A(n,"<svg",e)&&!_(n[e+4]))return!0;if(!A(n,"<?xml",e))return!1;let r=new TextDecoder("utf-8",{fatal:!1}).decode(n.subarray(0,Math.min(n.length,8192)));return/<svg\b/i.test(r)}function j(n){let e=C(n);return D(n,"%PDF-",e)}function C(n){let e=0;if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)e=3;while(e<n.length){let r=n[e];if(r!==9&&r!==10&&r!==13&&r!==32)break;e+=1}return e}function B(n,e){if(n.length<e.length)return!1;for(let r=0;r<e.length;r+=1)if(n[r]!==e[r])return!1;return!0}function D(n,e,r){if(r+e.length>n.length)return!1;for(let t=0;t<e.length;t+=1)if(n[r+t]!==e.charCodeAt(t))return!1;return!0}function A(n,e,r){if(r+e.length>n.length)return!1;for(let t=0;t<e.length;t+=1){let o=n[r+t],i=e.charCodeAt(t);if(o===i)continue;let a=o>=65&&o<=90?o+32:o,f=i>=65&&i<=90?i+32:i;if(a!==f)return!1}return!0}function _(n){if(n===void 0)return!1;return n>=48&&n<=57||n>=65&&n<=90||n>=97&&n<=122||n===45||n===95||n===58}import{ConvertError as P}from"@mdgate/core";import{documentToMarkdown as U,emptyDocument as R,heading as $,plain as g}from"@mdgate/document";function s(){return{id:"svg",sniff(n,e){if(u(n)==="image/svg+xml")return 3;if(e?.path!==void 0&&m(e.path)==="image/svg+xml")return 1;return 0},convert(n,e){if(p(n),c(n,e)!=="image/svg+xml")throw P.unsupported(e?.path===void 0?"unrecognized file content: name the format explicitly":`unrecognized file content and extension: ${e.path}`);return{markdown:U(M(n))}}}}function I(n){return U(M(n))}function M(n){let e=W(new TextDecoder("utf-8",{fatal:!1}).decode(n)),r=d(e,"title"),t=d(e,"desc"),o=d(e,"text"),i=R();if(r.length===0&&t.length===0&&o.length===0)return i.blocks.push({type:"paragraph",inlines:[g("This is an SVG image.")]}),i;for(let a=0;a<r.length;a+=1){let f=r[a];if(a===0)i.blocks.push($(1,[g(f)]));else i.blocks.push({type:"paragraph",inlines:[g(f)]})}for(let a of t)i.blocks.push({type:"paragraph",inlines:[g(a)]});for(let a of o)i.blocks.push({type:"paragraph",inlines:[g(a)]});return i}function W(n){return n.replace(/<!--[\s\S]*?-->/g,"").replace(/<(?:[A-Za-z_][\w.-]*:)?(?:script|style)\b[^>]*>[\s\S]*?<\/(?:[A-Za-z_][\w.-]*:)?(?:script|style)\s*>/gi,"")}function d(n,e){let r=new RegExp(`<(?:[A-Za-z_][\\w.-]*:)?${e}\\b([^>]*?)(/?)>`,"gi"),t=new RegExp(`</(?:[A-Za-z_][\\w.-]*:)?${e}\\s*>`,"i"),o=[],i;while((i=r.exec(n))!==null){if(i[2]==="/")continue;let a=r.lastIndex,f=n.slice(a),l=t.exec(f);if(l===null)break;let h=N(f.slice(0,l.index));if(h.length>0)o.push(h);r.lastIndex=a+l.index+l[0].length}return o}function N(n){let t=n.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g,"$1").replace(/<!--[\s\S]*?-->/g,"").replace(/<[^>]+>/g,"");return Z(t).replace(/[\s\u00a0]+/g," ").trim()}function Z(n){return n.replace(/&#x([0-9a-fA-F]+);/g,(e,r)=>w(Number.parseInt(r,16))).replace(/&#([0-9]+);/g,(e,r)=>w(Number.parseInt(r,10))).replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&quot;/g,'"').replace(/&apos;/g,"'").replace(/&amp;/g,"&")}function w(n){if(!Number.isFinite(n)||n<0||n>1114111)return"";return String.fromCodePoint(n)}function L(n){return{id:"image",sniff(e,r){if(u(e)!==void 0)return 2;if(r?.path!==void 0&&m(r.path)!==void 0)return 1;return 0},async convert(e,r){p(e);let t=c(e,r);if(t===void 0)throw G.unsupported(r?.path===void 0?"unrecognized file content: name the format explicitly":`unrecognized file content and extension: ${r.path}`);if(t==="image/svg+xml")return{markdown:I(e)};let o=await n({bytes:e,mime:t,page:r?.page});return{markdown:o.endsWith(`
1
+ import{ConvertError as J}from"@mdgate/core";import{ConvertError as x}from"@mdgate/core";import{inflateGzip as S}from"@mdgate/utils";var F=8388608,T={jpg:"image/jpeg",jpeg:"image/jpeg",png:"image/png",webp:"image/webp",gif:"image/gif",tif:"image/tiff",tiff:"image/tiff",heic:"image/heic",heif:"image/heic",bmp:"image/bmp",svg:"image/svg+xml",svgz:"image/svg+xml"},H=[208,207,17,224,161,177,26,225];function u(n){if(n.length>=3&&n[0]===255&&n[1]===216&&n[2]===255)return"image/jpeg";if(n.length>=8&&n[0]===137&&n[1]===80&&n[2]===78&&n[3]===71&&n[4]===13&&n[5]===10&&n[6]===26&&n[7]===10)return"image/png";if(n.length>=12&&n[0]===82&&n[1]===73&&n[2]===70&&n[3]===70&&n[8]===87&&n[9]===69&&n[10]===66&&n[11]===80)return"image/webp";if(E(n))return"image/gif";if(_(n))return"image/bmp";if(B(n))return"image/tiff";if(G(n))return"image/heic";if(A(n))return"image/svg+xml";if(j(n))try{if(A(S(n,F)))return"image/svg+xml"}catch{return}return}function m(n){let r=n.split(/[/\\]/).pop()??"",e=r.lastIndexOf(".");if(e<=0||e===r.length-1)return;return T[r.slice(e+1).toLowerCase()]}function c(n,r){return u(n)??(r?.path!==void 0?m(r.path):void 0)}function p(n){if(D(n))throw x.unsupported("pdf");if(P(n,H))throw x.unsupported("ole");if(n.length>=4&&n[0]===80&&n[1]===75&&n[2]===3&&n[3]===4)throw x.unsupported("zip")}function E(n){if(n.length<6)return!1;if(n[0]!==71||n[1]!==73||n[2]!==70||n[3]!==56)return!1;return(n[4]===55||n[4]===57)&&n[5]===97}function _(n){return n.length>=2&&n[0]===66&&n[1]===77}function B(n){if(n.length<4)return!1;if(n[0]===73&&n[1]===73&&n[2]===42&&n[3]===0)return!0;return n[0]===77&&n[1]===77&&n[2]===0&&n[3]===42}function G(n){if(n.length<12)return!1;if(n[4]!==102||n[5]!==116||n[6]!==121||n[7]!==112)return!1;let r=(n[0]<<24|n[1]<<16|n[2]<<8|n[3])>>>0,e=Math.min(n.length,r>=16?r:n.length,256);if(v(n,8))return!0;for(let t=16;t+4<=e;t+=4)if(v(n,t))return!0;return!1}function v(n,r){let e=n[r],t=n[r+1],o=n[r+2],i=n[r+3];if(e===104&&t===101&&o===105&&(i===99||i===102))return!0;return e===109&&t===105&&o===102&&i===49}function j(n){return n.length>=2&&n[0]===31&&n[1]===139}function A(n){let r=w(n);if(C(n,"<svg",r)&&!$(n[r+4]))return!0;if(!C(n,"<?xml",r))return!1;let e=new TextDecoder("utf-8",{fatal:!1}).decode(n.subarray(0,Math.min(n.length,8192)));return/<svg\b/i.test(e)}function D(n){let r=w(n);return R(n,"%PDF-",r)}function w(n){let r=0;if(n.length>=3&&n[0]===239&&n[1]===187&&n[2]===191)r=3;while(r<n.length){let e=n[r];if(e!==9&&e!==10&&e!==13&&e!==32)break;r+=1}return r}function P(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}function R(n,r,e){if(e+r.length>n.length)return!1;for(let t=0;t<r.length;t+=1)if(n[e+t]!==r.charCodeAt(t))return!1;return!0}function C(n,r,e){if(e+r.length>n.length)return!1;for(let t=0;t<r.length;t+=1){let o=n[e+t],i=r.charCodeAt(t);if(o===i)continue;let a=o>=65&&o<=90?o+32:o,f=i>=65&&i<=90?i+32:i;if(a!==f)return!1}return!0}function $(n){if(n===void 0)return!1;return n>=48&&n<=57||n>=65&&n<=90||n>=97&&n<=122||n===45||n===95||n===58}import{ConvertError as I}from"@mdgate/core";import{documentToMarkdown as M,emptyDocument as W,heading as N,plain as g}from"@mdgate/document";import{inflateGzip as X}from"@mdgate/utils";var Z=8388608;function s(){return{id:"svg",sniff(n,r){if(u(n)==="image/svg+xml")return 3;if(r?.path!==void 0&&m(r.path)==="image/svg+xml")return 1;return 0},convert(n,r){if(p(n),c(n,r)!=="image/svg+xml")throw I.unsupported(r?.path===void 0?"unrecognized file content: name the format explicitly":`unrecognized file content and extension: ${r.path}`);return{markdown:M(k(n))}}}}function z(n){return M(k(n))}function k(n){let r=L(new TextDecoder("utf-8",{fatal:!1}).decode(V(n))),e=d(r,"title"),t=d(r,"desc"),o=d(r,"text"),i=W();if(e.length===0&&t.length===0&&o.length===0)return i.blocks.push({type:"paragraph",inlines:[g("This is an SVG image.")]}),i;for(let a=0;a<e.length;a+=1){let f=e[a];if(a===0)i.blocks.push(N(1,[g(f)]));else i.blocks.push({type:"paragraph",inlines:[g(f)]})}for(let a of t)i.blocks.push({type:"paragraph",inlines:[g(a)]});for(let a of o)i.blocks.push({type:"paragraph",inlines:[g(a)]});return i}function V(n){if(n.length<2||n[0]!==31||n[1]!==139)return n;try{return X(n,Z)}catch{throw I.malformed("gzip svg")}}function L(n){return n.replace(/<!--[\s\S]*?-->/g,"").replace(/<(?:[A-Za-z_][\w.-]*:)?(?:script|style)\b[^>]*>[\s\S]*?<\/(?:[A-Za-z_][\w.-]*:)?(?:script|style)\s*>/gi,"")}function d(n,r){let e=new RegExp(`<(?:[A-Za-z_][\\w.-]*:)?${r}\\b([^>]*?)(/?)>`,"gi"),t=new RegExp(`</(?:[A-Za-z_][\\w.-]*:)?${r}\\s*>`,"i"),o=[],i;while((i=e.exec(n))!==null){if(i[2]==="/")continue;let a=e.lastIndex,f=n.slice(a),l=t.exec(f);if(l===null)break;let h=O(f.slice(0,l.index));if(h.length>0)o.push(h);e.lastIndex=a+l.index+l[0].length}return o}function O(n){let t=n.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g,"$1").replace(/<!--[\s\S]*?-->/g,"").replace(/<[^>]+>/g,"");return q(t).replace(/[\s\u00a0]+/g," ").trim()}function q(n){return n.replace(/&#x([0-9a-fA-F]+);/g,(r,e)=>U(Number.parseInt(e,16))).replace(/&#([0-9]+);/g,(r,e)=>U(Number.parseInt(e,10))).replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&quot;/g,'"').replace(/&apos;/g,"'").replace(/&amp;/g,"&")}function U(n){if(!Number.isFinite(n)||n<0||n>1114111)return"";return String.fromCodePoint(n)}function K(n){return{id:"image",sniff(r,e){if(u(r)!==void 0)return 2;if(e?.path!==void 0&&m(e.path)!==void 0)return 1;return 0},async convert(r,e){p(r);let t=c(r,e);if(t===void 0)throw J.unsupported(e?.path===void 0?"unrecognized file content: name the format explicitly":`unrecognized file content and extension: ${e.path}`);if(t==="image/svg+xml")return{markdown:z(r)};let o=await n({bytes:r,mime:t,page:e?.page});return{markdown:o.endsWith(`
2
2
  `)?o:`${o}
3
- `}}}}import{create as O}from"@mdgate/core";var X=O([s()]);function q(n,e){return X(n,e)}export{q as toMarkdown,s as svg,L as image};
3
+ `}}}}import{create as Q}from"@mdgate/core";var Y=Q([s()]);function y(n,r){return Y(n,r)}export{y as toMarkdown,s as svg,K as image};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdgate/image",
3
- "version": "0.6.2",
3
+ "version": "0.6.3",
4
4
  "description": "mdgate image converter",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -22,8 +22,9 @@
22
22
  "prepublishOnly": "bun run build"
23
23
  },
24
24
  "dependencies": {
25
- "@mdgate/core": "0.6.2",
26
- "@mdgate/document": "0.6.2"
25
+ "@mdgate/core": "0.6.3",
26
+ "@mdgate/document": "0.6.3",
27
+ "@mdgate/utils": "0.6.3"
27
28
  },
28
29
  "publishConfig": {
29
30
  "access": "public"