@scalar/import 0.2.23 → 0.2.25

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/CHANGELOG.md CHANGED
@@ -1,5 +1,30 @@
1
1
  # @scalar/import
2
2
 
3
+ ## 0.2.25
4
+
5
+ ### Patch Changes
6
+
7
+ - 54fdfcb: chore: remove spec wording
8
+ - fa6afe8: chore: code formatting
9
+ - 4d30a35: fix: can’t parse HTML attributes with HTML tags inside
10
+ - Updated dependencies [54fdfcb]
11
+ - Updated dependencies [1223c1f]
12
+ - @scalar/oas-utils@0.2.105
13
+ - @scalar/openapi-parser@0.10.7
14
+
15
+ ## 0.2.24
16
+
17
+ ### Patch Changes
18
+
19
+ - Updated dependencies [4de3124]
20
+ - Updated dependencies [f500435]
21
+ - Updated dependencies [da5cf08]
22
+ - Updated dependencies [b5727ef]
23
+ - Updated dependencies [4de3124]
24
+ - Updated dependencies [34e6f52]
25
+ - @scalar/openapi-parser@0.10.6
26
+ - @scalar/oas-utils@0.2.104
27
+
3
28
  ## 0.2.23
4
29
 
5
30
  ### Patch Changes
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export { resolve } from './resolve.js';
1
+ export { getConfigurationAttribute, getContentOfScriptTag, resolve } from './resolve.js';
package/dist/resolve.d.ts CHANGED
@@ -8,4 +8,16 @@ export declare function resolve(value?: string | null, options?: {
8
8
  */
9
9
  fetch?: (url: string) => Promise<Response>;
10
10
  }): Promise<string | Record<string, any> | undefined>;
11
+ /**
12
+ * Get the content between the script tags
13
+ *
14
+ * @example <script id="api-reference">console.log("Hello, world!");</script>
15
+ */
16
+ export declare function getContentOfScriptTag(html: string): string | undefined;
17
+ /**
18
+ * Get the configuration attribute from the script tag
19
+ *
20
+ * @example <script id="api-reference" data-configuration="{&quot;spec&quot;:{&quot;content&quot;:&quot;foo&quot;}}"></script>
21
+ */
22
+ export declare function getConfigurationAttribute(html: string): string | undefined;
11
23
  //# sourceMappingURL=resolve.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"resolve.d.ts","sourceRoot":"","sources":["../src/resolve.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,wBAAsB,OAAO,CAC3B,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,EACrB,OAAO,CAAC,EAAE;IACR;;OAEG;IACH,KAAK,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;CAC3C,GACA,OAAO,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,SAAS,CAAC,CAoHnD"}
