Skip to content

Commit ceab939

Browse files
authored
Fix broken links in JSON responses (Huachao#620)
* Fix broken links in JSON responses Links inside the a JSON response are not displayed properly because JSON encodes strings. This fix displays the original url but the underlying link is using the parsed string as the link `href` property. * Check contentType before attempting to JSON.parse When changing URLs into links, check contentType before attempting to JSON.parse
1 parent 1b03b6b commit ceab939

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

‎src/views/httpResponseWebview.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export class HttpResponseWebview extends BaseWebview {
217217
<div>
218218
${this.settings.disableAddingHrefLinkForLargeResponse && response.bodySizeInBytes > this.settings.largeResponseBodySizeLimitInMB * 1024 * 1024
219219
? innerHtml
220-
: this.addUrlLinks(innerHtml)}
220+
: this.addUrlLinks(innerHtml, contentType)}
221221
<a id="scroll-to-top" role="button" aria-label="scroll to top" title="Scroll To Top"><span class="icon"></span></a>
222222
</div>
223223
<script type="text/javascript" src="${panel.webview.asWebviewUri(this.scriptFilePath)}" nonce="${nonce}" charset="UTF-8"></script>
@@ -350,8 +350,15 @@ ${HttpResponseWebview.formatHeaders(response.headers)}`;
350350
});
351351
}
352352

353-
private addUrlLinks(innerHtml: string) {
354-
return innerHtml.replace(this.urlRegex, '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>');
353+
private addUrlLinks(innerHtml: string, contentType: string | undefined) {
354+
if (MimeUtility.isJSON(contentType)) {
355+
return innerHtml.replace(this.urlRegex, match => {
356+
const parsedLink = JSON.parse(`"${match}"`);
357+
return `<a href=${parsedLink} target="_blank" rel="noopener noreferrer">${match}</a>`;
358+
});
359+
} else {
360+
return innerHtml.replace(this.urlRegex, '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>');
361+
}
355362
}
356363

357364
private getFoldingRange(lines: string[]): Map<number, FoldingRange> {
@@ -417,4 +424,4 @@ ${HttpResponseWebview.formatHeaders(response.headers)}`;
417424
private static isHeadRequest({ request: { method } }: { request: HttpRequest }): boolean {
418425
return method.toLowerCase() === 'head';
419426
}
420-
}
427+
}

0 commit comments

Comments
 (0)