abstract-document 20.1.6 → 20.2.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 (74) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/lib/abstract-document/atoms/text-run.d.ts.map +1 -1
  3. package/lib/abstract-document/atoms/text-run.js +1 -7
  4. package/lib/abstract-document/atoms/text-run.js.map +1 -1
  5. package/lib/abstract-document/markdown/markdown.d.ts.map +1 -1
  6. package/lib/abstract-document/markdown/markdown.js +58 -60
  7. package/lib/abstract-document/markdown/markdown.js.map +1 -1
  8. package/lib/abstract-document/resources.d.ts.map +1 -1
  9. package/lib/abstract-document/resources.js +1 -3
  10. package/lib/abstract-document/resources.js.map +1 -1
  11. package/lib/abstract-document/styles/table-style.js +2 -2
  12. package/lib/abstract-document/styles/table-style.js.map +1 -1
  13. package/lib/abstract-document/styles/text-style.js +6 -6
  14. package/lib/abstract-document/styles/text-style.js.map +1 -1
  15. package/lib/abstract-document-exporters/docx/xml-writer.d.ts.map +1 -1
  16. package/lib/abstract-document-exporters/docx/xml-writer.js +14 -17
  17. package/lib/abstract-document-exporters/docx/xml-writer.js.map +1 -1
  18. package/lib/abstract-document-exporters/docx2/render-image.js +9 -11
  19. package/lib/abstract-document-exporters/docx2/render-image.js.map +1 -1
  20. package/lib/abstract-document-exporters/docx2/render.js +2 -6
  21. package/lib/abstract-document-exporters/docx2/render.js.map +1 -1
  22. package/lib/abstract-document-exporters/pdf/font.d.ts.map +1 -1
  23. package/lib/abstract-document-exporters/pdf/font.js +1 -2
  24. package/lib/abstract-document-exporters/pdf/font.js.map +1 -1
  25. package/lib/abstract-document-exporters/pdf/measure.js +3 -3
  26. package/lib/abstract-document-exporters/pdf/measure.js.map +1 -1
  27. package/lib/abstract-document-exporters/pdf/paginate.js +1 -0
  28. package/lib/abstract-document-exporters/pdf/paginate.js.map +1 -1
  29. package/lib/abstract-document-exporters/pdf/pre-process.js +4 -6
  30. package/lib/abstract-document-exporters/pdf/pre-process.js.map +1 -1
  31. package/lib/abstract-document-exporters/pdf/render-image.js +2 -2
  32. package/lib/abstract-document-exporters/pdf/render-image.js.map +1 -1
  33. package/lib/abstract-document-exporters/pdf/render.js +23 -26
  34. package/lib/abstract-document-exporters/pdf/render.js.map +1 -1
  35. package/lib/abstract-document-exporters/pdf/textRunRow.d.ts.map +1 -1
  36. package/lib/abstract-document-exporters/pdf/textRunRow.js +21 -23
  37. package/lib/abstract-document-exporters/pdf/textRunRow.js.map +1 -1
  38. package/lib/abstract-document-jsx/index.d.ts.map +1 -1
  39. package/lib/abstract-document-jsx/index.js +5 -5
  40. package/lib/abstract-document-jsx/index.js.map +1 -1
  41. package/lib/abstract-document-xml/abstract-doc-of-xml/abstract-doc-of-xml.d.ts.map +1 -1
  42. package/lib/abstract-document-xml/abstract-doc-of-xml/abstract-doc-of-xml.js +32 -29
  43. package/lib/abstract-document-xml/abstract-doc-of-xml/abstract-doc-of-xml.js.map +1 -1
  44. package/lib/abstract-document-xml/abstract-doc-of-xml/creator.d.ts.map +1 -1
  45. package/lib/abstract-document-xml/abstract-doc-of-xml/creator.js +29 -46
  46. package/lib/abstract-document-xml/abstract-doc-of-xml/creator.js.map +1 -1
  47. package/lib/abstract-document-xml/abstract-doc-of-xml/custom-elements.d.ts.map +1 -1
  48. package/lib/abstract-document-xml/abstract-doc-of-xml/custom-elements.js +16 -5
  49. package/lib/abstract-document-xml/abstract-doc-of-xml/custom-elements.js.map +1 -1
  50. package/package.json +4 -4
  51. package/src/abstract-document/atoms/text-run.ts +1 -7
  52. package/src/abstract-document/markdown/markdown.ts +74 -76
  53. package/src/abstract-document/resources.ts +1 -3
  54. package/src/abstract-document/styles/table-style.ts +2 -2
  55. package/src/abstract-document/styles/text-style.ts +6 -6
  56. package/src/abstract-document-exporters/docx/xml-writer.ts +16 -19
  57. package/src/abstract-document-exporters/docx2/render-image.ts +11 -13
  58. package/src/abstract-document-exporters/docx2/render.ts +3 -7
  59. package/src/abstract-document-exporters/pdf/font.ts +1 -2
  60. package/src/abstract-document-exporters/pdf/measure.ts +9 -9
  61. package/src/abstract-document-exporters/pdf/paginate.ts +3 -2
  62. package/src/abstract-document-exporters/pdf/pre-process.ts +5 -7
  63. package/src/abstract-document-exporters/pdf/render-image.ts +9 -9
  64. package/src/abstract-document-exporters/pdf/render.ts +25 -28
  65. package/src/abstract-document-exporters/pdf/textRunRow.ts +32 -35
  66. package/src/abstract-document-jsx/index.ts +7 -7
  67. package/src/abstract-document-xml/abstract-doc-of-xml/abstract-doc-of-xml.ts +30 -27
  68. package/src/abstract-document-xml/abstract-doc-of-xml/creator.ts +38 -51
  69. package/src/abstract-document-xml/abstract-doc-of-xml/custom-elements.tsx +22 -8
  70. package/src/custom_typings/pdf2json/index.d.ts +10 -1
  71. package/src/custom_typings/remark-parse/index.d.ts +4 -1
  72. package/src/custom_typings/remark-sub-super/index.d.ts +4 -1
  73. package/src/custom_typings/svg-to-pdfkit/index.d.ts +4 -1
  74. package/src/custom_typings/unified/index.d.ts +8 -1
