mdast2docx 0.0.0-beta.0 → 0.1.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.
Files changed (47) hide show
  1. package/README.md +203 -170
  2. package/dist/chunk-3ZZBJC3M.mjs +1 -0
  3. package/dist/chunk-7LR5HC5O.mjs +4 -0
  4. package/dist/chunk-7W4N3GRD.mjs +75 -0
  5. package/dist/chunk-GM6UUFDK.mjs +2 -0
  6. package/dist/chunk-TASCVLMX.mjs +1 -0
  7. package/dist/chunk-UXY5TDOP.mjs +1 -0
  8. package/dist/chunk-XIRSNIMN.mjs +1 -0
  9. package/dist/chunk-YKZOUIFR.mjs +1 -0
  10. package/dist/chunk-Z5DPYROV.mjs +31 -0
  11. package/dist/chunk-ZKNVKZAF.mjs +1 -0
  12. package/dist/index.d.ts +7 -8
  13. package/dist/index.js +2 -38
  14. package/dist/index.mjs +1 -1
  15. package/dist/multipart-parser-WKN2UV32.mjs +2 -0
  16. package/dist/plugins/html.d.ts +0 -0
  17. package/dist/plugins/html.js +1 -0
  18. package/dist/plugins/image.d.ts +37 -0
  19. package/dist/plugins/image.js +1 -0
  20. package/dist/plugins/image.mjs +1 -0
  21. package/dist/plugins/index.d.ts +4 -0
  22. package/dist/plugins/index.js +75 -0
  23. package/dist/plugins/index.mjs +1 -0
  24. package/dist/plugins/list.d.ts +57 -0
  25. package/dist/plugins/list.js +1 -0
  26. package/dist/plugins/list.mjs +1 -0
  27. package/dist/plugins/math.d.ts +11 -0
  28. package/dist/plugins/math.js +75 -0
  29. package/dist/plugins/math.mjs +1 -0
  30. package/dist/plugins/node-image.d.ts +27 -0
  31. package/dist/plugins/node-image.js +35 -0
  32. package/dist/plugins/node-image.mjs +1 -0
  33. package/dist/plugins/node.d.ts +1 -0
  34. package/dist/plugins/node.js +35 -0
  35. package/dist/plugins/node.mjs +1 -0
  36. package/dist/plugins/table.d.ts +26 -0
  37. package/dist/plugins/table.js +1 -0
  38. package/dist/plugins/table.mjs +1 -0
  39. package/dist/section.d.ts +9 -8
  40. package/dist/section.js +2 -38
  41. package/dist/section.mjs +1 -1
  42. package/dist/utils.d.ts +75 -20
  43. package/dist/utils.js +1 -37
  44. package/dist/utils.mjs +1 -1
  45. package/package.json +6 -20
  46. package/dist/chunk-KRMGJ4HN.mjs +0 -2
  47. package/dist/chunk-O6BHSWEF.mjs +0 -37
package/dist/section.mjs CHANGED
@@ -1 +1 @@
1
- import{a}from"./chunk-KRMGJ4HN.mjs";import"./chunk-O6BHSWEF.mjs";export{a as toSection};
1
+ import{a}from"./chunk-GM6UUFDK.mjs";import"./chunk-YKZOUIFR.mjs";import"./chunk-3ZZBJC3M.mjs";export{a as toSection};
package/dist/utils.d.ts CHANGED
@@ -1,6 +1,13 @@
1
- import { IImageOptions } from "@mayank1513/docx";
2
- import { BlockContent, DefinitionContent, RootContent } from "mdast";
1
+ import { ExternalHyperlink, ImageRun, InternalHyperlink, IParagraphOptions, TextRun, Table, Paragraph, IRunOptions, IPropertiesOptions, Math as DOCXMath } from "@mayank1513/docx";
2
+ import * as DOCX from "@mayank1513/docx";
3
+ import { BlockContent, DefinitionContent, Parent, Root, RootContent } from "mdast";
3
4
  export { convertInchesToTwip, convertMillimetersToTwip } from "@mayank1513/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
+ };
4
11
  /** Type representing definition mappings */
5
12
  export type Definitions = Record<string, string>;
6
13
  /** Type representing footnote definitions */
@@ -10,6 +17,7 @@ export type FootnoteDefinitions = Record<string, {
10
17
  }>;
11
18
  /**
12
19
  * Extracts definitions and footnote definitions from a list of MDAST nodes.
20
+ *
13
21
  * @param nodes - Array of MDAST nodes.
14
22
  * @returns An object containing `definitions` and `footnoteDefinitions`.
15
23
  */
