@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.
Files changed (231) hide show
  1. package/.mcp.json +8 -0
  2. package/README.md +438 -0
  3. package/biome.json +6 -0
  4. package/bun.lock +349 -0
  5. package/data/apps/component/file_tree.ttl +8 -0
  6. package/data/apps/component/key_value_pair.ttl +8 -0
  7. package/data/apps/component/navigation.ttl +8 -0
  8. package/data/apps/component/skeleton_loading.ttl +8 -0
  9. package/data/apps/component/status_bar.ttl +8 -0
  10. package/data/apps/component/toolbar.ttl +8 -0
  11. package/data/apps/layout/application_layout.ttl +8 -0
  12. package/data/apps/layout/empty_layout.ttl +8 -0
  13. package/data/apps/layout/list_page_layout.ttl +8 -0
  14. package/data/apps/layout/settings_page_layout.ttl +8 -0
  15. package/data/apps/pattern/code_diff.ttl +8 -0
  16. package/data/apps/pattern/data_table.ttl +8 -0
  17. package/data/apps/pattern/destructive_modal.ttl +8 -0
  18. package/data/apps/pattern/key_value_summary.ttl +8 -0
  19. package/data/apps/pattern/keyboard_shortcuts.ttl +8 -0
  20. package/data/apps/pattern/notification_center.ttl +8 -0
  21. package/data/apps/pattern/side_panel.ttl +8 -0
  22. package/data/apps/pattern/sidepanel_stacked.ttl +8 -0
  23. package/data/apps.ttl +5 -0
  24. package/data/apps_wpe.ttl +5 -0
  25. package/data/documentation/layout/documentation_layout.ttl +8 -0
  26. package/data/documentation.ttl +5 -0
  27. package/data/global/component/accordion.ttl +14 -0
  28. package/data/global/component/announcement.ttl +8 -0
  29. package/data/global/component/badge.ttl +9 -0
  30. package/data/global/component/button.ttl +20 -0
  31. package/data/global/component/card.ttl +8 -0
  32. package/data/global/component/chip.ttl +8 -0
  33. package/data/global/component/code_snippet.ttl +8 -0
  34. package/data/global/component/contextual_menu.ttl +8 -0
  35. package/data/global/component/heading.ttl +14 -0
  36. package/data/global/component/icon.ttl +14 -0
  37. package/data/global/component/icon_in_container.ttl +8 -0
  38. package/data/global/component/keyboard_key.ttl +8 -0
  39. package/data/global/component/label.ttl +15 -0
  40. package/data/global/component/link.ttl +8 -0
  41. package/data/global/component/notification_inline.ttl +8 -0
  42. package/data/global/component/notification_toast.ttl +8 -0
  43. package/data/global/component/pagination.ttl +8 -0
  44. package/data/global/component/popover.ttl +8 -0
  45. package/data/global/component/rich_text_editor.ttl +8 -0
  46. package/data/global/component/search_bar.ttl +8 -0
  47. package/data/global/component/table.ttl +8 -0
  48. package/data/global/component/tabs.ttl +8 -0
  49. package/data/global/component/theme_switcher.ttl +8 -0
  50. package/data/global/component/tile.ttl +14 -0
  51. package/data/global/component/tooltip.ttl +8 -0
  52. package/data/global/group/buttons.ttl +8 -0
  53. package/data/global/group/cards.ttl +8 -0
  54. package/data/global/group/tiles.ttl +8 -0
  55. package/data/global/layout/error_layout.ttl +8 -0
  56. package/data/global/layout/form_layout.ttl +8 -0
  57. package/data/global/layout/identity_page_form.ttl +8 -0
  58. package/data/global/modifier/alpha.ttl +6 -0
  59. package/data/global/modifier/beta.ttl +6 -0
  60. package/data/global/modifier/caution.ttl +6 -0
  61. package/data/global/modifier/comfortable.ttl +6 -0
  62. package/data/global/modifier/completed.ttl +6 -0
  63. package/data/global/modifier/constructive.ttl +6 -0
  64. package/data/global/modifier/dark.ttl +6 -0
  65. package/data/global/modifier/dense.ttl +6 -0
  66. package/data/global/modifier/destructive.ttl +6 -0
  67. package/data/global/modifier/editable.ttl +6 -0
  68. package/data/global/modifier/elevated.ttl +6 -0
  69. package/data/global/modifier/error.ttl +6 -0
  70. package/data/global/modifier/experimental.ttl +6 -0
  71. package/data/global/modifier/failed.ttl +6 -0
  72. package/data/global/modifier/focused.ttl +6 -0
  73. package/data/global/modifier/highlighted.ttl +6 -0
  74. package/data/global/modifier/in_progress.ttl +6 -0
  75. package/data/global/modifier/information.ttl +6 -0
  76. package/data/global/modifier/light.ttl +6 -0
  77. package/data/global/modifier/loading.ttl +6 -0
  78. package/data/global/modifier/muted.ttl +6 -0
  79. package/data/global/modifier/paper.ttl +6 -0
  80. package/data/global/modifier/planned.ttl +6 -0
  81. package/data/global/modifier/primary.ttl +6 -0
  82. package/data/global/modifier/secondary.ttl +6 -0
  83. package/data/global/modifier/stable.ttl +6 -0
  84. package/data/global/modifier/success.ttl +6 -0
  85. package/data/global/modifier/tertiary.ttl +6 -0
  86. package/data/global/modifier/warning.ttl +6 -0
  87. package/data/global/modifier/wireframe.ttl +6 -0
  88. package/data/global/modifier_family/_color_luminosity.ttl +7 -0
  89. package/data/global/modifier_family/_emphasis.ttl +7 -0
  90. package/data/global/modifier_family/_other.ttl +7 -0
  91. package/data/global/modifier_family/anticipation.ttl +7 -0
  92. package/data/global/modifier_family/criticality.ttl +7 -0
  93. package/data/global/modifier_family/density.ttl +7 -0
  94. package/data/global/modifier_family/importance.ttl +7 -0
  95. package/data/global/modifier_family/lifecycle.ttl +7 -0
  96. package/data/global/modifier_family/release.ttl +7 -0
  97. package/data/global/pattern/breadcrumbs.ttl +14 -0
  98. package/data/global/pattern/info_table.ttl +8 -0
  99. package/data/global/pattern/modal.ttl +8 -0
  100. package/data/global/pattern/navigation.ttl +8 -0
  101. package/data/global/pattern/select_button.ttl +8 -0
  102. package/data/global/pattern/stepper.ttl +8 -0
  103. package/data/global/pattern/table_of_contents.ttl +8 -0
  104. package/data/global/pattern/timeline.ttl +14 -0
  105. package/data/global/subcomponent/accordion-item.ttl +19 -0
  106. package/data/global/subcomponent/breadcrumbs-item.ttl +19 -0
  107. package/data/global/subcomponent/card-content.ttl +14 -0
  108. package/data/global/subcomponent/card-header.ttl +14 -0
  109. package/data/global/subcomponent/card-image.ttl +20 -0
  110. package/data/global/subcomponent/divider.ttl +8 -0
  111. package/data/global/subcomponent/modal-content.ttl +8 -0
  112. package/data/global/subcomponent/modal-footer.ttl +8 -0
  113. package/data/global/subcomponent/modal-header.ttl +8 -0
  114. package/data/global/subcomponent/spinner.ttl +8 -0
  115. package/data/global/subcomponent/table-cell.ttl +8 -0
  116. package/data/global/subcomponent/table-row.ttl +8 -0
  117. package/data/global/subcomponent/tile-content.ttl +14 -0
  118. package/data/global/subcomponent/tile-header.ttl +14 -0
  119. package/data/global/subcomponent/timeline-content.ttl +14 -0
  120. package/data/global/subcomponent/timeline-event.ttl +31 -0
  121. package/data/global/subcomponent/timeline-footer.ttl +8 -0
  122. package/data/global/subcomponent/timeline-header.ttl +8 -0
  123. package/data/global.ttl +5 -0
  124. package/data/global_form/component/checkbox_input.ttl +8 -0
  125. package/data/global_form/component/choices_input.ttl +8 -0
  126. package/data/global_form/component/combobox_input.ttl +8 -0
  127. package/data/global_form/component/custom_choices_input.ttl +8 -0
  128. package/data/global_form/component/datepicker_input.ttl +8 -0
  129. package/data/global_form/component/rating_input.ttl +8 -0
  130. package/data/global_form/component/segmented_control_input.ttl +8 -0
  131. package/data/global_form/component/slider_input.ttl +8 -0
  132. package/data/global_form/component/switch_input.ttl +8 -0
  133. package/data/global_form/component/text_input.ttl +8 -0
  134. package/data/global_form/component/textarea_input.ttl +8 -0
  135. package/data/global_form/component/wysiwginput.ttl +8 -0
  136. package/data/global_form/pattern/field.ttl +8 -0
  137. package/data/global_form/pattern/form.ttl +8 -0
  138. package/data/global_form/subcomponent/field-description.ttl +8 -0
  139. package/data/global_form/subcomponent/field-label.ttl +8 -0
  140. package/data/global_form.ttl +5 -0
  141. package/data/site/component/quote.ttl +8 -0
  142. package/data/site/component/rule.ttl +8 -0
  143. package/data/site/component/section.ttl +8 -0
  144. package/data/site/layout/site_layout.ttl +8 -0
  145. package/data/site/pattern/hero-github.ttl +8 -0
  146. package/data/site/pattern/hero.ttl +8 -0
  147. package/data/site/pattern/info_table.ttl +8 -0
  148. package/data/site/pattern/skip_link.ttl +8 -0
  149. package/data/site.ttl +5 -0
  150. package/data/stores.ttl +5 -0
  151. package/definitions/ontology.ttl +498 -0
  152. package/package.json +42 -0
  153. package/session/A.DSL.md +429 -0
  154. package/session/B.NOTES.md +84 -0
  155. package/session/global.component.accordion.yaml +13 -0
  156. package/session/global.component.button.yaml +26 -0
  157. package/session/global.component.card.yaml +24 -0
  158. package/session/global.component.icon.yaml +7 -0
  159. package/session/global.component.label.yaml +7 -0
  160. package/session/global.component.tile.yaml +21 -0
  161. package/session/global.component.timeline.yaml +20 -0
  162. package/session/global.pattern.breadcrumbs.yaml +13 -0
  163. package/session/global.subcomponent.accordion-item.yaml +37 -0
  164. package/session/global.subcomponent.breadcrumbs-item.yaml +21 -0
  165. package/session/global.subcomponent.card-content.yaml +7 -0
  166. package/session/global.subcomponent.card-header.yaml +22 -0
  167. package/session/global.subcomponent.card-image.yaml +7 -0
  168. package/session/global.subcomponent.tile-content.yaml +7 -0
  169. package/session/global.subcomponent.tile-header.yaml +8 -0
  170. package/session/global.subcomponent.timeline-content.yaml +13 -0
  171. package/session/global.subcomponent.timeline-event.yaml +20 -0
  172. package/session/global.subcomponent.timeline-footer.yaml +9 -0
  173. package/session/global.subcomponent.timeline-header.yaml +18 -0
  174. package/skills/anatomy-author/ANATOMY_DSL_SPEC.md +423 -0
  175. package/skills/anatomy-author/SKILL.md +911 -0
  176. package/skills/component-specifier/SKILL.md +413 -0
  177. package/skills/design-auditor/SKILL.md +428 -0
  178. package/source.json +101 -0
  179. package/src/cli.ts +45 -0
  180. package/src/collect/generateTurtle.tests.ts +200 -0
  181. package/src/collect/generateTurtle.ts +187 -0
  182. package/src/collect/index.ts +4 -0
  183. package/src/collect/scanAnnotations.tests.ts +138 -0
  184. package/src/collect/scanAnnotations.ts +78 -0
  185. package/src/collect/types.ts +57 -0
  186. package/src/collect/validateCollectConfig.tests.ts +116 -0
  187. package/src/collect/validateCollectConfig.ts +74 -0
  188. package/src/collect-implementations.ts +99 -0
  189. package/src/commands/extract.tests.ts +41 -0
  190. package/src/commands/extract.ts +48 -0
  191. package/src/commands/index.ts +3 -0
  192. package/src/commands/list.tests.ts +26 -0
  193. package/src/commands/list.ts +26 -0
  194. package/src/commands/transform.tests.ts +28 -0
  195. package/src/commands/transform.ts +133 -0
  196. package/src/config/index.ts +2 -0
  197. package/src/config/source.schema.json +110 -0
  198. package/src/config/types.ts +97 -0
  199. package/src/config/validateConfig.tests.ts +46 -0
  200. package/src/config/validateConfig.ts +21 -0
  201. package/src/constants.ts +20 -0
  202. package/src/graph/GraphStore.tests.ts +157 -0
  203. package/src/graph/GraphStore.ts +200 -0
  204. package/src/graph/PrefixMap.tests.ts +129 -0
  205. package/src/graph/PrefixMap.ts +95 -0
  206. package/src/graph/buildUri.tests.ts +158 -0
  207. package/src/graph/buildUri.ts +69 -0
  208. package/src/graph/index.ts +4 -0
  209. package/src/graph/types.ts +20 -0
  210. package/src/providers/CodaProvider.tests.ts +241 -0
  211. package/src/providers/CodaProvider.ts +174 -0
  212. package/src/providers/CodaProvider.types.generated.ts +12236 -0
  213. package/src/providers/index.ts +18 -0
  214. package/src/serializers/JSONSerializer.tests.ts +79 -0
  215. package/src/serializers/JSONSerializer.ts +87 -0
  216. package/src/serializers/TTLSerializer.tests.ts +75 -0
  217. package/src/serializers/TTLSerializer.ts +99 -0
  218. package/src/serializers/index.ts +3 -0
  219. package/src/serializers/types.ts +16 -0
  220. package/src/transform/resolveReferences.tests.ts +63 -0
  221. package/src/transform/resolveReferences.ts +36 -0
  222. package/src/transform/resolveValue.tests.ts +76 -0
  223. package/src/transform/resolveValue.ts +87 -0
  224. package/src/transform/transform.tests.ts +99 -0
  225. package/src/transform/transform.ts +109 -0
  226. package/src/transform/transformRow.tests.ts +269 -0
  227. package/src/transform/transformRow.ts +220 -0
  228. package/src/transform/types.ts +26 -0
  229. package/tsconfig.json +8 -0
  230. package/vite.config.ts +11 -0
  231. package/vitest.config.ts +21 -0