@@ -17,7 +17,7 @@ import {
17
17
  TocSeparator,
18
18
  ImageResource,
19
19
  } from "../../abstract-document/index.js";
20
- import type { TextRowProps, TextCellProps, TextParagraphProps, ImageCellProps, ImageParagraphProps, ImageRowProps } from "./custom-elements.js";
20
+ import type { ImageCellProps, ImageParagraphProps, ImageRowProps } from "./custom-elements.js";
21
21
  import { TextRow, TextCell, TextParagraph, ImageCell, ImageParagraph, ImageRow } from "./custom-elements.js";
22
22
 
23
23
  export type ADCreatorFn = (props?: Record<string, unknown>, children?: ReadonlyArray<unknown>) => unknown;
@@ -27,22 +27,23 @@ export const creators: (styleNames: Record<string, string>) => Record<string, AD
27
27
  AbstractDoc: ((props, children: ReadonlyArray<Section.Section>) => AbstractDoc.create(props, children)) as ADCreatorFn,
28
28
  Section: ((props, children: ReadonlyArray<SectionElement.SectionElement>) => Section.create(props, children)) as ADCreatorFn,
29
29
  Paragraph: ((props, children: ReadonlyArray<Atom.Atom>) => Paragraph.create(props, children)) as ADCreatorFn,
30
- TextRow: ((props: TextRowProps) => TextRow(props, styleNames)) as ADCreatorFn,
31
- TextCell: ((props: TextCellProps) => TextCell(props, styleNames)) as ADCreatorFn,
32
- TextParagraph: ((props: TextParagraphProps) => TextParagraph(props, styleNames)) as ADCreatorFn,
33
- TextRun: (props) => TextRun.create(props as unknown as TextRun.TextRunProps),
30
+ TextRow: (props) => TextRow({ ...props, text: requireText(props) }, styleNames),
31
+ TextCell: (props) => TextCell({ ...props, text: requireText(props) }, styleNames),
32
+ TextParagraph: (props) => TextParagraph({ ...props, text: requireText(props) }, styleNames),
33
+ TextRun: (props) => TextRun.create({ ...props, text: requireText(props) }),
34
34
  ImageRow: ((props: Record<string, unknown>) => ImageRow(imageProps(props) as unknown as ImageRowProps, styleNames)) as ADCreatorFn,
35
35
  ImageCell: ((props: Record<string, unknown>) => ImageCell(imageProps(props) as unknown as ImageCellProps, styleNames)) as ADCreatorFn,
36
36
  ImageParagraph: ((props: Record<string, unknown>) =>
37
37
  ImageParagraph(imageProps(props) as unknown as ImageParagraphProps, styleNames)) as ADCreatorFn,
38
38
  Image: ((props: Record<string, unknown>) => Image.create(imageProps(props) as unknown as Image.ImageProps)) as ADCreatorFn,
