abstract-document 16.0.35 → 16.0.36

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 (34) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/lib/abstract-document/markdown/markdown.d.ts.map +1 -1
  3. package/lib/abstract-document/markdown/markdown.js +10 -1
  4. package/lib/abstract-document/markdown/markdown.js.map +1 -1
  5. package/lib/abstract-document/section-elements/paragraph.d.ts +2 -0
  6. package/lib/abstract-document/section-elements/paragraph.d.ts.map +1 -1
  7. package/lib/abstract-document/section-elements/paragraph.js +2 -1
  8. package/lib/abstract-document/section-elements/paragraph.js.map +1 -1
  9. package/lib/abstract-document-exporters/pdf/render.js +109 -2
  10. package/lib/abstract-document-exporters/pdf/render.js.map +1 -1
  11. package/lib/abstract-document-xml/handlebars-xml/helpers.d.ts +2 -0
  12. package/lib/abstract-document-xml/handlebars-xml/helpers.d.ts.map +1 -0
  13. package/lib/abstract-document-xml/handlebars-xml/helpers.js +93 -0
  14. package/lib/abstract-document-xml/handlebars-xml/helpers.js.map +1 -0
  15. package/lib/abstract-document-xml/handlebars-xml/index.d.ts +3 -0
  16. package/lib/abstract-document-xml/handlebars-xml/index.d.ts.map +1 -0
  17. package/lib/abstract-document-xml/handlebars-xml/index.js +19 -0
  18. package/lib/abstract-document-xml/handlebars-xml/index.js.map +1 -0
  19. package/lib/abstract-document-xml/handlebars-xml/parse-mustache-xml.d.ts +21 -0
  20. package/lib/abstract-document-xml/handlebars-xml/parse-mustache-xml.d.ts.map +1 -0
  21. package/lib/abstract-document-xml/handlebars-xml/parse-mustache-xml.js +148 -0
  22. package/lib/abstract-document-xml/handlebars-xml/parse-mustache-xml.js.map +1 -0
  23. package/lib/abstract-document-xml/handlebars-xml/validate-mustache-xml.d.ts +21 -0
  24. package/lib/abstract-document-xml/handlebars-xml/validate-mustache-xml.d.ts.map +1 -0
  25. package/lib/abstract-document-xml/handlebars-xml/validate-mustache-xml.js +195 -0
  26. package/lib/abstract-document-xml/handlebars-xml/validate-mustache-xml.js.map +1 -0
  27. package/package.json +2 -2
  28. package/src/abstract-document/markdown/markdown.ts +11 -1
  29. package/src/abstract-document/section-elements/paragraph.ts +4 -1
  30. package/src/abstract-document-exporters/pdf/render.ts +151 -6
  31. package/lib/abstract-document-exporters/shared/to-base-64.d.ts +0 -3
  32. package/lib/abstract-document-exporters/shared/to-base-64.d.ts.map +0 -1
  33. package/lib/abstract-document-exporters/shared/to-base-64.js +0 -19
  34. package/lib/abstract-document-exporters/shared/to-base-64.js.map +0 -1
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.parseXsd = exports.parseXml = exports.renderHandlebars = exports.renderMustache = exports.parseMustacheXml = exports.TemplateMethod = void 0;
7
+ exports.parseXmlCustom = parseXmlCustom;
8
+ exports.findElement = findElement;
9
+ exports.getChildren = getChildren;
10
+ const fast_xml_parser_1 = require("fast-xml-parser");
11
+ const handlebars_1 = __importDefault(require("handlebars"));
12
+ const mustache_1 = __importDefault(require("mustache"));
13
+ const helpers_1 = require("./helpers");
14
+ var TemplateMethod;
15
+ (function (TemplateMethod) {
16
+ TemplateMethod[TemplateMethod["Mustache"] = 0] = "Mustache";
17
+ TemplateMethod[TemplateMethod["Handlebars"] = 1] = "Handlebars";
18
+ })(TemplateMethod || (exports.TemplateMethod = TemplateMethod = {}));
19
+ const parseMustacheXml = (template, data, partials, method = TemplateMethod.Handlebars) => {
20
+ switch (method) {
21
+ case TemplateMethod.Handlebars: {
22
+ return (0, exports.parseXml)((0, exports.renderHandlebars)(template, data, partials));
23
+ }
24
+ case TemplateMethod.Mustache:
25
+ default: {
26
+ return (0, exports.parseXml)((0, exports.renderMustache)(template, data, partials));
27
+ }
28
+ }
29
+ };
30
+ exports.parseMustacheXml = parseMustacheXml;
31
+ exports.renderMustache = mustache_1.default.render;
32
+ const renderHandlebars = (template, data, partials) => {
33
+ (0, helpers_1.registerHelpers)();
34
+ Object.entries(partials).forEach(([name, partial]) => handlebars_1.default.registerPartial(name, partial));
35
+ return handlebars_1.default.compile(template, { compat: true, preventIndent: true })(data);
36
+ };
37
+ exports.renderHandlebars = renderHandlebars;
38
+ function parseXmlCustom(text, options) {
39
+ const parser = new fast_xml_parser_1.XMLParser(options);
40
+ parser.addEntity("#x2F", "/");
41
+ parser.addEntity("#x3D", "=");
42
+ return transformFXP(parser.parse(text));
43
+ }
44
+ const parseXml = (text) => transformFXP(xmlParser.parse(text));
45
+ exports.parseXml = parseXml;
46
+ const parseXsd = (text) => transformFXP(xsdParser.parse(text.replace(/xs:/g, "")));
47
+ exports.parseXsd = parseXsd;
48
+ // Transforms fast-xml-parser format to a format that is much easier to work with
49
+ function transformFXP(parsedXml) {
50
+ return parsedXml.flatMap((element) => {
51
+ const key = Object.keys(element).find((k) => k !== ":@" && k !== "#text");
52
+ if (key === "?xml") {
53
+ return [];
54
+ }
55
+ const children = (element[key] || []).filter((c) => {
56
+ const key = Object.keys(c)[0];
57
+ return key !== "#text" && key !== ":@";
58
+ });
59
+ const attributes = element[":@"] || {};
60
+ const textChilds = (element[key] || [])
61
+ .filter((c) => {
62
+ const key = Object.keys(c)[0];
63
+ return key === "#text";
64
+ })
65
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
66
+ .flatMap((t) => t["#text"]);
67
+ const textContents = textChilds.flatMap((text) => {
68
+ const cleaned = text.replace(/\n/g, "").replace(/\t/g, "").trim();
69
+ return cleaned ? [text] : [];
70
+ });
71
+ const textContent = textContents.join("\n");
72
+ return { tagName: key, attributes: attributes, children: transformFXP(children), textContent: textContent };
73
+ });
74
+ }
75
+ function findElement(elements, elementName) {
76
+ if (!elementName) {
77
+ return undefined;
78
+ }
79
+ for (const elem of elements) {
80
+ if (elem.tagName === "annotation" || elem.tagName === "attribute") {
81
+ continue;
82
+ }
83
+ if (shouldSkipLevel(elem)) {
84
+ const childElement = findElement(Array.from(elem.children), elementName);
85
+ if (childElement) {
86
+ return shouldSkipLevel(childElement)
87
+ ? findElement(Array.from(childElement.children), elementName)
88
+ : childElement;
89
+ }
90
+ }
91
+ if (elem.attributes.name === elementName) {
92
+ return elem;
93
+ }
94
+ }
95
+ return undefined;
96
+ }
97
+ function getChildren(elements) {
98
+ for (const elem of elements) {
99
+ if (elem.tagName === "annotation" || elem.tagName === "attribute") {
100
+ continue;
101
+ }
102
+ if (shouldSkipLevel(elem)) {
103
+ const child = getChildren(Array.from(elem.children));
104
+ if (child.length > 0) {
105
+ return child.flatMap((c) => (shouldSkipLevel(c) ? getChildren(Array.from(c.children)) : c));
106
+ }
107
+ }
108
+ return elements;
109
+ }
110
+ return [];
111
+ }
112
+ function shouldSkipLevel(tag) {
113
+ return (tag.tagName === "all" || tag.tagName === "sequence" || tag.tagName === "choice" || tag.attributes.name === undefined);
114
+ }
115
+ const xmlParser = new fast_xml_parser_1.XMLParser({
116
+ preserveOrder: true,
117
+ ignoreAttributes: false,
118
+ attributeNamePrefix: "",
119
+ allowBooleanAttributes: true,
120
+ trimValues: false,
121
+ ignoreDeclaration: true,
122
+ processEntities: true,
123
+ htmlEntities: true,
124
+ attributeValueProcessor: (_name, value) => {
125
+ if (!value?.trim()) {
126
+ return value;
127
+ }
128
+ const nValue = Number(value);
129
+ if (!Number.isNaN(nValue)) {
130
+ return nValue;
131
+ }
132
+ return value;
133
+ },
134
+ });
135
+ xmlParser.addEntity("#x2F", "/");
136
+ xmlParser.addEntity("#x3D", "=");
137
+ const xsdParser = new fast_xml_parser_1.XMLParser({
138
+ preserveOrder: true,
139
+ ignoreAttributes: false,
140
+ attributeNamePrefix: "",
141
+ allowBooleanAttributes: true,
142
+ trimValues: false,
143
+ ignoreDeclaration: true,
144
+ stopNodes: ["*.documentation"],
145
+ });
146
+ xsdParser.addEntity("#x2F", "/");
147
+ xsdParser.addEntity("#x3D", "=");
148
+ //# sourceMappingURL=parse-mustache-xml.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-mustache-xml.js","sourceRoot":"","sources":["../../../src/abstract-document-xml/handlebars-xml/parse-mustache-xml.ts"],"names":[],"mappings":";;;;;;AAyCA,wCAKC;AAmCD,kCAwBC;AAED,kCAcC;AAzHD,qDAAwD;AACxD,4DAAoC;AACpC,wDAAgC;AAChC,uCAA4C;AAS5C,IAAY,cAGX;AAHD,WAAY,cAAc;IACxB,2DAAY,CAAA;IACZ,+DAAc,CAAA;AAChB,CAAC,EAHW,cAAc,8BAAd,cAAc,QAGzB;AAEM,MAAM,gBAAgB,GAAG,CAC9B,QAAgB,EAChB,IAAS,EACT,QAAgC,EAChC,SAAyB,cAAc,CAAC,UAAU,EACvB,EAAE;IAC7B,QAAQ,MAAM,EAAE,CAAC;QACf,KAAK,cAAc,CAAC,UAAU,CAAC,CAAC,CAAC;YAC/B,OAAO,IAAA,gBAAQ,EAAC,IAAA,wBAAgB,EAAC,QAAQ,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC;QAC9D,CAAC;QACD,KAAK,cAAc,CAAC,QAAQ,CAAC;QAC7B,OAAO,CAAC,CAAC,CAAC;YACR,OAAO,IAAA,gBAAQ,EAAC,IAAA,sBAAc,EAAC,QAAQ,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC;QAC5D,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AAfW,QAAA,gBAAgB,oBAe3B;AAEW,QAAA,cAAc,GAAG,kBAAQ,CAAC,MAAM,CAAC;AACvC,MAAM,gBAAgB,GAAG,CAAC,QAAgB,EAAE,IAAS,EAAE,QAAgC,EAAU,EAAE;IACxG,IAAA,yBAAe,GAAE,CAAC;IAClB,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,EAAE,OAAO,CAAC,EAAE,EAAE,CAAC,oBAAU,CAAC,eAAe,CAAC,IAAI,EAAE,OA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AAC;AAEjC,MAAM,SAAS,GAAG,IAAI,2BAAS,CAAC;IAC9B,aAAa,EAAE,IAAI;IACnB,gBAAgB,EAAE,KAAK;IACvB,mBAAmB,EAAE,EAAE;IACvB,sBAAsB,EAAE,IAAI;IAC5B,UAAU,EAAE,KAAK;IACjB,iBAAiB,EAAE,IAAI;IACvB,SAAS,EAAE,CAAC,iBAAiB,CAAC;CAC/B,CAAC,CAAC;AACH,SAAS,CAAC,SAAS,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AACjC,SAAS,CAAC,SAAS,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC"}
@@ -0,0 +1,21 @@
1
+ import { XmlElement } from "./parse-mustache-xml.js";
2
+ type Range = {
3
+ readonly startLineNumber: number;
4
+ readonly startColumn: number;
5
+ readonly endLineNumber: number;
6
+ readonly endColumn: number;
7
+ };
8
+ type ErrorObject = {
9
+ readonly range: Range;
10
+ readonly options: ErrorOptions;
11
+ };
12
+ type ErrorOptions = {
13
+ readonly className: string;
14
+ readonly hoverMessage: Array<{
15
+ readonly value: string;
16
+ }>;
17
+ };
18
+ export declare function validateXml(fullXml: string, xsdSchema: ReadonlyArray<XmlElement>): Array<ErrorObject>;
19
+ export declare function errorToReadableText(errors: ReadonlyArray<ErrorObject>, templateName?: string): string;
20
+ export {};
21
+ //# sourceMappingURL=validate-mustache-xml.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate-mustache-xml.d.ts","sourceRoot":"","sources":["../../../src/abstract-document-xml/handlebars-xml/validate-mustache-xml.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAyB,MAAM,yBAAyB,CAAC;AAO5E,KAAK,KAAK,GAAG;IACX,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B,CAAC;AAMF,KAAK,WAAW,GAAG;IACjB,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,YAAY,CAAC;CAChC,CAAC;AAEF,KAAK,YAAY,GAAG;IAClB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAE3B,QAAQ,CAAC,YAAY,EAAE,KAAK,CAAC;QAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;KACxB,CAAC,CAAC;CACJ,CAAC;AASF,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,aAAa,CAAC,UAAU,CAAC,GAAG,KAAK,CAAC,WAAW,CAAC,CAgErG;AAoJD,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,aAAa,CAAC,WAAW,CAAC,EAAE,YAAY,GAAE,MAAW,GAAG,MAAM,CAYzG"}
@@ -0,0 +1,195 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateXml = validateXml;
4
+ exports.errorToReadableText = errorToReadableText;
5
+ const fast_xml_parser_1 = require("fast-xml-parser");
6
+ const parse_mustache_xml_js_1 = require("./parse-mustache-xml.js");
7
+ var ErrorType;
8
+ (function (ErrorType) {
9
+ ErrorType[ErrorType["warning"] = 0] = "warning";
10
+ ErrorType[ErrorType["error"] = 1] = "error";
11
+ })(ErrorType || (ErrorType = {}));
12
+ // eslint-disable-next-line functional/prefer-readonly-type
13
+ function validateXml(fullXml, xsdSchema) {
14
+ const errors = [];
15
+ // ignore all mustache brackets
16
+ const matchMustacheBrackets = /{{.*}}(?!([\S]))/g;
17
+ // Ignore xml comments
18
+ const xmlComments = /<!--[^>]*-->/g;
19
+ // Replace matches with spaces of same length
20
+ let cleanedXml = fullXml.replace(matchMustacheBrackets, (m) => " ".repeat(m.length));
21
+ cleanedXml = cleanedXml.replace(xmlComments, (m) => {
22
+ const x = (m.match(/^.*$/gm) || []).map((m2) => " ".repeat(m2.length));
23
+ return x.join("\n");
24
+ });
25
+ const xml = `<xml>\n${cleanedXml}\n</xml>`;
26
+ if (xml) {
27
+ try {
28
+ const result = fast_xml_parser_1.XMLValidator.validate(xml, {
29
+ allowBooleanAttributes: true,
30
+ });
31
+ if (result !== true) {
32
+ errors.push(getErrorFromException(result, xml));
33
+ }
34
+ const entryPointXml = (0, parse_mustache_xml_js_1.parseXml)(xml)[0];
35
+ const entryPointSchema = xsdSchema[0];
36
+ let pos = 0;
37
+ const lines = cleanedXml.split("\n");
38
+ const getRangeOfElement = (text, incrementPosition = true) => {
39
+ if (text === undefined) {
40
+ const monacoPosition = getPositionFromIndex(lines, pos);
41
+ return toRange(monacoPosition.lineNumber, monacoPosition.column, monacoPosition.lineNumber, monacoPosition.column + 5);
42
+ }
43
+ const position = cleanedXml.indexOf(text, pos);
44
+ if (incrementPosition) {
45
+ pos = position >= pos ? position + text.length : pos;
46
+ }
47
+ const monacoPosition = getPositionFromIndex(lines, position);
48
+ return toRange(monacoPosition.lineNumber, monacoPosition.column, monacoPosition.lineNumber, monacoPosition.column + text.length);
49
+ };
50
+ const validationErrors = entryPointXml.children.flatMap((child) => validateElements(child, undefined, entryPointSchema, getRangeOfElement));
51
+ errors.push(...validationErrors);
52
+ }
53
+ catch (e) {
54
+ errors.push(createError(e.message, ErrorType.error, toRange(1, 1, 1, 100)));
55
+ }
56
+ }
57
+ return errors.map((e) => getDecorationsFromError(e));
58
+ }
59
+ function validateElements(element, schemaElement, completeSchema, getRangeOfElement) {
60
+ const errors = [];
61
+ // Validate element name
62
+ const tagName = element.tagName;
63
+ const range = getRangeOfElement(tagName);
64
+ const slashPosition = getRangeOfElement("/", false);
65
+ const closingTagPosition = getRangeOfElement(">", false);
66
+ const isClosed = rangeLessThan(slashPosition, closingTagPosition);
67
+ const validElements = Object.values(completeSchema.children);
68
+ const schemaName = schemaElement?.attributes.type || tagName;
69
+ const foundSchemaElement = (0, parse_mustache_xml_js_1.findElement)(validElements, schemaName);
70
+ if (!foundSchemaElement) {
71
+ return [createError(`"${tagName}" is not a valid element`, ErrorType.error, range)];
72
+ }
73
+ const possibleAttributes = Array.from(foundSchemaElement.children).flatMap((c) => c.tagName === "attribute" ? c : []);
74
+ // Validate required attributes
75
+ for (const possibleAttribute of possibleAttributes) {
76
+ const attributeName = possibleAttribute.attributes.name;
77
+ const isRequired = possibleAttribute.attributes.use;
78
+ if (attributeName && isRequired && isRequired === "required") {
79
+ if (element.attributes[attributeName] === undefined) {
80
+ errors.push(createError(`"${attributeName}" is a required attribute on "${tagName}"`, ErrorType.error, range));
81
+ }
82
+ }
83
+ }
84
+ // Validate existing attributes
85
+ for (const [attrKey, attrVal] of Object.entries(element.attributes)) {
86
+ const possibleAttrNames = possibleAttributes.flatMap((p) => p.attributes.name || []);
87
+ const attrText = typeof attrVal === "string" ? `${attrKey}="${attrVal}"` : attrKey;
88
+ const attrRange = getRangeOfElement(attrText, false);
89
+ if (!possibleAttrNames.includes(attrKey)) {
90
+ errors.push(createError(`"${attrKey}" is a not a valid attribute on "${tagName}"`, ErrorType.error, attrRange));
91
+ }
92
+ }
93
+ // Validate that the children are allowed as a child of current element
94
+ // and then validate the children themselves
95
+ const schemaChildren = Object.values(foundSchemaElement.children);
96
+ for (const child of element.children) {
97
+ const childName = child.tagName;
98
+ if (!childName) {
99
+ continue;
100
+ }
101
+ const foundChild = (0, parse_mustache_xml_js_1.findElement)(schemaChildren, childName);
102
+ if (!foundChild) {
103
+ const childRange = getRangeOfElement(childName, false);
104
+ if (childRange) {
105
+ errors.push(createError(`"${childName}" is not a valid child of "${tagName}"`, ErrorType.error, childRange));
106
+ }
107
+ }
108
+ const elementErrors = validateElements(child, foundChild, completeSchema, getRangeOfElement);
109
+ errors.push(...elementErrors);
110
+ }
111
+ if (!isClosed) {
112
+ // Increment position to after closing tag
113
+ getRangeOfElement(`</${tagName}`, true);
114
+ }
115
+ return errors;
116
+ }
117
+ function getDecorationsFromError(error) {
118
+ return {
119
+ range: error.range,
120
+ options: {
121
+ className: getErrorClassNames(error),
122
+ hoverMessage: [{ value: getErrorType(error) }, { value: getErrorMessage(error) }],
123
+ },
124
+ };
125
+ }
126
+ function getErrorFromException(result, xml) {
127
+ const { col, line, msg } = result.err;
128
+ const startLine = line - 1;
129
+ const lines = xml.split("\n");
130
+ const rowText = lines[startLine] || "";
131
+ const length = rowText.indexOf(">") - rowText.indexOf("<") || 4;
132
+ const range = toRange(startLine, col, startLine, col + length);
133
+ return createError(msg, ErrorType.error, range);
134
+ }
135
+ function getErrorClassNames(error) {
136
+ switch (error.type) {
137
+ case ErrorType.warning:
138
+ return "xml-lint xml-lint--warning";
139
+ case ErrorType.error:
140
+ return "xml-lint xml-lint--error";
141
+ default:
142
+ return "";
143
+ }
144
+ }
145
+ function getErrorType(error) {
146
+ switch (error.type) {
147
+ case ErrorType.warning:
148
+ return "**Warning**";
149
+ case ErrorType.error:
150
+ return "**Error**";
151
+ default:
152
+ return "";
153
+ }
154
+ }
155
+ const getErrorMessage = (error) => error.message.split(/[\t\n]/g)[1] ?? error.message;
156
+ const createError = (message, type, range) => ({ message, type, range });
157
+ function getPositionFromIndex(lines, index) {
158
+ let totalLength = 0;
159
+ for (const [lineNumber, line] of lines.entries()) {
160
+ totalLength += line.length;
161
+ if (totalLength >= index - lineNumber) {
162
+ return {
163
+ lineNumber: lineNumber + 1,
164
+ column: 1 + index - (totalLength - line.length) - lineNumber || 1, // "- lineNumber" because of \n characters
165
+ };
166
+ }
167
+ }
168
+ return { lineNumber: lines.length, column: 1 };
169
+ }
170
+ const toRange = (startLineNumber, startColumn, endLineNumber, endColumn) => ({
171
+ startLineNumber,
172
+ startColumn,
173
+ endLineNumber,
174
+ endColumn,
175
+ });
176
+ function errorToReadableText(errors, templateName = "") {
177
+ const errorLines = [];
178
+ if (templateName) {
179
+ errorLines.push(`Error in template ${templateName} \n`);
180
+ }
181
+ for (const error of errors) {
182
+ const hoverErrors = error.options.hoverMessage.map((e) => e.value.replace(/\*/g, ""));
183
+ errorLines.push(`${hoverErrors.join("\n")}`);
184
+ errorLines.push(`On line ${error.range.startLineNumber}, column ${error.range.startColumn}\n`);
185
+ }
186
+ return errorLines.join("\n");
187
+ }
188
+ function rangeLessThan(range1, range2) {
189
+ return range1.startLineNumber < range2.startLineNumber
190
+ ? true
191
+ : range1.startLineNumber > range2.startLineNumber
192
+ ? false
193
+ : range1.startColumn < range2.startColumn;
194
+ }
195
+ //# sourceMappingURL=validate-mustache-xml.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validate-mustache-xml.js","sourceRoot":"","sources":["../../../src/abstract-document-xml/handlebars-xml/validate-mustache-xml.ts"],"names":[],"mappings":";;AAuCA,kCAgEC;AAoJD,kDAYC;AAvQD,qDAAgE;AAChE,mEAA4E;AAE5E,IAAK,SAGJ;AAHD,WAAK,SAAS;IACZ,+CAAW,CAAA;IACX,2CAAS,CAAA;AACX,CAAC,EAHI,SAAS,KAAT,SAAS,QAGb;AAgCD,2DAA2D;AAC3D,SAAgB,WAAW,CAAC,OAAe,EAAE,SAAoC;IAC/E,MAAM,MAAM,GAAoB,EAAE,CAAC;IAEnC,+BAA+B;IAC/B,MAAM,qBAAqB,GAAG,mBAAmB,CAAC;IAClD,sBAAsB;IACtB,MAAM,WAAW,GAAG,eAAe,CAAC;IAEpC,6CAA6C;IAC7C,IAAI,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,qBAAqB,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;IACrF,UAAU,GAAG,UAAU,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC,EAAE,EAAE;QACjD,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;QACv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,CAAC,WAAW,CAAC;AAC9C,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "abstract-document",
3
- "version": "16.0.35",
3
+ "version": "16.0.36",
4
4
  "description": "Dynamically create documents using code or JSX and render to any format",
