@mdgate/image 0.5.1 → 0.5.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.
Files changed (2) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +3 -3
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 2;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 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(`
2
2
  `)?o:`${o}
3
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};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdgate/image",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "description": "mdgate image converter",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -22,8 +22,8 @@
22
22
  "prepublishOnly": "bun run build"
23
23
  },
24
24
  "dependencies": {
25
- "@mdgate/core": "0.5.1",
26
- "@mdgate/document": "0.5.1"
25
+ "@mdgate/core": "0.5.3",
26
+ "@mdgate/document": "0.5.3"
27
27
  },
28
28
  "publishConfig": {
29
29
  "access": "public"