@@ -0,0 +1,110 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "$id": "source.schema.json",
4
+ "title": "Design System Source Config",
5
+ "type": "object",
6
+ "required": ["document", "provider"],
7
+ "properties": {
8
+ "document": {
9
+ "type": "string",
10
+ "description": "Coda document ID"
11
+ },
12
+ "provider": {
13
+ "type": "string",
14
+ "enum": ["coda"],
15
+ "description": "Data provider (currently only 'coda' supported)"
16
+ },
17
+ "extract": {
18
+ "type": "object",
19
+ "required": ["output", "tables"],
20
+ "properties": {
21
+ "output": {
22
+ "type": "string",
23
+ "description": "Output path for extracted JSON"
24
+ },
25
+ "tables": {
26
+ "type": "object",
27
+ "additionalProperties": {
28
+ "type": "string"
29
+ },
30
+ "minProperties": 1,
31
+ "description": "Map of output name to table ID"
32
+ }
33
+ }
34
+ },
35
+ "transform": {
36
+ "type": "object",
37
+ "required": ["format", "outputDir", "atomicity", "tables"],
38
+ "properties": {
39
+ "format": {
40
+ "type": "string",
41
+ "enum": ["ttl", "json-ld"],
42
+ "description": "Output format"
43
+ },
44
+ "outputDir": {
45
+ "type": "string",
46
+ "description": "Output directory for transformed files"
47
+ },
48
+ "atomicity": {
49
+ "type": "string",
50
+ "enum": ["class", "instance"],
51
+ "description": "Output granularity: one file per class or per instance"
52
+ },
53
+ "cleanupExtract": {
54
+ "type": "boolean",
55
+ "default": false,
56
+ "description": "Delete extract output after transform"
57
+ },
58
+ "tables": {
59
+ "type": "object",
60
+ "additionalProperties": {
61
+ "$ref": "#/$defs/tableTransform"
62
+ },
63
+ "description": "Transform configuration per table"
64
+ },
65
+ "references": {
66
+ "type": "object",
67
+ "additionalProperties": {
68
+ "$ref": "#/$defs/reference"
69
+ },
70
+ "description": "Reference tables for resolving relations"
71
+ }
72
+ }
73
+ }
74
+ },
75
+ "$defs": {
76
+ "tableTransform": {
77
+ "type": "object",
78
+ "required": ["@context", "class", "uriTemplate"],
79
+ "properties": {
80
+ "@context": {
81
+ "type": "object",
82
+ "additionalProperties": true,
83
+ "description": "JSON-LD context mapping column names to predicates"
84
+ },
85
+ "class": {
86
+ "type": "string",
87
+ "description": "RDF class URI"
88
+ },
89
+ "uriTemplate": {
90
+ "type": "string",
91
+ "description": "URI template with {ColumnName} placeholders"
92
+ }
93
+ }
94
+ },
95
+ "reference": {
96
+ "type": "object",
97
+ "required": ["uriTemplate"],
98
+ "properties": {
99
+ "uriTemplate": {
100
+ "type": "string",
101
+ "description": "URI template for referenced instances"
102
+ },
103
+ "keyColumn": {
104
+ "type": "string",
105
+ "description": "Column to use as key (default: Name)"
106
+ }
107
+ }
108
+ }
109
+ }
110
+ }
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Supported data providers
3
+ */
4
+ export type Provider = "coda";
5
+
6
+ /**
7
+ * Output format for RDF serialization
8
+ */
9
+ export type Format = "ttl" | "json-ld";
10
+
11
+ /**
12
+ * Atomicity determines file output granularity
13
+ * - "class": One file per table/class
14
+ * - "instance": One file per row/instance
15
+ */
16
+ export type Atomicity = "class" | "instance";
17
+
18
+ /**
19
+ * Inline table configuration for embedding related data as blank nodes
20
+ */
21
+ export interface InlineConfig {
22
+ /** Name of the table in extractedData.tables to embed */
23
+ table: string;
24
+ /** Property mappings for the blank node (column → predicate) */
25
+ properties: Record<string, string>;
26
+ /** Optional: class type for the blank node */
27
+ class?: string;
28
+ }
29
+
30
+ /**
31
+ * JSON-LD context value - can be a string or object with @id, @type
32
+ */
33
+ export type ContextValue =
34
+ | string
35
+ | {
36
+ "@id": string;
37
+ "@type"?: string;
38
+ "@container"?: string;
39
+ /** Embed related data inline as blank nodes */
40
+ "@inline"?: InlineConfig;
41
+ };
42
+
43
+ /**
44
+ * JSON-LD @context - maps column names directly to predicates
45
+ */
46
+ export type JsonLdContext = {
47
+ "@vocab"?: string;
48
+ [columnName: string]: ContextValue | undefined;
49
+ };
50
+
51
+ /**
52
+ * Table transformation configuration
53
+ * Note: Column names in @context ARE the Coda column names
54
+ */
55
+ export interface TableTransform {
56
+ "@context": JsonLdContext;
57
+ class: string;
58
+ uriTemplate: string;
59
+ }
60
+
61
+ /**
62
+ * Reference configuration for cross-table lookups
63
+ */
64
+ export interface Reference {
65
+ uriTemplate: string;
66
+ keyColumn?: string;
67
+ }
68
+
69
+ /**
70
+ * Extract command configuration
71
+ */
72
+ export interface ExtractConfig {
73
+ output: string;
74
+ tables: Record<string, string>;
75
+ }
76
+
77
+ /**
78
+ * Transform command configuration
79
+ */
80
+ export interface TransformConfig {
81
+ format: Format;
82
+ outputDir: string;
83
+ atomicity: Atomicity;
84
+ cleanupExtract?: boolean;
85
+ tables: Record<string, TableTransform>;
86
+ references?: Record<string, Reference>;
87
+ }
88
+
89
+ /**
90
+ * Full source configuration
91
+ */
92
+ export interface Config {
93
+ document: string;
94
+ provider: Provider;
95
+ extract?: ExtractConfig;
96
+ transform?: TransformConfig;
97
+ }
@@ -0,0 +1,46 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import validateConfig from "./validateConfig.js";
3
+
4
+ describe("validateConfig", () => {
5
+ it("should return valid config when data is valid", () => {
6
+ const validConfig = {
7
+ document: "doc-123",
8
+ provider: "coda",
9
+ };
10
+
11
+ const result = validateConfig(validConfig);
12
+
13
+ expect(result).toEqual(validConfig);
14
+ });
15
+
16
+ it("should throw for missing required fields", () => {
17
+ const invalidConfig = {};
18
+
19
+ expect(() => validateConfig(invalidConfig)).toThrow("Invalid config");
20
+ });
21
+
22
+ it("should throw for unsupported provider", () => {
23
+ const invalidConfig = {
24
+ document: "doc-123",
25
+ provider: "notion",
26
+ };
27
+
28
+ expect(() => validateConfig(invalidConfig)).toThrow("Invalid config");
29
+ });
30
+
31
+ it("should throw for missing document", () => {
32
+ const invalidConfig = {
33
+ provider: "coda",
34
+ };
35
+
36
+ expect(() => validateConfig(invalidConfig)).toThrow("Invalid config");
37
+ });
38
+
39
+ it("should throw for missing provider", () => {
40
+ const invalidConfig = {
41
+ document: "doc-123",
42
+ };
43
+
44
+ expect(() => validateConfig(invalidConfig)).toThrow("Invalid config");
45
+ });
46
+ });
@@ -0,0 +1,21 @@
1
+ import Ajv from "ajv";
2
+ import type { ErrorObject } from "ajv";
3
+ import type { Config } from "./types.js";
4
+ import schema from "./source.schema.json" with { type: "json" };
5
+
6
+ const ajv = new Ajv.default({ allErrors: true });
7
+ const validateSchema = ajv.compile<Config>(schema);
8
+
9
+ /**
10
+ * Validate config data against the JSON Schema
11
+ * @throws Error if validation fails
12
+ */
13
+ export default function validateConfig(data: unknown): Config {
14
+ if (!validateSchema(data)) {
15
+ const errors = validateSchema.errors
16
+ ?.map((e: ErrorObject) => `${e.instancePath} ${e.message}`)
17
+ .join(", ");
18
+ throw new Error(`Invalid config: ${errors}`);
19
+ }
20
+ return data as Config;
21
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * RDF namespace URIs used throughout the application
3
+ */
4
+ export const NAMESPACES = {
5
+ ds: "https://ds.canonical.com/data/",
6
+ dso: "https://ds.canonical.com/ontology#",
7
+ rdf: "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
8
+ } as const;
9
+
10
+ /**
11
+ * Common RDF predicates
12
+ */
13
+ export const PREDICATES = {
14
+ type: `${NAMESPACES.rdf}type`,
15
+ name: `${NAMESPACES.dso}name`,
16
+ description: `${NAMESPACES.dso}description`,
17
+ tier: `${NAMESPACES.dso}tier`,
18
+ properties: `${NAMESPACES.dso}properties`,
19
+ hasModifierFamily: `${NAMESPACES.dso}hasModifierFamily`,
20
+ } as const;
@@ -0,0 +1,157 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import GraphStore from "./GraphStore.js";
3
+
4
+ describe("GraphStore", () => {
5
+ describe("addQuad", () => {
6
+ it("should add a quad with URI object", () => {
7
+ const store = new GraphStore();
8
+
9
+ store.addQuad("ds:button", "rdf:type", "dso:Component");
10
+
11
+ expect(store.size()).toBe(1);
12
+ });
13
+
14
+ it("should add a quad with literal object", () => {
15
+ const store = new GraphStore();
16
+
17
+ store.addLiteral("ds:button", "dso:name", "Button");
18
+
19
+ expect(store.size()).toBe(1);
20
+ });
21
+
22
+ it("should add multiple quads", () => {
23
+ const store = new GraphStore();
24
+
25
+ store.addQuad("ds:button", "rdf:type", "dso:Component");
26
+ store.addLiteral("ds:button", "dso:name", "Button");
27
+ store.addQuad("ds:button", "dso:tier", "ds:global");
28
+
29
+ expect(store.size()).toBe(3);
30
+ });
31
+ });
32
+
33
+ describe("getQuads", () => {
34
+ it("should return all quads", () => {
35
+ const store = new GraphStore();
36
+ store.addQuad("ds:button", "rdf:type", "dso:Component");
37
+ store.addLiteral("ds:button", "dso:name", "Button");
38
+
39
+ const quads = store.getQuads();
40
+
41
+ expect(quads).toHaveLength(2);
42
+ });
43
+
44
+ it("should return empty array for empty store", () => {
45
+ const store = new GraphStore();
46
+
47
+ const quads = store.getQuads();
48
+
49
+ expect(quads).toHaveLength(0);
50
+ });
51
+ });
52
+
53
+ describe("getQuadsForSubject", () => {
54
+ it("should return quads for a specific subject", () => {
55
+ const store = new GraphStore();
56
+ store.addQuad("ds:button", "rdf:type", "dso:Component");
57
+ store.addLiteral("ds:button", "dso:name", "Button");
58
+ store.addQuad("ds:input", "rdf:type", "dso:Component");
59
+
60
+ const quads = store.getQuadsForSubject("ds:button");
61
+
62
+ expect(quads).toHaveLength(2);
63
+ });
64
+
65
+ it("should return empty array for unknown subject", () => {
66
+ const store = new GraphStore();
67
+ store.addQuad("ds:button", "rdf:type", "dso:Component");
68
+
69
+ const quads = store.getQuadsForSubject("ds:unknown");
70
+
71
+ expect(quads).toHaveLength(0);
72
+ });
73
+ });
74
+
75
+ describe("getSubjects", () => {
76
+ it("should return unique subjects", () => {
77
+ const store = new GraphStore();
78
+ store.addQuad("ds:button", "rdf:type", "dso:Component");
79
+ store.addLiteral("ds:button", "dso:name", "Button");
80
+ store.addQuad("ds:input", "rdf:type", "dso:Component");
81
+
82
+ const subjects = store.getSubjects();
83
+
84
+ expect(subjects).toHaveLength(2);
85
+ expect(subjects).toContain("ds:button");
86
+ expect(subjects).toContain("ds:input");
87
+ });
88
+ });
89
+
90
+ describe("getN3Store", () => {
91
+ it("should return underlying n3 store for serializers", () => {
92
+ const store = new GraphStore();
93
+ store.addQuad("ds:button", "rdf:type", "dso:Component");
94
+
95
+ const n3Store = store.getN3Store();
96
+
97
+ expect(n3Store).toBeDefined();
98
+ expect(n3Store.size).toBe(1);
99
+ });
100
+ });
101
+
102
+ describe("clear", () => {
103
+ it("should remove all quads", () => {
104
+ const store = new GraphStore();
105
+ store.addQuad("ds:button", "rdf:type", "dso:Component");
106
+ store.addLiteral("ds:button", "dso:name", "Button");
107
+
108
+ store.clear();
109
+
110
+ expect(store.size()).toBe(0);
111
+ });
112
+ });
113
+
114
+ describe("blank nodes", () => {
115
+ it("should create a blank node", () => {
116
+ const store = new GraphStore();
117
+
118
+ const bn = store.createBlankNode();
119
+
120
+ expect(bn).toBeDefined();
121
+ expect(bn.termType).toBe("BlankNode");
122
+ });
123
+
124
+ it("should add a quad with blank node as object", () => {
125
+ const store = new GraphStore();
126
+ const bn = store.createBlankNode();
127
+
128
+ store.addBlankNodeQuad("ds:button", "dso:hasProperty", bn);
129
+
130
+ expect(store.size()).toBe(1);
131
+ const quads = store.getQuads();
132
+ expect(quads[0].object.termType).toBe("BlankNode");
133
+ });
134
+
135
+ it("should add quads from blank node subject", () => {
136
+ const store = new GraphStore();
137
+ const bn = store.createBlankNode();
138
+
139
+ store.addBlankNodeQuad("ds:button", "dso:hasProperty", bn);
140
+ store.addLiteralFromBlankNode(bn, "dso:name", "content");
141
+ store.addQuadFromBlankNode(bn, "rdf:type", "dso:Property");
142
+
143
+ expect(store.size()).toBe(3);
144
+ });
145
+
146
+ it("should serialize inline blank nodes in turtle", async () => {
147
+ const store = new GraphStore();
148
+ const bn = store.createBlankNode();
149
+
150
+ store.addBlankNodeQuad("ds:button", "dso:hasProperty", bn);
151
+ store.addLiteralFromBlankNode(bn, "dso:name", "content");
152
+
153
+ const quads = store.getQuads();
154
+ expect(quads).toHaveLength(2);
155
+ });
156
+ });
157
+ });
@@ -0,0 +1,200 @@
1
+ import { Store, DataFactory } from "n3";
2
+ import type { Quad, BlankNode } from "n3";
3
+
4
+ const { namedNode, blankNode, literal, defaultGraph, quad } = DataFactory;
5
+
6
+ /**
7
+ * RDF Graph Store - wraps n3.Store with a simpler API
8
+ *
9
+ * Provides methods for adding quads using prefixed URIs (e.g., "ds:button")
10
+ * and retrieving quads for serialization.
11
+ */
12
+ export default class GraphStore {
13
+ private store: Store;
14
+
15
+ constructor() {
16
+ this.store = new Store();
17
+ }
18
+
19
+ /**
20
+ * Add a quad with a URI object
21
+ * @param subject - Subject URI (e.g., "ds:button")
22
+ * @param predicate - Predicate URI (e.g., "rdf:type")
23
+ * @param object - Object URI (e.g., "dso:Component")
24
+ */
25
+ addQuad(subject: string, predicate: string, object: string): void {
26
+ this.store.addQuad(
27
+ quad(
28
+ namedNode(subject),
29
+ namedNode(predicate),
30
+ namedNode(object),
31
+ defaultGraph(),
32
+ ),
33
+ );
34
+ }
35
+
36
+ /**
37
+ * Add a quad with a literal object
38
+ * @param subject - Subject URI (e.g., "ds:button")
39
+ * @param predicate - Predicate URI (e.g., "dso:name")
40
+ * @param value - Literal string value
41
+ */
42
+ addLiteral(subject: string, predicate: string, value: string): void {
43
+ this.store.addQuad(
44
+ quad(
45
+ namedNode(subject),
46
+ namedNode(predicate),
47
+ literal(value),
48
+ defaultGraph(),
49
+ ),
50
+ );
51
+ }
52
+
53
+ /**
54
+ * Get all quads in the store
55
+ */
56
+ getQuads(): Quad[] {
57
+ return this.store.getQuads(null, null, null, null);
58
+ }
59
+
60
+ /**
61
+ * Get all quads for a specific subject, including blank node quads
62
+ * Orders quads so blank node definitions come immediately after their reference
63
+ * This allows the n3 Writer to serialize them inline as anonymous nodes
64
+ */
65
+ getQuadsForSubject(subject: string): Quad[] {
66
+ const result: Quad[] = [];
67
+ const processedBlankNodes = new Set<string>();
68
+
69
+ // Get direct quads for this subject
70
+ const directQuads = this.store.getQuads(namedNode(subject), null, null, null);
71
+
72
+ // Process each quad, inserting blank node quads immediately after
73
+ for (const q of directQuads) {
74
+ result.push(q);
75
+
76
+ // If object is a blank node, add its quads immediately after
77
+ if (q.object.termType === "BlankNode") {
78
+ this.addBlankNodeQuadsRecursive(
79
+ q.object as BlankNode,
80
+ result,
81
+ processedBlankNodes,
82
+ );
83
+ }
84
+ }
85
+
86
+ return result;
87
+ }
88
+
89
+ /**
90
+ * Recursively add blank node quads to result array
91
+ */
92
+ private addBlankNodeQuadsRecursive(
93
+ bn: BlankNode,
94
+ result: Quad[],
95
+ processed: Set<string>,
96
+ ): void {
97
+ if (processed.has(bn.value)) return;
98
+ processed.add(bn.value);
99
+
100
+ const bnQuads = this.store.getQuads(bn, null, null, null);
101
+ for (const q of bnQuads) {
102
+ result.push(q);
103
+
104
+ // Recursively handle nested blank nodes
105
+ if (q.object.termType === "BlankNode") {
106
+ this.addBlankNodeQuadsRecursive(q.object as BlankNode, result, processed);
107
+ }
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Get all unique subject URIs in the store
113
+ */
114
+ getSubjects(): string[] {
115
+ const subjects = new Set<string>();
116
+ for (const quad of this.store) {
117
+ subjects.add(quad.subject.value);
118
+ }
119
+ return Array.from(subjects);
120
+ }
121
+
122
+ /**
123
+ * Get the number of quads in the store
124
+ */
125
+ size(): number {
126
+ return this.store.size;
127
+ }
128
+
129
+ /**
130
+ * Get the underlying n3 Store for use with serializers
131
+ */
132
+ getN3Store(): Store {
133
+ return this.store;
134
+ }
135
+
136
+ /**
137
+ * Remove all quads from the store
138
+ */
139
+ clear(): void {
140
+ this.store = new Store();
141
+ }
142
+
143
+ /**
144
+ * Create a new blank node
145
+ */
146
+ createBlankNode(): BlankNode {
147
+ return blankNode();
148
+ }
149
+
150
+ /**
151
+ * Add a quad with a blank node as object
152
+ * @param subject - Subject URI
153
+ * @param predicate - Predicate URI
154
+ * @param blankNodeObj - Blank node object
155
+ */
156
+ addBlankNodeQuad(subject: string, predicate: string, blankNodeObj: BlankNode): void {
157
+ this.store.addQuad(
158
+ quad(
159
+ namedNode(subject),
160
+ namedNode(predicate),
161
+ blankNodeObj,
162
+ defaultGraph(),
163
+ ),
164
+ );
165
+ }
166
+
167
+ /**
168
+ * Add a quad with a blank node as subject and URI object
169
+ * @param blankNodeSubj - Blank node subject
170
+ * @param predicate - Predicate URI
171
+ * @param object - Object URI
172
+ */
173
+ addQuadFromBlankNode(blankNodeSubj: BlankNode, predicate: string, object: string): void {
174
+ this.store.addQuad(
175
+ quad(
176
+ blankNodeSubj,
177
+ namedNode(predicate),
178
+ namedNode(object),
179
+ defaultGraph(),
180
+ ),
181
+ );
182
+ }
183
+
184
+ /**
185
+ * Add a quad with a blank node as subject and literal object
186
+ * @param blankNodeSubj - Blank node subject
187
+ * @param predicate - Predicate URI
188
+ * @param value - Literal string value
189
+ */
190
+ addLiteralFromBlankNode(blankNodeSubj: BlankNode, predicate: string, value: string): void {
191
+ this.store.addQuad(
192
+ quad(
193
+ blankNodeSubj,
194
+ namedNode(predicate),
195
+ literal(value),
196
+ defaultGraph(),
197
+ ),
198
+ );
199
+ }
200
+ }