39
- Table: ((props, children: ReadonlyArray<TableRow.TableRow>) => Table.create(props as unknown as Table.TableProps, children)) as ADCreatorFn,
39
+ Table: ((props, children: ReadonlyArray<TableRow.TableRow>) =>
40
+ Table.create({ ...props, columnWidths: props?.["columnWidths"] ?? [] } as unknown as Table.TableProps, children)) as ADCreatorFn,
40
41
  TableRow: ((props, children: ReadonlyArray<TableCell.TableCell>) => TableRow.create(props, children)) as ADCreatorFn,
41
42
  TableCell: ((props, children: ReadonlyArray<SectionElement.SectionElement>) => TableCell.create(props, children)) as ADCreatorFn,
42
- TextField: (props) => TextField.create(props as unknown as TextField.TextFieldProps),
43
- Group: (props, children) => Group.create(props, children as ReadonlyArray<Group.Group>),
43
+ TextField: (props) => TextField.create({ ...props, fieldType: requireFieldType(props) }),
44
+ Group: (props, children) => Group.create(props, children as ReadonlyArray<SectionElement.SectionElement>),
44
45
  PageBreak: () => PageBreak.create(),
45
- Markdown: (props) => Markdown.create(props as unknown as Markdown.MarkdownProps),
46
+ Markdown: (props) => Markdown.create({ ...props, text: requireText(props) }),
46
47
  TocSeparator: (props) => TocSeparator.create(props as TocSeparator.TocSeparatorProps),
47
48
  };
48
49
  };
