mdast2docx 0.2.0 → 0.3.1

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 (46) hide show
  1. package/README.md +54 -15
  2. package/dist/chunk-KWOFOD3W.mjs +31 -0
  3. package/dist/chunk-NMFJ4KIZ.mjs +4 -0
  4. package/dist/index.d.ts +1 -23
  5. package/dist/index.js +1 -2
  6. package/dist/index.mjs +1 -1
  7. package/dist/{multipart-parser-ECCTUILZ.mjs → multipart-parser-I4OTWNXG.mjs} +1 -1
  8. package/dist/plugins/index.d.ts +5 -5
  9. package/dist/plugins/index.js +1 -79
  10. package/dist/plugins/index.mjs +1 -1
  11. package/dist/plugins/node/index.mjs +1 -1
  12. package/dist/plugins/node/node-image.d.ts +1 -1
  13. package/dist/plugins/node/node-image.mjs +1 -1
  14. package/package.json +15 -11
  15. package/dist/chunk-35E6UZDH.mjs +0 -4
  16. package/dist/chunk-ALSMH2CW.mjs +0 -75
  17. package/dist/chunk-CRVVOUD7.mjs +0 -1
  18. package/dist/chunk-ECHPYCZC.mjs +0 -2
  19. package/dist/chunk-I5QUWH2X.mjs +0 -1
  20. package/dist/chunk-JOIHCSIR.mjs +0 -31
  21. package/dist/chunk-LXTXD4UG.mjs +0 -1
  22. package/dist/chunk-MSGIMOZN.mjs +0 -1
  23. package/dist/chunk-Q6Q2NBIZ.mjs +0 -1
  24. package/dist/chunk-X6PWMGYB.mjs +0 -5
  25. package/dist/chunk-Z2MODMIK.mjs +0 -1
  26. package/dist/plugins/html.d.ts +0 -9
  27. package/dist/plugins/html.js +0 -5
  28. package/dist/plugins/html.mjs +0 -1
  29. package/dist/plugins/image.d.ts +0 -41
  30. package/dist/plugins/image.js +0 -1
  31. package/dist/plugins/image.mjs +0 -1
  32. package/dist/plugins/list.d.ts +0 -57
  33. package/dist/plugins/list.js +0 -1
  34. package/dist/plugins/list.mjs +0 -1
  35. package/dist/plugins/math.d.ts +0 -11
  36. package/dist/plugins/math.js +0 -75
  37. package/dist/plugins/math.mjs +0 -1
  38. package/dist/plugins/table.d.ts +0 -26
  39. package/dist/plugins/table.js +0 -1
  40. package/dist/plugins/table.mjs +0 -1
  41. package/dist/section.d.ts +0 -26
  42. package/dist/section.js +0 -2
  43. package/dist/section.mjs +0 -1
  44. package/dist/utils.d.ts +0 -113
  45. package/dist/utils.js +0 -1
  46. package/dist/utils.mjs +0 -1
