@canonical/design-system 0.1.0
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/.mcp.json +8 -0
- package/README.md +438 -0
- package/biome.json +6 -0
- package/bun.lock +349 -0
- package/data/apps/component/file_tree.ttl +8 -0
- package/data/apps/component/key_value_pair.ttl +8 -0
- package/data/apps/component/navigation.ttl +8 -0
- package/data/apps/component/skeleton_loading.ttl +8 -0
- package/data/apps/component/status_bar.ttl +8 -0
- package/data/apps/component/toolbar.ttl +8 -0
- package/data/apps/layout/application_layout.ttl +8 -0
- package/data/apps/layout/empty_layout.ttl +8 -0
- package/data/apps/layout/list_page_layout.ttl +8 -0
- package/data/apps/layout/settings_page_layout.ttl +8 -0
- package/data/apps/pattern/code_diff.ttl +8 -0
- package/data/apps/pattern/data_table.ttl +8 -0
- package/data/apps/pattern/destructive_modal.ttl +8 -0
- package/data/apps/pattern/key_value_summary.ttl +8 -0
- package/data/apps/pattern/keyboard_shortcuts.ttl +8 -0
- package/data/apps/pattern/notification_center.ttl +8 -0
- package/data/apps/pattern/side_panel.ttl +8 -0
- package/data/apps/pattern/sidepanel_stacked.ttl +8 -0
- package/data/apps.ttl +5 -0
- package/data/apps_wpe.ttl +5 -0
- package/data/documentation/layout/documentation_layout.ttl +8 -0
- package/data/documentation.ttl +5 -0
- package/data/global/component/accordion.ttl +14 -0
- package/data/global/component/announcement.ttl +8 -0
- package/data/global/component/badge.ttl +9 -0
- package/data/global/component/button.ttl +20 -0
- package/data/global/component/card.ttl +8 -0
- package/data/global/component/chip.ttl +8 -0
- package/data/global/component/code_snippet.ttl +8 -0
- package/data/global/component/contextual_menu.ttl +8 -0
- package/data/global/component/heading.ttl +14 -0
- package/data/global/component/icon.ttl +14 -0
- package/data/global/component/icon_in_container.ttl +8 -0
- package/data/global/component/keyboard_key.ttl +8 -0
- package/data/global/component/label.ttl +15 -0
- package/data/global/component/link.ttl +8 -0
- package/data/global/component/notification_inline.ttl +8 -0
- package/data/global/component/notification_toast.ttl +8 -0
- package/data/global/component/pagination.ttl +8 -0
- package/data/global/component/popover.ttl +8 -0
- package/data/global/component/rich_text_editor.ttl +8 -0
- package/data/global/component/search_bar.ttl +8 -0
- package/data/global/component/table.ttl +8 -0
- package/data/global/component/tabs.ttl +8 -0
- package/data/global/component/theme_switcher.ttl +8 -0
- package/data/global/component/tile.ttl +14 -0
- package/data/global/component/tooltip.ttl +8 -0
- package/data/global/group/buttons.ttl +8 -0
- package/data/global/group/cards.ttl +8 -0
- package/data/global/group/tiles.ttl +8 -0
- package/data/global/layout/error_layout.ttl +8 -0
- package/data/global/layout/form_layout.ttl +8 -0
- package/data/global/layout/identity_page_form.ttl +8 -0
- package/data/global/modifier/alpha.ttl +6 -0
- package/data/global/modifier/beta.ttl +6 -0
- package/data/global/modifier/caution.ttl +6 -0
- package/data/global/modifier/comfortable.ttl +6 -0
- package/data/global/modifier/completed.ttl +6 -0
- package/data/global/modifier/constructive.ttl +6 -0
- package/data/global/modifier/dark.ttl +6 -0
- package/data/global/modifier/dense.ttl +6 -0
- package/data/global/modifier/destructive.ttl +6 -0
- package/data/global/modifier/editable.ttl +6 -0
- package/data/global/modifier/elevated.ttl +6 -0
- package/data/global/modifier/error.ttl +6 -0
- package/data/global/modifier/experimental.ttl +6 -0
- package/data/global/modifier/failed.ttl +6 -0
- package/data/global/modifier/focused.ttl +6 -0
- package/data/global/modifier/highlighted.ttl +6 -0
- package/data/global/modifier/in_progress.ttl +6 -0
- package/data/global/modifier/information.ttl +6 -0
- package/data/global/modifier/light.ttl +6 -0
- package/data/global/modifier/loading.ttl +6 -0
- package/data/global/modifier/muted.ttl +6 -0
- package/data/global/modifier/paper.ttl +6 -0
- package/data/global/modifier/planned.ttl +6 -0
- package/data/global/modifier/primary.ttl +6 -0
- package/data/global/modifier/secondary.ttl +6 -0
- package/data/global/modifier/stable.ttl +6 -0
- package/data/global/modifier/success.ttl +6 -0
- package/data/global/modifier/tertiary.ttl +6 -0
- package/data/global/modifier/warning.ttl +6 -0
- package/data/global/modifier/wireframe.ttl +6 -0
- package/data/global/modifier_family/_color_luminosity.ttl +7 -0
- package/data/global/modifier_family/_emphasis.ttl +7 -0
- package/data/global/modifier_family/_other.ttl +7 -0
- package/data/global/modifier_family/anticipation.ttl +7 -0
- package/data/global/modifier_family/criticality.ttl +7 -0
- package/data/global/modifier_family/density.ttl +7 -0
- package/data/global/modifier_family/importance.ttl +7 -0
- package/data/global/modifier_family/lifecycle.ttl +7 -0
- package/data/global/modifier_family/release.ttl +7 -0
- package/data/global/pattern/breadcrumbs.ttl +14 -0
- package/data/global/pattern/info_table.ttl +8 -0
- package/data/global/pattern/modal.ttl +8 -0
- package/data/global/pattern/navigation.ttl +8 -0
- package/data/global/pattern/select_button.ttl +8 -0
- package/data/global/pattern/stepper.ttl +8 -0
- package/data/global/pattern/table_of_contents.ttl +8 -0
- package/data/global/pattern/timeline.ttl +14 -0
- package/data/global/subcomponent/accordion-item.ttl +19 -0
- package/data/global/subcomponent/breadcrumbs-item.ttl +19 -0
- package/data/global/subcomponent/card-content.ttl +14 -0
- package/data/global/subcomponent/card-header.ttl +14 -0
- package/data/global/subcomponent/card-image.ttl +20 -0
- package/data/global/subcomponent/divider.ttl +8 -0
- package/data/global/subcomponent/modal-content.ttl +8 -0
- package/data/global/subcomponent/modal-footer.ttl +8 -0
- package/data/global/subcomponent/modal-header.ttl +8 -0
- package/data/global/subcomponent/spinner.ttl +8 -0
- package/data/global/subcomponent/table-cell.ttl +8 -0
- package/data/global/subcomponent/table-row.ttl +8 -0
- package/data/global/subcomponent/tile-content.ttl +14 -0
- package/data/global/subcomponent/tile-header.ttl +14 -0
- package/data/global/subcomponent/timeline-content.ttl +14 -0
- package/data/global/subcomponent/timeline-event.ttl +31 -0
- package/data/global/subcomponent/timeline-footer.ttl +8 -0
- package/data/global/subcomponent/timeline-header.ttl +8 -0
- package/data/global.ttl +5 -0
- package/data/global_form/component/checkbox_input.ttl +8 -0
- package/data/global_form/component/choices_input.ttl +8 -0
- package/data/global_form/component/combobox_input.ttl +8 -0
- package/data/global_form/component/custom_choices_input.ttl +8 -0
- package/data/global_form/component/datepicker_input.ttl +8 -0
- package/data/global_form/component/rating_input.ttl +8 -0
- package/data/global_form/component/segmented_control_input.ttl +8 -0
- package/data/global_form/component/slider_input.ttl +8 -0
- package/data/global_form/component/switch_input.ttl +8 -0
- package/data/global_form/component/text_input.ttl +8 -0
- package/data/global_form/component/textarea_input.ttl +8 -0
- package/data/global_form/component/wysiwginput.ttl +8 -0
- package/data/global_form/pattern/field.ttl +8 -0
- package/data/global_form/pattern/form.ttl +8 -0
- package/data/global_form/subcomponent/field-description.ttl +8 -0
- package/data/global_form/subcomponent/field-label.ttl +8 -0
- package/data/global_form.ttl +5 -0
- package/data/site/component/quote.ttl +8 -0
- package/data/site/component/rule.ttl +8 -0
- package/data/site/component/section.ttl +8 -0
- package/data/site/layout/site_layout.ttl +8 -0
- package/data/site/pattern/hero-github.ttl +8 -0
- package/data/site/pattern/hero.ttl +8 -0
- package/data/site/pattern/info_table.ttl +8 -0
- package/data/site/pattern/skip_link.ttl +8 -0
- package/data/site.ttl +5 -0
- package/data/stores.ttl +5 -0
- package/definitions/ontology.ttl +498 -0
- package/package.json +42 -0
- package/session/A.DSL.md +429 -0
- package/session/B.NOTES.md +84 -0
- package/session/global.component.accordion.yaml +13 -0
- package/session/global.component.button.yaml +26 -0
- package/session/global.component.card.yaml +24 -0
- package/session/global.component.icon.yaml +7 -0
- package/session/global.component.label.yaml +7 -0
- package/session/global.component.tile.yaml +21 -0
- package/session/global.component.timeline.yaml +20 -0
- package/session/global.pattern.breadcrumbs.yaml +13 -0
- package/session/global.subcomponent.accordion-item.yaml +37 -0
- package/session/global.subcomponent.breadcrumbs-item.yaml +21 -0
- package/session/global.subcomponent.card-content.yaml +7 -0
- package/session/global.subcomponent.card-header.yaml +22 -0
- package/session/global.subcomponent.card-image.yaml +7 -0
- package/session/global.subcomponent.tile-content.yaml +7 -0
- package/session/global.subcomponent.tile-header.yaml +8 -0
- package/session/global.subcomponent.timeline-content.yaml +13 -0
- package/session/global.subcomponent.timeline-event.yaml +20 -0
- package/session/global.subcomponent.timeline-footer.yaml +9 -0
- package/session/global.subcomponent.timeline-header.yaml +18 -0
- package/skills/anatomy-author/ANATOMY_DSL_SPEC.md +423 -0
- package/skills/anatomy-author/SKILL.md +911 -0
- package/skills/component-specifier/SKILL.md +413 -0
- package/skills/design-auditor/SKILL.md +428 -0
- package/source.json +101 -0
- package/src/cli.ts +45 -0
- package/src/collect/generateTurtle.tests.ts +200 -0
- package/src/collect/generateTurtle.ts +187 -0
- package/src/collect/index.ts +4 -0
- package/src/collect/scanAnnotations.tests.ts +138 -0
- package/src/collect/scanAnnotations.ts +78 -0
- package/src/collect/types.ts +57 -0
- package/src/collect/validateCollectConfig.tests.ts +116 -0
- package/src/collect/validateCollectConfig.ts +74 -0
- package/src/collect-implementations.ts +99 -0
- package/src/commands/extract.tests.ts +41 -0
- package/src/commands/extract.ts +48 -0
- package/src/commands/index.ts +3 -0
- package/src/commands/list.tests.ts +26 -0
- package/src/commands/list.ts +26 -0
- package/src/commands/transform.tests.ts +28 -0
- package/src/commands/transform.ts +133 -0
- package/src/config/index.ts +2 -0
- package/src/config/source.schema.json +110 -0
- package/src/config/types.ts +97 -0
- package/src/config/validateConfig.tests.ts +46 -0
- package/src/config/validateConfig.ts +21 -0
- package/src/constants.ts +20 -0
- package/src/graph/GraphStore.tests.ts +157 -0
- package/src/graph/GraphStore.ts +200 -0
- package/src/graph/PrefixMap.tests.ts +129 -0
- package/src/graph/PrefixMap.ts +95 -0
- package/src/graph/buildUri.tests.ts +158 -0
- package/src/graph/buildUri.ts +69 -0
- package/src/graph/index.ts +4 -0
- package/src/graph/types.ts +20 -0
- package/src/providers/CodaProvider.tests.ts +241 -0
- package/src/providers/CodaProvider.ts +174 -0
- package/src/providers/CodaProvider.types.generated.ts +12236 -0
- package/src/providers/index.ts +18 -0
- package/src/serializers/JSONSerializer.tests.ts +79 -0
- package/src/serializers/JSONSerializer.ts +87 -0
- package/src/serializers/TTLSerializer.tests.ts +75 -0
- package/src/serializers/TTLSerializer.ts +99 -0
- package/src/serializers/index.ts +3 -0
- package/src/serializers/types.ts +16 -0
- package/src/transform/resolveReferences.tests.ts +63 -0
- package/src/transform/resolveReferences.ts +36 -0
- package/src/transform/resolveValue.tests.ts +76 -0
- package/src/transform/resolveValue.ts +87 -0
- package/src/transform/transform.tests.ts +99 -0
- package/src/transform/transform.ts +109 -0
- package/src/transform/transformRow.tests.ts +269 -0
- package/src/transform/transformRow.ts +220 -0
- package/src/transform/types.ts +26 -0
- package/tsconfig.json +8 -0
- package/vite.config.ts +11 -0
- package/vitest.config.ts +21 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { default as CodaProvider } from "./CodaProvider.js";
|
|
2
|
+
export type {
|
|
3
|
+
TableMetadata,
|
|
4
|
+
ColumnMetadata,
|
|
5
|
+
TableRow,
|
|
6
|
+
// Generated Coda API types
|
|
7
|
+
CodaTable,
|
|
8
|
+
CodaTableReference,
|
|
9
|
+
CodaTableList,
|
|
10
|
+
CodaRow,
|
|
11
|
+
CodaRowList,
|
|
12
|
+
CodaColumn,
|
|
13
|
+
CodaColumnList,
|
|
14
|
+
CodaCellValue,
|
|
15
|
+
} from "./CodaProvider.js";
|
|
16
|
+
|
|
17
|
+
// Re-export full generated types for advanced usage
|
|
18
|
+
export type * from "./CodaProvider.types.generated.js";
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { NAMESPACES, PREDICATES } from "../constants.js";
|
|
3
|
+
import { GraphStore, PrefixMap } from "../graph/index.js";
|
|
4
|
+
import serializeToJsonLd from "./JSONSerializer.js";
|
|
5
|
+
|
|
6
|
+
// Use realistic dot-separated URIs like the actual system
|
|
7
|
+
const buttonUri = `${NAMESPACES.ds}global.component.button`;
|
|
8
|
+
const inputUri = `${NAMESPACES.ds}global.component.input`;
|
|
9
|
+
const componentType = `${NAMESPACES.dso}Component`;
|
|
10
|
+
|
|
11
|
+
describe("serializeToJsonLd", () => {
|
|
12
|
+
it("should serialize a single subject to JSON-LD with compact URIs", async () => {
|
|
13
|
+
const store = new GraphStore();
|
|
14
|
+
store.addQuad(buttonUri, PREDICATES.type, componentType);
|
|
15
|
+
store.addLiteral(buttonUri, PREDICATES.name, "Button");
|
|
16
|
+
|
|
17
|
+
const prefixes = new PrefixMap();
|
|
18
|
+
prefixes.add("ds", NAMESPACES.ds);
|
|
19
|
+
prefixes.add("dso", NAMESPACES.dso);
|
|
20
|
+
|
|
21
|
+
const doc = await serializeToJsonLd(store, {
|
|
22
|
+
prefixes,
|
|
23
|
+
subject: buttonUri,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// JSON-LD compact uses prefixed URIs
|
|
27
|
+
expect(doc["@id"]).toBe("ds:global.component.button");
|
|
28
|
+
// @type can be a string or array in JSON-LD
|
|
29
|
+
const type = doc["@type"];
|
|
30
|
+
expect(Array.isArray(type) ? type[0] : type).toBe("dso:Component");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should include context from prefixes", async () => {
|
|
34
|
+
const store = new GraphStore();
|
|
35
|
+
store.addLiteral(buttonUri, PREDICATES.name, "Button");
|
|
36
|
+
|
|
37
|
+
const prefixes = new PrefixMap();
|
|
38
|
+
prefixes.add("ds", NAMESPACES.ds);
|
|
39
|
+
prefixes.add("dso", NAMESPACES.dso);
|
|
40
|
+
|
|
41
|
+
const doc = await serializeToJsonLd(store, {
|
|
42
|
+
prefixes,
|
|
43
|
+
subject: buttonUri,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
expect(doc["@context"]).toBeDefined();
|
|
47
|
+
expect((doc["@context"] as Record<string, string>)["ds"]).toBe(NAMESPACES.ds);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("should return empty object for unknown subject", async () => {
|
|
51
|
+
const store = new GraphStore();
|
|
52
|
+
store.addLiteral(buttonUri, PREDICATES.name, "Button");
|
|
53
|
+
|
|
54
|
+
const prefixes = new PrefixMap();
|
|
55
|
+
const unknownUri = `${NAMESPACES.ds}global.component.unknown`;
|
|
56
|
+
|
|
57
|
+
const doc = await serializeToJsonLd(store, {
|
|
58
|
+
prefixes,
|
|
59
|
+
subject: unknownUri,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Unknown subject returns full URI since no data to compact
|
|
63
|
+
expect(doc["@id"]).toBe(unknownUri);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("should serialize all subjects when no subject specified", async () => {
|
|
67
|
+
const store = new GraphStore();
|
|
68
|
+
store.addLiteral(buttonUri, PREDICATES.name, "Button");
|
|
69
|
+
store.addLiteral(inputUri, PREDICATES.name, "Input");
|
|
70
|
+
|
|
71
|
+
const prefixes = new PrefixMap();
|
|
72
|
+
prefixes.add("ds", NAMESPACES.ds);
|
|
73
|
+
prefixes.add("dso", NAMESPACES.dso);
|
|
74
|
+
|
|
75
|
+
const docs = await serializeToJsonLd(store, { prefixes });
|
|
76
|
+
|
|
77
|
+
expect(Array.isArray(docs) || docs["@graph"]).toBeTruthy();
|
|
78
|
+
});
|
|
79
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import jsonld from "jsonld";
|
|
2
|
+
import { Writer } from "n3";
|
|
3
|
+
import type { GraphStore, PrefixMap } from "../graph/index.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Options for JSON-LD serialization
|
|
7
|
+
*/
|
|
8
|
+
export interface JsonLdSerializeOptions {
|
|
9
|
+
prefixes: PrefixMap;
|
|
10
|
+
subject?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Serialize a GraphStore to JSON-LD format
|
|
15
|
+
*
|
|
16
|
+
* If subject is provided, returns a single document for that subject.
|
|
17
|
+
* Otherwise, returns all subjects as a graph.
|
|
18
|
+
*/
|
|
19
|
+
export default async function serializeToJsonLd(
|
|
20
|
+
store: GraphStore,
|
|
21
|
+
options: JsonLdSerializeOptions,
|
|
22
|
+
): Promise<Record<string, unknown>> {
|
|
23
|
+
const quads = options.subject
|
|
24
|
+
? store.getQuadsForSubject(options.subject)
|
|
25
|
+
: store.getQuads();
|
|
26
|
+
|
|
27
|
+
// Convert quads to N-Quads string format for jsonld.fromRDF
|
|
28
|
+
const nquads = await quadsToNQuads(quads);
|
|
29
|
+
|
|
30
|
+
if (!nquads.trim()) {
|
|
31
|
+
// No quads found, return minimal document
|
|
32
|
+
const context = options.prefixes.toRecord();
|
|
33
|
+
return {
|
|
34
|
+
"@context": context,
|
|
35
|
+
"@id": options.subject || "",
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Convert N-Quads to JSON-LD
|
|
40
|
+
const jsonldDoc = await jsonld.fromRDF(nquads, {
|
|
41
|
+
format: "application/n-quads",
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Build context from prefixes
|
|
45
|
+
const context = options.prefixes.toRecord();
|
|
46
|
+
|
|
47
|
+
// Frame the document to embed blank nodes inline
|
|
48
|
+
if (options.subject) {
|
|
49
|
+
const framed = await jsonld.frame(jsonldDoc, {
|
|
50
|
+
"@context": context,
|
|
51
|
+
"@id": options.subject,
|
|
52
|
+
"@embed": "@always",
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
// Compact to use prefixed URIs
|
|
56
|
+
const compacted = await jsonld.compact(framed, context);
|
|
57
|
+
return compacted as Record<string, unknown>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Compact the full document
|
|
61
|
+
const compacted = await jsonld.compact(
|
|
62
|
+
{ "@context": context, "@graph": jsonldDoc },
|
|
63
|
+
context,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
return compacted as Record<string, unknown>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Convert n3 quads to N-Quads string format
|
|
71
|
+
*/
|
|
72
|
+
function quadsToNQuads(
|
|
73
|
+
quads: ReturnType<GraphStore["getQuads"]>,
|
|
74
|
+
): Promise<string> {
|
|
75
|
+
return new Promise((resolve, reject) => {
|
|
76
|
+
const writer = new Writer({ format: "N-Quads" });
|
|
77
|
+
|
|
78
|
+
for (const quad of quads) {
|
|
79
|
+
writer.addQuad(quad);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
writer.end((error, result) => {
|
|
83
|
+
if (error) reject(error);
|
|
84
|
+
else resolve(result);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { NAMESPACES, PREDICATES } from "../constants.js";
|
|
3
|
+
import { GraphStore, PrefixMap } from "../graph/index.js";
|
|
4
|
+
import serializeToTurtle from "./TTLSerializer.js";
|
|
5
|
+
|
|
6
|
+
const buttonUri = `${NAMESPACES.ds}button`;
|
|
7
|
+
const globalUri = `${NAMESPACES.ds}global`;
|
|
8
|
+
const componentType = `${NAMESPACES.dso}Component`;
|
|
9
|
+
|
|
10
|
+
function createPrefixMap(): PrefixMap {
|
|
11
|
+
const prefixes = new PrefixMap();
|
|
12
|
+
prefixes.add("ds", NAMESPACES.ds);
|
|
13
|
+
prefixes.add("dso", NAMESPACES.dso);
|
|
14
|
+
prefixes.add("rdf", NAMESPACES.rdf);
|
|
15
|
+
return prefixes;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe("serializeToTurtle", () => {
|
|
19
|
+
it("should serialize empty store", async () => {
|
|
20
|
+
const store = new GraphStore();
|
|
21
|
+
const prefixes = new PrefixMap();
|
|
22
|
+
|
|
23
|
+
const ttl = await serializeToTurtle(store, { prefixes });
|
|
24
|
+
|
|
25
|
+
expect(ttl).toBe("");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("should include prefix declarations", async () => {
|
|
29
|
+
const store = new GraphStore();
|
|
30
|
+
store.addQuad(buttonUri, PREDICATES.type, componentType);
|
|
31
|
+
|
|
32
|
+
const prefixes = createPrefixMap();
|
|
33
|
+
const ttl = await serializeToTurtle(store, { prefixes });
|
|
34
|
+
|
|
35
|
+
expect(ttl).toContain("@prefix ds:");
|
|
36
|
+
expect(ttl).toContain("@prefix dso:");
|
|
37
|
+
expect(ttl).toContain("@prefix rdf:");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("should serialize quads with URI objects", async () => {
|
|
41
|
+
const store = new GraphStore();
|
|
42
|
+
store.addQuad(buttonUri, PREDICATES.type, componentType);
|
|
43
|
+
store.addQuad(buttonUri, PREDICATES.tier, globalUri);
|
|
44
|
+
|
|
45
|
+
const prefixes = createPrefixMap();
|
|
46
|
+
const ttl = await serializeToTurtle(store, { prefixes });
|
|
47
|
+
|
|
48
|
+
expect(ttl).toContain("ds:button");
|
|
49
|
+
// n3 uses 'a' shorthand for rdf:type in Turtle
|
|
50
|
+
expect(ttl).toMatch(/a |rdf:type/);
|
|
51
|
+
expect(ttl).toContain("dso:Component");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should serialize quads with literal objects", async () => {
|
|
55
|
+
const store = new GraphStore();
|
|
56
|
+
store.addLiteral(buttonUri, PREDICATES.name, "Button");
|
|
57
|
+
|
|
58
|
+
const prefixes = createPrefixMap();
|
|
59
|
+
const ttl = await serializeToTurtle(store, { prefixes });
|
|
60
|
+
|
|
61
|
+
expect(ttl).toContain('"Button"');
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should group statements by subject with semicolons", async () => {
|
|
65
|
+
const store = new GraphStore();
|
|
66
|
+
store.addQuad(buttonUri, PREDICATES.type, componentType);
|
|
67
|
+
store.addLiteral(buttonUri, PREDICATES.name, "Button");
|
|
68
|
+
|
|
69
|
+
const prefixes = createPrefixMap();
|
|
70
|
+
const ttl = await serializeToTurtle(store, { prefixes });
|
|
71
|
+
|
|
72
|
+
// n3 Writer groups by subject and uses semicolons
|
|
73
|
+
expect(ttl).toContain(";");
|
|
74
|
+
});
|
|
75
|
+
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { Writer } from "n3";
|
|
2
|
+
import type { GraphStore } from "../graph/index.js";
|
|
3
|
+
import type { SerializeOptions } from "./types.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Compact full URIs to prefixed form in Turtle output
|
|
7
|
+
* Handles URIs with slashes that n3 won't auto-compact
|
|
8
|
+
*/
|
|
9
|
+
function compactUris(turtle: string, prefixes: Record<string, string>): string {
|
|
10
|
+
let result = turtle;
|
|
11
|
+
for (const [prefix, namespace] of Object.entries(prefixes)) {
|
|
12
|
+
// Replace <namespace...> with prefix:...
|
|
13
|
+
const regex = new RegExp(`<${escapeRegex(namespace)}([^>]+)>`, "g");
|
|
14
|
+
result = result.replace(regex, `${prefix}:$1`);
|
|
15
|
+
}
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function escapeRegex(str: string): string {
|
|
20
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Convert named blank nodes to inline anonymous syntax
|
|
25
|
+
* Transforms: `dso:hasProperty _:n3-0.` + `_:n3-0 a dso:Property; ...`
|
|
26
|
+
* Into: `dso:hasProperty [ a dso:Property; ... ]`
|
|
27
|
+
*/
|
|
28
|
+
function inlineBlankNodes(turtle: string): string {
|
|
29
|
+
// Find all blank node definitions (blocks starting with _:... and ending with .)
|
|
30
|
+
// Pattern matches: _:id followed by content until a line ending with just "."
|
|
31
|
+
const bnPattern = /^(_:[\w-]+)\s+([\s\S]*?)\.\n/gm;
|
|
32
|
+
const blankNodes = new Map<string, string>();
|
|
33
|
+
|
|
34
|
+
let match;
|
|
35
|
+
while ((match = bnPattern.exec(turtle)) !== null) {
|
|
36
|
+
const bnId = match[1];
|
|
37
|
+
const content = match[2].trim();
|
|
38
|
+
// Format for inline: wrap in brackets with proper indentation
|
|
39
|
+
const indentedContent = content.replace(/\n\s+/g, "\n ");
|
|
40
|
+
blankNodes.set(bnId, `[\n ${indentedContent}\n ]`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (blankNodes.size === 0) {
|
|
44
|
+
return turtle;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let result = turtle;
|
|
48
|
+
|
|
49
|
+
// Replace references to blank nodes with inline syntax
|
|
50
|
+
for (const [bnId, inlineContent] of blankNodes) {
|
|
51
|
+
// Replace references like "_:n3-0" or "_:n3-0," or "_:n3-0."
|
|
52
|
+
result = result.replace(new RegExp(`${escapeRegex(bnId)}([,.;])`, "g"), `${inlineContent}$1`);
|
|
53
|
+
// Remove the standalone blank node definition block
|
|
54
|
+
result = result.replace(new RegExp(`^${escapeRegex(bnId)}\\s+[\\s\\S]*?\\.\n`, "gm"), "");
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return result.trim() + "\n";
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Serialize a GraphStore to Turtle format
|
|
62
|
+
*
|
|
63
|
+
* Uses n3 Writer for proper Turtle formatting with:
|
|
64
|
+
* - Prefix declarations
|
|
65
|
+
* - Grouped statements by subject with semicolons
|
|
66
|
+
* - Proper indentation
|
|
67
|
+
*/
|
|
68
|
+
export default function serializeToTurtle(
|
|
69
|
+
store: GraphStore,
|
|
70
|
+
options: SerializeOptions,
|
|
71
|
+
): Promise<string> {
|
|
72
|
+
return new Promise((resolve, reject) => {
|
|
73
|
+
const quads = store.getQuads();
|
|
74
|
+
|
|
75
|
+
if (quads.length === 0) {
|
|
76
|
+
resolve("");
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const writer = new Writer({
|
|
81
|
+
prefixes: options.prefixes.toRecord(),
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
for (const quad of quads) {
|
|
85
|
+
writer.addQuad(quad);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
writer.end((error, result) => {
|
|
89
|
+
if (error) {
|
|
90
|
+
reject(error);
|
|
91
|
+
} else {
|
|
92
|
+
// Post-process: compact URIs and inline blank nodes
|
|
93
|
+
let processed = compactUris(result, options.prefixes.toRecord());
|
|
94
|
+
processed = inlineBlankNodes(processed);
|
|
95
|
+
resolve(processed);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { GraphStore, PrefixMap } from "../graph/index.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Options for serialization
|
|
5
|
+
*/
|
|
6
|
+
export interface SerializeOptions {
|
|
7
|
+
prefixes: PrefixMap;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Serializer function type
|
|
12
|
+
*/
|
|
13
|
+
export type Serializer = (
|
|
14
|
+
store: GraphStore,
|
|
15
|
+
options: SerializeOptions,
|
|
16
|
+
) => Promise<string>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { NAMESPACES } from "../constants.js";
|
|
3
|
+
import resolveReferences from "./resolveReferences.js";
|
|
4
|
+
|
|
5
|
+
describe("resolveReferences", () => {
|
|
6
|
+
it("should build lookup map from rows by Coda ID", () => {
|
|
7
|
+
const rows = [
|
|
8
|
+
{ _codaId: "i-tier-1", Name: "Global", uri: `${NAMESPACES.ds}global` },
|
|
9
|
+
{ _codaId: "i-tier-2", Name: "Apps", uri: `${NAMESPACES.ds}apps` },
|
|
10
|
+
];
|
|
11
|
+
|
|
12
|
+
const map = resolveReferences(rows, {
|
|
13
|
+
uriTemplate: "{uri}",
|
|
14
|
+
keyColumn: "Name",
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
expect(map.get("i-tier-1")).toBe(`${NAMESPACES.ds}global`);
|
|
18
|
+
expect(map.get("i-tier-2")).toBe(`${NAMESPACES.ds}apps`);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("should build lookup map by lowercase name", () => {
|
|
22
|
+
const rows = [
|
|
23
|
+
{ _codaId: "i-tier-1", Name: "Global", uri: `${NAMESPACES.ds}global` },
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
const map = resolveReferences(rows, {
|
|
27
|
+
uriTemplate: "{uri}",
|
|
28
|
+
keyColumn: "Name",
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
expect(map.get("global")).toBe(`${NAMESPACES.ds}global`);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should use Name as default key column", () => {
|
|
35
|
+
const rows = [{ _codaId: "i-123", Name: "Test", uri: `${NAMESPACES.ds}test` }];
|
|
36
|
+
|
|
37
|
+
const map = resolveReferences(rows, { uriTemplate: "{uri}" });
|
|
38
|
+
|
|
39
|
+
expect(map.get("test")).toBe(`${NAMESPACES.ds}test`);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should handle custom key column", () => {
|
|
43
|
+
const rows = [{ _codaId: "i-1", name: "button", uri: `${NAMESPACES.ds}button` }];
|
|
44
|
+
|
|
45
|
+
const map = resolveReferences(rows, {
|
|
46
|
+
uriTemplate: "{uri}",
|
|
47
|
+
keyColumn: "name",
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
expect(map.get("button")).toBe(`${NAMESPACES.ds}button`);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should build URI from template", () => {
|
|
54
|
+
const rows = [{ _codaId: "i-1", Name: "Component" }];
|
|
55
|
+
|
|
56
|
+
const map = resolveReferences(rows, {
|
|
57
|
+
uriTemplate: `${NAMESPACES.dso}{Name}`,
|
|
58
|
+
keyColumn: "Name",
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
expect(map.get("component")).toBe(`${NAMESPACES.dso}component`);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Reference } from "../config/types.js";
|
|
2
|
+
import { buildUri } from "../graph/index.js";
|
|
3
|
+
import type { ReferenceMap } from "./types.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Build a reference map from table rows
|
|
7
|
+
*
|
|
8
|
+
* Maps both by:
|
|
9
|
+
* - Coda ID (e.g., "i-tier-1") for reference objects
|
|
10
|
+
* - Lowercase display name (e.g., "global") for string lookups
|
|
11
|
+
*/
|
|
12
|
+
export default function resolveReferences(
|
|
13
|
+
rows: Array<Record<string, unknown>>,
|
|
14
|
+
reference: Reference,
|
|
15
|
+
): ReferenceMap {
|
|
16
|
+
const refMap: ReferenceMap = new Map();
|
|
17
|
+
const keyColumn = reference.keyColumn || "Name";
|
|
18
|
+
|
|
19
|
+
for (const row of rows) {
|
|
20
|
+
const codaId = row._codaId as string;
|
|
21
|
+
const keyValue = row[keyColumn] as string;
|
|
22
|
+
const uriValue = buildUri(reference.uriTemplate, row);
|
|
23
|
+
|
|
24
|
+
// Map by Coda ID (for reference objects)
|
|
25
|
+
if (codaId) {
|
|
26
|
+
refMap.set(codaId, uriValue);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Map by display name lowercase (for string-based lookups)
|
|
30
|
+
if (keyValue) {
|
|
31
|
+
refMap.set(keyValue.toLowerCase(), uriValue);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return refMap;
|
|
36
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { NAMESPACES } from "../constants.js";
|
|
3
|
+
import resolveValue from "./resolveValue.js";
|
|
4
|
+
|
|
5
|
+
describe("resolveValue", () => {
|
|
6
|
+
const refMap = new Map([
|
|
7
|
+
["i-tier-1", `${NAMESPACES.ds}global`],
|
|
8
|
+
["global", `${NAMESPACES.ds}global`],
|
|
9
|
+
["i-tier-2", `${NAMESPACES.ds}apps`],
|
|
10
|
+
["apps", `${NAMESPACES.ds}apps`],
|
|
11
|
+
]);
|
|
12
|
+
|
|
13
|
+
it("should resolve Coda reference object by ID", () => {
|
|
14
|
+
const value = { id: "i-tier-1", name: "Global" };
|
|
15
|
+
|
|
16
|
+
const resolved = resolveValue(value, refMap);
|
|
17
|
+
|
|
18
|
+
expect(resolved).toBe(`${NAMESPACES.ds}global`);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("should resolve Coda reference object by name if ID not found", () => {
|
|
22
|
+
const value = { id: "unknown", name: "Global" };
|
|
23
|
+
|
|
24
|
+
const resolved = resolveValue(value, refMap);
|
|
25
|
+
|
|
26
|
+
expect(resolved).toBe(`${NAMESPACES.ds}global`);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("should resolve plain string by lowercase lookup", () => {
|
|
30
|
+
const value = "Global";
|
|
31
|
+
|
|
32
|
+
const resolved = resolveValue(value, refMap);
|
|
33
|
+
|
|
34
|
+
expect(resolved).toBe(`${NAMESPACES.ds}global`);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should return original string if not found", () => {
|
|
38
|
+
const value = "Unknown";
|
|
39
|
+
|
|
40
|
+
const resolved = resolveValue(value, refMap);
|
|
41
|
+
|
|
42
|
+
expect(resolved).toBe("Unknown");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("should resolve CSV string to array of URIs", () => {
|
|
46
|
+
const value = "Global, Apps";
|
|
47
|
+
|
|
48
|
+
const resolved = resolveValue(value, refMap);
|
|
49
|
+
|
|
50
|
+
expect(resolved).toEqual([`${NAMESPACES.ds}global`, `${NAMESPACES.ds}apps`]);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should resolve array of references", () => {
|
|
54
|
+
const value = [
|
|
55
|
+
{ id: "i-tier-1", name: "Global" },
|
|
56
|
+
{ id: "i-tier-2", name: "Apps" },
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
const resolved = resolveValue(value, refMap);
|
|
60
|
+
|
|
61
|
+
expect(resolved).toEqual([`${NAMESPACES.ds}global`, `${NAMESPACES.ds}apps`]);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("should return null/undefined unchanged", () => {
|
|
65
|
+
expect(resolveValue(null, refMap)).toBeNull();
|
|
66
|
+
expect(resolveValue(undefined, refMap)).toBeUndefined();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("should return numbers unchanged", () => {
|
|
70
|
+
expect(resolveValue(42, refMap)).toBe(42);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("should return booleans unchanged", () => {
|
|
74
|
+
expect(resolveValue(true, refMap)).toBe(true);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { ReferenceMap } from "./types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Coda reference object structure
|
|
5
|
+
*/
|
|
6
|
+
interface CodaReference {
|
|
7
|
+
id: string;
|
|
8
|
+
name: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Check if a value is a Coda reference object
|
|
13
|
+
*/
|
|
14
|
+
function isCodaReference(value: unknown): value is CodaReference {
|
|
15
|
+
return (
|
|
16
|
+
typeof value === "object" &&
|
|
17
|
+
value !== null &&
|
|
18
|
+
"id" in value &&
|
|
19
|
+
"name" in value
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Resolve a single string value against the reference map
|
|
25
|
+
*/
|
|
26
|
+
function resolveStringValue(value: string, refMap: ReferenceMap): string {
|
|
27
|
+
// Try exact match first
|
|
28
|
+
const exactMatch = refMap.get(value);
|
|
29
|
+
if (exactMatch) {
|
|
30
|
+
return exactMatch;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Try lowercase match
|
|
34
|
+
const lowerMatch = refMap.get(value.toLowerCase().trim());
|
|
35
|
+
if (lowerMatch) {
|
|
36
|
+
return lowerMatch;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Resolve a value that might be a reference
|
|
44
|
+
*
|
|
45
|
+
* Handles:
|
|
46
|
+
* - Coda reference objects ({ id, name })
|
|
47
|
+
* - Plain strings (looked up by lowercase)
|
|
48
|
+
* - CSV strings (split and resolved individually)
|
|
49
|
+
* - Arrays (each element resolved)
|
|
50
|
+
* - Primitives (returned unchanged)
|
|
51
|
+
*/
|
|
52
|
+
export default function resolveValue(
|
|
53
|
+
value: unknown,
|
|
54
|
+
refMap: ReferenceMap,
|
|
55
|
+
): unknown {
|
|
56
|
+
if (value === null || value === undefined) {
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (Array.isArray(value)) {
|
|
61
|
+
return value.map((v) => resolveValue(v, refMap));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (isCodaReference(value)) {
|
|
65
|
+
// Try by ID first, then by name
|
|
66
|
+
const byId = refMap.get(value.id);
|
|
67
|
+
if (byId) {
|
|
68
|
+
return byId;
|
|
69
|
+
}
|
|
70
|
+
return resolveStringValue(value.name, refMap);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (typeof value === "string" && value.length > 0) {
|
|
74
|
+
// Check if it's a CSV (multiple values)
|
|
75
|
+
if (value.includes(",")) {
|
|
76
|
+
const parts = value
|
|
77
|
+
.split(",")
|
|
78
|
+
.map((s) => s.trim())
|
|
79
|
+
.filter(Boolean);
|
|
80
|
+
return parts.map((part) => resolveStringValue(part, refMap));
|
|
81
|
+
}
|
|
82
|
+
return resolveStringValue(value, refMap);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Return primitives unchanged
|
|
86
|
+
return value;
|
|
87
|
+
}
|