mdast2docx 0.1.1 → 0.3.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 +242 -203
- package/dist/chunk-GP6KO6J3.mjs +3 -0
- package/dist/{chunk-D4MVNKG6.mjs → chunk-GS5L4UWU.mjs} +1 -1
- package/dist/chunk-IW743AEN.mjs +1 -0
- package/dist/chunk-LXTXD4UG.mjs +1 -0
- package/dist/chunk-M5XHL7AG.mjs +4 -0
- package/dist/chunk-QR3NYQYH.mjs +1 -0
- package/dist/chunk-YNJKJBFY.mjs +3 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +3 -2
- package/dist/index.mjs +1 -1
- package/dist/mdast.d.ts +1076 -0
- package/dist/mdast.js +1 -0
- package/dist/plugins/html.d.ts +9 -0
- package/dist/plugins/html.js +3 -1
- package/dist/plugins/html.mjs +1 -0
- package/dist/plugins/image.d.ts +8 -4
- package/dist/plugins/image.js +1 -1
- package/dist/plugins/image.mjs +1 -1
- package/dist/plugins/index.d.ts +1 -0
- package/dist/plugins/index.js +2 -74
- package/dist/plugins/index.mjs +1 -1
- package/dist/plugins/list.js +1 -1
- package/dist/plugins/list.mjs +1 -1
- package/dist/plugins/math.js +1 -75
- package/dist/plugins/math.mjs +1 -1
- package/dist/plugins/{node.js → node/index.js} +1 -1
- package/dist/plugins/node/index.mjs +1 -0
- package/dist/plugins/{node-image.d.ts → node/node-image.d.ts} +9 -5
- package/dist/plugins/{node-image.js → node/node-image.js} +1 -1
- package/dist/plugins/node/node-image.mjs +1 -0
- package/dist/section.d.ts +2 -7
- package/dist/section.js +3 -2
- package/dist/section.mjs +1 -1
- package/dist/utils.d.ts +22 -9
- package/dist/utils.js +1 -1
- package/dist/utils.mjs +1 -1
- package/package.json +95 -92
- package/dist/chunk-7LR5HC5O.mjs +0 -4
- package/dist/chunk-7W4N3GRD.mjs +0 -75
- package/dist/chunk-AHXJDPOO.mjs +0 -2
- package/dist/chunk-IBVI6OKS.mjs +0 -1
- package/dist/chunk-XIRSNIMN.mjs +0 -1
- package/dist/chunk-ZKNVKZAF.mjs +0 -1
- package/dist/plugins/node-image.mjs +0 -1
- package/dist/plugins/node.mjs +0 -1
- /package/dist/plugins/{node.d.ts → node/index.d.ts} +0 -0
package/dist/mdast.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";var o=Object.defineProperty;var i=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var l=Object.prototype.hasOwnProperty;var s=(t,e,r,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of p(e))!l.call(t,n)&&n!==r&&o(t,n,{get:()=>e[n],enumerable:!(a=i(e,n))||a.enumerable});return t};var d=t=>s(o({},"__esModule",{value:!0}),t);var c={};module.exports=d(c);
|
package/dist/plugins/html.d.ts
CHANGED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { IPlugin } from "../utils";
|
|
2
|
+
/**
|
|
3
|
+
* @beta
|
|
4
|
+
* htmlPlugin is in beta and is subject to change or removal without notice.
|
|
5
|
+
* This plugin is a placeholder for future implementation of HTML conversion.
|
|
6
|
+
*
|
|
7
|
+
* Keep this before image and other plugins to ensure html images, tables, checkboxes, etc. are processed.
|
|
8
|
+
*/
|
|
9
|
+
export declare const htmlPlugin: () => IPlugin;
|
package/dist/plugins/html.js
CHANGED
|
@@ -1 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";var b=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var R=Object.prototype.hasOwnProperty;var S=(e,n)=>{for(var t in n)b(e,t,{get:n[t],enumerable:!0})},k=(e,n,t,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let o of N(n))!R.call(e,o)&&o!==t&&b(e,o,{get:()=>n[o],enumerable:!(r=O(n,o))||r.enumerable});return e};var B=e=>k(b({},"__esModule",{value:!0}),e);var U={};S(U,{htmlPlugin:()=>G});module.exports=B(U);var x=require("docx");var T=e=>{let n=document.createElement("canvas").getContext("2d");return n?(n.fillStyle=e,n.fillStyle.slice(1)):e.startsWith("#")?e.slice(1):"auto"};var L=["BR","IMG","EM","I","STRONG","B","DEL","S","A","SUP","SUB","IMG"],A={BR:"break",IMG:"image",EM:"emphasis",I:"emphasis",STRONG:"strong",B:"strong",DEL:"delete",S:"delete",A:"link"},E=["solid","dashed","dotted","double","none","ridge","groove","inset","outset"],H=e=>{var r;if(!e)return{};let n={},t=e.match(/border(-\w+)?:\s*[^;]+/gi);if(!t)return{};for(let o of t){let[s,c]=o.split(":").map(l=>l.trim()),p=c.split(/\s+/),d=(r=p.find(l=>l.endsWith("px")))==null?void 0:r.replace("px",""),i=p.find(l=>E.includes(l.toLowerCase())),f=p.find(l=>!l.endsWith("px")&&!E.includes(l.toLowerCase())),y=s==="border"?"border":s.replace("border-","");n[y]={...d?{width:parseInt(d,1)}:{},...i?{style:i}:{},...f?{color:f}:{}}}return n},v={solid:x.BorderStyle.SINGLE,dashed:x.BorderStyle.DASHED,dotted:x.BorderStyle.DOTTED,double:x.BorderStyle.DOUBLE,none:x.BorderStyle.NONE,ridge:x.BorderStyle.THREE_D_EMBOSS,groove:x.BorderStyle.THREE_D_ENGRAVE,inset:x.BorderStyle.INSET,outset:x.BorderStyle.OUTSET},h=e=>{if(!e||!Object.keys(e).length)return;let{width:n,color:t,style:r}=e,o={style:r?v[r]:x.BorderStyle.SINGLE};return n&&(o.size=n),t&&(o.color=T(t)),o},P=(e,n=!0)=>{let t={};if(!(e instanceof HTMLElement||e instanceof SVGElement))return t;let{textAlign:r,fontWeight:o,fontStyle:s,textDecoration:c,textTransform:p,color:d}=e.style,i=H(e.getAttribute("style"));if(n&&i.border)t.border=h(i.border);else if(Object.keys(i).length){let f={...i.border,...i.left},y={...i.border,...i.right},l={...i.border,...i.top},C={...i.border,...i.bottom};t.border={left:h(f),right:h(y),top:h(l),bottom:h(C)}}switch(r&&(Object.keys(x.AlignmentType).includes(r)?t.alignment=r:r==="justify"&&(t.alignment=x.AlignmentType.JUSTIFIED)),(/bold/.test(o)||parseInt(o)>=500)&&(t.bold=!0),/(italic|oblique)/.test(s)&&(t.italics=!0),c){case"underline":t.underline={};break;case"overline":t.emphasisMark={};break;case"line-through":t.strike=!0;break}return p==="uppercase"&&(t.allCaps=!0),p==="lowercase"&&(t.smallCaps=!0),d&&(t.color=T(d)),e.tagName==="SUP"?t.superScript=!0:e.tagName==="SUB"?t.subScript=!0:["STRONG","B"].includes(e.tagName)?t.bold=!0:["EM","I"].includes(e.tagName)?t.italics=!0:["DEL","S"].includes(e.tagName)?t.strike=!0:["U","INS"].includes(e.tagName)?t.underline={}:e.tagName==="MARK"?(t.highlight="yellow",t.emphasisMark={}):e.tagName==="PRE"&&(t.pre=!0),t},u=e=>{var o,s,c,p;if(!(e instanceof HTMLElement||e instanceof SVGElement))return{type:"text",value:(o=e.textContent)!=null?o:""};let n=Array.from(e.childNodes).map(u),t=P(e),r=e.getAttributeNames().reduce((d,i)=>({...d,[i]:e.getAttribute(i)}),{});switch(e.tagName){case"BR":return{type:"break"};case"IMG":return{type:"image",url:(s=r.src)!=null?s:"",alt:(c=r.alt)!=null?c:"",data:{...t,...r}};case"SVG":return{type:"image",url:`data:image/svg+xml;base64,${e.outerHTML}`,data:t};case"EM":case"I":case"STRONG":case"B":case"DEL":case"S":return{type:A[e.tagName],children:n,data:t};case"A":return{type:"link",url:(p=r.href)!=null?p:"",children:n,data:t};case"INPUT":if(/(radio|checkbox)/.test(e.type))return{type:"checkbox"}}return{type:"fragment",children:n,data:t}},D=(e,n)=>Array.from(e.children).map(t=>{let r={...n,...P(t)};return t instanceof HTMLTableRowElement?{type:"tableRow",children:Array.from(t.children).map(o=>({type:"tableCell",children:[u(o)]})),data:r}:D(t,r)}).flat(),g={style:"single"},I={left:g,right:g,top:g,bottom:g},w=e=>{let n=P(e);switch(e.tagName){case"H1":case"H2":case"H3":case"H4":case"H5":case"H6":return{type:"heading",depth:parseInt(e.tagName[1]),children:Array.from(e.childNodes).map(u),data:n};case"PRE":case"P":case"DIV":case"DETAILS":case"SUMMARY":case"FORM":case"LI":return m(e,n);case"UL":case"OL":return{type:"list",ordered:e.tagName==="OL",children:Array.from(e.childNodes).map(t=>({type:"listItem",children:[m(t)],data:n}))};case"HR":return{type:"thematicBreak",data:n};case"BLOCKQUOTE":return{type:"blockquote",children:Array.from(e.childNodes).map(t=>m(t)),data:n};case"TABLE":return{type:"table",children:D(e),data:n};case"STYLE":return{type:"paragraph",children:[{type:"text",value:`Not supported yet!
|
|
2
|
+
|
|
3
|
+
${e.textContent}`}],data:{...n,pre:!0,border:I}};case"INPUT":if(!/(radio|checkbox)/.test(e.type))return{type:"paragraph",children:[],data:{...n,frame:{width:5e3,height:90,alignment:{x:x.HorizontalPositionAlign.LEFT,y:x.VerticalPositionAlign.CENTER},anchor:{horizontal:x.FrameAnchorType.TEXT,vertical:x.FrameAnchorType.TEXT},type:"alignment"},border:I}}}return{type:"paragraph",children:[u(e)],data:n}},m=(e,n)=>{let t=Array.from(e.childNodes),r=[],o=[];for(let s of t)(s instanceof HTMLElement||s instanceof SVGElement)&&!L.includes(s.tagName)?(o.length&&(r.push({type:"paragraph",children:o.map(u)}),o.length=0),r.push(w(s))):o.push(s);return o.length&&r.push({type:"paragraph",children:o.map(u)}),r.length===1?{...r[0],data:{...n,...r[0].data}}:{type:"fragment",children:r,data:n}},M=e=>{var r,o;let n=[],t=[];for(let s of e.children)if((r=s.children)!=null&&r.length&&M(s),s.type==="html"&&/^<[^>]*[^/]>$/.test(s.value)){let c=s.value.split(" ")[0].slice(1);c[0]==="/"?((o=t[0])==null?void 0:o.tag)===c.slice(1,-1)&&n.push(t.shift()):t.unshift({...s,children:[],tag:c})}else t.length?t[0].children.push(s):n.push(s);e.children=n},G=()=>({block:async(e,n)=>{if(n.type==="html"){let t=document.createElement("div");t.innerHTML=n.value,Object.assign(n,m(t))}return[]},inline:async(e,n)=>{var t,r;if(n.type==="html"){let o=(r=(t=n.value)==null?void 0:t.trim())!=null?r:"",s=o.split(" ")[0].slice(1),c=document.createElement("div");c.innerHTML=o.endsWith("/>")?o:`${o}</${s}>`,Object.assign(n,{...u(c.children[0]),children:n.children})}return[]},preprocess:M});0&&(module.exports={htmlPlugin});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{a}from"../chunk-GP6KO6J3.mjs";import"../chunk-IW743AEN.mjs";import"../chunk-3ZZBJC3M.mjs";export{a as htmlPlugin};
|
package/dist/plugins/image.d.ts
CHANGED
|
@@ -4,6 +4,10 @@ import { IPlugin } from "../utils";
|
|
|
4
4
|
* List of image types fully supported by docx. SVG requires fallback thus we do not include that here.
|
|
5
5
|
*/
|
|
6
6
|
export declare const SUPPORTED_IMAGE_TYPES: readonly ["jpeg", "jpg", "bmp", "gif", "png"];
|
|
7
|
+
/**
|
|
8
|
+
* Resolves an image source URL into the appropriate image options for DOCX conversion.
|
|
9
|
+
*/
|
|
10
|
+
export type ImageResolver = (src: string, options?: IImagePluginOptions) => Promise<IImageOptions>;
|
|
7
11
|
/**
|
|
8
12
|
* Options for the image plugin.
|
|
9
13
|
*/
|
|
@@ -18,11 +22,11 @@ export interface IImagePluginOptions {
|
|
|
18
22
|
* @default "png"
|
|
19
23
|
*/
|
|
20
24
|
fallbackImageType?: "png" | "jpg" | "bmp" | "gif";
|
|
25
|
+
/**
|
|
26
|
+
* Custom image resolver to resolve an image source URL into the appropriate image options for DOCX conversion.
|
|
27
|
+
*/
|
|
28
|
+
imageResolver?: ImageResolver;
|
|
21
29
|
}
|
|
22
|
-
/**
|
|
23
|
-
* Resolves an image source URL into the appropriate image options for DOCX conversion.
|
|
24
|
-
*/
|
|
25
|
-
export type ImageResolver = (src: string, options?: IImagePluginOptions) => Promise<IImageOptions>;
|
|
26
30
|
/**
|
|
27
31
|
* Determines the MIME type of an image based on its binary signature.
|
|
28
32
|
*
|
package/dist/plugins/image.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var p=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var
|
|
1
|
+
"use strict";var p=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var v=(e,t)=>{for(var a in t)p(e,a,{get:t[a],enumerable:!0})},P=(e,t,a,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of w(t))!F.call(e,n)&&n!==a&&p(e,n,{get:()=>t[n],enumerable:!(r=y(t,n))||r.enumerable});return e};var x=e=>P(p({},"__esModule",{value:!0}),e);var D={};v(D,{SUPPORTED_IMAGE_TYPES:()=>h,getImageMimeType:()=>f,imagePlugin:()=>T});module.exports=x(D);var h=["jpeg","jpg","bmp","gif","png"],f=e=>{let t=new Uint8Array(e).slice(0,4);if(t[0]===66&&t[1]===77)return"bmp";switch(t.reduce((r,n)=>r+n.toString(16).padStart(2,"0"),"")){case"89504E47":return"png";case"47494638":return"gif";case"FFD8FFE0":case"FFD8FFE1":case"FFD8FFE2":case"FFD8FFE3":case"FFD8FFE8":return"jpg"}},E=3,l=async(e,t)=>{var I,u;let a=(I=t==null?void 0:t.scale)!=null?I:E,r=document.createElement("canvas"),n=r.getContext("2d");if(!n)throw new Error("Canvas context not available");let i=new Image;i.src=e,await new Promise(d=>i.onload=d);let s=i.width*a,g=i.height*a;r.width=s,r.height=g,n.drawImage(i,0,0,s,g);let m=e.split(";")[0].split("/")[1];if(h.includes(m))return{data:e,type:m,transformation:{width:s/a,height:g/a}};let o=(u=t==null?void 0:t.fallbackImageType)!=null?u:"png",c={data:r.toDataURL(`image/${o}`),type:o,transformation:{width:s/a,height:g/a}};return e.includes("svg")?{...c,type:"svg",data:e,fallback:{type:o,data:c.data}}:c},b=async(e,t)=>{var s;let a=await fetch(e.startsWith("http")?e:`${window.location.origin}/${e.replace(/^\/+/,"")}`);if(/(svg|xml)/.test((s=a.headers.get("content-type"))!=null?s:"")||e.endsWith(".svg")){let g=await a.text();return l(`data:image/svg+xml;base64,${btoa(g)}`,t)}let r=await a.arrayBuffer(),n=f(r)||"png",i=await createImageBitmap(new Blob([r],{type:n}));return h.includes(n)?{type:n,data:r,transformation:{width:i.width,height:i.height}}:(console.warn(`${n} not supported by docx. Using one of the supported mime types.`),l(e,t))},O=async(e,t)=>{try{return e.startsWith("data:")?await l(e,t):await b(e,t)}catch(a){return console.error(`Error resolving image: ${e}`,a),{type:"png",data:Buffer.from([]),transformation:{width:100,height:100}}}},T=e=>({inline:async(t,a,r,n)=>{var i,s,g,m;if(/^image/.test(a.type)){let o=(s=a.url)!=null?s:n[(i=a.identifier)==null?void 0:i.toUpperCase()],c=(g=a.alt)!=null?g:o==null?void 0:o.split("/").pop();return a.type="",[new t.ImageRun({...await((m=e==null?void 0:e.imageResolver)!=null?m:O)(o,e),altText:{description:c,name:c,title:c}})]}return[]}});0&&(module.exports={SUPPORTED_IMAGE_TYPES,getImageMimeType,imagePlugin});
|
package/dist/plugins/image.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{a,b,c}from"../chunk-
|
|
1
|
+
import{a,b,c}from"../chunk-LXTXD4UG.mjs";import"../chunk-3ZZBJC3M.mjs";export{a as SUPPORTED_IMAGE_TYPES,b as getImageMimeType,c as imagePlugin};
|
package/dist/plugins/index.d.ts
CHANGED