@@ -1 +0,0 @@
1
- import{a,b}from"../chunk-ALSMH2CW.mjs";import"../chunk-Z2MODMIK.mjs";import"../chunk-I5QUWH2X.mjs";export{b as mathPlugin,a as parseLatex};
@@ -1,26 +0,0 @@
1
- import { AlignmentType, type ITableCellOptions, type ITableOptions, type ITableRowOptions, VerticalAlign } from "docx";
2
- import { IPlugin, Optional } from "../utils";
3
- export type RowProps = Omit<ITableRowOptions, "children">;
4
- export type TableProps = Omit<ITableOptions, "rows">;
5
- export type CellProps = Omit<ITableCellOptions, "children">;
6
- export interface ITableAlignments {
7
- defaultVerticalAlign?: (typeof VerticalAlign)[keyof typeof VerticalAlign];
8
- defaultHorizontalAlign?: (typeof AlignmentType)[keyof typeof AlignmentType];
9
- /**
10
- * Use MDAST data for horizontal aligning columns
11
- * @default true
12
- */
13
- preferMdData?: boolean;
14
- }
15
- interface IDefaultTablePluginProps {
16
- tableProps: TableProps;
17
- rowProps: RowProps;
18
- cellProps: CellProps;
19
- firstRowProps: RowProps;
20
- firstRowCellProps: CellProps;
21
- alignments: ITableAlignments;
22
- }
23
- type ITablePluginProps = Optional<IDefaultTablePluginProps>;
24
- export declare const defaultTableOptions: IDefaultTablePluginProps;
25
- export declare const tablePlugin: (options?: ITablePluginProps) => IPlugin;
26
- export {};
@@ -1 +0,0 @@
1
- "use strict";var P=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var H=(t,l)=>{for(var r in l)P(t,r,{get:l[r],enumerable:!0})},M=(t,l,r,n)=>{if(l&&typeof l=="object"||typeof l=="function")for(let o of V(l))!z.call(t,o)&&o!==r&&P(t,o,{get:()=>l[o],enumerable:!(n=D(l,o))||n.enumerable});return t};var N=t=>M(P({},"__esModule",{value:!0}),t);var S={};H(S,{defaultTableOptions:()=>g,tablePlugin:()=>k});module.exports=N(S);var e=require("docx"),i={style:e.BorderStyle.SINGLE,color:"auto",size:1},g={tableProps:{width:{size:100,type:e.WidthType.PERCENTAGE},margins:{top:(0,e.convertMillimetersToTwip)(2),right:(0,e.convertMillimetersToTwip)(4),bottom:(0,e.convertMillimetersToTwip)(2),left:(0,e.convertMillimetersToTwip)(4)},borders:{top:i,right:i,bottom:i,left:i,insideHorizontal:i,insideVertical:i}},rowProps:{},cellProps:{},firstRowProps:{tableHeader:!0},firstRowCellProps:{shading:{type:e.ShadingType.SOLID,fill:"b79c2f"}},alignments:{defaultVerticalAlign:e.VerticalAlign.CENTER,defaultHorizontalAlign:e.AlignmentType.CENTER,preferMdData:!0}},k=t=>({block:async(l,r,n,o,T)=>{var c;if(r.type!=="table")return[];let{Table:b,TableRow:u,TableCell:m,Paragraph:w}=l,{tableProps:y,firstRowProps:d,firstRowCellProps:R,rowProps:A,cellProps:h,alignments:a}={...g,...t},p=(c=r.align)==null?void 0:c.map(s=>{switch(s){case"left":return l.AlignmentType.LEFT;case"right":return l.AlignmentType.RIGHT;case"center":return l.AlignmentType.CENTER;default:return}}),I=async(s,f)=>new u({...A,...f===0?d:{},children:await Promise.all(s.children.map(async(O,E)=>new m({verticalAlign:a.defaultVerticalAlign,...h,...f===0?R:{},children:[new w({children:await T(O),alignment:a.preferMdData?p==null?void 0:p[E]:a.defaultHorizontalAlign})]})))}),C=await Promise.all(r.children.map(I));return r.type="",[new b({...y,rows:C})]}});0&&(module.exports={defaultTableOptions,tablePlugin});
@@ -1 +0,0 @@
1
- import{a,b}from"../chunk-Q6Q2NBIZ.mjs";import"../chunk-I5QUWH2X.mjs";export{a as defaultTableOptions,b as tablePlugin};
package/dist/section.d.ts DELETED
@@ -1,26 +0,0 @@
1
- import type { Root } from "mdast";
2
- import type { Definitions, FootnoteDefinitions, ISectionProps } from "./utils";
3
- import { Paragraph } from "docx";
4
- import * as docx from "docx";
5
- /**
6
- * Converts an MDAST tree to a DOCX document section.
7
- * @param node - The root MDAST node
8
- * @param definitions - Definitions mapping
9
- * @param footnoteDefinitions - Footnote definitions mapping
10
- * @param props - Section properties (optional)
11
- * @returns A DOCX section representation
12
- */
13
- export declare const toSection: (node: Root, definitions: Definitions, footnoteDefinitions: FootnoteDefinitions, props?: ISectionProps) => Promise<{
14
- children: (Paragraph | docx.Table)[];
15
- headers?: {
16
- readonly default?: docx.Header;
17
- readonly first?: docx.Header;
18
- readonly even?: docx.Header;
19
- };
20
- footers?: {
21
- readonly default?: docx.Footer;
22
- readonly first?: docx.Footer;
23
- readonly even?: docx.Footer;
24
- };
25
- properties?: docx.ISectionPropertiesOptions;
26
- }>;
package/dist/section.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";var O=Object.create;var m=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var S=Object.getPrototypeOf,B=Object.prototype.hasOwnProperty;var v=(e,o)=>{for(var l in o)m(e,l,{get:o[l],enumerable:!0})},D=(e,o,l,f)=>{if(o&&typeof o=="object"||typeof o=="function")for(let a of N(o))!B.call(e,a)&&a!==l&&m(e,a,{get:()=>o[a],enumerable:!(f=M(o,a))||f.enumerable});return e};var E=(e,o,l)=>(l=e!=null?O(S(e)):{},D(o||!e||!e.__esModule?m(l,"default",{value:e,enumerable:!0}):l,e)),F=e=>D(m({},"__esModule",{value:!0}),e);var j={};v(j,{toSection:()=>X});module.exports=F(j);var R=require("docx");var y=e=>{var o,l;return(o=e.children)!=null&&o.length?e.children.map(y).join(""):(l=e.value)!=null?l:""};var k={useTitle:!0,plugins:[]};var T=E(require("docx")),H=(e,o,l)=>{let f=async(i,h)=>{var t,u,p;let s=Object.assign({},h),c=(await Promise.all(l.map(r=>{var P,x;return(x=(P=r.inline)==null?void 0:P.call(r,T,i,h,e,o,a))!=null?x:[]}))).flat(),d=(u=i.url)!=null?u:e[(t=i.identifier)==null?void 0:t.toUpperCase()];switch(i.type){case"text":return[...c,new R.TextRun({text:i.value,...s})];case"break":return[...c,new R.TextRun({break:1})];case"inlineCode":return[...c,new R.TextRun({text:i.value,...s,style:"code",font:{name:"Consolas"}})];case"emphasis":return s.italics=!0,[...c,...await a(i,s)];case"strong":return s.bold=!0,[...c,...await a(i,s)];case"delete":return s.strike=!0,[...c,...await a(i,s)];case"link":case"linkReference":return[...c,d.startsWith("#")?new R.InternalHyperlink({anchor:d.slice(1),children:await a(i,s)}):new R.ExternalHyperlink({link:d,children:await a(i,s)})];case"footnoteReference":return[...c,new R.FootnoteReferenceRun((p=o[i.identifier].id)!=null?p:0)];case"":return[...c];default:return console.warn(`Unsupported inline node type: ${i.type}`),[...c]}},a=async(i,h={})=>{var s,c;return(await Promise.all((c=(s=i.children)==null?void 0:s.map(d=>f(d,h)))!=null?c:[])).flat()};return a},X=async(e,o,l,f)=>{let{plugins:a,useTitle:i,...h}={...k,...f},s=H(o,l,a),c=async(t,u)=>{var P,x,b,C;let p=Object.assign({},u),r=(await Promise.all(a.map(g=>{var I,w;return(w=(I=g.block)==null?void 0:I.call(g,T,t,p,d,s))!=null?w:[]}))).flat();switch(t.type){case"root":return[...r,...await d(t,p)];case"paragraph":return[...r,new R.Paragraph({...u,children:await s(t)})];case"heading":return[new R.Paragraph({...r,heading:i?t.depth===1?"Title":`Heading${t.depth-1}`:`Heading${t.depth}`,children:[new R.Bookmark({id:y(t).replace(/[. ]+/g,"-").toLowerCase(),children:await s(t)})]})];case"code":return[...r,new R.Paragraph({alignment:"start",style:"blockCode",children:t.value.split(`
2
- `).map(g=>new R.TextRun({text:g,break:1,style:"code",font:{name:"Consolas"}}))})];case"list":return t.ordered?(p.bullet={level:((x=(P=p.bullet)==null?void 0:P.level)!=null?x:-1)+1},console.warn("Please add numbering plugin to support ordered lists. For now, we use only bullets for both the ordered and the unordered list.")):p.bullet={level:((C=(b=p.bullet)==null?void 0:b.level)!=null?C:-1)+1},[...r,...await d(t,p)];case"blockquote":return[...r,...await d(t,p)];case"listItem":return[...r,...await d(t,p)];case"thematicBreak":return[...r,new R.Paragraph({border:{top:{style:R.BorderStyle.SINGLE,size:6}}})];case"definition":case"footnoteDefinition":return r;case"table":return console.warn("Please add table plugin to support tables."),r;case"":return r;case"yaml":case"html":default:return console.warn(`May be an unsupported node type: ${t.type}`,t),r}},d=async(t,u)=>{var p;return(await Promise.all((p=t.children)==null?void 0:p.map(r=>c(r,u)))).flat()};return{...h,children:await d(e,{})}};0&&(module.exports={toSection});
package/dist/section.mjs DELETED
@@ -1 +0,0 @@
1
- import{a}from"./chunk-ECHPYCZC.mjs";import"./chunk-MSGIMOZN.mjs";import"./chunk-I5QUWH2X.mjs";export{a as toSection};
package/dist/utils.d.ts DELETED
@@ -1,113 +0,0 @@
1
- import { ExternalHyperlink, ImageRun, InternalHyperlink, IParagraphOptions, TextRun, Table, Paragraph, IRunOptions, IPropertiesOptions, Math as DOCXMath } from "docx";
2
- import * as DOCX from "docx";
3
- import { BlockContent, DefinitionContent, Parent, Root, RootContent } from "mdast";
4
- export { convertInchesToTwip, convertMillimetersToTwip } from "docx";
5
- export type Optional<T> = {
6
- [K in keyof T]?: T[K];
7
- };
8
- export type Mutable<T> = {
9
- -readonly [K in keyof T]: T[K] extends object ? Mutable<T> : T[K];
10
- };
11
- /** Type representing definition mappings */
12
- export type Definitions = Record<string, string>;
13
- /** Type representing footnote definitions */
14
- export type FootnoteDefinitions = Record<string, {
15
- children: (BlockContent | DefinitionContent)[];
16
- id?: number;
17
- }>;
18
- /**
19
- * Extracts definitions and footnote definitions from a list of MDAST nodes.
20
- *
21
- * @param nodes - Array of MDAST nodes.
22
- * @returns An object containing `definitions` and `footnoteDefinitions`.
23
- */
24
- export declare const getDefinitions: (nodes: RootContent[]) => {
25
- definitions: Definitions;
26
- footnoteDefinitions: FootnoteDefinitions;
27
- };
28
- /** Type representing an extended RootContent node
29
- * - this type is used to avoid type errors when setting type to empty string (in case you want to avoid reprocessing that node.) in plugins
30
- */
31
- type ExtendedRootContent<T extends {
32
- type: string;
33
- } = {
34
- type: "";
35
- }> = RootContent | Root | T;
36
- /**
37
- * Extracts the textual content from a given MDAST node.
38
- * Recursively processes child nodes if present.
39
- *
40
- * @param node - The MDAST node to extract text from.
41
- * @returns The combined text content of the node and its children.
42
- */
43
- export declare const getTextContent: (node: ExtendedRootContent) => string;
44
- /**
45
- * Creates a random string - used to add as a random suffix to make style/numbering reference names unique
46
- * @returns
47
- */
48
- export declare const uuid: () => string;
49
- /**
50
- * Default configuration for converting MDAST to DOCX, including title handling and plugin extensions.
51
- */
52
- interface IDefaultMdastToDocxSectionProps extends Omit<DOCX.ISectionOptions, "children"> {
53
- /**
54
- * If true, H1 corresponds to the title, H2 to Heading1, etc.
55
- * @default true
56
- */
57
- useTitle: boolean;
58
- /**
59
- * List of plugins to extend conversion functionality.
60
- */
61
- plugins: Array<IPlugin>;
62
- }
63
- /**
64
- * Defines properties for a document section, omitting the "children" property from ISectionOptions.
65
- * Also defining properties for MDAST to DOCX conversion
66
- */
67
- export type ISectionProps = Optional<IDefaultMdastToDocxSectionProps>;
68
- export declare const defaultSectionProps: IDefaultMdastToDocxSectionProps;
69
- /**
70
- * Defines document properties, excluding sections and footnotes (which are managed internally).
71
- */
72
- export type IDocxProps = Omit<Mutable<IPropertiesOptions>, "sections" | "footnotes">;
73
- export declare const defaultDocxProps: IDocxProps;
74
- /**
75
- * Mutable version of IRunOptions where all properties are writable.
76
- */
77
- export type MutableRunOptions = Mutable<Omit<IRunOptions, "children">>;
78
- export type InlineDocxNodes = TextRun | ImageRun | InternalHyperlink | ExternalHyperlink | DOCXMath;
79
- export type InlineProcessor = (node: ExtendedRootContent, runProps: MutableRunOptions) => Promise<InlineDocxNodes[]>;
80
- export type InlineChildrenProcessor = (node: Parent, runProps?: MutableRunOptions) => Promise<InlineDocxNodes[]>;
81
- /**
82
- * Mutable version of IParagraphOptions where all properties are writable.
83
- */
84
- export type MutableParaOptions = Omit<Mutable<IParagraphOptions>, "children">;
85
- export type BlockNodeProcessor = (node: ExtendedRootContent, paraProps: MutableParaOptions) => Promise<(Paragraph | Table)[]>;
86
- export type BlockNodeChildrenProcessor = (node: Parent | Root, paraProps: MutableParaOptions) => Promise<(Paragraph | Table)[]>;
87
- /**
88
- * Interface for extending MDAST to DOCX conversion using plugins.
89
- */
90
- export interface IPlugin<T extends {
91
- type: string;
92
- } = {
93
- type: "";
94
- }> {
95
- /**
96
- * Processes block-level nodes.
97
- */
98
- block?: (docx: typeof DOCX, node: ExtendedRootContent<T>, paraProps: MutableParaOptions, blockChildrenProcessor: BlockNodeChildrenProcessor, inlineChildrenProcessor: InlineChildrenProcessor) => Promise<(Paragraph | Table)[]>;
99
- /**
100
- * Processes inline-level nodes.
101
- */
102
- inline?: (docx: typeof DOCX, node: ExtendedRootContent<T>, runProps: MutableRunOptions, definitions: Definitions, footnoteDefinitions: FootnoteDefinitions, inlineChildrenProcessor: InlineChildrenProcessor) => Promise<InlineDocxNodes[]>;
103
- /**
104
- * Allows plugins to modify document-level DOCX properties, such as styles, numbering, headers, and footers. This is useful for global formatting customizations.
105
- */
106
- root?: (props: IDocxProps) => void;
107
- }
108
- export declare const inlineMdastNodes: string[];
109
- export declare const standardize_color: (str: string) => string;
110
- /**
111
- * @mayank/docx is a fork of the `docx` library with minor modifications,
112
- * primarily adding exports for additional types missing from the original `docx` library.
113
- */
package/dist/utils.js DELETED
@@ -1 +0,0 @@
1
- "use strict";var a=Object.defineProperty;var l=Object.getOwnPropertyDescriptor;var c=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var f=(t,e)=>{for(var n in e)a(t,n,{get:e[n],enumerable:!0})},u=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of c(e))!d.call(t,r)&&r!==n&&a(t,r,{get:()=>e[r],enumerable:!(o=l(e,r))||o.enumerable});return t};var x=t=>u(a({},"__esModule",{value:!0}),t);var b={};f(b,{convertInchesToTwip:()=>i.convertInchesToTwip,convertMillimetersToTwip:()=>i.convertMillimetersToTwip,defaultDocxProps:()=>h,defaultSectionProps:()=>g,getDefinitions:()=>s,getTextContent:()=>p,inlineMdastNodes:()=>y,standardize_color:()=>D,uuid:()=>P});module.exports=x(b);var i=require("docx"),s=t=>{let e={},n={};return t.forEach(o=>{var r;o.type==="definition"?e[o.identifier.toUpperCase()]=o.url:o.type==="footnoteDefinition"?n[o.identifier.toUpperCase()]={children:o.children}:(r=o.children)!=null&&r.length&&Object.assign(e,s(o.children))}),{definitions:e,footnoteDefinitions:n}},p=t=>{var e,n;return(e=t.children)!=null&&e.length?t.children.map(p).join(""):(n=t.value)!=null?n:""},P=()=>Math.random().toString(16).slice(2),g={useTitle:!0,plugins:[]},h={styles:{default:{document:{paragraph:{spacing:{before:175,line:300},alignment:"thaiDistribute"},run:{size:24}},heading1:{paragraph:{spacing:{before:350}}},heading2:{paragraph:{spacing:{before:350}}},heading3:{paragraph:{spacing:{before:350}}},heading4:{paragraph:{spacing:{before:350}}},heading5:{paragraph:{spacing:{before:350}}},heading6:{paragraph:{spacing:{before:350}}}}}},y=["text","break","inlineCode","image","emphasis","strong","delete","link","linkReference","footnoteReference"],D=t=>{let e=document.createElement("canvas").getContext("2d");return e?(e.fillStyle=t,e.fillStyle):t.startsWith("#")?t:"auto"};0&&(module.exports={convertInchesToTwip,convertMillimetersToTwip,defaultDocxProps,defaultSectionProps,getDefinitions,getTextContent,inlineMdastNodes,standardize_color,uuid});
package/dist/utils.mjs DELETED
@@ -1 +0,0 @@
1
- import{a,b,c,d,e,f,g,h,i}from"./chunk-MSGIMOZN.mjs";import"./chunk-I5QUWH2X.mjs";export{h as convertInchesToTwip,i as convertMillimetersToTwip,e as defaultDocxProps,d as defaultSectionProps,a as getDefinitions,b as getTextContent,f as inlineMdastNodes,g as standardize_color,c as uuid};