Skip to content

Commit

Permalink
fix(vsc): relative markdown file link in sample readme (#12975)
Browse files Browse the repository at this point in the history
  • Loading branch information
tecton authored Jan 3, 2025
1 parent db74b8e commit 1c4512e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/vscode-extension/src/controls/webviewPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import {
TelemetryProperty,
TelemetryTriggerFrom,
} from "../telemetry/extTelemetryEvents";
import { getTriggerFromProperty, isTriggerFromWalkThrough } from "../utils/telemetryUtils";
import { localize } from "../utils/localizeUtils";
import { getTriggerFromProperty, isTriggerFromWalkThrough } from "../utils/telemetryUtils";
import { compare } from "../utils/versionUtil";
import { Commands } from "./Commands";
import { PanelType } from "./PanelType";
Expand Down Expand Up @@ -265,6 +265,7 @@ export class WebviewPanel {
}
if (this.panel && this.panel.webview) {
let readme = this.replaceRelativeImagePaths(htmlContent, sample);
readme = this.replaceRelativeMarkdownPaths(htmlContent, sample);
readme = this.replaceMermaidRelatedContent(readme);
readme = this.addTabIndex(readme);
await this.panel.webview.postMessage({
Expand Down Expand Up @@ -296,6 +297,14 @@ export class WebviewPanel {
return loaderRemovedHtmlContent.replace(mermaidRegex, `<pre class="mermaid"`);
}

private replaceRelativeMarkdownPaths(htmlContent: string, sample: SampleConfig) {
const markdownRegex = /a\shref="([\.\-_/\\\w]*\.md)"/g;
const urlInfo = sample.downloadUrlInfo;
const markdownUrl = `https://github.com/${urlInfo.owner}/${urlInfo.repository}/tree/${urlInfo.ref}/${urlInfo.dir}`;
const result = htmlContent.replace(markdownRegex, `a href="${markdownUrl}/$1"`);
return result;
}

private addTabIndex(htmlContent: string): string {
const tabIndexRegex = /<(p|h1|h2|h3|li)/gm;
return htmlContent.replace(tabIndexRegex, `<$1 tabIndex="0"`);
Expand Down

0 comments on commit 1c4512e

Please sign in to comment.