@adobe/design-data 3.2.12 → 3.3.1
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/package.json +5 -4
- package/src/component.js +178 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adobe/design-data",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.1",
|
|
4
4
|
"description": "Node.js helpers for @adobe/design-data-wasm: filesystem dataset loading, token write operations, and authoring-session management",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
"./write": "./src/write.js",
|
|
11
11
|
"./session": "./src/session.js",
|
|
12
12
|
"./validate": "./src/validate.js",
|
|
13
|
-
"./guideline": "./src/guideline.js"
|
|
13
|
+
"./guideline": "./src/guideline.js",
|
|
14
|
+
"./component": "./src/component.js"
|
|
14
15
|
},
|
|
15
16
|
"files": [
|
|
16
17
|
"src/",
|
|
@@ -45,8 +46,8 @@
|
|
|
45
46
|
"dependencies": {
|
|
46
47
|
"ajv": "^8.17.1",
|
|
47
48
|
"ajv-formats": "^3.0.1",
|
|
48
|
-
"@adobe/design-data-spec": "4.
|
|
49
|
-
"@adobe/design-data-wasm": "0.
|
|
49
|
+
"@adobe/design-data-spec": "4.11.0",
|
|
50
|
+
"@adobe/design-data-wasm": "0.14.1"
|
|
50
51
|
},
|
|
51
52
|
"devDependencies": {
|
|
52
53
|
"ava": "^6.1.2"
|
package/src/component.js
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// Copyright 2026 Adobe. All rights reserved.
|
|
2
|
+
// This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
// you may not use this file except in compliance with the License. You may obtain a copy
|
|
4
|
+
// of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
5
|
+
//
|
|
6
|
+
// Unless required by applicable law or agreed to in writing, software distributed under
|
|
7
|
+
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
8
|
+
// OF ANY KIND, either express or implied. See the License for the specific language
|
|
9
|
+
// governing permissions and limitations under the License.
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Shared component search over `<package>/components/*.json`, used by
|
|
13
|
+
* @adobe/design-data-mcp's `design-data-component-search` tool.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { readFileSync, readdirSync, existsSync } from "node:fs";
|
|
17
|
+
import { join } from "node:path";
|
|
18
|
+
|
|
19
|
+
const SNIPPET_LENGTH = 200;
|
|
20
|
+
const MIN_WORD_LENGTH = 3;
|
|
21
|
+
const STOPWORDS = new Set([
|
|
22
|
+
"the",
|
|
23
|
+
"and",
|
|
24
|
+
"for",
|
|
25
|
+
"from",
|
|
26
|
+
"with",
|
|
27
|
+
"that",
|
|
28
|
+
"this",
|
|
29
|
+
"are",
|
|
30
|
+
"was",
|
|
31
|
+
"has",
|
|
32
|
+
"have",
|
|
33
|
+
"can",
|
|
34
|
+
"any",
|
|
35
|
+
"into",
|
|
36
|
+
"your",
|
|
37
|
+
"you",
|
|
38
|
+
"not",
|
|
39
|
+
"but",
|
|
40
|
+
"all",
|
|
41
|
+
"use",
|
|
42
|
+
]);
|
|
43
|
+
|
|
44
|
+
const asString = (v) => (typeof v === "string" ? v : "");
|
|
45
|
+
|
|
46
|
+
function queryWords(query) {
|
|
47
|
+
return [
|
|
48
|
+
...new Set(
|
|
49
|
+
query
|
|
50
|
+
.toLowerCase()
|
|
51
|
+
.split(/[^a-z0-9]+/)
|
|
52
|
+
.filter((w) => w.length >= MIN_WORD_LENGTH && !STOPWORDS.has(w)),
|
|
53
|
+
),
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Match at a word start so "pick" finds "picker" but "tor" doesn't find "button".
|
|
58
|
+
const wordIndex = (text, word) =>
|
|
59
|
+
text.search(new RegExp(`(^|[^a-z0-9])${word}`));
|
|
60
|
+
|
|
61
|
+
function snippetAround(text, index) {
|
|
62
|
+
const start = Math.max(0, index - 60);
|
|
63
|
+
const slice = text.slice(start, start + SNIPPET_LENGTH).replace(/\s+/g, " ");
|
|
64
|
+
return (start > 0 ? "…" : "") + slice.trim();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function loadComponent(componentsDir, file) {
|
|
68
|
+
try {
|
|
69
|
+
return JSON.parse(readFileSync(join(componentsDir, file), "utf-8"));
|
|
70
|
+
} catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Search components by natural-language query.
|
|
77
|
+
*
|
|
78
|
+
* Query words are lowercased, split on non-alphanumerics, and filtered for
|
|
79
|
+
* stopwords and words under 3 characters. A word matches at the start of a
|
|
80
|
+
* word in the displayName, description or documentBlocks text. Components
|
|
81
|
+
* containing every word win; if none do, components with any word in the
|
|
82
|
+
* displayName or description are returned (docs-text-only hits are ignored). Ranking: exact displayName, displayName, description, then docs
|
|
83
|
+
* text; within a tier, more matched words first, then alphabetical. Files that
|
|
84
|
+
* can't be parsed are skipped.
|
|
85
|
+
*
|
|
86
|
+
* shortcut: no stemming or fuzzy matching; revisit once real use-case queries
|
|
87
|
+
* exist to evaluate against.
|
|
88
|
+
*
|
|
89
|
+
* @param {string} componentsDir - absolute path to the components directory.
|
|
90
|
+
* @param {string} query
|
|
91
|
+
* @param {{category?: string, limit?: number}} [options] `limit` is clamped to
|
|
92
|
+
* an integer of at least 1.
|
|
93
|
+
* @returns {Array<{id: string, displayName: string, category: string, snippet: string}>}
|
|
94
|
+
*/
|
|
95
|
+
export function searchComponents(
|
|
96
|
+
componentsDir,
|
|
97
|
+
query,
|
|
98
|
+
{ category, limit = 10 } = {},
|
|
99
|
+
) {
|
|
100
|
+
const words = queryWords(asString(query));
|
|
101
|
+
if (words.length === 0 || !existsSync(componentsDir)) return [];
|
|
102
|
+
const max = Number.isFinite(limit) ? Math.max(1, Math.floor(limit)) : 10;
|
|
103
|
+
const phrase = asString(query).trim().toLowerCase();
|
|
104
|
+
|
|
105
|
+
const scored = [];
|
|
106
|
+
for (const file of readdirSync(componentsDir).sort()) {
|
|
107
|
+
if (!file.endsWith(".json")) continue;
|
|
108
|
+
const doc = loadComponent(componentsDir, file);
|
|
109
|
+
if (!doc) continue;
|
|
110
|
+
const componentCategory = doc.meta?.category ?? "uncategorized";
|
|
111
|
+
if (category && componentCategory !== category) continue;
|
|
112
|
+
|
|
113
|
+
const id = asString(doc.name) || file.replace(/\.json$/, "");
|
|
114
|
+
const displayName = asString(doc.displayName) || id;
|
|
115
|
+
const name = displayName.toLowerCase();
|
|
116
|
+
const description = asString(doc.description);
|
|
117
|
+
const descriptionLower = description.toLowerCase();
|
|
118
|
+
const blocks = (Array.isArray(doc.documentBlocks) ? doc.documentBlocks : [])
|
|
119
|
+
.flatMap((b) => [asString(b?.content), asString(b?.dont)])
|
|
120
|
+
.filter(Boolean);
|
|
121
|
+
const blocksLower = blocks.map((b) => b.toLowerCase());
|
|
122
|
+
|
|
123
|
+
const hits = words.filter((w) =>
|
|
124
|
+
[name, descriptionLower, ...blocksLower].some(
|
|
125
|
+
(f) => wordIndex(f, w) >= 0,
|
|
126
|
+
),
|
|
127
|
+
);
|
|
128
|
+
if (hits.length === 0) continue;
|
|
129
|
+
|
|
130
|
+
// Snippet comes from the highest-ranked place a word matched.
|
|
131
|
+
let snippet = "";
|
|
132
|
+
const inDescription = hits
|
|
133
|
+
.map((w) => wordIndex(descriptionLower, w))
|
|
134
|
+
.filter((i) => i >= 0);
|
|
135
|
+
if (inDescription.length > 0) {
|
|
136
|
+
snippet = snippetAround(description, Math.min(...inDescription));
|
|
137
|
+
} else {
|
|
138
|
+
const blockIndex = blocksLower.findIndex((b) =>
|
|
139
|
+
hits.some((w) => wordIndex(b, w) >= 0),
|
|
140
|
+
);
|
|
141
|
+
if (blockIndex >= 0) {
|
|
142
|
+
const at = Math.min(
|
|
143
|
+
...hits
|
|
144
|
+
.map((w) => wordIndex(blocksLower[blockIndex], w))
|
|
145
|
+
.filter((i) => i >= 0),
|
|
146
|
+
);
|
|
147
|
+
snippet = snippetAround(blocks[blockIndex], at);
|
|
148
|
+
} else {
|
|
149
|
+
snippet = snippetAround(description, 0);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const tier =
|
|
154
|
+
name === phrase || words.includes(name)
|
|
155
|
+
? 0
|
|
156
|
+
: hits.some((w) => wordIndex(name, w) >= 0)
|
|
157
|
+
? 1
|
|
158
|
+
: hits.some((w) => wordIndex(descriptionLower, w) >= 0)
|
|
159
|
+
? 2
|
|
160
|
+
: 3;
|
|
161
|
+
|
|
162
|
+
scored.push({
|
|
163
|
+
all: hits.length === words.length,
|
|
164
|
+
tier,
|
|
165
|
+
count: hits.length,
|
|
166
|
+
result: { id, displayName, category: componentCategory, snippet },
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Fallback ignores docs-text-only hits (tier 3): too loose without all words.
|
|
171
|
+
const pool = scored.some((s) => s.all)
|
|
172
|
+
? scored.filter((s) => s.all)
|
|
173
|
+
: scored.filter((s) => s.tier < 3);
|
|
174
|
+
return pool
|
|
175
|
+
.sort((a, b) => a.tier - b.tier || b.count - a.count)
|
|
176
|
+
.slice(0, max)
|
|
177
|
+
.map((s) => s.result);
|
|
178
|
+
}
|