Path: blob/main/extensions/markdown-language-features/src/util/url.ts
3292 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as vscode from 'vscode';67/**8* Tries to convert an url into a vscode uri and returns undefined if this is not possible.9* `url` can be absolute or relative.10*/11export function urlToUri(url: string, base: vscode.Uri): vscode.Uri | undefined {12try {13// `vscode.Uri.joinPath` cannot be used, since it understands14// `src` as path, not as relative url. This is problematic for query args.15const parsedUrl = new URL(url, base.toString());16const uri = vscode.Uri.parse(parsedUrl.toString());17return uri;18} catch (e) {19// Don't crash if `URL` cannot parse `src`.20return undefined;21}22}232425