5
5
  "repository": "https://github.com/dividab/abstract-visuals/tree/master/packages/abstract-document",
6
6
  "main": "lib/index.js",
@@ -48,5 +48,5 @@
48
48
  "vite": "^6.0.1",
49
49
  "vitest": "^2.1.6"
50
50
  },
51
- "gitHead": "238a184dbb6658068579166db384d2c28b653ec7"
51
+ "gitHead": "be91668b1cef7bf6944910322f284e49cc4148c3"
52
52
  }
@@ -86,6 +86,7 @@ function preProcessMarkdownAst(
86
86
  {
87
87
  styleName: paragraphStyle,
88
88
  numbering: paragraphNumbering,
89
+ isMarkdown: true,
89
90
  },
90
91
  atoms
91
92
  )
@@ -94,8 +95,17 @@ function preProcessMarkdownAst(
94
95
  } else if (child.type === "break") {
95
96
  atoms.push({ type: "LineBreak" });
96
97
  } else if (child.type === "text") {
98
+
99
+ // Due to formatting with bold and italic etc,
100
+ // markdown will split the whole text into separate
101
+ // textruns. Doing this only for the special text
102
+ // will make the renderer unhappy when aligning the
103
+ // textruns. Thereofre we should split all of the
104
+ // words here, and let the renderer align them word
105
+ // for word
106
+ const spaceRegex = /([\p{Zs}])/u;
97
107
  atoms = atoms.concat(
98
- child.value.split("\n").map(
108
+ child.value.split(spaceRegex).filter((v) => v.length !== 0).map(
99
109
  (v: string) =>
100
110
  ({
101
111
  type: "TextRun",
@@ -11,21 +11,24 @@ export type Paragraph = Resources & {
11
11
  readonly style: ParagraphStyle.ParagraphStyle;
12
12
  readonly numbering: ParagraphNumbering | undefined;
13
13
  readonly children: ReadonlyArray<Atom>;
14
+ readonly isMarkdown?: boolean;
14
15
  };
15
16
 
16
17
  export type ParagraphProps = Resources & {
17
18
  readonly styleName?: string;
18
19
  readonly style?: ParagraphStyle.ParagraphStyle;
19
20
  readonly numbering?: ParagraphNumbering;
21
+ readonly isMarkdown?: boolean;
20
22
  };
21
23
 
22
24
  export function create(props?: ParagraphProps, children?: ReadonlyArray<Atom>): Paragraph {
23
- const { styleName = "", style = ParagraphStyle.create(), numbering = undefined, ...rest } = props || {};
25
+ const { styleName = "", style = ParagraphStyle.create(), numbering = undefined, isMarkdown, ...rest } = props || {};
24
26
  return {
25
27
  type: sectionType,
26
28
  styleName,
27
29
  style,
28
30
  numbering,
31
+ isMarkdown,
29
32
  children: children || [],
30
33
  ...rest,
31
34
  };
@@ -280,14 +280,21 @@ function renderParagraph(
280
280
  rows.push(currentRow);
281
281
  }
282
282
 
283
- let y = finalRect.y + style.margins.top;
283
+ //if the paragraph is markdown, we need to render it separately
284
+ //since alignment is different for that
285
+ if(paragraph.isMarkdown !== undefined && paragraph.isMarkdown) {
286
+ renderMarkdownRows(rows, resources, pdfKit, desiredSizes, finalRect, style, availableWidth);
287
+ return;
288
+ }
284
289
 
290
+ let y = finalRect.y + style.margins.top;
285
291
  for (let row of rows) {
286
292
  if (row.length === 0) {
287
293
  continue;
288
294
  }
289
-
295
+
290
296
  const rowWidth = row.reduce((a, b) => a + getDesiredSize(b, desiredSizes).width, 0);
297
+ const remainingWidth = availableWidth - rowWidth;
291
298
  let x = finalRect.x;
292
299
 
293
300
  if (style.alignment === "Start" || style.alignment === "Justify") {
@@ -302,14 +309,13 @@ function renderParagraph(
302
309
  // Therefore we have to position it ourself
303
310
  // NOTE: Texts with multiple atoms with width over the available width are not supported.
304
311
  if (style.alignment === "Center") {
305
- x += 0.5 * (availableWidth - rowWidth);
312
+ x += 0.5 * remainingWidth;
306
313
  } else if (style.alignment === "End") {
307
- x += availableWidth - rowWidth;
314
+ x += remainingWidth;
308
315
  }
309
316
  }
310
317
 
311
318
  let rowHeight = 0;
312
-
313
319
  const lastIndex = row[row.length - 1]?.type === "LineBreak" ? row.length - 2 : row.length - 1;
314
320
  for (const [i, atom] of row.entries()) {
315
321
  const atomSize = getDesiredSize(atom, desiredSizes);
@@ -324,7 +330,6 @@ function renderParagraph(
324
330
  i === 0,
325
331
  i === lastIndex
326
332
  );
327
-
328
333
  x += atomSize.width;
329
334
  rowHeight = Math.max(rowHeight, atomSize.height);
330
335
  }
@@ -333,6 +338,146 @@ function renderParagraph(
333
338
  }
334
339
  }
335
340
 
341
+ function renderMarkdownRows(
342
+ rawRows: ReadonlyArray<ReadonlyArray<AD.Atom.Atom>>,
343
+ resources: AD.Resources.Resources,
344
+ pdfKit: PDFKit.PDFDocument,
345
+ desiredSizes: Map<{}, AD.Size.Size>,
346
+ finalRect: AD.Rect.Rect,
347
+ paragraphStyle: AD.ParagraphStyle.ParagraphStyle,
348
+ availableWidth: number,
349
+ ): void {
350
+ const rows = splitRows(rawRows, availableWidth, desiredSizes);
351
+ let y = finalRect.y + paragraphStyle.margins.top;
352
+ let markdownTextStyle: AD.TextStyle.TextStyle | undefined = undefined;
353
+ for(const row of rows) {
354
+ for(const atom of row) {
355
+ if(atom.type === "TextRun") {
356
+ markdownTextStyle = AD.Resources.getNestedStyle(
357
+ paragraphStyle.textStyle,
358
+ atom.style,
359
+ "TextStyle",
360
+ atom.styleName,
361
+ resources,
362
+ atom.nestedStyleNames || []
363
+ ) as AD.TextStyle.TextStyle;
364
+ break;
365
+ }
366
+ }
367
+ }
368
+
369
+ const defaultStyle = parseAlignment(paragraphStyle.alignment);
370
+ const alignment = markdownTextStyle?.alignment ?? defaultStyle;
371
+ for (let row of rows) {
372
+ if (row.length === 0) {
373
+ continue;
374
+ }
375
+
376
+ const rowWidth = row.reduce((a, b) => a + getDesiredSize(b, desiredSizes).width, 0);
377
+ const remainingWidth = availableWidth - rowWidth;
378
+ const justificationWidth = row.length > 1 ? remainingWidth / (row.length - 1) : 0;
379
+ let x = finalRect.x;
380
+
381
+ if (alignment === "left" || alignment === "justify") {
382
+ x += paragraphStyle.margins.left;
383
+ } else if (alignment === "right") {
384
+ x -= paragraphStyle.margins.right;
385
+ }
386
+
387
+ if (row.length > 1 || row[0].type === "Image") {
388
+ if (alignment === "center") {
389
+ x += 0.5 * remainingWidth;
390
+ } else if (alignment === "right") {
391
+ x += remainingWidth;
392
+ }
393
+ }
394
+
395
+ let rowHeight = 0;
396
+ const lastIndex = row[row.length - 1]?.type === "LineBreak" ? row.length - 2 : row.length - 1;
397
+ for (const [i, atom] of row.entries()) {
398
+ const atomSize = getDesiredSize(atom, desiredSizes);
399
+ renderAtom(
400
+ resources,
401
+ pdfKit,
402
+ AD.Rect.create(x, y, atomSize.width, atomSize.height),
403
+ paragraphStyle.textStyle,
404
+ atom,
405
+ alignment,
406
+ availableWidth,
407
+ i === 0,
408
+ i === lastIndex
409
+ );
410
+ x += atomSize.width + (alignment === "justify" ? justificationWidth : 0);
411
+ rowHeight = Math.max(rowHeight, atomSize.height);
412
+ }
413
+ y += rowHeight;
414
+ }
415
+ }
416
+
417
+ function splitRows(rows: ReadonlyArray<ReadonlyArray<AD.Atom.Atom>>, availableWidth: number, desiredSizes: Map<{}, AD.Size.Size>): ReadonlyArray<ReadonlyArray<AD.Atom.Atom>> {
418
+ const newRows: Array<ReadonlyArray<AD.Atom.Atom>> = [];
419
+
420
+ for(const row of rows) {
421
+ if(row.length <= 1) {
422
+ newRows.push(row);
423
+ continue;
424
+ }
425
+
426
+ const width = row.reduce((a, b) => a + getDesiredSize(b, desiredSizes).width, 0);
427
+ if(width <= availableWidth) {
428
+ newRows.push(row);
429
+ continue;
430
+ }
431
+
432
+ //we need to split it, because it doesn't fit
433
+ let currentRow: Array<AD.Atom.Atom> = [];
434
+ let currentWidth = 0;
435
+ let lastWasSpace = false;
436
+
437
+ for(let i = 0; i < row.length; i++) {
438
+ const atom = row[i];
439
+ if(atom.type !== "TextRun") {
440
+ continue;
441
+ }
442
+
443
+ const width = getDesiredSize(atom, desiredSizes).width;
444
+ if(atom.type !== "TextRun") {
445
+ currentRow.push(atom);
446
+ currentWidth += width;
447
+ continue;
448
+ }
449
+
450
+ const isSpace = atom.text.replaceAll(/[\p{Zs}]/ug, "").length === 0;
451
+ const lastSpace = lastWasSpace;
452
+ lastWasSpace = isSpace;
453
+
454
+ if(isSpace && currentWidth === 0 && i !== 0) {
455
+ continue;
456
+ }
457
+
458
+ if(currentWidth + width <= availableWidth) {
459
+ currentRow.push(atom);
460
+ currentWidth += width;
461
+ continue;
462
+ }
463
+
464
+ //was it a space??
465
+ if(isSpace) {
466
+ newRows.push(currentRow);
467
+ currentRow = [];
468
+ currentWidth = 0;
469
+ continue;
470
+ }
471
+
472
+ newRows.push(lastSpace ? currentRow.slice(0, -1) : currentRow);
473
+ currentRow = [atom];
474
+ currentWidth = width;
475
+ }
476
+ newRows.push(lastWasSpace ? currentRow.slice(0, -1) : currentRow);
477
+ }
478
+ return newRows;
479
+ }
480
+
336
481
  function parseAlignment(paragraphAlignment: AD.ParagraphStyle.TextAlignment | undefined): AD.TextStyle.TextAlignment {
337
482
  switch (paragraphAlignment) {
338
483
  case "Start":
@@ -1,3 +0,0 @@
1
- export declare function toBase64String(u8: Uint8Array): string;
2
- export declare const rawSvgPrefix = "data:image/svg+xml,";
3
- //# sourceMappingURL=to-base-64.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"to-base-64.d.ts","sourceRoot":"","sources":["../../../src/abstract-document-exporters/shared/to-base-64.ts"],"names":[],"mappings":"AAAA,wBAAgB,cAAc,CAAC,EAAE,EAAE,UAAU,GAAG,MAAM,CAarD;AAED,eAAO,MAAM,YAAY,wBAAwB,CAAC"}