This repository was archived by the owner on May 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
feat: add button to open markdown in VSCode preview #10773
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
webview-ui/src/components/chat/OpenMarkdownPreviewButton.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import React, { memo } from "react" | ||
| import { SquareArrowOutUpRight } from "lucide-react" | ||
|
|
||
| import { vscode } from "@src/utils/vscode" | ||
| import { hasComplexMarkdown } from "@src/utils/markdown" | ||
| import { StandardTooltip } from "@src/components/ui" | ||
|
|
||
| interface OpenMarkdownPreviewButtonProps { | ||
| markdown: string | undefined | ||
| className?: string | ||
| } | ||
|
|
||
| export const OpenMarkdownPreviewButton = memo(({ markdown, className }: OpenMarkdownPreviewButtonProps) => { | ||
| if (!hasComplexMarkdown(markdown)) { | ||
| return null | ||
| } | ||
|
|
||
| const handleClick = (e: React.MouseEvent) => { | ||
| e.stopPropagation() | ||
| if (markdown) { | ||
| vscode.postMessage({ | ||
| type: "openMarkdownPreview", | ||
| text: markdown, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <StandardTooltip content="Open in preview"> | ||
| <button | ||
| onClick={handleClick} | ||
| className={`opacity-0 group-hover:opacity-100 transition-opacity cursor-pointer ${className ?? ""}`} | ||
| aria-label="Open markdown in preview"> | ||
| <SquareArrowOutUpRight className="w-4 h-4" /> | ||
| </button> | ||
| </StandardTooltip> | ||
| ) | ||
| }) |
53 changes: 53 additions & 0 deletions
53
webview-ui/src/components/chat/__tests__/OpenMarkdownPreviewButton.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import React from "react" | ||
| import { describe, expect, it, vi, beforeEach } from "vitest" | ||
| import { render, screen, fireEvent } from "@testing-library/react" | ||
| import { TooltipProvider } from "@radix-ui/react-tooltip" | ||
|
|
||
| import { OpenMarkdownPreviewButton } from "../OpenMarkdownPreviewButton" | ||
|
|
||
| const { postMessageMock } = vi.hoisted(() => ({ | ||
| postMessageMock: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock("@src/utils/vscode", () => ({ | ||
| vscode: { | ||
| postMessage: postMessageMock, | ||
| }, | ||
| })) | ||
|
|
||
| describe("OpenMarkdownPreviewButton", () => { | ||
| const complex = "# One\n## Two" | ||
| const simple = "Just text" | ||
|
|
||
| beforeEach(() => { | ||
| postMessageMock.mockClear() | ||
| }) | ||
|
|
||
| it("does not render when markdown has fewer than 2 headings", () => { | ||
| render( | ||
| <TooltipProvider> | ||
| <OpenMarkdownPreviewButton markdown={simple} /> | ||
| </TooltipProvider>, | ||
| ) | ||
| expect(screen.queryByLabelText("Open markdown in preview")).toBeNull() | ||
| }) | ||
|
|
||
| it("renders when markdown has 2+ headings", () => { | ||
| render( | ||
| <TooltipProvider> | ||
| <OpenMarkdownPreviewButton markdown={complex} /> | ||
| </TooltipProvider>, | ||
| ) | ||
| expect(screen.getByLabelText("Open markdown in preview")).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it("posts message on click", () => { | ||
| render( | ||
| <TooltipProvider> | ||
| <OpenMarkdownPreviewButton markdown={complex} /> | ||
| </TooltipProvider>, | ||
| ) | ||
| fireEvent.click(screen.getByLabelText("Open markdown in preview")) | ||
| expect(postMessageMock).toHaveBeenCalledWith({ type: "openMarkdownPreview", text: complex }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { describe, expect, it } from "vitest" | ||
|
|
||
| import { countMarkdownHeadings, hasComplexMarkdown } from "../markdown" | ||
|
|
||
| describe("markdown heading helpers", () => { | ||
| it("returns 0 for empty or undefined", () => { | ||
| expect(countMarkdownHeadings(undefined)).toBe(0) | ||
| expect(countMarkdownHeadings("")) | ||
| }) | ||
|
|
||
| it("counts single and multiple headings", () => { | ||
| expect(countMarkdownHeadings("# One")) | ||
| expect(countMarkdownHeadings("# One\nContent")) | ||
| expect(countMarkdownHeadings("# One\n## Two")) | ||
| expect(countMarkdownHeadings("# One\n## Two\n### Three")) | ||
| }) | ||
|
|
||
| it("handles all heading levels", () => { | ||
| const md = `# h1\n## h2\n### h3\n#### h4\n##### h5\n###### h6` | ||
| expect(countMarkdownHeadings(md)) | ||
| }) | ||
|
|
||
| it("ignores headings inside code fences", () => { | ||
| const md = "# real\n```\n# not a heading\n```\n## real" | ||
| expect(countMarkdownHeadings(md)) | ||
| }) | ||
|
|
||
| it("hasComplexMarkdown requires at least two headings", () => { | ||
| expect(hasComplexMarkdown("# One")) | ||
| expect(hasComplexMarkdown("# One\n## Two")) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** | ||
| * Counts the number of markdown headings in the given text. | ||
| * Matches headings from level 1 to 6 (e.g. #, ##, ###, etc.). | ||
| * Code fences are stripped before matching to avoid false positives. | ||
| */ | ||
| export function countMarkdownHeadings(text: string | undefined): number { | ||
| if (!text) return 0 | ||
|
|
||
| // Remove fenced code blocks to avoid counting headings inside code | ||
| const withoutCodeBlocks = text.replace(/```[\s\S]*?```/g, "") | ||
|
|
||
| // Up to 3 leading spaces are allowed before the hashes per the markdown spec | ||
| const headingRegex = /^\s{0,3}#{1,6}\s+.+$/gm | ||
| const matches = withoutCodeBlocks.match(headingRegex) | ||
| return matches ? matches.length : 0 | ||
| } | ||
|
|
||
| /** | ||
| * Returns true if the markdown contains at least two headings. | ||
| */ | ||
| export function hasComplexMarkdown(text: string | undefined): boolean { | ||
| return countMarkdownHeadings(text) >= 2 | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.