1
+ {"version":3,"file":"resolve.d.ts","sourceRoot":"","sources":["../src/resolve.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,wBAAsB,OAAO,CAC3B,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,EACrB,OAAO,CAAC,EAAE;IACR;;OAEG;IACH,KAAK,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;CAC3C,GACA,OAAO,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,SAAS,CAAC,CA+GnD;AA6ED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAetE;AAgCD;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAiB1E"}
package/dist/resolve.js CHANGED
@@ -18,8 +18,7 @@ async function resolve(value, options) {
18
18
  return value;
19
19
  }
20
20
  // https://*.yaml
21
- if (value?.toLowerCase().endsWith('.yaml') ||
22
- value?.toLowerCase().endsWith('.yml')) {
21
+ if (value?.toLowerCase().endsWith('.yaml') || value?.toLowerCase().endsWith('.yml')) {
23
22
  return value;
24
23
  }
25
24
  // https://sandbox.scalar.com
@@ -166,21 +165,46 @@ function parseHtml(html) {
166
165
  }
167
166
  return undefined;
168
167
  }
168
+ /**
169
+ * Get the content between the script tags
170
+ *
171
+ * @example <script id="api-reference">console.log("Hello, world!");</script>
172
+ */
173
+ function getContentOfScriptTag(html) {
174
+ const patterns = [
175
+ // Double quotes
176
+ /<script[^>]*id="api-reference[\s\S]*?">([\s\S]*?)<\/script>/i,
177
+ // Single quotes
178
+ /<script[^>]*id='api-reference[\s\S]*?'>([\s\S]*?)<\/script>/i,
179
+ ];
180
+ for (const pattern of patterns) {
181
+ const match = html.match(pattern);
182
+ if (match?.[1])
183
+ return match[1].trim();
184
+ }
185
+ return undefined;
186
+ }
169
187
  /**
170
188
  * Parse OpenAPI document directly from script tag content
171
189
  */
172
190
  function parseScriptContent(html) {
173
- const match = html.match(/<script[^>]*id="api-reference"[^>]*>([\s\S]*?)<\/script>/);
174
- if (!match?.[1])
175
- return undefined;
191
+ const content = getContentOfScriptTag(html);
176
192
  try {
177
- const content = match[1].trim();
178
193
  if (content) {
179
194
  try {
195
+ // JSON
180
196
  return JSON.parse(content);
181
197
  }
182
198
  catch {
183
- return parse(content);
199
+ try {
200
+ // JSON with escaped whitespace
201
+ const sanitizedContent = content.replace(/\\s/g, '\\\\s');
202
+ return JSON.parse(sanitizedContent);
203
+ }
204
+ catch {
205
+ // YAML
206
+ return parse(content);
207
+ }
184
208
  }
185
209
  }
186
210
  }
@@ -189,16 +213,35 @@ function parseScriptContent(html) {
189
213
  }
190
214
  return undefined;
191
215
  }
216
+ /**
217
+ * Get the configuration attribute from the script tag
218
+ *
219
+ * @example <script id="api-reference" data-configuration="{&quot;spec&quot;:{&quot;content&quot;:&quot;foo&quot;}}"></script>
220
+ */
221
+ function getConfigurationAttribute(html) {
222
+ const patterns = [
223
+ // Double quotes
224
+ /<script[^>]*id="api-reference"[^>]*data-configuration=["]([^"]+)["][^>]*>(.*?)<\/script>/s,
225
+ // Single quotes
226
+ /<script[^>]*id='api-reference'[^>]*data-configuration=[']([^']+)['][^>]*>(.*?)<\/script>/s,
227
+ ];
228
+ for (const pattern of patterns) {
229
+ const match = html.match(pattern);
230
+ if (match?.[1]) {
231
+ return match[1];
232
+ }
233
+ }
234
+ return undefined;
235
+ }
192
236
  /**
193
237
  * Parse embedded OpenAPI document from HTML
194
238
  */
195
239
  function parseEmbeddedOpenApi(html) {
196
- const match = html.match(/<script[^>]*data-configuration=['"]([^'"]+)['"][^>]*>(.*?)<\/script>/s);
197
- if (!match?.[1])
240
+ const configString = getConfigurationAttribute(html);
241
+ if (!configString)
198
242
  return undefined;
199
243
  try {
200
- const configString = decodeHtmlEntities(match[1]);
201
- const config = JSON.parse(configString);
244
+ const config = JSON.parse(decodeHtmlEntities(configString));
202
245
  // Handle both direct JSON content and YAML content
203
246
  if (config.spec?.content) {
204
247
  // If content is a string, assume it's YAML
@@ -248,4 +291,4 @@ function transformGitHubUrl(url) {
248
291
  return undefined;
249
292
  }
250
293
 
251
- export { resolve };
294
+ export { getConfigurationAttribute, getContentOfScriptTag, resolve };
package/package.json CHANGED
@@ -16,7 +16,7 @@
16
16
  "postman",
17
17
  "scalar"
18
18
  ],
19
- "version": "0.2.23",
19
+ "version": "0.2.25",
20
20
  "engines": {
21
21
  "node": ">=18"
22
22
  },
@@ -36,16 +36,18 @@
36
36
  "module": "dist/index.js",
37
37
  "dependencies": {
38
38
  "yaml": "^2.4.5",
39
- "@scalar/oas-utils": "0.2.103",
40
- "@scalar/openapi-parser": "0.10.5"
39
+ "@scalar/oas-utils": "0.2.105",
40
+ "@scalar/openapi-parser": "0.10.7"
41
41
  },
42
42
  "devDependencies": {
43
43
  "vite": "^5.4.10",
44
- "@scalar/build-tooling": "0.1.12"
44
+ "@scalar/build-tooling": "0.1.13"
45
45
  },
46
46
  "scripts": {
47
47
  "build": "scalar-build-rollup",
48
48
  "dev": "nodemon --exec \"vite-node playground/index.ts\" --ext ts --quiet",
49
+ "format": "scalar-format",
50
+ "format:check": "scalar-format-check",
49
51
  "lint:check": "eslint .",
50
52
  "lint:fix": "eslint . --fix",
51
53
  "test": "vitest",