@@ -17,39 +25,86 @@ export declare const getDefinitions: (nodes: RootContent[]) => {
17
25
  definitions: Definitions;
18
26
  footnoteDefinitions: FootnoteDefinitions;
19
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 | T;
20
36
  /**
21
- * Type definition for an image resolver function.
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.
22
42
  */
23
- export type ImageResolver = (src: string) => Promise<IImageOptions>;
43
+ export declare const getTextContent: (node: ExtendedRootContent) => string;
24
44
  /**
25
- * Interface defining properties for MDAST to DOCX conversion.
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.
26
51
  */
27
- export interface IMdastToDocxSectionProps {
52
+ interface IDefaultMdastToDocxSectionProps {
28
53
  /**
29
54
  * If true, H1 corresponds to the title, H2 to Heading1, etc.
30
55
  * @default true
31
56
  */
32
- useTitle?: boolean;
57
+ useTitle: boolean;
33
58
  /**
34
- * Custom image resolver function. Defaults to assuming client-side code.
59
+ * List of plugins to extend conversion functionality.
35
60
  */
36
- imageResolver?: ImageResolver;
61
+ plugins: Array<IPlugin>;
37
62
  }
38
63
  /**
39
- * Determines the MIME type of an image from its binary buffer.
40
- * @param buffer - The image buffer (ArrayBuffer or Buffer).
41
- * @returns The detected MIME type as a string.
64
+ * Interface defining properties for MDAST to DOCX conversion.
65
+ */
66
+ export type IMdastToDocxSectionProps = Optional<IDefaultMdastToDocxSectionProps>;
67
+ export declare const defaultProps: IDefaultMdastToDocxSectionProps;
68
+ /**
69
+ * Defines document properties, excluding sections and footnotes (which are managed internally).
42
70
  */
43
- export declare function getImageMimeType(buffer: Buffer | ArrayBuffer): "bmp" | "png" | "gif" | "jpg" | undefined;
71
+ export type IDocxProps = Omit<Mutable<IPropertiesOptions>, "sections" | "footnotes">;
44
72
  /**
45
- * Default properties for MDAST to DOCX conversion.
73
+ * Mutable version of IRunOptions where all properties are writable.
46
74
  */
47
- interface IDefaultMdastToDocxSectionProps extends IMdastToDocxSectionProps {
48
- useTitle: boolean;
49
- imageResolver: ImageResolver;
75
+ export type MutableRunOptions = Mutable<Omit<IRunOptions, "children">>;
76
+ export type InlineParentType = "strong" | "emphasis" | "delete" | "link";
77
+ export type InlineDocxNodes = TextRun | ImageRun | InternalHyperlink | ExternalHyperlink | DOCXMath;
78
+ export type InlineProcessor = (node: ExtendedRootContent, runProps: MutableRunOptions) => Promise<InlineDocxNodes[]>;
79
+ export type InlineChildrenProcessor = (node: Parent, runProps?: MutableRunOptions) => Promise<InlineDocxNodes[]>;
80
+ /**
81
+ * Mutable version of IParagraphOptions where all properties are writable.
82
+ */
83
+ export type MutableParaOptions = Omit<Mutable<IParagraphOptions>, "children">;
84
+ export type BlockNodeProcessor = (node: ExtendedRootContent, paraProps: MutableParaOptions) => Promise<(Paragraph | Table)[]>;
85
+ export type BlockNodeChildrenProcessor = (node: Parent | Root, paraProps: MutableParaOptions) => Promise<(Paragraph | Table)[]>;
86
+ /**
87
+ * Interface for extending MDAST to DOCX conversion using plugins.
88
+ */
89
+ export interface IPlugin<T extends {
90
+ type: string;
91
+ } = {
92
+ type: "";
93
+ }> {
94
+ /**
95
+ * Processes block-level nodes.
96
+ */
97
+ block?: (docx: typeof DOCX, node: ExtendedRootContent<T>, paraProps: MutableParaOptions, blockChildrenProcessor: BlockNodeChildrenProcessor, inlineChildrenProcessor: InlineChildrenProcessor) => Promise<(Paragraph | Table)[]>;
98
+ /**
99
+ * Processes inline-level nodes.
100
+ */
101
+ inline?: (docx: typeof DOCX, node: ExtendedRootContent<T>, parentSet: MutableRunOptions, definitions: Definitions, footnoteDefinitions: FootnoteDefinitions, inlineChildrenProcessor: InlineChildrenProcessor) => Promise<InlineDocxNodes[]>;
102
+ /**
103
+ * Allows plugins to modify document-level DOCX properties, such as styles, numbering, headers, and footers. This is useful for global formatting customizations.
104
+ */
105
+ root?: (props: IDocxProps) => void;
50
106
  }
51
- export declare const defaultProps: IDefaultMdastToDocxSectionProps;
52
107
  /**
53
- * @mayank/docx is a fork of the `docx` library with minor changes,
54
- * specifically exporting additional types that were not included in the original `docx` library.
108
+ * @mayank/docx is a fork of the `docx` library with minor modifications,
109
+ * primarily adding exports for additional types missing from the original `docx` library.
55
110
  */