@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
|
@@ -12,12 +12,9 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import BaseMacro from '../base-macro.js';
|
|
15
|
-
import { Calculate } from '../../commands/index.js';
|
|
16
|
-
import type { MacroOptions } from '../index.js';
|
|
17
15
|
import { createImage, validateMacroContent } from '../index.js';
|
|
18
16
|
import Handlebars from 'handlebars';
|
|
19
17
|
import { join } from 'node:path';
|
|
20
|
-
import { getChildLogger } from '../../utils/log-utils.js';
|
|
21
18
|
import type { MacroGenerationContext } from '../../interfaces/macros.js';
|
|
22
19
|
import macroMetadata from './metadata.js';
|
|
23
20
|
import { pathExists } from '../../utils/file-utils.js';
|
|
@@ -26,18 +23,14 @@ import { resourceName } from '../../utils/resource-utils.js';
|
|
|
26
23
|
import type { Schema } from 'jsonschema';
|
|
27
24
|
import { validateJson } from '../../utils/validate.js';
|
|
28
25
|
import type TaskQueue from '../task-queue.js';
|
|
26
|
+
import { ClingoError } from '@cyberismo/node-clingo';
|
|
29
27
|
|
|
30
|
-
export interface GraphOptions
|
|
28
|
+
export interface GraphOptions {
|
|
31
29
|
model: string;
|
|
32
30
|
view: string;
|
|
33
31
|
}
|
|
34
32
|
|
|
35
33
|
class ReportMacro extends BaseMacro {
|
|
36
|
-
private get logger() {
|
|
37
|
-
return getChildLogger({
|
|
38
|
-
module: 'graphMacro',
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
34
|
constructor(tasksQueue: TaskQueue) {
|
|
42
35
|
super(macroMetadata, tasksQueue);
|
|
43
36
|
}
|
|
@@ -51,8 +44,6 @@ class ReportMacro extends BaseMacro {
|
|
|
51
44
|
};
|
|
52
45
|
|
|
53
46
|
handleInject = async (context: MacroGenerationContext, input: unknown) => {
|
|
54
|
-
const calculate = new Calculate(context.project);
|
|
55
|
-
|
|
56
47
|
const resourceNameToPath = (name: string, fileName: string) => {
|
|
57
48
|
const { identifier, prefix, type } = resourceName(name);
|
|
58
49
|
if (prefix === context.project.projectPrefix) {
|
|
@@ -117,7 +108,21 @@ class ReportMacro extends BaseMacro {
|
|
|
117
108
|
const view = handlebars.compile(viewContent)(handlebarsContext);
|
|
118
109
|
|
|
119
110
|
const modelContent = await readFile(modelLocation, { encoding: 'utf-8' });
|
|
120
|
-
|
|
111
|
+
let result: string;
|
|
112
|
+
try {
|
|
113
|
+
result = await context.project.calculationEngine.runGraph(
|
|
114
|
+
modelContent,
|
|
115
|
+
view,
|
|
116
|
+
context.context,
|
|
117
|
+
);
|
|
118
|
+
} catch (error) {
|
|
119
|
+
if (error instanceof ClingoError) {
|
|
120
|
+
throw new Error(
|
|
121
|
+
`Error running graph in view '${options.view}' in model '${options.model}': ${error.details.errors.join('\n')}`,
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
throw error;
|
|
125
|
+
}
|
|
121
126
|
|
|
122
127
|
if (typeof result !== 'string') {
|
|
123
128
|
throw new Error(
|
|
@@ -0,0 +1,121 @@
|
|
|
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
|
+
|
|
14
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
15
|
+
import { join } from 'node:path';
|
|
16
|
+
import mime from 'mime-types';
|
|
17
|
+
|
|
18
|
+
import { validateMacroContent } from '../index.js';
|
|
19
|
+
|
|
20
|
+
import type { MacroGenerationContext } from '../../interfaces/macros.js';
|
|
21
|
+
import macroMetadata from './metadata.js';
|
|
22
|
+
import BaseMacro from '../base-macro.js';
|
|
23
|
+
import type TaskQueue from '../task-queue.js';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Options for the image macro.
|
|
27
|
+
* @param fileName - Name of the file to include.
|
|
28
|
+
* @param cardKey - Key of the card to include the file from.
|
|
29
|
+
* @param alt - Alternative text for the image.
|
|
30
|
+
* @param title - Title of the image.
|
|
31
|
+
*/
|
|
32
|
+
export interface ImageMacroOptions {
|
|
33
|
+
fileName: string;
|
|
34
|
+
cardKey?: string;
|
|
35
|
+
alt?: string;
|
|
36
|
+
title?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Macro for including images in the content
|
|
41
|
+
*/
|
|
42
|
+
export default class ImageMacro extends BaseMacro {
|
|
43
|
+
constructor(tasksQueue: TaskQueue) {
|
|
44
|
+
super(macroMetadata, tasksQueue);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
handleValidate = (input: unknown) => {
|
|
48
|
+
this.validate(input);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
handleStatic = async (
|
|
52
|
+
context: MacroGenerationContext,
|
|
53
|
+
input: unknown,
|
|
54
|
+
): Promise<string> => {
|
|
55
|
+
const options = this.validate(input);
|
|
56
|
+
const cardKey = options.cardKey || context.cardKey;
|
|
57
|
+
|
|
58
|
+
// Get the attachment folder path
|
|
59
|
+
const attachmentFolder =
|
|
60
|
+
await context.project.cardAttachmentFolder(cardKey);
|
|
61
|
+
if (!attachmentFolder) {
|
|
62
|
+
throw new Error(`Card '${cardKey}' not found`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Read the file and convert to base64
|
|
66
|
+
const attachmentPath = join(attachmentFolder, options.fileName);
|
|
67
|
+
if (!existsSync(attachmentPath)) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Attachment file '${options.fileName}' not found in card '${cardKey}'`,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const fileBuffer = readFileSync(attachmentPath);
|
|
74
|
+
const base64Data = fileBuffer.toString('base64');
|
|
75
|
+
|
|
76
|
+
// Get mime type
|
|
77
|
+
const mimeType = mime.lookup(attachmentPath) || 'application/octet-stream';
|
|
78
|
+
|
|
79
|
+
// Build image attributes
|
|
80
|
+
const attributes = this.buildImageAttributes(options);
|
|
81
|
+
|
|
82
|
+
// Return as data URI for static mode (for export/PDF generation)
|
|
83
|
+
return `image::data:${mimeType};base64,${base64Data}[${attributes}]`;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
handleInject = async (context: MacroGenerationContext, input: unknown) => {
|
|
87
|
+
const options = this.validate(input);
|
|
88
|
+
const cardKey = options.cardKey || context.cardKey;
|
|
89
|
+
|
|
90
|
+
// Verify that the card and attachment folder exist
|
|
91
|
+
const attachmentFolder =
|
|
92
|
+
await context.project.cardAttachmentFolder(cardKey);
|
|
93
|
+
if (!attachmentFolder) {
|
|
94
|
+
throw new Error(`Card '${cardKey}' not found`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Build image attributes
|
|
98
|
+
const attributes = this.buildImageAttributes(options);
|
|
99
|
+
|
|
100
|
+
// In inject mode, always use the API path for consistency
|
|
101
|
+
return `image::/api/cards/${cardKey}/a/${options.fileName}[${attributes}]`;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
private buildImageAttributes(options: ImageMacroOptions): string {
|
|
105
|
+
const attributes: string[] = [];
|
|
106
|
+
|
|
107
|
+
if (options.alt !== undefined) {
|
|
108
|
+
attributes.push(`alt="${options.alt}"`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (options.title !== undefined) {
|
|
112
|
+
attributes.push(`title="${options.title}"`);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return attributes.join(',');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private validate(input: unknown): ImageMacroOptions {
|
|
119
|
+
return validateMacroContent<ImageMacroOptions>(this.metadata, input);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
|
|
14
|
+
import type { MacroMetadata } from '../../interfaces/macros.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Metadata for the image macro.
|
|
18
|
+
*/
|
|
19
|
+
const macroMetadata: MacroMetadata = {
|
|
20
|
+
name: 'image',
|
|
21
|
+
tagName: 'image',
|
|
22
|
+
schema: 'imageMacroSchema',
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export default macroMetadata;
|
|
@@ -0,0 +1,147 @@
|
|
|
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
|
+
|
|
14
|
+
import { evaluateMacros, validateMacroContent } from '../index.js';
|
|
15
|
+
|
|
16
|
+
import type { MacroGenerationContext } from '../../interfaces/macros.js';
|
|
17
|
+
import macroMetadata from './metadata.js';
|
|
18
|
+
import BaseMacro from '../base-macro.js';
|
|
19
|
+
import type TaskQueue from '../task-queue.js';
|
|
20
|
+
import { MAX_LEVEL_OFFSET } from '../../utils/constants.js';
|
|
21
|
+
|
|
22
|
+
export interface IncludeMacroOptions {
|
|
23
|
+
cardKey: string;
|
|
24
|
+
levelOffset?: string;
|
|
25
|
+
title?: 'include' | 'exclude' | 'only';
|
|
26
|
+
pageTitles?: 'normal' | 'discrete';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export default class IncludeMacro extends BaseMacro {
|
|
30
|
+
constructor(tasksQueue: TaskQueue) {
|
|
31
|
+
super(macroMetadata, tasksQueue);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
handleValidate = (input: unknown) => {
|
|
35
|
+
this.validate(input);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
handleStatic = async (
|
|
39
|
+
context: MacroGenerationContext,
|
|
40
|
+
input: unknown,
|
|
41
|
+
): Promise<string> => {
|
|
42
|
+
const options = this.validate(input);
|
|
43
|
+
if (!options.title) {
|
|
44
|
+
options.title = 'include';
|
|
45
|
+
}
|
|
46
|
+
if (!options.pageTitles) {
|
|
47
|
+
options.pageTitles = 'normal';
|
|
48
|
+
}
|
|
49
|
+
const card = await context.project.cardDetailsById(options.cardKey, {
|
|
50
|
+
content: true,
|
|
51
|
+
metadata: true,
|
|
52
|
+
});
|
|
53
|
+
if (!card) {
|
|
54
|
+
throw new Error(`Card key ${options.cardKey} not found`);
|
|
55
|
+
}
|
|
56
|
+
const newContext = {
|
|
57
|
+
...context,
|
|
58
|
+
cardKey: options.cardKey,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const anchor = this.generateAnchor(options);
|
|
62
|
+
const title = this.generateTitle(options, card.metadata?.title);
|
|
63
|
+
const cardContent = await this.generateCardContent(
|
|
64
|
+
options,
|
|
65
|
+
card.content,
|
|
66
|
+
newContext,
|
|
67
|
+
);
|
|
68
|
+
const content = `\n\n${anchor}${title}${cardContent}`;
|
|
69
|
+
|
|
70
|
+
let levelOffset = 0;
|
|
71
|
+
if (options.levelOffset) {
|
|
72
|
+
levelOffset = parseInt(options.levelOffset, 10);
|
|
73
|
+
if (isNaN(levelOffset)) {
|
|
74
|
+
throw new Error(`Invalid level offset: ${options.levelOffset}`);
|
|
75
|
+
}
|
|
76
|
+
levelOffset = Math.min(
|
|
77
|
+
Math.max(levelOffset, -MAX_LEVEL_OFFSET),
|
|
78
|
+
MAX_LEVEL_OFFSET,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const adjustedContent = this.adjustTitles(
|
|
83
|
+
content,
|
|
84
|
+
levelOffset,
|
|
85
|
+
options.pageTitles === 'discrete',
|
|
86
|
+
);
|
|
87
|
+
return adjustedContent;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
handleInject = async (context: MacroGenerationContext, input: unknown) => {
|
|
91
|
+
return this.handleStatic(context, input);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
private validate(input: unknown): IncludeMacroOptions {
|
|
95
|
+
return validateMacroContent<IncludeMacroOptions>(this.metadata, input);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private generateAnchor(options: IncludeMacroOptions): string {
|
|
99
|
+
return options.title !== 'exclude' && options.pageTitles === 'normal'
|
|
100
|
+
? `[[${options.cardKey}]]\n`
|
|
101
|
+
: '';
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private generateTitle(
|
|
105
|
+
options: IncludeMacroOptions,
|
|
106
|
+
cardTitle?: string,
|
|
107
|
+
): string {
|
|
108
|
+
if (options.title === 'only' || options.title === 'include') {
|
|
109
|
+
return `= ${cardTitle}\n\n`;
|
|
110
|
+
}
|
|
111
|
+
return '';
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private async generateCardContent(
|
|
115
|
+
options: IncludeMacroOptions,
|
|
116
|
+
cardContent: string | undefined,
|
|
117
|
+
context: MacroGenerationContext,
|
|
118
|
+
): Promise<string> {
|
|
119
|
+
if (options.title !== 'only') {
|
|
120
|
+
return await evaluateMacros(cardContent ?? '', context, true);
|
|
121
|
+
}
|
|
122
|
+
return '';
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Adjust asciidoc titles to match the level offset
|
|
126
|
+
private adjustTitles(
|
|
127
|
+
content: string,
|
|
128
|
+
levelOffset: number,
|
|
129
|
+
discrete: boolean,
|
|
130
|
+
) {
|
|
131
|
+
const lines = content.split('\n');
|
|
132
|
+
const adjustedLines = lines.map((line) => {
|
|
133
|
+
const match = line.match(/^(\s*)([=#]+)(.*?)\s*$/);
|
|
134
|
+
if (match) {
|
|
135
|
+
const currentLevel = match[2].length;
|
|
136
|
+
const newLevel = Math.min(
|
|
137
|
+
Math.max(1, currentLevel + levelOffset),
|
|
138
|
+
MAX_LEVEL_OFFSET + 1,
|
|
139
|
+
);
|
|
140
|
+
const equals = '='.repeat(newLevel);
|
|
141
|
+
return `${discrete ? '[discrete]\n' : ''}${match[1]}${equals} ${match[3].trim()}`;
|
|
142
|
+
}
|
|
143
|
+
return line;
|
|
144
|
+
});
|
|
145
|
+
return adjustedLines.join('\n');
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
|
|
14
|
+
import type { MacroMetadata } from '../../interfaces/macros.js';
|
|
15
|
+
|
|
16
|
+
const macroMetadata: MacroMetadata = {
|
|
17
|
+
name: 'include',
|
|
18
|
+
tagName: 'include',
|
|
19
|
+
schema: 'includeMacroSchema',
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export default macroMetadata;
|