-
Notifications
You must be signed in to change notification settings - Fork 0
fix(chat): scaffold a Markdown renderer so chat renders answers #3358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fef28cf
fix(chat): render assistant Markdown through a built-in extension
kojiwakayama 2fbb14b
feat(markdown): render LaTeX math, and address review findings
kojiwakayama a902356
build: refresh proxy dependency lock for markdown math deps
kojiwakayama 9b26997
fix(chat): scaffold a Markdown renderer so chat renders answers
kojiwakayama 9d0ef95
docs: refresh generated API reference for the chat prose styling
kojiwakayama a19dd48
fix(templates): make the scaffolded Markdown renderer type-check
kojiwakayama 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| # @veryfront/ext-markdown-react | ||
|
|
||
| Rich Markdown rendering for React surfaces, built on | ||
| [react-markdown](https://github.com/remarkjs/react-markdown) and | ||
| [remark-gfm](https://github.com/remarkjs/remark-gfm). | ||
|
|
||
| Provides the `MarkdownRendererProvider` contract. Chat installs this renderer by | ||
| default, so assistant answers render as semantic HTML without any project setup. | ||
|
|
||
| ## What it renders | ||
|
|
||
| - CommonMark: headings, paragraphs, lists, blockquotes, links, images, emphasis, | ||
| inline code, and fenced code. | ||
| - GFM: tables, task lists, strikethrough, and autolinks. | ||
| - Fenced code through the shared `CodeBlock` primitive (language label, copy | ||
| button, and an optional syntax-highlight renderer). | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Safety | ||
|
|
||
| - Raw HTML in Markdown source is never injected. `rehype-raw` is not installed, | ||
| so `<script>` in source reaches the DOM as escaped text. | ||
| - react-markdown's default URL transform drops unsafe link protocols such as | ||
| `javascript:`. | ||
| - Links open in a new tab with `rel="noopener noreferrer"`. | ||
|
|
||
| ## Use it directly | ||
|
|
||
| Chat wires this up for you. Install it explicitly when you render | ||
| `veryfront/markdown` outside chat: | ||
|
|
||
| ```tsx | ||
| import { Markdown, MarkdownRendererProvider } from "veryfront/markdown"; | ||
| import { MarkdownRenderer } from "@veryfront/ext-markdown-react/renderer"; | ||
|
|
||
| export default function Answer({ source }: { source: string }) { | ||
| return ( | ||
| <MarkdownRendererProvider renderer={MarkdownRenderer}> | ||
| <Markdown>{source}</Markdown> | ||
| </MarkdownRendererProvider> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| Import from `@veryfront/ext-markdown-react/renderer` in client code. The package | ||
| root additionally exports the extension factory, which pulls in the extension | ||
| registry. | ||
|
|
||
| ## Replace it | ||
|
|
||
| Pass your own renderer to `MarkdownRendererProvider` to override this one | ||
| everywhere, including inside chat. Pass `renderer={null}` on a single `Markdown` | ||
| instance to show plain escaped source instead. | ||
|
|
||
| ## Test | ||
|
|
||
| ```bash | ||
| deno task test | ||
| ``` | ||
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,45 @@ | ||
| { | ||
| "name": "@veryfront/ext-markdown-react", | ||
| "version": "0.1.0", | ||
| "exports": { | ||
| ".": "./src/index.ts", | ||
| "./renderer": "./src/renderer.tsx" | ||
| }, | ||
| "veryfront": { | ||
| "extension": true, | ||
| "activation": "auto", | ||
| "contracts": { | ||
| "provides": [ | ||
| "MarkdownRendererProvider" | ||
| ] | ||
| }, | ||
| "capabilities": [], | ||
| "npm": { | ||
| "runtimeVersionFromManifest": true, | ||
| "peerDependencies": { | ||
| "react": "^19.2.4", | ||
| "react-dom": "^19.2.4" | ||
| } | ||
| } | ||
| }, | ||
| "imports": { | ||
| "react": "../../react/react.ts", | ||
| "react/jsx-runtime": "../../react/jsx-runtime.ts", | ||
| "react-dom/server": "../../react/react-dom-server.ts", | ||
| "react-markdown": "npm:react-markdown@9.0.3", | ||
| "remark-gfm": "npm:remark-gfm@4.0.1", | ||
| "@std/assert": "jsr:@std/assert@1.0.19", | ||
| "@std/testing/bdd": "jsr:@std/testing@1.0.17/bdd", | ||
| "veryfront/extensions": "../../src/extensions/types.ts", | ||
| "veryfront/extensions/markdown": "../../src/extensions/markdown/index.ts", | ||
| "veryfront/markdown": "../../src/markdown/index.ts" | ||
| }, | ||
| "compilerOptions": { | ||
| "jsx": "react-jsx", | ||
| "jsxImportSource": "react" | ||
| }, | ||
| "tasks": { | ||
| "test": "deno test --no-check --allow-all src/", | ||
| "check": "deno check src/index.ts src/renderer.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,37 @@ | ||
| /** | ||
| * react-markdown implementation of the `MarkdownRendererProvider` extension | ||
| * contract. | ||
| * | ||
| * @module extensions/ext-markdown-react | ||
| */ | ||
|
|
||
| import type { ExtensionFactory } from "veryfront/extensions"; | ||
| import { | ||
| createMarkdownRendererProvider, | ||
| MarkdownRendererProviderName, | ||
| } from "veryfront/extensions/markdown"; | ||
| import extensionPackage from "../deno.json" with { type: "json" }; | ||
| import { MarkdownRenderer } from "./renderer.tsx"; | ||
|
|
||
| const extMarkdownReact: ExtensionFactory = () => { | ||
| const provider = createMarkdownRendererProvider("ext-markdown-react", MarkdownRenderer); | ||
|
|
||
| return { | ||
| name: "ext-markdown-react", | ||
| version: extensionPackage.version, | ||
| contracts: { | ||
| provides: [MarkdownRendererProviderName], | ||
| }, | ||
| capabilities: [], | ||
| setup(ctx) { | ||
| ctx.provide(MarkdownRendererProviderName, provider); | ||
| ctx.logger.debug("[ext-markdown-react] MarkdownRendererProvider registered"); | ||
| }, | ||
| teardown() { | ||
| // No resources to release. | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| export default extMarkdownReact; | ||
| export { MarkdownRenderer } from "./renderer.tsx"; |
Oops, something went wrong.
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.