@cyberismo/data-handler 0.0.7 → 0.0.9
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/dist/command-handler.d.ts +11 -2
- package/dist/command-handler.js +61 -18
- package/dist/command-handler.js.map +1 -1
- package/dist/command-manager.js +8 -8
- package/dist/command-manager.js.map +1 -1
- package/dist/commands/calculate.d.ts +7 -44
- package/dist/commands/calculate.js +8 -389
- package/dist/commands/calculate.js.map +1 -1
- package/dist/commands/create.d.ts +7 -4
- package/dist/commands/create.js +42 -15
- package/dist/commands/create.js.map +1 -1
- package/dist/commands/edit.d.ts +9 -3
- package/dist/commands/edit.js +33 -9
- package/dist/commands/edit.js.map +1 -1
- package/dist/commands/export.d.ts +13 -11
- package/dist/commands/export.js +80 -28
- package/dist/commands/export.js.map +1 -1
- package/dist/commands/import.d.ts +7 -0
- package/dist/commands/import.js +21 -2
- package/dist/commands/import.js.map +1 -1
- package/dist/commands/move.d.ts +11 -12
- package/dist/commands/move.js +12 -13
- package/dist/commands/move.js.map +1 -1
- package/dist/commands/remove.d.ts +2 -4
- package/dist/commands/remove.js +8 -16
- package/dist/commands/remove.js.map +1 -1
- package/dist/commands/rename.d.ts +1 -3
- package/dist/commands/rename.js +3 -6
- package/dist/commands/rename.js.map +1 -1
- package/dist/commands/show.d.ts +37 -5
- package/dist/commands/show.js +85 -7
- package/dist/commands/show.js.map +1 -1
- package/dist/commands/transition.d.ts +1 -3
- package/dist/commands/transition.js +3 -5
- package/dist/commands/transition.js.map +1 -1
- package/dist/commands/update.d.ts +5 -1
- package/dist/commands/update.js +7 -1
- package/dist/commands/update.js.map +1 -1
- package/dist/commands/validate.d.ts +7 -8
- package/dist/commands/validate.js +30 -35
- package/dist/commands/validate.js.map +1 -1
- package/dist/containers/card-container.d.ts +6 -0
- package/dist/containers/card-container.js +61 -0
- package/dist/containers/card-container.js.map +1 -1
- package/dist/containers/project/calculation-engine.d.ts +90 -0
- package/dist/containers/project/calculation-engine.js +402 -0
- package/dist/containers/project/calculation-engine.js.map +1 -0
- package/dist/containers/project/resource-collector.d.ts +2 -1
- package/dist/containers/project/resource-collector.js +41 -33
- package/dist/containers/project/resource-collector.js.map +1 -1
- package/dist/containers/project.d.ts +18 -6
- package/dist/containers/project.js +37 -72
- package/dist/containers/project.js.map +1 -1
- package/dist/containers/template.d.ts +5 -0
- package/dist/containers/template.js +9 -0
- package/dist/containers/template.js.map +1 -1
- package/dist/exceptions/index.d.ts +20 -0
- package/dist/exceptions/index.js +16 -0
- package/dist/exceptions/index.js.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/interfaces/macros.d.ts +7 -2
- package/dist/interfaces/project-interfaces.d.ts +21 -0
- package/dist/interfaces/project-interfaces.js +4 -0
- package/dist/interfaces/project-interfaces.js.map +1 -1
- package/dist/interfaces/resource-interfaces.d.ts +1 -1
- package/dist/interfaces/resource-interfaces.js.map +1 -1
- package/dist/macros/base-macro.d.ts +2 -0
- package/dist/macros/base-macro.js +66 -19
- package/dist/macros/base-macro.js.map +1 -1
- package/dist/macros/common.d.ts +16 -9
- package/dist/macros/common.js +22 -9
- package/dist/macros/common.js.map +1 -1
- package/dist/macros/createCards/index.d.ts +1 -2
- package/dist/macros/createCards/index.js.map +1 -1
- package/dist/macros/graph/index.d.ts +1 -3
- package/dist/macros/graph/index.js +11 -9
- package/dist/macros/graph/index.js.map +1 -1
- package/dist/macros/image/index.d.ts +39 -0
- package/dist/macros/image/index.js +78 -0
- package/dist/macros/image/index.js.map +1 -0
- package/dist/macros/image/metadata.d.ts +18 -0
- package/dist/macros/image/metadata.js +22 -0
- package/dist/macros/image/metadata.js.map +1 -0
- package/dist/macros/include/index.d.ts +32 -0
- package/dist/macros/include/index.js +97 -0
- package/dist/macros/include/index.js.map +1 -0
- package/dist/macros/include/metadata.d.ts +15 -0
- package/dist/macros/include/metadata.js +19 -0
- package/dist/macros/include/metadata.js.map +1 -0
- package/dist/macros/index.d.ts +39 -31
- package/dist/macros/index.js +167 -73
- package/dist/macros/index.js.map +1 -1
- package/dist/macros/percentage/index.d.ts +29 -0
- package/dist/macros/percentage/index.js +36 -0
- package/dist/macros/percentage/index.js.map +1 -0
- package/dist/macros/percentage/metadata.d.ts +15 -0
- package/dist/macros/percentage/metadata.js +19 -0
- package/dist/macros/percentage/metadata.js.map +1 -0
- package/dist/macros/report/index.d.ts +2 -5
- package/dist/macros/report/index.js +20 -12
- package/dist/macros/report/index.js.map +1 -1
- package/dist/macros/scoreCard/index.d.ts +15 -15
- package/dist/macros/scoreCard/index.js +16 -17
- package/dist/macros/scoreCard/index.js.map +1 -1
- package/dist/macros/vega/index.d.ts +28 -0
- package/dist/macros/vega/index.js +27 -0
- package/dist/macros/vega/index.js.map +1 -0
- package/dist/macros/vega/metadata.d.ts +15 -0
- package/dist/macros/vega/metadata.js +7 -0
- package/dist/macros/vega/metadata.js.map +1 -0
- package/dist/macros/vegalite/index.d.ts +27 -0
- package/dist/macros/vegalite/index.js +27 -0
- package/dist/macros/vegalite/index.js.map +1 -0
- package/dist/macros/vegalite/metadata.d.ts +15 -0
- package/dist/macros/vegalite/metadata.js +7 -0
- package/dist/macros/vegalite/metadata.js.map +1 -0
- package/dist/macros/xref/index.d.ts +26 -0
- package/dist/macros/xref/index.js +53 -0
- package/dist/macros/xref/index.js.map +1 -0
- package/dist/macros/xref/metadata.d.ts +15 -0
- package/dist/macros/xref/metadata.js +19 -0
- package/dist/macros/xref/metadata.js.map +1 -0
- package/dist/module-manager.d.ts +17 -4
- package/dist/module-manager.js +192 -58
- package/dist/module-manager.js.map +1 -1
- package/dist/permissions/action-guard.d.ts +2 -2
- package/dist/permissions/action-guard.js +1 -1
- package/dist/permissions/action-guard.js.map +1 -1
- package/dist/project-settings.js +2 -8
- package/dist/project-settings.js.map +1 -1
- package/dist/resources/card-type-resource.d.ts +2 -0
- package/dist/resources/card-type-resource.js +63 -0
- package/dist/resources/card-type-resource.js.map +1 -1
- package/dist/resources/file-resource.d.ts +2 -0
- package/dist/resources/file-resource.js +12 -4
- package/dist/resources/file-resource.js.map +1 -1
- package/dist/resources/folder-resource.d.ts +19 -0
- package/dist/resources/folder-resource.js +51 -2
- package/dist/resources/folder-resource.js.map +1 -1
- package/dist/resources/graph-model-resource.js +1 -1
- package/dist/resources/graph-model-resource.js.map +1 -1
- package/dist/resources/graph-view-resource.js +1 -1
- package/dist/resources/graph-view-resource.js.map +1 -1
- package/dist/resources/report-resource.js +1 -1
- package/dist/resources/report-resource.js.map +1 -1
- package/dist/resources/resource-object.d.ts +8 -0
- package/dist/resources/resource-object.js +9 -0
- package/dist/resources/resource-object.js.map +1 -1
- package/dist/resources/template-resource.js +1 -1
- package/dist/resources/template-resource.js.map +1 -1
- package/dist/resources/workflow-resource.d.ts +2 -0
- package/dist/resources/workflow-resource.js +53 -9
- package/dist/resources/workflow-resource.js.map +1 -1
- package/dist/svg/index.d.ts +15 -0
- package/dist/svg/index.js +16 -0
- package/dist/svg/index.js.map +1 -0
- package/dist/svg/lib.d.ts +9 -0
- package/dist/svg/lib.js +26 -0
- package/dist/svg/lib.js.map +1 -0
- package/dist/svg/percentage.d.ts +25 -0
- package/dist/svg/percentage.js +90 -0
- package/dist/svg/percentage.js.map +1 -0
- package/dist/svg/scoreCard.d.ts +19 -0
- package/dist/svg/scoreCard.js +55 -0
- package/dist/svg/scoreCard.js.map +1 -0
- package/dist/types/queries.d.ts +8 -7
- package/dist/types/queries.js.map +1 -1
- package/dist/utils/card-utils.d.ts +6 -0
- package/dist/utils/card-utils.js +12 -0
- package/dist/utils/card-utils.js.map +1 -1
- package/dist/utils/clingo-facts.d.ts +2 -1
- package/dist/utils/clingo-facts.js +19 -2
- package/dist/utils/clingo-facts.js.map +1 -1
- package/dist/utils/clingo-parser.d.ts +1 -0
- package/dist/utils/clingo-parser.js +22 -100
- package/dist/utils/clingo-parser.js.map +1 -1
- package/dist/utils/constants.d.ts +7 -0
- package/dist/utils/constants.js +14 -0
- package/dist/utils/constants.js.map +1 -1
- package/dist/utils/csv.js.map +1 -1
- package/dist/utils/report.d.ts +17 -3
- package/dist/utils/report.js +38 -2
- package/dist/utils/report.js.map +1 -1
- package/dist/utils/resource-utils.d.ts +1 -0
- package/dist/utils/resource-utils.js +9 -0
- package/dist/utils/resource-utils.js.map +1 -1
- package/dist/utils/user-preferences.d.ts +1 -11
- package/dist/utils/user-preferences.js +30 -13
- package/dist/utils/user-preferences.js.map +1 -1
- package/dist/utils/validate.d.ts +2 -3
- package/dist/utils/validate.js +2 -2
- package/dist/utils/validate.js.map +1 -1
- package/package.json +8 -6
- package/src/command-handler.ts +96 -17
- package/src/command-manager.ts +8 -8
- package/src/commands/calculate.ts +11 -525
- package/src/commands/create.ts +53 -16
- package/src/commands/edit.ts +53 -11
- package/src/commands/export.ts +108 -34
- package/src/commands/import.ts +31 -2
- package/src/commands/move.ts +12 -15
- package/src/commands/remove.ts +10 -19
- package/src/commands/rename.ts +3 -12
- package/src/commands/show.ts +121 -8
- package/src/commands/transition.ts +3 -7
- package/src/commands/update.ts +6 -0
- package/src/commands/validate.ts +39 -47
- package/src/containers/card-container.ts +74 -0
- package/src/containers/project/calculation-engine.ts +535 -0
- package/src/containers/project/resource-collector.ts +45 -26
- package/src/containers/project.ts +66 -84
- package/src/containers/template.ts +16 -0
- package/src/exceptions/index.ts +36 -0
- package/src/index.ts +13 -2
- package/src/interfaces/macros.ts +7 -1
- package/src/interfaces/project-interfaces.ts +27 -0
- package/src/interfaces/resource-interfaces.ts +1 -0
- package/src/macros/base-macro.ts +89 -25
- package/src/macros/common.ts +22 -9
- package/src/macros/createCards/index.ts +1 -2
- package/src/macros/graph/index.ts +17 -12
- package/src/macros/image/index.ts +121 -0
- package/src/macros/image/metadata.ts +25 -0
- package/src/macros/include/index.ts +147 -0
- package/src/macros/include/metadata.ts +22 -0
- package/src/macros/index.ts +179 -100
- package/src/macros/percentage/index.ts +54 -0
- package/src/macros/percentage/metadata.ts +22 -0
- package/src/macros/report/index.ts +22 -17
- package/src/macros/scoreCard/index.ts +23 -23
- package/src/macros/vega/index.ts +55 -0
- package/src/macros/vega/metadata.ts +21 -0
- package/src/macros/vegalite/index.ts +50 -0
- package/src/macros/vegalite/metadata.ts +21 -0
- package/src/macros/xref/index.ts +73 -0
- package/src/macros/xref/metadata.ts +22 -0
- package/src/module-manager.ts +241 -69
- package/src/permissions/action-guard.ts +3 -3
- package/src/project-settings.ts +2 -11
- package/src/resources/card-type-resource.ts +100 -0
- package/src/resources/file-resource.ts +16 -4
- package/src/resources/folder-resource.ts +59 -2
- package/src/resources/graph-model-resource.ts +1 -1
- package/src/resources/graph-view-resource.ts +1 -1
- package/src/resources/report-resource.ts +1 -1
- package/src/resources/resource-object.ts +14 -0
- package/src/resources/template-resource.ts +1 -1
- package/src/resources/workflow-resource.ts +68 -13
- package/src/svg/index.ts +15 -0
- package/src/svg/lib.ts +31 -0
- package/src/svg/percentage.ts +97 -0
- package/src/svg/scoreCard.ts +88 -0
- package/src/types/queries.ts +8 -7
- package/src/types/string-pixel-width.d.ts +23 -0
- package/src/utils/card-utils.ts +13 -0
- package/src/utils/clingo-facts.ts +65 -3
- package/src/utils/clingo-parser.ts +31 -144
- package/src/utils/constants.ts +16 -0
- package/src/utils/csv.ts +1 -1
- package/src/utils/report.ts +45 -4
- package/src/utils/resource-utils.ts +9 -0
- package/src/utils/user-preferences.ts +32 -14
- package/src/utils/validate.ts +3 -3
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Cyberismo
|
|
3
|
+
Copyright © Cyberismo Ltd and contributors 2025
|
|
4
|
+
This program is free software: you can redistribute it and/or modify it under
|
|
5
|
+
the terms of the GNU Affero General Public License version 3 as published by
|
|
6
|
+
the Free Software Foundation.
|
|
7
|
+
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
8
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
9
|
+
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
10
|
+
details. You should have received a copy of the GNU Affero General Public
|
|
11
|
+
License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
12
|
+
*/
|
|
13
|
+
import { evaluateMacros, validateMacroContent } from '../index.js';
|
|
14
|
+
import macroMetadata from './metadata.js';
|
|
15
|
+
import BaseMacro from '../base-macro.js';
|
|
16
|
+
import { MAX_LEVEL_OFFSET } from '../../utils/constants.js';
|
|
17
|
+
export default class IncludeMacro extends BaseMacro {
|
|
18
|
+
constructor(tasksQueue) {
|
|
19
|
+
super(macroMetadata, tasksQueue);
|
|
20
|
+
}
|
|
21
|
+
handleValidate = (input) => {
|
|
22
|
+
this.validate(input);
|
|
23
|
+
};
|
|
24
|
+
handleStatic = async (context, input) => {
|
|
25
|
+
const options = this.validate(input);
|
|
26
|
+
if (!options.title) {
|
|
27
|
+
options.title = 'include';
|
|
28
|
+
}
|
|
29
|
+
if (!options.pageTitles) {
|
|
30
|
+
options.pageTitles = 'normal';
|
|
31
|
+
}
|
|
32
|
+
const card = await context.project.cardDetailsById(options.cardKey, {
|
|
33
|
+
content: true,
|
|
34
|
+
metadata: true,
|
|
35
|
+
});
|
|
36
|
+
if (!card) {
|
|
37
|
+
throw new Error(`Card key ${options.cardKey} not found`);
|
|
38
|
+
}
|
|
39
|
+
const newContext = {
|
|
40
|
+
...context,
|
|
41
|
+
cardKey: options.cardKey,
|
|
42
|
+
};
|
|
43
|
+
const anchor = this.generateAnchor(options);
|
|
44
|
+
const title = this.generateTitle(options, card.metadata?.title);
|
|
45
|
+
const cardContent = await this.generateCardContent(options, card.content, newContext);
|
|
46
|
+
const content = `\n\n${anchor}${title}${cardContent}`;
|
|
47
|
+
let levelOffset = 0;
|
|
48
|
+
if (options.levelOffset) {
|
|
49
|
+
levelOffset = parseInt(options.levelOffset, 10);
|
|
50
|
+
if (isNaN(levelOffset)) {
|
|
51
|
+
throw new Error(`Invalid level offset: ${options.levelOffset}`);
|
|
52
|
+
}
|
|
53
|
+
levelOffset = Math.min(Math.max(levelOffset, -MAX_LEVEL_OFFSET), MAX_LEVEL_OFFSET);
|
|
54
|
+
}
|
|
55
|
+
const adjustedContent = this.adjustTitles(content, levelOffset, options.pageTitles === 'discrete');
|
|
56
|
+
return adjustedContent;
|
|
57
|
+
};
|
|
58
|
+
handleInject = async (context, input) => {
|
|
59
|
+
return this.handleStatic(context, input);
|
|
60
|
+
};
|
|
61
|
+
validate(input) {
|
|
62
|
+
return validateMacroContent(this.metadata, input);
|
|
63
|
+
}
|
|
64
|
+
generateAnchor(options) {
|
|
65
|
+
return options.title !== 'exclude' && options.pageTitles === 'normal'
|
|
66
|
+
? `[[${options.cardKey}]]\n`
|
|
67
|
+
: '';
|
|
68
|
+
}
|
|
69
|
+
generateTitle(options, cardTitle) {
|
|
70
|
+
if (options.title === 'only' || options.title === 'include') {
|
|
71
|
+
return `= ${cardTitle}\n\n`;
|
|
72
|
+
}
|
|
73
|
+
return '';
|
|
74
|
+
}
|
|
75
|
+
async generateCardContent(options, cardContent, context) {
|
|
76
|
+
if (options.title !== 'only') {
|
|
77
|
+
return await evaluateMacros(cardContent ?? '', context, true);
|
|
78
|
+
}
|
|
79
|
+
return '';
|
|
80
|
+
}
|
|
81
|
+
// Adjust asciidoc titles to match the level offset
|
|
82
|
+
adjustTitles(content, levelOffset, discrete) {
|
|
83
|
+
const lines = content.split('\n');
|
|
84
|
+
const adjustedLines = lines.map((line) => {
|
|
85
|
+
const match = line.match(/^(\s*)([=#]+)(.*?)\s*$/);
|
|
86
|
+
if (match) {
|
|
87
|
+
const currentLevel = match[2].length;
|
|
88
|
+
const newLevel = Math.min(Math.max(1, currentLevel + levelOffset), MAX_LEVEL_OFFSET + 1);
|
|
89
|
+
const equals = '='.repeat(newLevel);
|
|
90
|
+
return `${discrete ? '[discrete]\n' : ''}${match[1]}${equals} ${match[3].trim()}`;
|
|
91
|
+
}
|
|
92
|
+
return line;
|
|
93
|
+
});
|
|
94
|
+
return adjustedLines.join('\n');
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/macros/include/index.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Cyberismo
|
|
3
|
+
Copyright © Cyberismo Ltd and contributors 2025
|
|
4
|
+
This program is free software: you can redistribute it and/or modify it under
|
|
5
|
+
the terms of the GNU Affero General Public License version 3 as published by
|
|
6
|
+
the Free Software Foundation.
|
|
7
|
+
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
8
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
9
|
+
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
10
|
+
details. You should have received a copy of the GNU Affero General Public
|
|
11
|
+
License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
12
|
+
*/
|
|
13
|
+
import type { MacroMetadata } from '../../interfaces/macros.js';
|
|
14
|
+
declare const macroMetadata: MacroMetadata;
|
|
15
|
+
export default macroMetadata;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Cyberismo
|
|
3
|
+
Copyright © Cyberismo Ltd and contributors 2025
|
|
4
|
+
This program is free software: you can redistribute it and/or modify it under
|
|
5
|
+
the terms of the GNU Affero General Public License version 3 as published by
|
|
6
|
+
the Free Software Foundation.
|
|
7
|
+
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
8
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
9
|
+
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
10
|
+
details. You should have received a copy of the GNU Affero General Public
|
|
11
|
+
License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
12
|
+
*/
|
|
13
|
+
const macroMetadata = {
|
|
14
|
+
name: 'include',
|
|
15
|
+
tagName: 'include',
|
|
16
|
+
schema: 'includeMacroSchema',
|
|
17
|
+
};
|
|
18
|
+
export default macroMetadata;
|
|
19
|
+
//# sourceMappingURL=metadata.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metadata.js","sourceRoot":"","sources":["../../../src/macros/include/metadata.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;EAWE;AAIF,MAAM,aAAa,GAAkB;IACnC,IAAI,EAAE,SAAS;IACf,OAAO,EAAE,SAAS;IAClB,MAAM,EAAE,oBAAoB;CAC7B,CAAC;AAEF,eAAe,aAAa,CAAC"}
|
package/dist/macros/index.d.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
Cyberismo
|
|
3
|
+
Copyright © Cyberismo Ltd and contributors 2024
|
|
4
|
+
This program is free software: you can redistribute it and/or modify it under
|
|
5
|
+
the terms of the GNU Affero General Public License version 3 as published by
|
|
6
|
+
the Free Software Foundation.
|
|
7
|
+
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
8
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
9
|
+
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
10
|
+
details. You should have received a copy of the GNU Affero General Public
|
|
11
|
+
License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
11
12
|
*/
|
|
12
13
|
import Handlebars from 'handlebars';
|
|
13
14
|
import type { AdmonitionType } from '../interfaces/adoc.js';
|
|
14
|
-
import type { Validator } from 'jsonschema';
|
|
15
15
|
import type { MacroGenerationContext, MacroMetadata, MacroName } from '../interfaces/macros.js';
|
|
16
16
|
import type BaseMacro from './base-macro.js';
|
|
17
17
|
import TaskQueue from './task-queue.js';
|
|
18
|
-
import type {
|
|
18
|
+
import type { Schema } from 'jsonschema';
|
|
19
19
|
/**
|
|
20
20
|
* Constructor for all macros except report macros
|
|
21
21
|
*/
|
|
@@ -26,7 +26,7 @@ export interface SimpleMacroConstructor {
|
|
|
26
26
|
* Constructor for report macros
|
|
27
27
|
*/
|
|
28
28
|
export interface ReportMacroConstructor {
|
|
29
|
-
new (tasks: TaskQueue
|
|
29
|
+
new (tasks: TaskQueue): BaseMacro;
|
|
30
30
|
}
|
|
31
31
|
/**
|
|
32
32
|
* Constructor for all macros
|
|
@@ -39,15 +39,14 @@ export declare const macros: {
|
|
|
39
39
|
* Validates the content inside a macro
|
|
40
40
|
* @param macro - The macro to validate the content of
|
|
41
41
|
* @param data - The data to validate
|
|
42
|
-
* @param validator - The validator to use
|
|
43
42
|
* @returns The validated data
|
|
44
43
|
*/
|
|
45
|
-
export declare function validateMacroContent<T>(macro: MacroMetadata, data: unknown,
|
|
44
|
+
export declare function validateMacroContent<T>(macro: MacroMetadata, data: unknown, schema?: Schema): T;
|
|
46
45
|
/**
|
|
47
46
|
* Registers the macros with Handlebars
|
|
48
47
|
* @param {Mode} mode - The mode to register the macros in
|
|
49
48
|
*/
|
|
50
|
-
export declare function registerMacros(instance: typeof Handlebars, context: MacroGenerationContext, tasks: TaskQueue
|
|
49
|
+
export declare function registerMacros(instance: typeof Handlebars, context: MacroGenerationContext, tasks: TaskQueue): BaseMacro[];
|
|
51
50
|
/**
|
|
52
51
|
* Calculate amount of handlebars templates inside a string
|
|
53
52
|
* @param input
|
|
@@ -63,9 +62,11 @@ export declare function registerEmptyMacros(instance: typeof Handlebars): void;
|
|
|
63
62
|
/**
|
|
64
63
|
* Handle the macros in the content
|
|
65
64
|
* @param content - The content to handle the macros in
|
|
66
|
-
* @param
|
|
65
|
+
* @param context - The context for macro generation
|
|
66
|
+
* @param calculate - The calculate function
|
|
67
|
+
* @param preserveRawBlocks - If true, don't unescape raw blocks at the end (for nested evaluations)
|
|
67
68
|
*/
|
|
68
|
-
export declare function evaluateMacros(content: string, context: MacroGenerationContext,
|
|
69
|
+
export declare function evaluateMacros(content: string, context: MacroGenerationContext, preserveRawBlocks?: boolean): Promise<string>;
|
|
69
70
|
/**
|
|
70
71
|
* This function assumes that tasks, which were started by macros, are complete.
|
|
71
72
|
* It replaces the placeholders of the tasks with the actual results
|
|
@@ -83,20 +84,14 @@ export declare function applyMacroResults(input: string, tasks: TaskQueue, conte
|
|
|
83
84
|
* @returns The error message that is valid adoc
|
|
84
85
|
*/
|
|
85
86
|
export declare function handleMacroError(error: unknown, macro: string, context: MacroGenerationContext): string;
|
|
86
|
-
type Value = string | number | boolean | undefined;
|
|
87
|
-
/**
|
|
88
|
-
* Macro options can be a flat object or a nested object
|
|
89
|
-
* The nested object will be flattened into dot notation attributes
|
|
90
|
-
*/
|
|
91
|
-
export type MacroOptions = {
|
|
92
|
-
[key: string]: Value | MacroOptions;
|
|
93
|
-
};
|
|
94
87
|
/**
|
|
95
|
-
* Creates
|
|
96
|
-
*
|
|
97
|
-
* @param
|
|
88
|
+
* Creates a placeholder for a macro
|
|
89
|
+
* Options are encoded as base64
|
|
90
|
+
* @param macro - The macro to create a placeholder for
|
|
91
|
+
* @param options - The options for the macro
|
|
92
|
+
* @returns The placeholder for the macro
|
|
98
93
|
*/
|
|
99
|
-
export declare function createHtmlPlaceholder(macro: MacroMetadata, options:
|
|
94
|
+
export declare function createHtmlPlaceholder(macro: MacroMetadata, options: unknown): string;
|
|
100
95
|
/**
|
|
101
96
|
* Creates an adoc admonition
|
|
102
97
|
* @param type - The type of admonition
|
|
@@ -105,10 +100,23 @@ export declare function createHtmlPlaceholder(macro: MacroMetadata, options: Mac
|
|
|
105
100
|
* @returns The adoc admonition as a string
|
|
106
101
|
*/
|
|
107
102
|
export declare function createAdmonition(type: AdmonitionType, label: string, content: string): string;
|
|
103
|
+
/**
|
|
104
|
+
* Creates a code block
|
|
105
|
+
* @param content - The content of the code block
|
|
106
|
+
* @returns The code block as a string
|
|
107
|
+
*/
|
|
108
|
+
export declare function createCodeBlock(content: string): string;
|
|
108
109
|
/**
|
|
109
110
|
* Helper function for including base64 encoded images for now
|
|
110
111
|
* @param image base64 encoded image
|
|
111
112
|
* @returns valid asciidoc with the image
|
|
112
113
|
*/
|
|
113
|
-
export declare function createImage(image: string): string;
|
|
114
|
-
|
|
114
|
+
export declare function createImage(image: string, controls?: boolean): string;
|
|
115
|
+
/**
|
|
116
|
+
* Creates a Handlebars macro block string with the given macro name and options.
|
|
117
|
+
*
|
|
118
|
+
* @param macro - The name of the macro to create (e.g., 'scoreCard', 'include').
|
|
119
|
+
* @param options - The options object to be stringified and inserted as macro content.
|
|
120
|
+
* @returns The Handlebars macro block as a string, e.g. {{#macro}}...{{/macro}}
|
|
121
|
+
*/
|
|
122
|
+
export declare function createMacro(macro: MacroName, options: unknown): string;
|
package/dist/macros/index.js
CHANGED
|
@@ -1,45 +1,128 @@
|
|
|
1
1
|
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
Cyberismo
|
|
3
|
+
Copyright © Cyberismo Ltd and contributors 2024
|
|
4
|
+
This program is free software: you can redistribute it and/or modify it under
|
|
5
|
+
the terms of the GNU Affero General Public License version 3 as published by
|
|
6
|
+
the Free Software Foundation.
|
|
7
|
+
This program is distributed in the hope that it will be useful, but WITHOUT
|
|
8
|
+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
9
|
+
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
10
|
+
details. You should have received a copy of the GNU Affero General Public
|
|
11
|
+
License along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
11
12
|
*/
|
|
12
13
|
import Handlebars from 'handlebars';
|
|
13
14
|
import createCards from './createCards/index.js';
|
|
14
15
|
import graph from './graph/index.js';
|
|
16
|
+
import image from './image/index.js';
|
|
17
|
+
import include from './include/index.js';
|
|
15
18
|
import report from './report/index.js';
|
|
16
19
|
import scoreCard from './scoreCard/index.js';
|
|
20
|
+
import xref from './xref/index.js';
|
|
21
|
+
import percentage from './percentage/index.js';
|
|
22
|
+
import vega from './vega/index.js';
|
|
23
|
+
import vegaLite from './vegalite/index.js';
|
|
17
24
|
import { validateJson } from '../utils/validate.js';
|
|
18
|
-
import { DHValidationError } from '../exceptions/index.js';
|
|
25
|
+
import { DHValidationError, MacroError } from '../exceptions/index.js';
|
|
19
26
|
import TaskQueue from './task-queue.js';
|
|
27
|
+
import { ClingoError } from '@cyberismo/node-clingo';
|
|
20
28
|
const CURLY_LEFT = '{';
|
|
21
29
|
const CURLY_RIGHT = '}';
|
|
30
|
+
const RAW_BLOCK_OPEN = '{{#raw}}';
|
|
31
|
+
const RAW_BLOCK_CLOSE = '{{/raw}}';
|
|
32
|
+
/**
|
|
33
|
+
* Pre-processes the content to handle {{#raw}} blocks by escaping all handlebars syntax inside them
|
|
34
|
+
* @param content The template content to process
|
|
35
|
+
* @returns The processed content with raw blocks escaped
|
|
36
|
+
* @throws Error if nested raw blocks are found or if a raw block is not properly closed
|
|
37
|
+
*/
|
|
38
|
+
function preprocessRawBlocks(content) {
|
|
39
|
+
const result = [];
|
|
40
|
+
let i = 0;
|
|
41
|
+
// Helper function to check if a target string matches at a given position without creating substrings
|
|
42
|
+
const matchesAt = (pos, target) => {
|
|
43
|
+
if (pos + target.length > content.length)
|
|
44
|
+
return false;
|
|
45
|
+
for (let k = 0; k < target.length; k++) {
|
|
46
|
+
if (content[pos + k] !== target[k])
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
};
|
|
51
|
+
// Helper function to get line number at position
|
|
52
|
+
const getLineNumber = (pos) => {
|
|
53
|
+
let lineNum = 1;
|
|
54
|
+
for (let k = 0; k < pos; k++) {
|
|
55
|
+
if (content[k] === '\n') {
|
|
56
|
+
lineNum++;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return lineNum;
|
|
60
|
+
};
|
|
61
|
+
while (i < content.length) {
|
|
62
|
+
// Check for {{#raw}}
|
|
63
|
+
if (matchesAt(i, RAW_BLOCK_OPEN)) {
|
|
64
|
+
const openingLine = getLineNumber(i);
|
|
65
|
+
// Find the matching {{/raw}} - no nesting allowed
|
|
66
|
+
let j = i + RAW_BLOCK_OPEN.length;
|
|
67
|
+
while (j < content.length) {
|
|
68
|
+
if (matchesAt(j, RAW_BLOCK_OPEN)) {
|
|
69
|
+
// Found nested raw block - not supported
|
|
70
|
+
const nestedLine = getLineNumber(j);
|
|
71
|
+
throw new Error(`Nested ${RAW_BLOCK_OPEN} blocks are not supported. Found nested raw block inside another raw block on line ${nestedLine} (original raw block started on line ${openingLine}).`);
|
|
72
|
+
}
|
|
73
|
+
else if (matchesAt(j, RAW_BLOCK_CLOSE)) {
|
|
74
|
+
// Found matching closing tag
|
|
75
|
+
const rawContent = content.slice(i + RAW_BLOCK_OPEN.length, j);
|
|
76
|
+
const escapedContent = rawContent
|
|
77
|
+
.replaceAll('{', CURLY_LEFT)
|
|
78
|
+
.replaceAll('}', CURLY_RIGHT);
|
|
79
|
+
result.push(escapedContent);
|
|
80
|
+
i = j + RAW_BLOCK_CLOSE.length;
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
j++;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// If we reached the end without finding a closing tag
|
|
88
|
+
if (j >= content.length) {
|
|
89
|
+
throw new Error(`Unclosed ${RAW_BLOCK_OPEN} block found on line ${openingLine}. Every ${RAW_BLOCK_OPEN} must have a matching ${RAW_BLOCK_CLOSE}.`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
// Not a raw block, keep as-is
|
|
94
|
+
result.push(content[i]);
|
|
95
|
+
i++;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return result.join('');
|
|
99
|
+
}
|
|
22
100
|
export const macros = {
|
|
23
101
|
createCards,
|
|
24
102
|
graph,
|
|
103
|
+
image,
|
|
104
|
+
include,
|
|
25
105
|
report,
|
|
26
106
|
scoreCard,
|
|
107
|
+
xref,
|
|
108
|
+
percentage,
|
|
109
|
+
vega,
|
|
110
|
+
vegaLite,
|
|
27
111
|
};
|
|
28
112
|
/**
|
|
29
113
|
* Validates the content inside a macro
|
|
30
114
|
* @param macro - The macro to validate the content of
|
|
31
115
|
* @param data - The data to validate
|
|
32
|
-
* @param validator - The validator to use
|
|
33
116
|
* @returns The validated data
|
|
34
117
|
*/
|
|
35
|
-
export function validateMacroContent(macro, data,
|
|
118
|
+
export function validateMacroContent(macro, data, schema) {
|
|
36
119
|
if (!macro.schema) {
|
|
37
120
|
throw new Error(`Macro ${macro.name} does not have a schema`);
|
|
38
121
|
}
|
|
39
122
|
try {
|
|
40
123
|
return validateJson(data, {
|
|
41
124
|
schemaId: macro.schema,
|
|
42
|
-
|
|
125
|
+
schema,
|
|
43
126
|
});
|
|
44
127
|
}
|
|
45
128
|
catch (error) {
|
|
@@ -54,28 +137,16 @@ export function validateMacroContent(macro, data, validator) {
|
|
|
54
137
|
* Registers the macros with Handlebars
|
|
55
138
|
* @param {Mode} mode - The mode to register the macros in
|
|
56
139
|
*/
|
|
57
|
-
export function registerMacros(instance, context, tasks
|
|
140
|
+
export function registerMacros(instance, context, tasks) {
|
|
58
141
|
const macroInstances = [];
|
|
59
142
|
for (const macro of Object.keys(macros)) {
|
|
60
143
|
const MacroClass = macros[macro];
|
|
61
|
-
const macroInstance = new MacroClass(tasks
|
|
144
|
+
const macroInstance = new MacroClass(tasks);
|
|
62
145
|
instance.registerHelper(macro, function (options) {
|
|
63
|
-
if (this != null &&
|
|
64
|
-
typeof this === 'object' &&
|
|
65
|
-
'__isRaw' in this &&
|
|
66
|
-
this.__isRaw) {
|
|
67
|
-
// we use escaped chars so that they will not be re-run
|
|
68
|
-
return `${CURLY_LEFT}${CURLY_LEFT}#${macro}${CURLY_RIGHT}${CURLY_RIGHT}${options.fn(this)}${CURLY_LEFT}${CURLY_LEFT}/${macro}${CURLY_RIGHT}${CURLY_RIGHT}`;
|
|
69
|
-
}
|
|
70
146
|
return macroInstance.invokeMacro(context, options);
|
|
71
147
|
});
|
|
72
148
|
macroInstances.push(macroInstance);
|
|
73
149
|
}
|
|
74
|
-
instance.registerHelper('raw', function (options) {
|
|
75
|
-
return options.fn({
|
|
76
|
-
__isRaw: true,
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
150
|
return macroInstances;
|
|
80
151
|
}
|
|
81
152
|
/**
|
|
@@ -103,20 +174,22 @@ export function registerEmptyMacros(instance) {
|
|
|
103
174
|
/**
|
|
104
175
|
* Handle the macros in the content
|
|
105
176
|
* @param content - The content to handle the macros in
|
|
106
|
-
* @param
|
|
177
|
+
* @param context - The context for macro generation
|
|
178
|
+
* @param calculate - The calculate function
|
|
179
|
+
* @param preserveRawBlocks - If true, don't unescape raw blocks at the end (for nested evaluations)
|
|
107
180
|
*/
|
|
108
|
-
export async function evaluateMacros(content, context,
|
|
181
|
+
export async function evaluateMacros(content, context, preserveRawBlocks = false) {
|
|
109
182
|
const handlebars = Handlebars.create();
|
|
110
183
|
const tasks = new TaskQueue();
|
|
111
|
-
registerMacros(handlebars, context, tasks
|
|
184
|
+
registerMacros(handlebars, context, tasks);
|
|
112
185
|
let result = content;
|
|
113
|
-
while (maxTries
|
|
186
|
+
while ((context.maxTries ?? 10) > 0) {
|
|
114
187
|
tasks.reset();
|
|
115
|
-
const compiled = handlebars.compile(result, {
|
|
116
|
-
strict: true,
|
|
117
|
-
});
|
|
118
188
|
try {
|
|
119
|
-
|
|
189
|
+
const compiled = handlebars.compile(preprocessRawBlocks(result), {
|
|
190
|
+
strict: true,
|
|
191
|
+
});
|
|
192
|
+
result = compiled({ cardKey: context.cardKey });
|
|
120
193
|
await tasks.waitAll();
|
|
121
194
|
result = applyMacroResults(result, tasks, context);
|
|
122
195
|
if (macroCount(result) === 0) {
|
|
@@ -131,7 +204,10 @@ export async function evaluateMacros(content, context, calculate, maxTries = 10)
|
|
|
131
204
|
if (macroCount(result) !== 0) {
|
|
132
205
|
return handleMacroError(new Error(`Too many recursive macro evaluations.`), '', context);
|
|
133
206
|
}
|
|
134
|
-
|
|
207
|
+
// Only unescape raw blocks if we're not preserving them for nested evaluations
|
|
208
|
+
return preserveRawBlocks
|
|
209
|
+
? result
|
|
210
|
+
: result.replaceAll(CURLY_LEFT, '{').replaceAll(CURLY_RIGHT, '}');
|
|
135
211
|
}
|
|
136
212
|
/**
|
|
137
213
|
* This function assumes that tasks, which were started by macros, are complete.
|
|
@@ -144,7 +220,11 @@ export async function evaluateMacros(content, context, calculate, maxTries = 10)
|
|
|
144
220
|
*/
|
|
145
221
|
export function applyMacroResults(input, tasks, context) {
|
|
146
222
|
for (const item of tasks) {
|
|
147
|
-
if (item.
|
|
223
|
+
if (item.error) {
|
|
224
|
+
input = input.replace(item.placeholder, handleMacroError(item.error, item.macro, context));
|
|
225
|
+
} // It should not be possible that promiseResult is null if there never was an error
|
|
226
|
+
// Unless the function itself returns null / undefined
|
|
227
|
+
else if (item.promiseResult == null) {
|
|
148
228
|
input = handleMacroError(new Error(`Tried to access result before it was resolved for ${item.placeholder}`), item.macro, context);
|
|
149
229
|
}
|
|
150
230
|
else {
|
|
@@ -164,6 +244,16 @@ export function handleMacroError(error, macro, context) {
|
|
|
164
244
|
if (error instanceof DHValidationError) {
|
|
165
245
|
message = `Check json syntax of macro ${macro}: ${error.errors?.map((e) => e.message).join(', ')}`;
|
|
166
246
|
}
|
|
247
|
+
else if (error instanceof MacroError) {
|
|
248
|
+
const { cardKey, macroName, dependency } = error.context;
|
|
249
|
+
message = `Macro error in card '${cardKey}' in macro '${macroName}':\n\n${error.message}.`;
|
|
250
|
+
if (dependency) {
|
|
251
|
+
message += `\n\nParameters:\n\n${context.mode === 'validate' ? dependency.parameters : createCodeBlock(dependency.parameters)}.\n\n${dependency.output ? `Output:\n\n${context.mode === 'validate' ? dependency.output : createCodeBlock(dependency.output)}` : ''}`;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
else if (error instanceof ClingoError) {
|
|
255
|
+
message = `Error running logic program in macro '${macro}':${error.details.errors.join('\n')}`;
|
|
256
|
+
}
|
|
167
257
|
if (typeof error === 'object' &&
|
|
168
258
|
error != null &&
|
|
169
259
|
'lineNumber' in error &&
|
|
@@ -180,38 +270,19 @@ export function handleMacroError(error, macro, context) {
|
|
|
180
270
|
// This is used to generate unique keys for macros
|
|
181
271
|
// There might be a better way to do this
|
|
182
272
|
let macroCounter = 0;
|
|
273
|
+
function objectToBase64(obj) {
|
|
274
|
+
return Buffer.from(JSON.stringify(obj), 'utf-8').toString('base64');
|
|
275
|
+
}
|
|
183
276
|
/**
|
|
184
|
-
* Creates
|
|
185
|
-
*
|
|
186
|
-
* @param
|
|
277
|
+
* Creates a placeholder for a macro
|
|
278
|
+
* Options are encoded as base64
|
|
279
|
+
* @param macro - The macro to create a placeholder for
|
|
280
|
+
* @param options - The options for the macro
|
|
281
|
+
* @returns The placeholder for the macro
|
|
187
282
|
*/
|
|
188
283
|
export function createHtmlPlaceholder(macro, options) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
// Helper function to flatten nested objects
|
|
192
|
-
const flatten = (obj, prefix = '') => {
|
|
193
|
-
Object.entries(obj).forEach(([key, value]) => {
|
|
194
|
-
const newKey = prefix ? `${prefix}.${key}` : key;
|
|
195
|
-
if (value !== null &&
|
|
196
|
-
typeof value === 'object' &&
|
|
197
|
-
!Array.isArray(value)) {
|
|
198
|
-
// Recursively flatten nested objects
|
|
199
|
-
flatten(value, newKey);
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
// Add leaf values to flattened options
|
|
203
|
-
flattenedOptions[newKey] = value;
|
|
204
|
-
}
|
|
205
|
-
});
|
|
206
|
-
};
|
|
207
|
-
flatten(options);
|
|
208
|
-
// Convert flattened options to attribute strings
|
|
209
|
-
const attributeStrings = Object.entries(flattenedOptions)
|
|
210
|
-
.filter(([, value]) => value !== undefined)
|
|
211
|
-
.map(([key, value]) => `${key}="${value}"`);
|
|
212
|
-
const optionString = attributeStrings.join(' ');
|
|
213
|
-
// start with a line change to ensure that inline passthrough +++ is on its own line
|
|
214
|
-
return `\n+++\n<${macro.tagName}${optionString ? ` ${optionString}` : ''} key="macro-${macroCounter++}"></${macro.tagName}>\n+++\n`;
|
|
284
|
+
const optionsBase64 = objectToBase64(options);
|
|
285
|
+
return `\n\n++++\n<${macro.tagName} options="${optionsBase64}" key="macro-${macroCounter++}"></${macro.tagName}>\n++++\n\n`;
|
|
215
286
|
}
|
|
216
287
|
/**
|
|
217
288
|
* Creates an adoc admonition
|
|
@@ -223,22 +294,45 @@ export function createHtmlPlaceholder(macro, options) {
|
|
|
223
294
|
export function createAdmonition(type, label, content) {
|
|
224
295
|
return `[${type}]\n.${label}\n====\n${content}\n====\n\n`;
|
|
225
296
|
}
|
|
297
|
+
/**
|
|
298
|
+
* Creates a code block
|
|
299
|
+
* @param content - The content of the code block
|
|
300
|
+
* @returns The code block as a string
|
|
301
|
+
*/
|
|
302
|
+
export function createCodeBlock(content) {
|
|
303
|
+
return `\n\n----\n${content}\n----\n\n`;
|
|
304
|
+
}
|
|
226
305
|
/**
|
|
227
306
|
* Helper function for including base64 encoded images for now
|
|
228
307
|
* @param image base64 encoded image
|
|
229
308
|
* @returns valid asciidoc with the image
|
|
230
309
|
*/
|
|
231
|
-
export function createImage(image) {
|
|
310
|
+
export function createImage(image, controls = true) {
|
|
232
311
|
if (process.env.EXPORT_FORMAT) {
|
|
233
312
|
return `image::data:image/svg+xml;base64,${image}[]\n`;
|
|
234
313
|
}
|
|
235
314
|
else {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
${
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
`;
|
|
315
|
+
const svg = Buffer.from(image, 'base64').toString('utf-8');
|
|
316
|
+
if (controls) {
|
|
317
|
+
return `++++\n<div class="cyberismo-svg-wrapper" data-type="cyberismo-svg-wrapper">\n${svg}\n</div>\n++++\n`;
|
|
318
|
+
}
|
|
319
|
+
else {
|
|
320
|
+
return `++++\n${svg}\n++++\n`;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Creates a Handlebars macro block string with the given macro name and options.
|
|
326
|
+
*
|
|
327
|
+
* @param macro - The name of the macro to create (e.g., 'scoreCard', 'include').
|
|
328
|
+
* @param options - The options object to be stringified and inserted as macro content.
|
|
329
|
+
* @returns The Handlebars macro block as a string, e.g. {{#macro}}...{{/macro}}
|
|
330
|
+
*/
|
|
331
|
+
export function createMacro(macro, options) {
|
|
332
|
+
let optionsString = JSON.stringify(options, null, 0);
|
|
333
|
+
if (optionsString.length > 1) {
|
|
334
|
+
optionsString = optionsString.slice(1, -1);
|
|
242
335
|
}
|
|
336
|
+
return `{{#${macro}}}${optionsString}{{/${macro}}}`;
|
|
243
337
|
}
|
|
244
338
|
//# sourceMappingURL=index.js.map
|