@@ -50,23 +51,17 @@ export const creators: (styleNames: Record<string, string>) => Record<string, AD
50
51
  export const propsCreators: Record<string, ADCreatorFn> = {
51
52
  styles: ((props: { readonly styles: Record<string, Record<string, string | number> & { readonly type: string }> }): unknown => {
52
53
  const fixedStyles: Record<string, Record<string, string | number>> = {};
53
- if (props.styles) {
54
- Object.keys(props.styles).forEach((key: string) => {
55
- fixedStyles[key] = { ...props.styles[key] };
56
- });
57
- }
54
+ Object.keys(props.styles).forEach((key: string) => {
55
+ fixedStyles[key] = { ...props.styles[key] };
56
+ });
58
57
 
59
58
  return { ...fixedStyles, ...DefaultStyles.createStandardStyles() };
60
59
  }) as ADCreatorFn,
61
60
  columnWidths: ((props: { readonly columnWidths: string; readonly columnMultiplier: string }): unknown => {
62
- const columnWidths = (props.columnWidths ?? "")
63
- // oxlint-disable-next-line typescript/no-unnecessary-type-conversion -- allProps can carry non-strings
64
- .toString()
65
- .split(",")
66
- .map((item: string) => {
67
- const number = Number(item);
68
- return number === 0 || Number.isNaN(number) ? Infinity : number;
69
- });
61
+ const columnWidths = props.columnWidths.split(",").map((item: string) => {
62
+ const number = Number(item);
63
+ return number === 0 || Number.isNaN(number) ? Infinity : number;
64
+ });
70
65
  if (props.columnMultiplier) {
71
66
  return columnWidths.map((l) => l * Number(props.columnMultiplier));
72
67
  }
@@ -83,11 +78,7 @@ export const propsCreators: Record<string, ADCreatorFn> = {
83
78
  }) as ADCreatorFn,
84
79
  borders: ((props: { readonly borders: string }): unknown => {
85
80
  const borders: { [k: string]: number } = { top: 0, right: 0, bottom: 0, left: 0 };
86
- // oxlint-disable-next-line typescript/no-unnecessary-type-conversion -- allProps can carry non-strings
87
- const propBorders = props.borders.toString().split(" ");
88
- if (!propBorders) {
89
- return borders;
90
- }
81
+ const propBorders = props.borders.split(" ");
91
82
 
92
83
  if (propBorders.length === 1) {
93
84
  borders["top"] = Number(propBorders[0]);
@@ -150,11 +141,7 @@ export const propsCreators: Record<string, ADCreatorFn> = {
150
141
  padding: ((props: { readonly padding: string }): unknown => {
151
142
  const padding: { [k: string]: number } = { top: 0, right: 0, bottom: 0, left: 0 };
152
143
 
153
- // oxlint-disable-next-line typescript/no-unnecessary-type-conversion -- allProps can carry non-strings
154
- const paddings = props.padding.toString().split(" ");
155
- if (!paddings) {
156
- return padding;
157
- }
144
+ const paddings = props.padding.split(" ");
158
145
 
159
146
  if (paddings.length === 1) {
160
147
  padding["top"] = Number(paddings[0]);
@@ -216,11 +203,7 @@ export const propsCreators: Record<string, ADCreatorFn> = {
216
203
  }) as ADCreatorFn,
217
204
  margins: ((props: { readonly margins: string }): unknown => {
218
205
  const margins: { [k: string]: number } = { top: 0, right: 0, bottom: 0, left: 0 };
219
- // oxlint-disable-next-line typescript/no-unnecessary-type-conversion -- allProps can carry non-strings
220
- const propMargins = props.margins.toString().split(" ");
221
- if (!propMargins) {
222
- return margins;
223
- }
206
+ const propMargins = props.margins.split(" ");
224
207
 
225
208
  if (propMargins.length === 1) {
226
209
  margins["top"] = Number(propMargins[0]);
@@ -358,6 +341,21 @@ export const propsCreators: Record<string, ADCreatorFn> = {
358
341
  differentFirstPage: ((props: { readonly differentFirstPage: string }): unknown => strToBool(props.differentFirstPage)) as ADCreatorFn,
359
342
  };
360
343
 
344
+ function requireText(props: Record<string, unknown> | undefined): string {
345
+ const text = props?.["text"];
346
+ return typeof text === "string" ? text : "";
347
+ }
348
+
349
+ const validFieldTypes: ReadonlyArray<TextField.FieldType> = ["Date", "PageNumber", "TotalPages", "PageNumberOf"];
350
+
351
+ function requireFieldType(props: Record<string, unknown> | undefined): TextField.FieldType {
352
+ const fieldType = props?.["fieldType"];
353
+ if (validFieldTypes.includes(fieldType as TextField.FieldType)) {
354
+ return fieldType as TextField.FieldType;
355
+ }
356
+ throw new Error(`<TextField> requires a "fieldType" attribute of ${validFieldTypes.join(", ")}, got: ${String(fieldType)}`);
357
+ }
358
+
361
359
  const zero = createPoint(0, 0);
362
360
  const size = createSize(0, 0);
363
361
  function imageProps(props: Record<string, unknown>): Record<string, unknown> {
@@ -373,21 +371,10 @@ function imageProps(props: Record<string, unknown>): Record<string, unknown> {
373
371
  };
374
372
  }
375
373
 
376
- const strToBool = (str: string | undefined): boolean => {
377
- if (str === undefined) {
378
- return false;
379
- }
380
- const validBools = {
381
- "1": true,
382
- true: true,
383
- };
384
- return str in validBools;
385
- };
374
+ // bold/italic/etc. are declared `xs:boolean` in the XSD schema; XSD's boolean lexical space is {true, false, 1, 0}
375
+ const strToBool = (str: string | undefined): boolean => str === "true" || str === "1";
386
376
 
387
377
  const strToNum = (str: string | undefined): number => {
388
378
  const num = Number(str);
389
- if (Number.isNaN(num)) {
390
- return 0;
391
- }
392
- return num;
379
+ return Number.isFinite(num) ? num : 0;
393
380
  };
@@ -35,7 +35,9 @@ export function extractStyleNames(styleNames: string | undefined, styleNameTypes
35
35
  return styleNameProps;
36
36
  }
37
37
 
38
- export type TextRowProps = Omit<TextCellProps, "style"> & { readonly cellStyle?: TableCellStyle.TableCellStyle };
38
+ export type TextRowProps = Omit<TextCellProps, "style"> & {
39
+ readonly cellStyle?: TableCellStyle.TableCellStyle;
40
+ };
39
41
 
40
42
  export function TextRow(props: TextRowProps, styleNameTypes: Record<string, string>): TableRow.TableRow {
41
43
  return TableRow.create({}, [TextCell({ ...props, style: props.cellStyle }, styleNameTypes)]);
@@ -85,7 +87,9 @@ export function TextParagraph(props: TextParagraphProps, styleNameTypes: Record<
85
87
  return Paragraph.create({ style, styleName: styleNames.ParagraphStyle }, [textRun]);
86
88
  }
87
89
 
88
- export type ImageRowProps = Omit<ImageCellProps, "style"> & { readonly cellStyle?: TableCellStyle.TableCellStyle };
90
+ export type ImageRowProps = Omit<ImageCellProps, "style"> & {
91
+ readonly cellStyle?: TableCellStyle.TableCellStyle;
92
+ };
89
93
 
90
94
  export function ImageRow(props: ImageRowProps, styleNameTypes: Record<string, string>): TableRow.TableRow {
91
95
  return TableRow.create({}, [ImageCell({ ...props, style: props.cellStyle }, styleNameTypes)]);
@@ -101,12 +105,18 @@ export type ImageCellProps = Omit<ImageParagraphProps, "style"> & {
101
105
  export function ImageCell(props: ImageCellProps, styleNameTypes: Record<string, string>): TableCell.TableCell {
102
106
  const { imageResource, width, height, horizontalAlignment, verticalAlignment, paragraphStyle, style, columnSpan, rowSpan } = props;
103
107
  const styleNames = extractStyleNames(props.styleNames, styleNameTypes);
104
- const imageElement = imageResource && Image.create({ imageResource, width, height, horizontalAlignment, verticalAlignment });
108
+ const imageElement = Image.create({
109
+ imageResource,
110
+ width,
111
+ height,
112
+ horizontalAlignment,
113
+ verticalAlignment,
114
+ });
105
115
  const pararaphProps = {
106
116
  style: paragraphStyle ? { ...paragraphStyle, type: "ParagraphStyle" } : undefined,
107
117
  styleName: styleNames.ParagraphStyle,
108
118
  } as ParagraphProps;
109
- const paragraph = imageElement ? Paragraph.create(pararaphProps, [imageElement]) : Paragraph.create(pararaphProps, [ImageMissing]);
119
+ const paragraph = Paragraph.create(pararaphProps, [imageElement]);
110
120
  return TableCell.create({ columnSpan, rowSpan, style, styleName: styleNames.TableCellStyle }, [paragraph]);
111
121
  }
112
122
 
@@ -118,9 +128,13 @@ export type ImageParagraphProps = ImageProps & {
118
128
  export function ImageParagraph(props: ImageParagraphProps, styleNameTypes: Record<string, string>): Paragraph.Paragraph | undefined {
119
129
  const { imageResource, width, height, style, horizontalAlignment, verticalAlignment } = props;
120
130
  const styleNames = extractStyleNames(props.styleNames, styleNameTypes);
121
- const imageElement = imageResource && Image.create({ imageResource, width, height, horizontalAlignment, verticalAlignment });
131
+ const imageElement = Image.create({
132
+ imageResource,
133
+ width,
134
+ height,
135
+ horizontalAlignment,
136
+ verticalAlignment,
137
+ });
122
138
  const pararaphProps = { style, styleName: styleNames.ParagraphStyle };
123
- return imageElement ? Paragraph.create(pararaphProps, [imageElement]) : Paragraph.create(pararaphProps, [ImageMissing]);
139
+ return Paragraph.create(pararaphProps, [imageElement]);
124
140
  }
125
-
126
- const ImageMissing = TextRun.create({ text: "Image missing" });
@@ -1 +1,10 @@
1
- declare module "pdf2json";
1
+ declare module "pdf2json" {
2
+ // oxlint-disable-next-line no-redeclare -- value + type share a name, the standard pattern for typing a constructible class from an untyped module
3
+ interface PDFParser {
4
+ on: ((event: "pdfParser_dataError", callback: (data: { parserError: string }) => void) => void) &
5
+ ((event: "pdfParser_dataReady", callback: (data: unknown) => void) => void);
6
+ parseBuffer: (buffer: Buffer) => void;
7
+ }
8
+ const PDFParser: new () => PDFParser;
9
+ export default PDFParser;
10
+ }
@@ -1 +1,4 @@
1
- declare module "remark-parse";
1
+ declare module "remark-parse" {
2
+ const remarkParse: unknown;
3
+ export default remarkParse;
4
+ }
@@ -1 +1,4 @@
1
- declare module "remark-sub-super";
1
+ declare module "remark-sub-super" {
2
+ const remarkSubSuper: unknown;
3
+ export default remarkSubSuper;
4
+ }
@@ -1 +1,4 @@
1
- declare module "svg-to-pdfkit";
1
+ declare module "svg-to-pdfkit" {
2
+ function svgToPdfKit(doc: PDFKit.PDFDocument, svg: string, x: number, y: number, options?: Record<string, unknown>): void;
3
+ export default svgToPdfKit;
4
+ }
@@ -1 +1,8 @@
1
- declare module "unified";
1
+ declare module "unified" {
2
+ interface Processor {
3
+ use: (plugin: unknown, ...settings: Array<unknown>) => Processor;
4
+ parse: (source: string) => unknown;
5
+ }
6
+ function unified(): Processor;
7
+ export default unified;
8
+ }