@scalar/import 0.4.35 → 0.4.36
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 +9 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +2 -2
- package/dist/resolve.d.ts +0 -2
- package/dist/resolve.d.ts.map +1 -1
- package/dist/resolve.js +0 -1
- package/dist/resolve.js.map +2 -2
- package/package.json +3 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @scalar/import
|
|
2
2
|
|
|
3
|
+
## 0.4.36
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#7408](https://github.com/scalar/scalar/pull/7408) [`612eb05`](https://github.com/scalar/scalar/commit/612eb05f61ad97272206f1b7ff85a725131ca917) Thanks [@marcalexiei](https://github.com/marcalexiei)! - fix(import): remove `@scalar/openapi-parser` unused dependency
|
|
8
|
+
|
|
9
|
+
- Updated dependencies [[`bfd814a`](https://github.com/scalar/scalar/commit/bfd814a4219660face190041cc4845182b56ab03), [`86f028d`](https://github.com/scalar/scalar/commit/86f028deb0b456f923edd261f5f4b0fa9b616b7d)]:
|
|
10
|
+
- @scalar/helpers@0.1.3
|
|
11
|
+
|
|
3
12
|
## 0.4.35
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,iEAAiE;AACjE,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA"}
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["/** biome-ignore-all lint/performance/noBarrelFile: entrypoint */\nexport { resolve } from './resolve'\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,eAAe;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/resolve.d.ts
CHANGED
|
@@ -20,6 +20,4 @@ export declare function getContentOfScriptTag(html: string): string | undefined;
|
|
|
20
20
|
* @example <script id="api-reference" data-configuration="{"spec":{"content":"foo"}}"></script>
|
|
21
21
|
*/
|
|
22
22
|
export declare function getConfigurationAttribute(html: string): string | undefined;
|
|
23
|
-
/** Grab the URL from the configuration data attribute */
|
|
24
|
-
export declare const getConfigurationAttributeUrl: (html: string) => string | undefined;
|
|
25
23
|
//# sourceMappingURL=resolve.d.ts.map
|
package/dist/resolve.d.ts.map
CHANGED
|
@@ -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,CAyHnD;AA6ED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAiBtE;AAgCD;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAqB1E
|
|
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,CAyHnD;AA6ED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAiBtE;AAgCD;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAqB1E"}
|
package/dist/resolve.js
CHANGED
package/dist/resolve.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/resolve.ts"],
|
|
4
|
-
"sourcesContent": ["import { makeUrlAbsolute } from '@scalar/helpers/url/make-url-absolute'\nimport { parse } from 'yaml'\n\n/**\n * Find an OpenAPI document URL in the HTML of @scalar/api-reference and other places.\n * This is useful to open the OpenAPI document from basically any source.\n */\nexport async function resolve(\n value?: string | null,\n options?: {\n /**\n * Fetch function to use instead of the global fetch. Use this to intercept requests.\n */\n fetch?: (url: string) => Promise<Response>\n },\n): Promise<string | Record<string, any> | undefined> {\n // URLs\n if (value?.startsWith('http://') || value?.startsWith('https://')) {\n // Transform GitHub URLs to raw file URLs\n const githubRawUrl = transformGitHubUrl(value)\n\n if (githubRawUrl) {\n return githubRawUrl\n }\n\n // https://*.json\n if (value?.toLowerCase().endsWith('.json')) {\n return value\n }\n\n // https://*.yaml\n if (value?.toLowerCase().endsWith('.yaml') || value?.toLowerCase().endsWith('.yml')) {\n return value\n }\n\n // https://sandbox.scalar.com\n const sandboxUrl = value.match(/https:\\/\\/sandbox\\.scalar\\.com\\/(p|e)\\/([a-zA-Z0-9]+)/)\n\n if (sandboxUrl?.[2]) {\n return `https://sandbox.scalar.com/files/${sandboxUrl[2]}/openapi.yaml`\n }\n\n // Fetch URL\n try {\n let result = await (options?.fetch\n ? options.fetch(value)\n : fetch(value, {\n cache: 'no-cache',\n }))\n\n // If the custom fetch failed, try again with regular fetch\n if (!result.ok && options?.fetch) {\n result = await fetch(value, {\n cache: 'no-cache',\n })\n }\n\n if (result.ok) {\n const content = await result.text()\n const forwardedHost = result.headers.get('X-Forwarded-Host')\n\n // Check if content is directly JSON/YAML\n try {\n // Try parsing as JSON\n const jsonContent = JSON.parse(content)\n if (jsonContent.openapi || jsonContent.swagger) {\n return value\n }\n } catch {\n // Try parsing as YAML\n try {\n const yamlContent = parse(content)\n if (yamlContent?.openapi || yamlContent?.swagger) {\n return value\n }\n } catch {\n // Not YAML\n }\n }\n\n const urlOrPathOrDocument = parseHtml(content)\n\n // Document (string)\n if (typeof urlOrPathOrDocument === 'string') {\n try {\n // JSON?\n return JSON.parse(urlOrPathOrDocument)\n } catch {\n // No JSON\n try {\n // YAML?\n const yaml = parse(urlOrPathOrDocument)\n\n if (typeof yaml === 'object') {\n return yaml\n }\n } catch {\n // Not YAML\n }\n }\n }\n\n // Document (object)\n if (typeof urlOrPathOrDocument === 'object') {\n return urlOrPathOrDocument\n }\n\n // Relative or absolute URL\n if (urlOrPathOrDocument) {\n return makeUrlAbsolute(urlOrPathOrDocument, {\n baseUrl: forwardedHost || value,\n })\n }\n\n // Check for configuration attribute URL\n const configUrl = getConfigurationAttributeUrl(content)\n if (configUrl) {\n return makeUrlAbsolute(configUrl, {\n baseUrl: forwardedHost || value,\n })\n }\n\n // Check for embedded OpenAPI document\n const embeddedSpec = parseEmbeddedOpenApi(content)\n if (embeddedSpec) {\n return embeddedSpec\n }\n } else {\n console.warn(`[@scalar/import] Failed to fetch ${value}`)\n }\n } catch (error) {\n console.error(`[@scalar/import] Failed to fetch ${value}`, error)\n }\n }\n\n return undefined\n}\n\n/**\n * Go through the HTML and try to find the OpenAPI document URL\n */\nfunction parseHtml(html?: string) {\n // Check whether it could be HTML\n if (!html?.includes('<')) {\n return undefined\n }\n\n // id=\"api-reference\" data-url=\"*\"\n const dataUrlMatch = html\n .match(/id=[\"']api-reference[\"'][\\s\\S]*?data-url=[\"']([^\"']+)[\"']/)\n ?.slice(1)\n .find(Boolean)\n\n if (dataUrlMatch) {\n return dataUrlMatch\n }\n\n // spec-url=\"*\"\n const specUrlMatch = html.match(/spec-url=[\"']([^\"']+)[\"']/)\n if (specUrlMatch?.[1]) {\n return specUrlMatch[1]\n }\n\n // Redoc.init('*')\n const redocInit = html.match(/Redoc.init\\([\"']([^\"']+)[\"']/)\n\n if (redocInit?.[1]) {\n return redocInit[1]\n }\n\n // &quot;url&quot;:&quot;MY_CUSTOM_URL&quot;\n const doubleEncodedConfigurationUrl = html.match(/&quot;url&quot;:&quot;([^;]+)&quot;/)\n\n if (doubleEncodedConfigurationUrl?.[1]) {\n return doubleEncodedConfigurationUrl[1]\n }\n\n // &quot;url&quot;:&quot;MY_CUSTOM_URL&quot;\n const encodedConfigurationUrl = html.match(/"url":"([^;]+)"/)\n if (encodedConfigurationUrl?.[1]) {\n return encodedConfigurationUrl[1]\n }\n\n // Try to find embedded OpenAPI document in script tag first\n const scriptContent = parseScriptContent(html)\n if (scriptContent) {\n return scriptContent\n }\n\n // Check for configuration in script tag\n const scriptConfigMatch = html.match(/url:\\s*[\"']([^\"']+)[\"']/i)\n\n if (scriptConfigMatch?.[1]) {\n return scriptConfigMatch?.[1]\n }\n\n // Check for OpenAPI URLs in the HTML\n const linkMatch = html.match(/<a[^>]*href=[\"']([^\"']+\\.(?:yaml|yml|json))[\"'][^>]*>/i)\n\n if (linkMatch?.[1]) {\n return linkMatch[1]\n }\n\n // Check for URLs in escaped JS objects\n const escapedJsonMatch = html.match(/\\\\\"spec\\\\\":\\{.*?\\\\\"url\\\\\":\\\\\"([^\"\\\\]+)\\\\\"/)\n\n if (escapedJsonMatch?.[1]) {\n return escapedJsonMatch[1]\n }\n\n return undefined\n}\n\n/**\n * Get the content between the script tags\n *\n * @example <script id=\"api-reference\">console.log(\"Hello, world!\");</script>\n */\nexport function getContentOfScriptTag(html: string): string | undefined {\n const patterns = [\n // Double quotes\n /<script[^>]*id=\"api-reference[\\s\\S]*?\">([\\s\\S]*?)<\\/script>/i,\n // Single quotes\n /<script[^>]*id='api-reference[\\s\\S]*?'>([\\s\\S]*?)<\\/script>/i,\n ]\n\n for (const pattern of patterns) {\n const match = html.match(pattern)\n\n if (match?.[1]) {\n return match[1].trim()\n }\n }\n\n return undefined\n}\n\n/**\n * Parse OpenAPI document directly from script tag content\n */\nfunction parseScriptContent(html: string): Record<string, any> | undefined {\n const content = getContentOfScriptTag(html)\n\n try {\n if (content) {\n try {\n // JSON\n return JSON.parse(content)\n } catch {\n try {\n // JSON with escaped whitespace\n const sanitizedContent = content.replace(/\\\\s/g, '\\\\\\\\s')\n\n return JSON.parse(sanitizedContent)\n } catch {\n // YAML\n return parse(content)\n }\n }\n }\n } catch (error) {\n console.error('[@scalar/import] Failed to parse script content:', error)\n }\n\n return undefined\n}\n\n/**\n * Get the configuration attribute from the script tag\n *\n * @example <script id=\"api-reference\" data-configuration=\"{"spec":{"content":"foo"}}\"></script>\n */\nexport function getConfigurationAttribute(html: string): string | undefined {\n const patterns = [\n // Double quotes\n /<script[^>]*id=\"api-reference\"[^>]*data-configuration=[\"]([^\"]+)[\"][^>]*>(.*?)<\\/script>/s,\n // Single quotes\n /<script[^>]*id='api-reference'[^>]*data-configuration=[']([^']+)['][^>]*>(.*?)<\\/script>/s,\n // Mix quote single first\n /<script[^>]*id='api-reference'[^>]*data-configuration=[\"]([^\"]+)[\"][^>]*>(.*?)<\\/script>/s,\n // Mix quote double first\n /<script[^>]*id=\"api-reference\"[^>]*data-configuration=[']([^']+)['][^>]*>(.*?)<\\/script>/s,\n ]\n\n for (const pattern of patterns) {\n const match = html.match(pattern)\n\n if (match?.[1]) {\n return match[1]\n }\n }\n\n return undefined\n}\n\n/** Grab the URL from the configuration data attribute */\nexport const getConfigurationAttributeUrl = (html: string): string | undefined => {\n const configString = getConfigurationAttribute(html)\n if (!configString) {\n return undefined\n }\n\n const config = JSON.parse(decodeHtmlEntities(configString))\n return config.url || config.spec?.url\n}\n\n/**\n * Parse embedded OpenAPI document from HTML\n */\nfunction parseEmbeddedOpenApi(html: string): object | undefined {\n const configString = getConfigurationAttribute(html)\n if (!configString) {\n return undefined\n }\n\n try {\n const config = JSON.parse(decodeHtmlEntities(configString))\n\n // Handle the old spec format while we transition to the new one\n const content = config.content || config.spec?.content\n\n // Handle both direct JSON content and YAML content\n if (content) {\n // If content is a string, assume it's YAML\n if (typeof content === 'string') {\n return parse(content)\n }\n // If content is an object, return it directly\n return content\n }\n } catch (error) {\n console.error('[@scalar/import] Failed to parse embedded OpenAPI document:', error)\n }\n\n return undefined\n}\n\n/**\n * Decode HTML entities in a string\n */\nfunction decodeHtmlEntities(text: string): string {\n const entities = {\n '&': '&',\n '<': '<',\n '>': '>',\n '"': '\"',\n ''': \"'\",\n } as const\n\n const updatedText = text\n .replace(new RegExp(Object.keys(entities).join('|'), 'g'), (match) => entities[match as keyof typeof entities])\n .replace(/\\n/g, '\\\\n')\n .trim()\n\n if (updatedText.startsWith('{"')) {\n // console.log('ok', updatedText)\n return decodeHtmlEntities(updatedText)\n }\n\n return updatedText\n}\n\n/**\n * Transform GitHub URLs to raw file URLs, preserving the branch information\n */\nfunction transformGitHubUrl(url: string): string | undefined {\n const githubRegex = /^https:\\/\\/github\\.com\\/([^/]+)\\/([^/]+)\\/blob\\/([^/]+)\\/(.+)$/\n const match = url.match(githubRegex)\n\n if (match) {\n const [, owner, repo, branch, path] = match\n return `https://raw.githubusercontent.com/${owner}/${repo}/refs/heads/${branch}/${path}`\n }\n\n return undefined\n}\n"],
|
|
5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EACzE;AAEA,SAAO;AACT;AAOO,SAAS,0BAA0B,MAAkC;AAC1E,QAAM,WAAW;AAAA;AAAA,IAEf;AAAA;AAAA,IAEA;AAAA;AAAA,IAEA;AAAA;AAAA,IAEA;AAAA,EACF;AAEA,aAAW,WAAW,UAAU;AAC9B,UAAM,QAAQ,KAAK,MAAM,OAAO;AAEhC,QAAI,QAAQ,CAAC,GAAG;AACd,aAAO,MAAM,CAAC;AAAA,IAChB;AAAA,EACF;AAEA,SAAO;AACT;
|
|
4
|
+
"sourcesContent": ["import { makeUrlAbsolute } from '@scalar/helpers/url/make-url-absolute'\nimport { parse } from 'yaml'\n\n/**\n * Find an OpenAPI document URL in the HTML of @scalar/api-reference and other places.\n * This is useful to open the OpenAPI document from basically any source.\n */\nexport async function resolve(\n value?: string | null,\n options?: {\n /**\n * Fetch function to use instead of the global fetch. Use this to intercept requests.\n */\n fetch?: (url: string) => Promise<Response>\n },\n): Promise<string | Record<string, any> | undefined> {\n // URLs\n if (value?.startsWith('http://') || value?.startsWith('https://')) {\n // Transform GitHub URLs to raw file URLs\n const githubRawUrl = transformGitHubUrl(value)\n\n if (githubRawUrl) {\n return githubRawUrl\n }\n\n // https://*.json\n if (value?.toLowerCase().endsWith('.json')) {\n return value\n }\n\n // https://*.yaml\n if (value?.toLowerCase().endsWith('.yaml') || value?.toLowerCase().endsWith('.yml')) {\n return value\n }\n\n // https://sandbox.scalar.com\n const sandboxUrl = value.match(/https:\\/\\/sandbox\\.scalar\\.com\\/(p|e)\\/([a-zA-Z0-9]+)/)\n\n if (sandboxUrl?.[2]) {\n return `https://sandbox.scalar.com/files/${sandboxUrl[2]}/openapi.yaml`\n }\n\n // Fetch URL\n try {\n let result = await (options?.fetch\n ? options.fetch(value)\n : fetch(value, {\n cache: 'no-cache',\n }))\n\n // If the custom fetch failed, try again with regular fetch\n if (!result.ok && options?.fetch) {\n result = await fetch(value, {\n cache: 'no-cache',\n })\n }\n\n if (result.ok) {\n const content = await result.text()\n const forwardedHost = result.headers.get('X-Forwarded-Host')\n\n // Check if content is directly JSON/YAML\n try {\n // Try parsing as JSON\n const jsonContent = JSON.parse(content)\n if (jsonContent.openapi || jsonContent.swagger) {\n return value\n }\n } catch {\n // Try parsing as YAML\n try {\n const yamlContent = parse(content)\n if (yamlContent?.openapi || yamlContent?.swagger) {\n return value\n }\n } catch {\n // Not YAML\n }\n }\n\n const urlOrPathOrDocument = parseHtml(content)\n\n // Document (string)\n if (typeof urlOrPathOrDocument === 'string') {\n try {\n // JSON?\n return JSON.parse(urlOrPathOrDocument)\n } catch {\n // No JSON\n try {\n // YAML?\n const yaml = parse(urlOrPathOrDocument)\n\n if (typeof yaml === 'object') {\n return yaml\n }\n } catch {\n // Not YAML\n }\n }\n }\n\n // Document (object)\n if (typeof urlOrPathOrDocument === 'object') {\n return urlOrPathOrDocument\n }\n\n // Relative or absolute URL\n if (urlOrPathOrDocument) {\n return makeUrlAbsolute(urlOrPathOrDocument, {\n baseUrl: forwardedHost || value,\n })\n }\n\n // Check for configuration attribute URL\n const configUrl = getConfigurationAttributeUrl(content)\n if (configUrl) {\n return makeUrlAbsolute(configUrl, {\n baseUrl: forwardedHost || value,\n })\n }\n\n // Check for embedded OpenAPI document\n const embeddedSpec = parseEmbeddedOpenApi(content)\n if (embeddedSpec) {\n return embeddedSpec\n }\n } else {\n console.warn(`[@scalar/import] Failed to fetch ${value}`)\n }\n } catch (error) {\n console.error(`[@scalar/import] Failed to fetch ${value}`, error)\n }\n }\n\n return undefined\n}\n\n/**\n * Go through the HTML and try to find the OpenAPI document URL\n */\nfunction parseHtml(html?: string) {\n // Check whether it could be HTML\n if (!html?.includes('<')) {\n return undefined\n }\n\n // id=\"api-reference\" data-url=\"*\"\n const dataUrlMatch = html\n .match(/id=[\"']api-reference[\"'][\\s\\S]*?data-url=[\"']([^\"']+)[\"']/)\n ?.slice(1)\n .find(Boolean)\n\n if (dataUrlMatch) {\n return dataUrlMatch\n }\n\n // spec-url=\"*\"\n const specUrlMatch = html.match(/spec-url=[\"']([^\"']+)[\"']/)\n if (specUrlMatch?.[1]) {\n return specUrlMatch[1]\n }\n\n // Redoc.init('*')\n const redocInit = html.match(/Redoc.init\\([\"']([^\"']+)[\"']/)\n\n if (redocInit?.[1]) {\n return redocInit[1]\n }\n\n // &quot;url&quot;:&quot;MY_CUSTOM_URL&quot;\n const doubleEncodedConfigurationUrl = html.match(/&quot;url&quot;:&quot;([^;]+)&quot;/)\n\n if (doubleEncodedConfigurationUrl?.[1]) {\n return doubleEncodedConfigurationUrl[1]\n }\n\n // &quot;url&quot;:&quot;MY_CUSTOM_URL&quot;\n const encodedConfigurationUrl = html.match(/"url":"([^;]+)"/)\n if (encodedConfigurationUrl?.[1]) {\n return encodedConfigurationUrl[1]\n }\n\n // Try to find embedded OpenAPI document in script tag first\n const scriptContent = parseScriptContent(html)\n if (scriptContent) {\n return scriptContent\n }\n\n // Check for configuration in script tag\n const scriptConfigMatch = html.match(/url:\\s*[\"']([^\"']+)[\"']/i)\n\n if (scriptConfigMatch?.[1]) {\n return scriptConfigMatch?.[1]\n }\n\n // Check for OpenAPI URLs in the HTML\n const linkMatch = html.match(/<a[^>]*href=[\"']([^\"']+\\.(?:yaml|yml|json))[\"'][^>]*>/i)\n\n if (linkMatch?.[1]) {\n return linkMatch[1]\n }\n\n // Check for URLs in escaped JS objects\n const escapedJsonMatch = html.match(/\\\\\"spec\\\\\":\\{.*?\\\\\"url\\\\\":\\\\\"([^\"\\\\]+)\\\\\"/)\n\n if (escapedJsonMatch?.[1]) {\n return escapedJsonMatch[1]\n }\n\n return undefined\n}\n\n/**\n * Get the content between the script tags\n *\n * @example <script id=\"api-reference\">console.log(\"Hello, world!\");</script>\n */\nexport function getContentOfScriptTag(html: string): string | undefined {\n const patterns = [\n // Double quotes\n /<script[^>]*id=\"api-reference[\\s\\S]*?\">([\\s\\S]*?)<\\/script>/i,\n // Single quotes\n /<script[^>]*id='api-reference[\\s\\S]*?'>([\\s\\S]*?)<\\/script>/i,\n ]\n\n for (const pattern of patterns) {\n const match = html.match(pattern)\n\n if (match?.[1]) {\n return match[1].trim()\n }\n }\n\n return undefined\n}\n\n/**\n * Parse OpenAPI document directly from script tag content\n */\nfunction parseScriptContent(html: string): Record<string, any> | undefined {\n const content = getContentOfScriptTag(html)\n\n try {\n if (content) {\n try {\n // JSON\n return JSON.parse(content)\n } catch {\n try {\n // JSON with escaped whitespace\n const sanitizedContent = content.replace(/\\\\s/g, '\\\\\\\\s')\n\n return JSON.parse(sanitizedContent)\n } catch {\n // YAML\n return parse(content)\n }\n }\n }\n } catch (error) {\n console.error('[@scalar/import] Failed to parse script content:', error)\n }\n\n return undefined\n}\n\n/**\n * Get the configuration attribute from the script tag\n *\n * @example <script id=\"api-reference\" data-configuration=\"{"spec":{"content":"foo"}}\"></script>\n */\nexport function getConfigurationAttribute(html: string): string | undefined {\n const patterns = [\n // Double quotes\n /<script[^>]*id=\"api-reference\"[^>]*data-configuration=[\"]([^\"]+)[\"][^>]*>(.*?)<\\/script>/s,\n // Single quotes\n /<script[^>]*id='api-reference'[^>]*data-configuration=[']([^']+)['][^>]*>(.*?)<\\/script>/s,\n // Mix quote single first\n /<script[^>]*id='api-reference'[^>]*data-configuration=[\"]([^\"]+)[\"][^>]*>(.*?)<\\/script>/s,\n // Mix quote double first\n /<script[^>]*id=\"api-reference\"[^>]*data-configuration=[']([^']+)['][^>]*>(.*?)<\\/script>/s,\n ]\n\n for (const pattern of patterns) {\n const match = html.match(pattern)\n\n if (match?.[1]) {\n return match[1]\n }\n }\n\n return undefined\n}\n\n/** Grab the URL from the configuration data attribute */\nconst getConfigurationAttributeUrl = (html: string): string | undefined => {\n const configString = getConfigurationAttribute(html)\n if (!configString) {\n return undefined\n }\n\n const config = JSON.parse(decodeHtmlEntities(configString))\n return config.url || config.spec?.url\n}\n\n/**\n * Parse embedded OpenAPI document from HTML\n */\nfunction parseEmbeddedOpenApi(html: string): object | undefined {\n const configString = getConfigurationAttribute(html)\n if (!configString) {\n return undefined\n }\n\n try {\n const config = JSON.parse(decodeHtmlEntities(configString))\n\n // Handle the old spec format while we transition to the new one\n const content = config.content || config.spec?.content\n\n // Handle both direct JSON content and YAML content\n if (content) {\n // If content is a string, assume it's YAML\n if (typeof content === 'string') {\n return parse(content)\n }\n // If content is an object, return it directly\n return content\n }\n } catch (error) {\n console.error('[@scalar/import] Failed to parse embedded OpenAPI document:', error)\n }\n\n return undefined\n}\n\n/**\n * Decode HTML entities in a string\n */\nfunction decodeHtmlEntities(text: string): string {\n const entities = {\n '&': '&',\n '<': '<',\n '>': '>',\n '"': '\"',\n ''': \"'\",\n } as const\n\n const updatedText = text\n .replace(new RegExp(Object.keys(entities).join('|'), 'g'), (match) => entities[match as keyof typeof entities])\n .replace(/\\n/g, '\\\\n')\n .trim()\n\n if (updatedText.startsWith('{"')) {\n // console.log('ok', updatedText)\n return decodeHtmlEntities(updatedText)\n }\n\n return updatedText\n}\n\n/**\n * Transform GitHub URLs to raw file URLs, preserving the branch information\n */\nfunction transformGitHubUrl(url: string): string | undefined {\n const githubRegex = /^https:\\/\\/github\\.com\\/([^/]+)\\/([^/]+)\\/blob\\/([^/]+)\\/(.+)$/\n const match = url.match(githubRegex)\n\n if (match) {\n const [, owner, repo, branch, path] = match\n return `https://raw.githubusercontent.com/${owner}/${repo}/refs/heads/${branch}/${path}`\n }\n\n return undefined\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,uBAAuB;AAChC,SAAS,aAAa;AAMtB,eAAsB,QACpB,OACA,SAMmD;AAEnD,MAAI,OAAO,WAAW,SAAS,KAAK,OAAO,WAAW,UAAU,GAAG;AAEjE,UAAM,eAAe,mBAAmB,KAAK;AAE7C,QAAI,cAAc;AAChB,aAAO;AAAA,IACT;AAGA,QAAI,OAAO,YAAY,EAAE,SAAS,OAAO,GAAG;AAC1C,aAAO;AAAA,IACT;AAGA,QAAI,OAAO,YAAY,EAAE,SAAS,OAAO,KAAK,OAAO,YAAY,EAAE,SAAS,MAAM,GAAG;AACn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/package.json
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"postman",
|
|
17
17
|
"scalar"
|
|
18
18
|
],
|
|
19
|
-
"version": "0.4.
|
|
19
|
+
"version": "0.4.36",
|
|
20
20
|
"engines": {
|
|
21
21
|
"node": ">=20"
|
|
22
22
|
},
|
|
@@ -36,12 +36,11 @@
|
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"yaml": "2.8.0",
|
|
39
|
-
"@scalar/
|
|
40
|
-
"@scalar/helpers": "0.1.2"
|
|
39
|
+
"@scalar/helpers": "0.1.3"
|
|
41
40
|
},
|
|
42
41
|
"devDependencies": {
|
|
43
42
|
"vite": "7.1.11",
|
|
44
|
-
"@scalar/build-tooling": "0.3.
|
|
43
|
+
"@scalar/build-tooling": "0.3.1"
|
|
45
44
|
},
|
|
46
45
|
"scripts": {
|
|
47
46
|
"build": "scalar-build-esbuild",
|