@legalplace/prerenderx 3.8.18-staging.2 → 3.8.18
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/CHANGELOG.md +8 -0
- package/dist/libs/DocumentRender.d.ts +2 -3
- package/dist/libs/DocumentRender.js +4 -13
- package/dist/libs/FillPdfForm.d.ts +1 -1
- package/dist/libs/FillPdfForm.js +15 -25
- package/dist/libs/OptionRender.d.ts +0 -4
- package/dist/libs/OptionRender.js +29 -125
- package/dist/libs/OutputParser.d.ts +1 -1
- package/dist/libs/OutputParser.js +6 -18
- package/dist/libs/Prerender.d.ts +2 -5
- package/dist/libs/Prerender.js +27 -97
- package/dist/libs/SectionRender.d.ts +0 -3
- package/dist/libs/SectionRender.js +22 -64
- package/dist/libs/misc/FillPdfFormBase.d.ts +1 -1
- package/dist/libs/misc/FillPdfFormBase.js +37 -11
- package/dist/types/types.d.ts +0 -13
- package/package.json +9 -8
- package/src/libs/DocumentRender.ts +5 -33
- package/src/libs/FillPdfForm.ts +22 -39
- package/src/libs/OptionRender.ts +63 -218
- package/src/libs/OutputParser.ts +9 -40
- package/src/libs/Prerender.ts +38 -153
- package/src/libs/SectionRender.ts +28 -111
- package/src/libs/misc/FillPdfFormBase.ts +67 -34
- package/src/types/types.ts +0 -27
- package/dist/libs/outputTableRendering.d.ts +0 -16
- package/dist/libs/outputTableRendering.js +0 -130
- package/dist/libs/regionHelpers.d.ts +0 -5
- package/dist/libs/regionHelpers.js +0 -22
- package/dist/libs/tableOutputExpansion.d.ts +0 -20
- package/dist/libs/tableOutputExpansion.js +0 -68
- package/src/libs/tableOutputExpansion.ts +0 -145
- package/tests/Prerender.documentMultiple.test.ts +0 -187
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import LpLogic from "@legalplace/lplogic";
|
|
2
|
-
import type { ConditionV3 } from "@legalplace/models-v3-types";
|
|
3
|
-
import type ConditionsRunner from "@legalplace/conditions-runner";
|
|
4
|
-
import DataPopulator from "@legalplace/conditions-runner/dist/DataPopulator";
|
|
5
|
-
import type { Types } from "@legalplace/referencesparser";
|
|
6
|
-
import {
|
|
7
|
-
expandTableMarkup,
|
|
8
|
-
extractConditionDataMap,
|
|
9
|
-
hasTableDynamicMarkup,
|
|
10
|
-
stripConditionUiMetadata,
|
|
11
|
-
type ExpandTableMarkupContext,
|
|
12
|
-
} from "@legalplace/referencesparser";
|
|
13
|
-
import type InputsType from "@legalplace/types/dist/inputs";
|
|
14
|
-
import {
|
|
15
|
-
getRelatedVariablesValues,
|
|
16
|
-
parseOutputWithVariables,
|
|
17
|
-
} from "./OutputParser";
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Extracts variable ids referenced as `[var:N]` tags in an HTML fragment.
|
|
21
|
-
*/
|
|
22
|
-
const extractVariableIds = (html: string): number[] => {
|
|
23
|
-
const matches = html.match(/\[var:([0-9]+)\]/gi);
|
|
24
|
-
if (matches === null) return [];
|
|
25
|
-
|
|
26
|
-
return Array.from(
|
|
27
|
-
new Set(
|
|
28
|
-
matches.map((tag) => {
|
|
29
|
-
const idMatch = tag.match(/([0-9]+)/);
|
|
30
|
-
return idMatch ? parseInt(idMatch[1], 10) : 0;
|
|
31
|
-
})
|
|
32
|
-
)
|
|
33
|
-
).filter((id) => id > 0);
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Evaluates an inline table condition at a given occurrence index.
|
|
38
|
-
*/
|
|
39
|
-
export const evaluateInlineTableCondition = (
|
|
40
|
-
condition: ConditionV3,
|
|
41
|
-
contextOptionId: number,
|
|
42
|
-
occurrenceIndex: number,
|
|
43
|
-
conditionsRunner: ConditionsRunner,
|
|
44
|
-
references: Types.ReferencesType,
|
|
45
|
-
ovc: InputsType
|
|
46
|
-
): boolean => {
|
|
47
|
-
const dataMap = extractConditionDataMap(condition);
|
|
48
|
-
const currentData = new DataPopulator(
|
|
49
|
-
conditionsRunner,
|
|
50
|
-
references,
|
|
51
|
-
ovc,
|
|
52
|
-
dataMap,
|
|
53
|
-
contextOptionId,
|
|
54
|
-
occurrenceIndex
|
|
55
|
-
).getData();
|
|
56
|
-
const result = LpLogic(stripConditionUiMetadata(condition), currentData);
|
|
57
|
-
return result !== false;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
type CreateTableMarkupContextParams = {
|
|
61
|
-
references: Types.ReferencesType;
|
|
62
|
-
ovc: InputsType;
|
|
63
|
-
conditions: ConditionsRunner;
|
|
64
|
-
contextOptionId: number;
|
|
65
|
-
defaultOccurrenceIndex: number;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Builds the expandTableMarkup context for prerenderx output rendering.
|
|
70
|
-
*/
|
|
71
|
-
export const createTableMarkupContext = ({
|
|
72
|
-
references,
|
|
73
|
-
ovc,
|
|
74
|
-
conditions,
|
|
75
|
-
contextOptionId,
|
|
76
|
-
defaultOccurrenceIndex,
|
|
77
|
-
}: CreateTableMarkupContextParams): ExpandTableMarkupContext => ({
|
|
78
|
-
defaultContextOptionId: contextOptionId,
|
|
79
|
-
defaultOccurrenceIndex,
|
|
80
|
-
getOccurrenceInputs: (multipleOptionId: number) =>
|
|
81
|
-
ovc.options[String(multipleOptionId)] || [],
|
|
82
|
-
evaluateCondition: (condition, multipleOptionId, occurrenceIndex) =>
|
|
83
|
-
evaluateInlineTableCondition(
|
|
84
|
-
condition,
|
|
85
|
-
multipleOptionId,
|
|
86
|
-
occurrenceIndex,
|
|
87
|
-
conditions,
|
|
88
|
-
references,
|
|
89
|
-
ovc
|
|
90
|
-
),
|
|
91
|
-
substituteVariables: (html, multipleOptionId, occurrenceIndex) => {
|
|
92
|
-
const variableIds = extractVariableIds(html);
|
|
93
|
-
const variables = getRelatedVariablesValues(
|
|
94
|
-
multipleOptionId,
|
|
95
|
-
occurrenceIndex,
|
|
96
|
-
variableIds,
|
|
97
|
-
ovc,
|
|
98
|
-
references
|
|
99
|
-
);
|
|
100
|
-
return parseOutputWithVariables(html, occurrenceIndex, variables, []);
|
|
101
|
-
},
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
type ExpandOutputTableMarkupParams = CreateTableMarkupContextParams & {
|
|
105
|
-
output: string;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Expands dynamic table markup in an output HTML string when needed.
|
|
110
|
-
*/
|
|
111
|
-
export const expandOutputTableMarkup = ({
|
|
112
|
-
output,
|
|
113
|
-
references,
|
|
114
|
-
ovc,
|
|
115
|
-
conditions,
|
|
116
|
-
contextOptionId,
|
|
117
|
-
defaultOccurrenceIndex,
|
|
118
|
-
}: ExpandOutputTableMarkupParams): string => {
|
|
119
|
-
if (!hasTableDynamicMarkup(output)) return output;
|
|
120
|
-
|
|
121
|
-
return expandTableMarkup(
|
|
122
|
-
output,
|
|
123
|
-
createTableMarkupContext({
|
|
124
|
-
references,
|
|
125
|
-
ovc,
|
|
126
|
-
conditions,
|
|
127
|
-
contextOptionId,
|
|
128
|
-
defaultOccurrenceIndex,
|
|
129
|
-
})
|
|
130
|
-
);
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Applies post-processing cleanup to rendered output HTML.
|
|
135
|
-
*/
|
|
136
|
-
export const cleanupRenderedOutputHtml = (output: string): string => {
|
|
137
|
-
if (output.includes("<table")) {
|
|
138
|
-
return output.replace(/\xA0/g, " ");
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return output
|
|
142
|
-
.replace(/<\/([a-z]+)>([\n\s]+)</gi, "</$1><")
|
|
143
|
-
.replace(/\n/g, "<br />")
|
|
144
|
-
.replace(/\xA0/g, " ");
|
|
145
|
-
};
|
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
import type { ModelV3 } from "@legalplace/models-v3-types";
|
|
2
|
-
import Prerender from "../src/libs/Prerender";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Builds a Model V3 with one document `main` configured as multiple
|
|
6
|
-
* (`params.multiple.enabled = true`, source = option #2). The output
|
|
7
|
-
* `[var:3]` interpolates the partner name at each occurrence.
|
|
8
|
-
*/
|
|
9
|
-
const buildMultipleDocumentModel = (
|
|
10
|
-
filenameTemplate?: string
|
|
11
|
-
): ModelV3 => ({
|
|
12
|
-
documents: {
|
|
13
|
-
main: {
|
|
14
|
-
name: "Contract",
|
|
15
|
-
sections: [
|
|
16
|
-
{
|
|
17
|
-
id: 1,
|
|
18
|
-
label: "Section",
|
|
19
|
-
options: [2, 5],
|
|
20
|
-
},
|
|
21
|
-
],
|
|
22
|
-
params: {
|
|
23
|
-
multiple: {
|
|
24
|
-
enabled: true,
|
|
25
|
-
repeatOption: 2,
|
|
26
|
-
...(filenameTemplate ? { filenameTemplate } : {}),
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
},
|
|
31
|
-
options: {
|
|
32
|
-
"2": {
|
|
33
|
-
meta: {
|
|
34
|
-
id: 2,
|
|
35
|
-
type: "static",
|
|
36
|
-
label: "Partner",
|
|
37
|
-
step: "*",
|
|
38
|
-
multiple: {
|
|
39
|
-
enabled: true,
|
|
40
|
-
incrementationStart: 1,
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
options: [],
|
|
44
|
-
variables: [3],
|
|
45
|
-
},
|
|
46
|
-
"5": {
|
|
47
|
-
meta: {
|
|
48
|
-
id: 5,
|
|
49
|
-
type: "hidden",
|
|
50
|
-
label: "Body",
|
|
51
|
-
step: "*",
|
|
52
|
-
output: "<p>Hello [var:3]</p>",
|
|
53
|
-
},
|
|
54
|
-
options: [],
|
|
55
|
-
variables: [3],
|
|
56
|
-
},
|
|
57
|
-
},
|
|
58
|
-
variables: {
|
|
59
|
-
"3": {
|
|
60
|
-
id: 3,
|
|
61
|
-
type: "text",
|
|
62
|
-
label: "Partner name",
|
|
63
|
-
step: "*",
|
|
64
|
-
},
|
|
65
|
-
},
|
|
66
|
-
customization: {},
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
const ovcWithThreePartners = {
|
|
70
|
-
options: {
|
|
71
|
-
"2": [true, true, true],
|
|
72
|
-
},
|
|
73
|
-
variables: {
|
|
74
|
-
"3": ["Alice", "Bob", "Charlie"],
|
|
75
|
-
},
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
describe("Prerender — document multiple", () => {
|
|
79
|
-
it("emits one entry per occurrence with distinct hashes", () => {
|
|
80
|
-
const prerender = new Prerender(
|
|
81
|
-
buildMultipleDocumentModel(),
|
|
82
|
-
ovcWithThreePartners
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
const index = prerender.getDocumentsIndex();
|
|
86
|
-
const entries = Object.entries(index);
|
|
87
|
-
|
|
88
|
-
expect(entries).toHaveLength(3);
|
|
89
|
-
const slugs = entries.map(([, e]) => e.slug);
|
|
90
|
-
expect(slugs).toEqual(["main", "main", "main"]);
|
|
91
|
-
|
|
92
|
-
const occurrenceIndexes = entries.map(([, e]) => e.occurrenceIndex);
|
|
93
|
-
expect(occurrenceIndexes.sort()).toEqual([0, 1, 2]);
|
|
94
|
-
|
|
95
|
-
// Hashes must be distinct across occurrences
|
|
96
|
-
const hashes = entries.map(([h]) => h);
|
|
97
|
-
expect(new Set(hashes).size).toBe(hashes.length);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
it("uses the document title for each occurrence when no template is set", () => {
|
|
101
|
-
const prerender = new Prerender(
|
|
102
|
-
buildMultipleDocumentModel(),
|
|
103
|
-
ovcWithThreePartners
|
|
104
|
-
);
|
|
105
|
-
const index = prerender.getDocumentsIndex();
|
|
106
|
-
const names = Object.values(index)
|
|
107
|
-
.map((e) => e.name)
|
|
108
|
-
.sort();
|
|
109
|
-
|
|
110
|
-
expect(names).toEqual(["Contract", "Contract", "Contract"]);
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it("interpolates document name with variables at each occurrence", () => {
|
|
114
|
-
const model = buildMultipleDocumentModel();
|
|
115
|
-
model.documents.main.name = "Contract - [var:3]";
|
|
116
|
-
if (model.documents.main.params?.multiple) {
|
|
117
|
-
delete model.documents.main.params.multiple.filenameTemplate;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const prerender = new Prerender(model, ovcWithThreePartners);
|
|
121
|
-
const index = prerender.getDocumentsIndex();
|
|
122
|
-
const names = Object.values(index)
|
|
123
|
-
.map((e) => e.name)
|
|
124
|
-
.sort();
|
|
125
|
-
|
|
126
|
-
expect(names).toEqual([
|
|
127
|
-
"Contract - Alice",
|
|
128
|
-
"Contract - Bob",
|
|
129
|
-
"Contract - Charlie",
|
|
130
|
-
]);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it("interpolates filenameTemplate with the variable value at each occurrence", () => {
|
|
134
|
-
const prerender = new Prerender(
|
|
135
|
-
buildMultipleDocumentModel("Contract - [var:3]"),
|
|
136
|
-
ovcWithThreePartners
|
|
137
|
-
);
|
|
138
|
-
const index = prerender.getDocumentsIndex();
|
|
139
|
-
const names = Object.values(index)
|
|
140
|
-
.map((e) => e.name)
|
|
141
|
-
.sort();
|
|
142
|
-
|
|
143
|
-
expect(names).toEqual([
|
|
144
|
-
"Contract - Alice",
|
|
145
|
-
"Contract - Bob",
|
|
146
|
-
"Contract - Charlie",
|
|
147
|
-
]);
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
it("renders the body with the right variable for each occurrence", () => {
|
|
151
|
-
const prerender = new Prerender(
|
|
152
|
-
buildMultipleDocumentModel(),
|
|
153
|
-
ovcWithThreePartners
|
|
154
|
-
);
|
|
155
|
-
const index = prerender.getDocumentsIndex();
|
|
156
|
-
|
|
157
|
-
const entries = Object.entries(index).sort(
|
|
158
|
-
(a, b) =>
|
|
159
|
-
((a[1].occurrenceIndex ?? 0) as number) -
|
|
160
|
-
((b[1].occurrenceIndex ?? 0) as number)
|
|
161
|
-
);
|
|
162
|
-
|
|
163
|
-
const rendered = entries.map(([hash]) =>
|
|
164
|
-
prerender.getDocument(hash) as string
|
|
165
|
-
);
|
|
166
|
-
|
|
167
|
-
expect(rendered[0]).toContain("Hello Alice");
|
|
168
|
-
expect(rendered[1]).toContain("Hello Bob");
|
|
169
|
-
expect(rendered[2]).toContain("Hello Charlie");
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
it("returns a single entry when params.multiple.enabled is false", () => {
|
|
173
|
-
const model = buildMultipleDocumentModel();
|
|
174
|
-
if (model.documents.main.params?.multiple) {
|
|
175
|
-
model.documents.main.params.multiple.enabled = false;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const prerender = new Prerender(model, ovcWithThreePartners);
|
|
179
|
-
const index = prerender.getDocumentsIndex();
|
|
180
|
-
|
|
181
|
-
expect(Object.keys(index)).toHaveLength(1);
|
|
182
|
-
const [entry] = Object.values(index);
|
|
183
|
-
expect(entry.slug).toBe("main");
|
|
184
|
-
// No occurrenceIndex when not multiple
|
|
185
|
-
expect(entry.occurrenceIndex).toBeUndefined();
|
|
186
|
-
});
|
|
187
|
-
});
|