@mdgate/image 0.1.1 → 0.1.2

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,15 +1,18 @@
1
1
  # @mdgate/image
2
2
 
3
- Converter for JPEG, PNG, and WebP. Pass any `(input) => markdown` function — `@mdgate/ai`, Apple OCR, or your own.
3
+ Converters for JPEG, PNG, WebP, GIF, TIFF, HEIC, BMP, and SVG.
4
+
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()`.
4
6
 
5
7
  ```ts
6
8
  import { create } from '@mdgate/core';
7
- import { image } from '@mdgate/image';
9
+ import { image, svg } from '@mdgate/image';
8
10
  import { pdf } from '@mdgate/pdf';
9
11
  import { ai } from '@mdgate/ai';
10
12
 
11
13
  const convert = create([
12
14
  pdf(),
15
+ svg(),
13
16
  image(ai({ baseURL, apiKey, model }).convertImage),
14
17
  ]);
15
18
  ```
package/dist/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export { image } from './image.js';
2
+ export { svg } from './svg.js';
3
+ export { toMarkdown } from './to-markdown.js';
2
4
  export type { ConvertImage, ImageInput, ImageMime } from './types.js';
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import{ConvertError as m}from"@mdgate/core";var g={jpg:"image/jpeg",jpeg:"image/jpeg",png:"image/png",webp:"image/webp"};function p(e){return{id:"image",sniff(r,n){if(o(r)!==void 0)return 2;if(n?.path!==void 0&&a(n.path)!==void 0)return 1;return 0},async convert(r,n){let t=o(r)??(n?.path!==void 0?a(n.path):void 0);if(t===void 0)throw m.unsupported(n?.path===void 0?"unrecognized file content: name the format explicitly":`unrecognized file content and extension: ${n.path}`);let i=await e({bytes:r,mime:t,page:n?.page});return{markdown:i.endsWith(`
2
- `)?i:`${i}
3
- `}}}}function o(e){if(e.length>=3&&e[0]===255&&e[1]===216&&e[2]===255)return"image/jpeg";if(e.length>=8&&e[0]===137&&e[1]===80&&e[2]===78&&e[3]===71&&e[4]===13&&e[5]===10&&e[6]===26&&e[7]===10)return"image/png";if(e.length>=12&&e[0]===82&&e[1]===73&&e[2]===70&&e[3]===70&&e[8]===87&&e[9]===69&&e[10]===66&&e[11]===80)return"image/webp";return}function a(e){let r=e.split(/[/\\]/).pop()??"",n=r.lastIndexOf(".");if(n<=0||n===r.length-1)return;return g[r.slice(n+1).toLowerCase()]}export{p as image};
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(`
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};
package/dist/svg.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import type { Converter } from '@mdgate/core';
2
+ export declare function svg(): Converter;
3
+ export declare function svgToMarkdown(bytes: Uint8Array): string;
@@ -0,0 +1,6 @@
1
+ import { type ConvertHint } from '@mdgate/core';
2
+ /**
3
+ * Convert a single file's bytes to Markdown with the svg 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/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type ImageMime = 'image/jpeg' | 'image/png' | 'image/webp';
1
+ export type ImageMime = 'image/jpeg' | 'image/png' | 'image/webp' | 'image/gif' | 'image/tiff' | 'image/heic' | 'image/bmp' | 'image/svg+xml';
2
2
  export type ImageInput = {
3
3
  bytes: Uint8Array;
4
4
  mime: ImageMime;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mdgate/image",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "mdgate image converter",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -22,7 +22,8 @@
22
22
  "prepublishOnly": "bun run build"
23
23
  },
24
24
  "dependencies": {
25
- "@mdgate/core": "0.1.3"
25
+ "@mdgate/core": "0.1.3",
26
+ "@mdgate/document": "0.1.0"
26
27
  },
27
28
  "publishConfig": {
28
29
  "access": "public"