@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,99 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { NAMESPACES, PREDICATES } from "../constants.js";
3
+ import transform from "./transform.js";
4
+
5
+ describe("transform", () => {
6
+ const extractedData = {
7
+ document: "test-doc",
8
+ extractedAt: "2024-01-01",
9
+ tables: {
10
+ uiBlocks: [
11
+ {
12
+ _codaId: "i-1",
13
+ name: "Button",
14
+ tier: "Global",
15
+ type: "Component",
16
+ uri: `${NAMESPACES.ds}global:component:button`,
17
+ },
18
+ {
19
+ _codaId: "i-2",
20
+ name: "Input",
21
+ tier: "Global",
22
+ type: "Component",
23
+ uri: `${NAMESPACES.ds}global:component:input`,
24
+ },
25
+ ],
26
+ tiers: [
27
+ { _codaId: "i-tier-1", name: "Global", uri: `${NAMESPACES.ds}global` },
28
+ ],
29
+ uiBlockTypes: [
30
+ { _codaId: "i-type-1", Name: "Component" },
31
+ ],
32
+ },
33
+ };
34
+
35
+ const config = {
36
+ format: "json-ld" as const,
37
+ outputDir: "instances/",
38
+ atomicity: "instance" as const,
39
+ tables: {
40
+ uiBlocks: {
41
+ "@context": {
42
+ dso: NAMESPACES.dso,
43
+ ds: NAMESPACES.ds,
44
+ name: "dso:name",
45
+ tier: { "@id": "dso:tier", "@type": "@id" },
46
+ },
47
+ class: "{type}",
48
+ uriTemplate: "{uri}",
49
+ },
50
+ },
51
+ references: {
52
+ tiers: { uriTemplate: "{uri}", keyColumn: "name" },
53
+ uiBlockTypes: { uriTemplate: `${NAMESPACES.dso}{Name}`, keyColumn: "Name" },
54
+ },
55
+ };
56
+
57
+ it("should transform extracted data into a graph store", () => {
58
+ const { store, subjects } = transform(config, extractedData);
59
+
60
+ expect(store.size()).toBeGreaterThan(0);
61
+ expect(subjects).toHaveLength(2);
62
+ });
63
+
64
+ it("should create quads for each row", () => {
65
+ const { store } = transform(config, extractedData);
66
+
67
+ const buttonQuads = store.getQuadsForSubject(`${NAMESPACES.ds}global:component:button`);
68
+ expect(buttonQuads.length).toBeGreaterThan(0);
69
+
70
+ const inputQuads = store.getQuadsForSubject(`${NAMESPACES.ds}global:component:input`);
71
+ expect(inputQuads.length).toBeGreaterThan(0);
72
+ });
73
+
74
+ it("should resolve reference values", () => {
75
+ const { store } = transform(config, extractedData);
76
+
77
+ const buttonQuads = store.getQuadsForSubject(`${NAMESPACES.ds}global:component:button`);
78
+ const tierQuad = buttonQuads.find((q) => q.predicate.value === PREDICATES.tier);
79
+
80
+ expect(tierQuad?.object.value).toBe(`${NAMESPACES.ds}global`);
81
+ });
82
+
83
+ it("should resolve dynamic class from reference", () => {
84
+ const { store } = transform(config, extractedData);
85
+
86
+ const buttonQuads = store.getQuadsForSubject(`${NAMESPACES.ds}global:component:button`);
87
+ const typeQuad = buttonQuads.find((q) => q.predicate.value === PREDICATES.type);
88
+
89
+ // buildUri lowercases interpolated values for URI safety
90
+ expect(typeQuad?.object.value).toBe(`${NAMESPACES.dso}component`);
91
+ });
92
+
93
+ it("should return prefixes from context", () => {
94
+ const { prefixes } = transform(config, extractedData);
95
+
96
+ expect(prefixes.get("dso")).toBe(NAMESPACES.dso);
97
+ expect(prefixes.get("ds")).toBe(NAMESPACES.ds);
98
+ });
99
+ });
@@ -0,0 +1,109 @@
1
+ import type { TransformConfig } from "../config/types.js";
2
+ import { GraphStore, PrefixMap } from "../graph/index.js";
3
+ import resolveReferences from "./resolveReferences.js";
4
+ import transformRow from "./transformRow.js";
5
+ import type { ReferenceMap, InlineDataMap } from "./types.js";
6
+
7
+ /**
8
+ * Extracted data structure from the extract command
9
+ */
10
+ interface ExtractedData {
11
+ document: string;
12
+ extractedAt: string;
13
+ tables: Record<string, Array<Record<string, unknown>>>;
14
+ }
15
+
16
+ /**
17
+ * Result of the transform operation
18
+ */
19
+ export interface TransformResult {
20
+ store: GraphStore;
21
+ prefixes: PrefixMap;
22
+ subjects: string[];
23
+ }
24
+
25
+ /**
26
+ * Transform extracted data into an RDF graph
27
+ *
28
+ * Takes the extracted table data and configuration, builds reference maps
29
+ * for cross-table lookups, then transforms each row into quads.
30
+ */
31
+ export default function transform(
32
+ config: TransformConfig,
33
+ extractedData: ExtractedData,
34
+ ): TransformResult {
35
+ const store = new GraphStore();
36
+ const subjects: string[] = [];
37
+
38
+ // Build reference maps for all reference tables
39
+ const mergedRefMap: ReferenceMap = new Map();
40
+
41
+ if (config.references) {
42
+ for (const [tableName, refConfig] of Object.entries(config.references)) {
43
+ const tableData = extractedData.tables[tableName];
44
+ if (tableData) {
45
+ const refMap = resolveReferences(tableData, refConfig);
46
+ // Merge into the combined map
47
+ for (const [key, value] of refMap) {
48
+ mergedRefMap.set(key, value);
49
+ }
50
+ }
51
+ }
52
+ }
53
+
54
+ // Collect all prefixes from table contexts
55
+ const prefixes = new PrefixMap();
56
+
57
+ // Build inline data maps for all tables (indexed by _codaId and common name columns)
58
+ const inlineData: Record<string, InlineDataMap> = {};
59
+ const nameColumns = ["name", "property name", "Name"];
60
+ for (const [tableName, tableData] of Object.entries(extractedData.tables)) {
61
+ const dataMap: InlineDataMap = new Map();
62
+ for (const row of tableData) {
63
+ // Index by Coda ID
64
+ const codaId = row._codaId as string;
65
+ if (codaId) {
66
+ dataMap.set(codaId, row);
67
+ }
68
+ // Also index by common name columns for name-based lookups
69
+ for (const col of nameColumns) {
70
+ const nameValue = row[col];
71
+ if (typeof nameValue === "string" && nameValue.length > 0) {
72
+ dataMap.set(nameValue.toLowerCase(), row);
73
+ }
74
+ }
75
+ }
76
+ inlineData[tableName] = dataMap;
77
+ }
78
+
79
+ // Transform each configured table
80
+ for (const [tableName, tableConfig] of Object.entries(config.tables)) {
81
+ const tableData = extractedData.tables[tableName];
82
+ if (!tableData) {
83
+ continue;
84
+ }
85
+
86
+ // Extract prefixes from this table's context
87
+ const tablePrefixes = PrefixMap.fromContext(
88
+ tableConfig["@context"] as Record<string, unknown>,
89
+ );
90
+ for (const [prefix, namespace] of tablePrefixes.entries()) {
91
+ prefixes.add(prefix, namespace);
92
+ }
93
+
94
+ // Transform each row
95
+ for (const row of tableData) {
96
+ const subjectUri = transformRow(
97
+ row,
98
+ tableConfig,
99
+ store,
100
+ prefixes,
101
+ mergedRefMap,
102
+ inlineData,
103
+ );
104
+ subjects.push(subjectUri);
105
+ }
106
+ }
107
+
108
+ return { store, prefixes, subjects };
109
+ }
@@ -0,0 +1,269 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { NAMESPACES, PREDICATES } from "../constants.js";
3
+ import { GraphStore, PrefixMap } from "../graph/index.js";
4
+ import transformRow from "./transformRow.js";
5
+ import type { ReferenceMap } from "./types.js";
6
+
7
+ const buttonUri = `${NAMESPACES.ds}global:component:button`;
8
+ const globalUri = `${NAMESPACES.ds}global`;
9
+ const componentType = `${NAMESPACES.dso}Component`;
10
+
11
+ describe("transformRow", () => {
12
+ it("should add type quad from class config", () => {
13
+ const store = new GraphStore();
14
+ const prefixes = new PrefixMap();
15
+ prefixes.add("dso", NAMESPACES.dso);
16
+
17
+ const row = { name: "Button", uri: buttonUri };
18
+ const config = {
19
+ "@context": {
20
+ dso: NAMESPACES.dso,
21
+ name: "dso:name",
22
+ },
23
+ class: "dso:Component",
24
+ uriTemplate: "{uri}",
25
+ };
26
+
27
+ transformRow(row, config, store, prefixes, new Map());
28
+
29
+ const quads = store.getQuads();
30
+ const typeQuad = quads.find((q) => q.predicate.value === PREDICATES.type);
31
+ expect(typeQuad).toBeDefined();
32
+ expect(typeQuad?.object.value).toBe(componentType);
33
+ });
34
+
35
+ it("should add literal properties", () => {
36
+ const store = new GraphStore();
37
+ const prefixes = new PrefixMap();
38
+ prefixes.add("dso", NAMESPACES.dso);
39
+
40
+ const row = { name: "Button", description: "A clickable button", uri: buttonUri };
41
+ const config = {
42
+ "@context": {
43
+ dso: NAMESPACES.dso,
44
+ name: "dso:name",
45
+ description: "dso:description",
46
+ },
47
+ class: "dso:Component",
48
+ uriTemplate: "{uri}",
49
+ };
50
+
51
+ transformRow(row, config, store, prefixes, new Map());
52
+
53
+ const quads = store.getQuads();
54
+ const nameQuad = quads.find((q) => q.predicate.value === PREDICATES.name);
55
+ expect(nameQuad?.object.value).toBe("Button");
56
+ });
57
+
58
+ it("should resolve reference properties to URIs", () => {
59
+ const store = new GraphStore();
60
+ const prefixes = new PrefixMap();
61
+ prefixes.add("dso", NAMESPACES.dso);
62
+ prefixes.add("ds", NAMESPACES.ds);
63
+
64
+ const refMap: ReferenceMap = new Map([
65
+ ["global", globalUri],
66
+ ]);
67
+
68
+ const row = { name: "Button", tier: "Global", uri: buttonUri };
69
+ const config = {
70
+ "@context": {
71
+ dso: NAMESPACES.dso,
72
+ ds: NAMESPACES.ds,
73
+ name: "dso:name",
74
+ tier: { "@id": "dso:tier", "@type": "@id" },
75
+ },
76
+ class: "dso:Component",
77
+ uriTemplate: "{uri}",
78
+ };
79
+
80
+ transformRow(row, config, store, prefixes, refMap);
81
+
82
+ const quads = store.getQuads();
83
+ const tierQuad = quads.find((q) => q.predicate.value === PREDICATES.tier);
84
+ expect(tierQuad?.object.value).toBe(globalUri);
85
+ });
86
+
87
+ it("should skip null/undefined values", () => {
88
+ const store = new GraphStore();
89
+ const prefixes = new PrefixMap();
90
+ prefixes.add("dso", NAMESPACES.dso);
91
+
92
+ const row = { name: "Button", description: null, uri: buttonUri };
93
+ const config = {
94
+ "@context": {
95
+ dso: NAMESPACES.dso,
96
+ name: "dso:name",
97
+ description: "dso:description",
98
+ },
99
+ class: "dso:Component",
100
+ uriTemplate: "{uri}",
101
+ };
102
+
103
+ transformRow(row, config, store, prefixes, new Map());
104
+
105
+ const quads = store.getQuads();
106
+ const descQuad = quads.find((q) => q.predicate.value === PREDICATES.description);
107
+ expect(descQuad).toBeUndefined();
108
+ });
109
+
110
+ it("should add empty string literals", () => {
111
+ const store = new GraphStore();
112
+ const prefixes = new PrefixMap();
113
+ prefixes.add("dso", NAMESPACES.dso);
114
+
115
+ const row = { name: "Button", description: "", uri: buttonUri };
116
+ const config = {
117
+ "@context": {
118
+ dso: NAMESPACES.dso,
119
+ name: "dso:name",
120
+ description: "dso:description",
121
+ },
122
+ class: "dso:Component",
123
+ uriTemplate: "{uri}",
124
+ };
125
+
126
+ transformRow(row, config, store, prefixes, new Map());
127
+
128
+ const quads = store.getQuads();
129
+ const descQuad = quads.find((q) => q.predicate.value === PREDICATES.description);
130
+ expect(descQuad).toBeDefined();
131
+ expect(descQuad?.object.value).toBe("");
132
+ });
133
+
134
+ it("should handle dynamic class from row data", () => {
135
+ const store = new GraphStore();
136
+ const prefixes = new PrefixMap();
137
+ prefixes.add("dso", NAMESPACES.dso);
138
+
139
+ const refMap: ReferenceMap = new Map([
140
+ ["component", componentType],
141
+ ]);
142
+
143
+ const row = { name: "Button", type: "Component", uri: buttonUri };
144
+ const config = {
145
+ "@context": {
146
+ dso: NAMESPACES.dso,
147
+ name: "dso:name",
148
+ },
149
+ class: "{type}",
150
+ uriTemplate: "{uri}",
151
+ };
152
+
153
+ transformRow(row, config, store, prefixes, refMap);
154
+
155
+ const quads = store.getQuads();
156
+ const typeQuad = quads.find((q) => q.predicate.value === PREDICATES.type);
157
+ expect(typeQuad?.object.value).toBe(componentType);
158
+ });
159
+
160
+ it("should return the subject URI", () => {
161
+ const store = new GraphStore();
162
+ const prefixes = new PrefixMap();
163
+ prefixes.add("dso", NAMESPACES.dso);
164
+
165
+ const row = { name: "Button", uri: buttonUri };
166
+ const config = {
167
+ "@context": { dso: NAMESPACES.dso, name: "dso:name" },
168
+ class: "dso:Component",
169
+ uriTemplate: "{uri}",
170
+ };
171
+
172
+ const subjectUri = transformRow(row, config, store, prefixes, new Map());
173
+
174
+ expect(subjectUri).toBe(buttonUri);
175
+ });
176
+
177
+ it("should embed inline properties as blank nodes", () => {
178
+ const store = new GraphStore();
179
+ const prefixes = new PrefixMap();
180
+ prefixes.add("dso", NAMESPACES.dso);
181
+ prefixes.add("ds", NAMESPACES.ds);
182
+
183
+ const row = { name: "Button", properties: "prop-1,prop-2", uri: buttonUri };
184
+ const config = {
185
+ "@context": {
186
+ dso: NAMESPACES.dso,
187
+ ds: NAMESPACES.ds,
188
+ name: "dso:name",
189
+ properties: {
190
+ "@id": "dso:hasProperty",
191
+ "@inline": {
192
+ table: "properties",
193
+ class: "dso:Property",
194
+ properties: {
195
+ name: "dso:propertyName",
196
+ type: "dso:propertyType",
197
+ },
198
+ },
199
+ },
200
+ },
201
+ class: "dso:Component",
202
+ uriTemplate: "{uri}",
203
+ };
204
+
205
+ const inlineData = {
206
+ properties: new Map([
207
+ ["prop-1", { _codaId: "prop-1", name: "content", type: "string" }],
208
+ ["prop-2", { _codaId: "prop-2", name: "variant", type: "enum" }],
209
+ ]),
210
+ };
211
+
212
+ transformRow(row, config, store, prefixes, new Map(), inlineData);
213
+
214
+ const quads = store.getQuads();
215
+
216
+ // Should have: 1 type quad + 1 name quad + 2 blank node links + 2x3 blank node properties
217
+ expect(quads.length).toBeGreaterThanOrEqual(8);
218
+
219
+ // Find blank node quads
220
+ const hasPropertyQuads = quads.filter(
221
+ (q) => q.predicate.value === `${NAMESPACES.dso}hasProperty`,
222
+ );
223
+ expect(hasPropertyQuads).toHaveLength(2);
224
+
225
+ // Blank nodes should have the right type
226
+ const blankNodeTypes = quads.filter(
227
+ (q) =>
228
+ q.subject.termType === "BlankNode" &&
229
+ q.predicate.value === PREDICATES.type,
230
+ );
231
+ expect(blankNodeTypes).toHaveLength(2);
232
+ expect(blankNodeTypes[0].object.value).toBe(`${NAMESPACES.dso}Property`);
233
+ });
234
+
235
+ it("should skip inline properties when no matching data", () => {
236
+ const store = new GraphStore();
237
+ const prefixes = new PrefixMap();
238
+ prefixes.add("dso", NAMESPACES.dso);
239
+
240
+ const row = { name: "Button", properties: "unknown-id", uri: buttonUri };
241
+ const config = {
242
+ "@context": {
243
+ dso: NAMESPACES.dso,
244
+ name: "dso:name",
245
+ properties: {
246
+ "@id": "dso:hasProperty",
247
+ "@inline": {
248
+ table: "properties",
249
+ properties: { name: "dso:propertyName" },
250
+ },
251
+ },
252
+ },
253
+ class: "dso:Component",
254
+ uriTemplate: "{uri}",
255
+ };
256
+
257
+ const inlineData = {
258
+ properties: new Map<string, Record<string, unknown>>(),
259
+ };
260
+
261
+ transformRow(row, config, store, prefixes, new Map(), inlineData);
262
+
263
+ const quads = store.getQuads();
264
+ const hasPropertyQuads = quads.filter(
265
+ (q) => q.predicate.value === `${NAMESPACES.dso}hasProperty`,
266
+ );
267
+ expect(hasPropertyQuads).toHaveLength(0);
268
+ });
269
+ });
@@ -0,0 +1,220 @@
1
+ import type { TableTransform, ContextValue, InlineConfig } from "../config/types.js";
2
+ import type { GraphStore, PrefixMap } from "../graph/index.js";
3
+ import { buildUri } from "../graph/index.js";
4
+ import { NAMESPACES } from "../constants.js";
5
+ import resolveValue from "./resolveValue.js";
6
+ import type { ReferenceMap, InlineDataMap } from "./types.js";
7
+
8
+ /**
9
+ * Check if a context value indicates an object property (URI reference)
10
+ */
11
+ function isObjectProperty(contextValue: ContextValue): boolean {
12
+ return (
13
+ typeof contextValue === "object" &&
14
+ contextValue !== null &&
15
+ "@type" in contextValue &&
16
+ contextValue["@type"] === "@id"
17
+ );
18
+ }
19
+
20
+ /**
21
+ * Check if a context value has inline configuration
22
+ */
23
+ function hasInlineConfig(contextValue: ContextValue): contextValue is { "@id": string; "@inline": InlineConfig } {
24
+ return (
25
+ typeof contextValue === "object" &&
26
+ contextValue !== null &&
27
+ "@inline" in contextValue &&
28
+ contextValue["@inline"] !== undefined
29
+ );
30
+ }
31
+
32
+ /**
33
+ * Extract Coda IDs from a reference value (e.g., "#r1,#r2" or array of refs)
34
+ */
35
+ function extractCodaIds(value: unknown): string[] {
36
+ if (typeof value === "string") {
37
+ // Handle CSV format: "#r1,#r2" or "content,size"
38
+ return value.split(",").map((v) => v.trim()).filter(Boolean);
39
+ }
40
+ if (Array.isArray(value)) {
41
+ return value.flatMap((v) => {
42
+ if (typeof v === "object" && v !== null && "id" in v) {
43
+ return [String(v.id)];
44
+ }
45
+ return typeof v === "string" ? [v.trim()] : [];
46
+ });
47
+ }
48
+ if (typeof value === "object" && value !== null && "id" in value) {
49
+ return [String((value as { id: string }).id)];
50
+ }
51
+ return [];
52
+ }
53
+
54
+ /**
55
+ * Get the predicate URI from a context value
56
+ */
57
+ function getPredicateUri(
58
+ contextValue: ContextValue,
59
+ prefixes: PrefixMap,
60
+ ): string {
61
+ if (typeof contextValue === "string") {
62
+ return prefixes.expand(contextValue);
63
+ }
64
+ return prefixes.expand(contextValue["@id"]);
65
+ }
66
+
67
+ /**
68
+ * Resolve the class for @type, supporting templates like "{type}"
69
+ */
70
+ function resolveClass(
71
+ classConfig: string,
72
+ row: Record<string, unknown>,
73
+ refMap: ReferenceMap,
74
+ prefixes: PrefixMap,
75
+ ): string {
76
+ // Check if class is a template (contains {column})
77
+ if (classConfig.includes("{")) {
78
+ const columnMatch = classConfig.match(/\{(\w+)\}/);
79
+ if (columnMatch) {
80
+ const columnName = columnMatch[1];
81
+ const rawValue = row[columnName];
82
+ if (typeof rawValue === "string" && rawValue.length > 0) {
83
+ // Resolve through reference map and expand
84
+ const resolved = resolveValue(rawValue, refMap);
85
+ if (typeof resolved === "string") {
86
+ return prefixes.expand(resolved);
87
+ }
88
+ }
89
+ }
90
+ }
91
+ // Expand prefixed URI
92
+ return prefixes.expand(classConfig);
93
+ }
94
+
95
+ /**
96
+ * Add inline blank nodes for a property
97
+ */
98
+ function addInlineBlankNodes(
99
+ subjectUri: string,
100
+ predicateUri: string,
101
+ ids: string[],
102
+ inlineConfig: InlineConfig,
103
+ inlineData: Record<string, InlineDataMap>,
104
+ store: GraphStore,
105
+ prefixes: PrefixMap,
106
+ ): void {
107
+ const tableData = inlineData[inlineConfig.table];
108
+ if (!tableData) return;
109
+
110
+ for (const id of ids) {
111
+ // Try exact match first, then lowercase for name-based lookups
112
+ let rowData = tableData.get(id);
113
+ if (!rowData) {
114
+ rowData = tableData.get(id.toLowerCase());
115
+ }
116
+ if (!rowData) continue;
117
+
118
+ // Create blank node
119
+ const bn = store.createBlankNode();
120
+ store.addBlankNodeQuad(subjectUri, predicateUri, bn);
121
+
122
+ // Add class if specified
123
+ if (inlineConfig.class) {
124
+ store.addQuadFromBlankNode(bn, `${NAMESPACES.rdf}type`, prefixes.expand(inlineConfig.class));
125
+ }
126
+
127
+ // Add property mappings
128
+ for (const [column, predicate] of Object.entries(inlineConfig.properties)) {
129
+ const value = rowData[column];
130
+ if (value === null || value === undefined || value === "") continue;
131
+ store.addLiteralFromBlankNode(bn, prefixes.expand(predicate), String(value));
132
+ }
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Transform a single row into quads and add them to the store
138
+ *
139
+ * @returns The subject URI of the transformed row
140
+ */
141
+ export default function transformRow(
142
+ row: Record<string, unknown>,
143
+ config: TableTransform,
144
+ store: GraphStore,
145
+ prefixes: PrefixMap,
146
+ refMap: ReferenceMap,
147
+ inlineData: Record<string, InlineDataMap> = {},
148
+ ): string {
149
+ const rawSubjectUri = buildUri(config.uriTemplate, row);
150
+ const subjectUri = prefixes.expand(rawSubjectUri);
151
+ const classUri = resolveClass(config.class, row, refMap, prefixes);
152
+
153
+ // Add rdf:type quad
154
+ store.addQuad(subjectUri, `${NAMESPACES.rdf}type`, classUri);
155
+
156
+ // Process each property in the context
157
+ for (const [columnName, contextValue] of Object.entries(config["@context"])) {
158
+ // Skip JSON-LD keywords and namespace declarations
159
+ if (columnName.startsWith("@") || typeof contextValue === "string" && contextValue.startsWith("http")) {
160
+ continue;
161
+ }
162
+
163
+ if (!contextValue) continue;
164
+
165
+ const rawValue = row[columnName];
166
+
167
+ // Skip null/undefined values (empty strings allowed for literals)
168
+ if (rawValue === null || rawValue === undefined) {
169
+ continue;
170
+ }
171
+
172
+ const predicateUri = getPredicateUri(contextValue, prefixes);
173
+
174
+ // Handle inline blank nodes
175
+ if (hasInlineConfig(contextValue)) {
176
+ const ids = extractCodaIds(rawValue);
177
+ if (ids.length > 0) {
178
+ addInlineBlankNodes(
179
+ subjectUri,
180
+ predicateUri,
181
+ ids,
182
+ contextValue["@inline"],
183
+ inlineData,
184
+ store,
185
+ prefixes,
186
+ );
187
+ }
188
+ continue;
189
+ }
190
+
191
+ const isReference = isObjectProperty(contextValue);
192
+
193
+ // Resolve value - only references need to go through refMap
194
+ const resolvedValue = isReference
195
+ ? resolveValue(rawValue, refMap)
196
+ : rawValue;
197
+
198
+ // Add quads for the value(s)
199
+ if (Array.isArray(resolvedValue)) {
200
+ for (const val of resolvedValue) {
201
+ if (val === null || val === undefined || val === "") continue;
202
+ if (isReference) {
203
+ store.addQuad(subjectUri, predicateUri, prefixes.expand(String(val)));
204
+ } else {
205
+ store.addLiteral(subjectUri, predicateUri, String(val));
206
+ }
207
+ }
208
+ } else {
209
+ // Skip empty strings for references (they create invalid quads)
210
+ if (isReference) {
211
+ if (resolvedValue === "") continue;
212
+ store.addQuad(subjectUri, predicateUri, prefixes.expand(String(resolvedValue)));
213
+ } else {
214
+ store.addLiteral(subjectUri, predicateUri, String(resolvedValue));
215
+ }
216
+ }
217
+ }
218
+
219
+ return subjectUri;
220
+ }