Skip to content

Commit

Permalink
fix(openapi): avoid extra line-break chars in copied code (#2147)
Browse files Browse the repository at this point in the history
* fix(openapi): avoid extra line-break chars in copied code

* feat: add server URL prefix
  • Loading branch information
zchsh authored Aug 30, 2023
1 parent 66dbb80 commit 22d4c3b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
14 changes: 13 additions & 1 deletion src/views/open-api-docs-view/utils/get-operation-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ import type { OpenAPIV3 } from 'openapi-types'
export async function getOperationProps(
schemaJson: OpenAPIV3.Document
): Promise<OperationProps[]> {
// Grab the server URL, we'll use this to build URL paths for each operation
let serverUrl = ''
if (schemaJson.servers?.length > 0) {
const rawServerUrl = schemaJson.servers[0].url
/**
* If we up-converted from an older OpenAPI version, then the spec would
* have defined a `host` rather than explicit server URL, and the resulting
* server URL will start with `//` rather than a valid protocol.
* In this case we assume HTTPs, and update the serverUrl accordingly.
*/
serverUrl = rawServerUrl.replace(/^\/\//, 'https://')
}
// Set up an accumulator array
const operationObjects: OperationProps[] = []
/**
Expand Down Expand Up @@ -90,7 +102,7 @@ export async function getOperationProps(
summary,
requestData,
responseData,
urlPathForCodeBlock: getUrlPathCodeHtml(path),
urlPathForCodeBlock: getUrlPathCodeHtml(serverUrl + path),
_placeholder: {
__type: type,
__path: path,
Expand Down
11 changes: 7 additions & 4 deletions src/views/open-api-docs-view/utils/get-url-path-code-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
* SPDX-License-Identifier: MPL-2.0
*/

import { addWordBreaksToUrl } from './add-word-breaks-to-url'

/**
* Given a URL path,
* Return HTML that represents the URL path, with `{parameters}` syntax
* highlighted and with word breaks inserted before forward slashes
* to allow long URLs to wrap to multiple lines.
*/
export default function getUrlPathCodeHtml(urlPath: string): string {
// Insert word breaks before forward slashes for more logical line breaks
const urlWithWordBreaks = addWordBreaksToUrl(urlPath)
/**
* Insert <wbr/> before forward slashes for more logical line breaks
*
* Note: we can't use zero-width spaces here as in other use cases,
* as they show up when the code block contents are copied.
*/
const urlWithWordBreaks = urlPath.replace(/\//g, '/<wbr/>')
// Add syntax highlighting around any {parameters} in the path
const parameterRegex = /{([^}]+)}/g
const urlPathForCodeBlock = urlWithWordBreaks.replace(
Expand Down

1 comment on commit 22d4c3b

@vercel
Copy link

@vercel vercel bot commented on 22d4c3b Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.