@canonical/design-system 0.2.1 → 0.2.2

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/bun.lock CHANGED
@@ -8,6 +8,7 @@
8
8
  "ajv": "^8.17.1",
9
9
  "jsonld": "^9.0.0",
10
10
  "n3": "^2.0.1",
11
+ "tinyglobby": "^0.2.14",
11
12
  },
12
13
  "devDependencies": {
13
14
  "@biomejs/biome": "^2.3.14",
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "types": "src/index.ts",
5
5
  "type": "module",
6
6
  "license": "LGPL-3.0",
7
- "version": "0.2.1",
7
+ "version": "0.2.2",
8
8
  "bin": {
9
9
  "collect-implementations": "./src/collect-implementations.ts"
10
10
  },
@@ -39,6 +39,7 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "ajv": "^8.17.1",
42
+ "tinyglobby": "^0.2.14",
42
43
  "jsonld": "^9.0.0",
43
44
  "n3": "^2.0.1"
44
45
  }
@@ -43,6 +43,18 @@ describe("generateLibraryTurtle", () => {
43
43
  expect(ttl).toContain("ds:libraryTier ds:global");
44
44
  });
45
45
 
46
+ it("should strip the npm scope when slugifying package names", async () => {
47
+ const config: CollectConfig = {
48
+ ...minimalConfig,
49
+ name: "@canonical/react-ds-global",
50
+ };
51
+
52
+ const ttl = await generateLibraryTurtle(config);
53
+
54
+ expect(ttl).toContain("ds:implementation.library.react-ds-global");
55
+ expect(ttl).not.toContain("canonical-react-ds-global");
56
+ });
57
+
46
58
  it("should slugify library name for URI", async () => {
47
59
  const config: CollectConfig = {
48
60
  ...minimalConfig,
@@ -186,3 +198,139 @@ describe("generateObjectsTurtle", () => {
186
198
  expect(ttl).toContain('ds:headLink "src/deep/nested/Component.tsx"');
187
199
  });
188
200
  });
201
+
202
+ describe("generateLibraryTurtle with release version", () => {
203
+ const config: CollectConfig = {
204
+ name: "Test Library",
205
+ platform: "react",
206
+ link: "https://github.com/example/test",
207
+ prefix: {
208
+ short: "ds",
209
+ namespace: "https://ds.canonical.com/",
210
+ },
211
+ pattern: "src/**/*.tsx",
212
+ version: "0.35.0",
213
+ };
214
+
215
+ it("should emit ds:version when a release version is configured", async () => {
216
+ const ttl = await generateLibraryTurtle(config);
217
+
218
+ expect(ttl).toContain('ds:version "0.35.0"');
219
+ });
220
+
221
+ it("should not emit ds:version without a release version", async () => {
222
+ const ttl = await generateLibraryTurtle({ ...config, version: undefined });
223
+
224
+ expect(ttl).not.toContain("ds:version");
225
+ });
226
+ });
227
+
228
+ describe("generateObjectsTurtle named IRIs and links", () => {
229
+ const config: CollectConfig = {
230
+ name: "React DS Global",
231
+ platform: "react",
232
+ link: "https://github.com/canonical/pragma/tree/main/packages/react/ds-global",
233
+ prefix: {
234
+ short: "ds",
235
+ namespace: "https://ds.canonical.com/",
236
+ },
237
+ pattern: "src/**/*.tsx",
238
+ repository: "https://github.com/canonical/pragma",
239
+ sourcePath: "packages/react/ds-global",
240
+ version: "0.35.0",
241
+ };
242
+
243
+ const annotations: ImplementsAnnotation[] = [
244
+ {
245
+ filePath: "/base/src/lib/component/Chip/Chip.tsx",
246
+ blockUri: "ds:global.component.chip",
247
+ },
248
+ ];
249
+
250
+ it("should emit named IRIs instead of blank nodes", async () => {
251
+ const ttl = await generateObjectsTurtle(config, annotations, "/base");
252
+
253
+ expect(ttl).toContain(
254
+ "ds:implementation.react-ds-global.global.component.chip",
255
+ );
256
+ expect(ttl).not.toContain("_:");
257
+ expect(ttl).not.toContain("[");
258
+ });
259
+
260
+ it("should link the library to the named implementation", async () => {
261
+ const ttl = await generateObjectsTurtle(config, annotations, "/base");
262
+
263
+ expect(ttl).toContain("ds:implementation.library.react-ds-global");
264
+ expect(ttl).toContain("ds:hasImplementation");
265
+ });
266
+
267
+ it("should emit full blob URLs for headLink when repository is set", async () => {
268
+ const ttl = await generateObjectsTurtle(config, annotations, "/base");
269
+
270
+ expect(ttl).toContain(
271
+ 'ds:headLink "https://github.com/canonical/pragma/blob/main/packages/react/ds-global/src/lib/component/Chip/Chip.tsx"',
272
+ );
273
+ });
274
+
275
+ it("should emit versionedLink pinned to the release tag", async () => {
276
+ const ttl = await generateObjectsTurtle(config, annotations, "/base");
277
+
278
+ expect(ttl).toContain(
279
+ 'ds:versionedLink "https://github.com/canonical/pragma/blob/v0.35.0/packages/react/ds-global/src/lib/component/Chip/Chip.tsx"',
280
+ );
281
+ });
282
+
283
+ it("should not emit versionedLink without a release version", async () => {
284
+ const ttl = await generateObjectsTurtle(
285
+ { ...config, version: undefined },
286
+ annotations,
287
+ "/base",
288
+ );
289
+
290
+ expect(ttl).not.toContain("ds:versionedLink");
291
+ });
292
+
293
+ it("should disambiguate duplicate block implementations with a file slug", async () => {
294
+ const duplicated: ImplementsAnnotation[] = [
295
+ {
296
+ filePath: "/base/src/lib/component/Chip/Chip.tsx",
297
+ blockUri: "ds:global.component.chip",
298
+ },
299
+ {
300
+ filePath: "/base/src/lib/component/Chip/variants/Dense.tsx",
301
+ blockUri: "ds:global.component.chip",
302
+ },
303
+ ];
304
+
305
+ const ttl = await generateObjectsTurtle(config, duplicated, "/base");
306
+
307
+ expect(ttl).toContain(
308
+ "ds:implementation.react-ds-global.global.component.chip a ds:ImplementationObject",
309
+ );
310
+ expect(ttl).toContain(
311
+ "ds:implementation.react-ds-global.global.component.chip.component-dense a ds:ImplementationObject",
312
+ );
313
+ });
314
+
315
+ it("should produce deterministic output regardless of annotation order", async () => {
316
+ const shuffled: ImplementsAnnotation[] = [
317
+ {
318
+ filePath: "/base/src/lib/Modal.tsx",
319
+ blockUri: "ds:global.pattern.modal",
320
+ },
321
+ {
322
+ filePath: "/base/src/lib/component/Chip/Chip.tsx",
323
+ blockUri: "ds:global.component.chip",
324
+ },
325
+ ];
326
+
327
+ const forward = await generateObjectsTurtle(config, shuffled, "/base");
328
+ const reversed = await generateObjectsTurtle(
329
+ config,
330
+ [...shuffled].reverse(),
331
+ "/base",
332
+ );
333
+
334
+ expect(forward).toBe(reversed);
335
+ });
336
+ });
@@ -21,6 +21,7 @@ const RESERVED_PREFIXES = new Set(["rdf", "rdfs", "xsd", "owl"]);
21
21
  function slugify(name: string): string {
22
22
  return name
23
23
  .toLowerCase()
24
+ .replace(/^@[^/]+\//, "") // Remove npm scope
24
25
  .replace(/[^a-z0-9]+/g, "-")
25
26
  .replace(/^-|-$/g, "");
26
27
  }
@@ -32,7 +33,7 @@ function slugify(name: string): string {
32
33
  * e.g., "src/lib/Tile/common/Header/Header.tsx" -> "tile-header"
33
34
  * e.g., "src/lib/Accordion/common/Item/Item.tsx" -> "accordion-item"
34
35
  */
35
- function _filePathSlug(filePath: string): string {
36
+ function filePathSlug(filePath: string): string {
36
37
  // Remove extension and get path parts
37
38
  const withoutExt = filePath.replace(/\.[^.]+$/, "");
38
39
  const parts = withoutExt.split("/").filter(Boolean);
@@ -158,12 +159,40 @@ export function generateLibraryTurtle(config: CollectConfig): Promise<string> {
158
159
  );
159
160
  }
160
161
 
162
+ if (config.version) {
163
+ store.addLiteral(
164
+ libraryUri,
165
+ prefixes.expand(`${p}:version`),
166
+ config.version,
167
+ );
168
+ }
169
+
161
170
  return serializeToTurtle(store, { prefixes });
162
171
  }
163
172
 
173
+ /**
174
+ * Build the source link for an implementation file.
175
+ * With a repository configured, returns a full blob URL pinned to the given
176
+ * ref; otherwise falls back to the repo-relative path.
177
+ */
178
+ function buildSourceLink(
179
+ config: CollectConfig,
180
+ ref: string,
181
+ relativePath: string,
182
+ ): string {
183
+ if (!config.repository) {
184
+ return relativePath;
185
+ }
186
+ const repo = config.repository.replace(/\/$/, "");
187
+ const prefix = config.sourcePath ? `${config.sourcePath}/` : "";
188
+ return `${repo}/blob/${ref}/${prefix}${relativePath}`;
189
+ }
190
+
164
191
  /**
165
192
  * Generate the ImplementationObject .ttl content from annotations
166
- * ImplementationObjects are blank nodes attached to the library
193
+ * ImplementationObjects are named IRIs (ds:implementation.{librarySlug}.{blockLocalName})
194
+ * attached to the library, so they keep a stable identity across releases
195
+ * when ingested into a knowledge graph.
167
196
  */
168
197
  export function generateObjectsTurtle(
169
198
  config: CollectConfig,
@@ -181,44 +210,68 @@ export function generateObjectsTurtle(
181
210
  const librarySlug = slugify(config.name);
182
211
  const libraryUri = `${config.prefix.namespace}implementation.library.${librarySlug}`;
183
212
 
184
- for (const annotation of annotations) {
213
+ // Sort for deterministic output so regenerated files diff cleanly
214
+ const sorted = [...annotations].sort((a, b) => {
215
+ const byBlock = a.blockUri.localeCompare(b.blockUri);
216
+ return byBlock !== 0 ? byBlock : a.filePath.localeCompare(b.filePath);
217
+ });
218
+
219
+ // Track emitted IRIs so a second implementation of the same block in the
220
+ // same library gets a file-path discriminator instead of merging silently
221
+ const seen = new Set<string>();
222
+
223
+ for (const annotation of sorted) {
185
224
  const relativePath = relative(basePath, annotation.filePath);
186
225
 
187
- // Create a blank node for the ImplementationObject
188
- const implNode = store.createBlankNode();
226
+ // Named IRI: ds:implementation.{librarySlug}.{blockLocalName}
227
+ const blockLocalName = annotation.blockUri.replace(/^[^:]+:/, "");
228
+ let implUri = `${config.prefix.namespace}implementation.${librarySlug}.${blockLocalName}`;
229
+ if (seen.has(implUri)) {
230
+ implUri = `${implUri}.${filePathSlug(relativePath)}`;
231
+ }
232
+ seen.add(implUri);
189
233
 
190
234
  // Link library to implementation object
191
- store.addBlankNodeQuad(
235
+ store.addQuad(
192
236
  libraryUri,
193
237
  prefixes.expand(`${p}:hasImplementation`),
194
- implNode,
238
+ implUri,
195
239
  );
196
240
 
197
241
  // Add type
198
- store.addQuadFromBlankNode(
199
- implNode,
242
+ store.addQuad(
243
+ implUri,
200
244
  prefixes.expand("rdf:type"),
201
245
  prefixes.expand(`${p}:ImplementationObject`),
202
246
  );
203
247
 
204
248
  // Add implements block reference
205
- store.addQuadFromBlankNode(
206
- implNode,
249
+ store.addQuad(
250
+ implUri,
207
251
  prefixes.expand(`${p}:implementsBlock`),
208
252
  prefixes.expand(annotation.blockUri),
209
253
  );
210
254
 
211
- // Add head link (relative path to source file)
212
- store.addLiteralFromBlankNode(
213
- implNode,
255
+ // Add head link (source file at the repository head)
256
+ store.addLiteral(
257
+ implUri,
214
258
  prefixes.expand(`${p}:headLink`),
215
- relativePath,
259
+ buildSourceLink(config, "main", relativePath),
216
260
  );
217
261
 
262
+ // Add versioned link (source file pinned to the release tag)
263
+ if (config.repository && config.version) {
264
+ store.addLiteral(
265
+ implUri,
266
+ prefixes.expand(`${p}:versionedLink`),
267
+ buildSourceLink(config, `v${config.version}`, relativePath),
268
+ );
269
+ }
270
+
218
271
  // Add version if specified
219
272
  if (annotation.version) {
220
- store.addLiteralFromBlankNode(
221
- implNode,
273
+ store.addLiteral(
274
+ implUri,
222
275
  prefixes.expand(`${p}:implementationVersion`),
223
276
  annotation.version,
224
277
  );
@@ -226,11 +279,7 @@ export function generateObjectsTurtle(
226
279
 
227
280
  // Add draft status if true
228
281
  if (annotation.isDraft) {
229
- store.addLiteralFromBlankNode(
230
- implNode,
231
- prefixes.expand(`${p}:isDraft`),
232
- "true",
233
- );
282
+ store.addLiteral(implUri, prefixes.expand(`${p}:isDraft`), "true");
234
283
  }
235
284
  }
236
285
 
@@ -35,6 +35,26 @@ export interface CollectConfig {
35
35
 
36
36
  /** Output directory for generated .ttl files (default: "data") */
37
37
  outputDir?: string;
38
+
39
+ /**
40
+ * Release version of the library (e.g., "0.35.0").
41
+ * When set, emits ds:version on the library and enables versionedLink
42
+ * generation for implementation objects.
43
+ */
44
+ version?: string;
45
+
46
+ /**
47
+ * Base URL of the source repository (e.g., "https://github.com/canonical/pragma").
48
+ * When set, headLink/versionedLink are emitted as full blob URLs instead of
49
+ * repo-relative paths.
50
+ */
51
+ repository?: string;
52
+
53
+ /**
54
+ * Path of the library within the repository (e.g., "packages/react/ds-global").
55
+ * Prepended to file paths when building full blob URLs.
56
+ */
57
+ sourcePath?: string;
38
58
  }
39
59
 
40
60
  /**