-
Notifications
You must be signed in to change notification settings - Fork 812
add markdown rendering for tables in popout #7299
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| /* Copyright 2024 Marimo. All rights reserved. */ | ||
|
|
||
| import { marked } from "marked"; | ||
| import { useState } from "react"; | ||
| import { | ||
| Popover, | ||
| PopoverContent, | ||
| PopoverTrigger, | ||
| } from "@/components/ui/popover"; | ||
| import { Events } from "@/utils/events"; | ||
| import { MarkdownRenderer } from "../chat/markdown-renderer"; | ||
|
|
||
| const urlRegex = /(https?:\/\/\S+)/; | ||
| const imageRegex = /\.(png|jpe?g|gif|webp|svg|ico)(\?.*)?$/i; | ||
|
|
@@ -82,6 +84,49 @@ export function parseContent(text: string): ContentPart[] { | |
| }); | ||
| } | ||
|
|
||
| export function isMarkdown(text: string): boolean { | ||
| const tokens = marked.lexer(text); | ||
|
|
||
| const commonMarkdownIndicators = [ | ||
Light2Dark marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "space", | ||
| "code", | ||
| "fences", | ||
| "heading", | ||
| "hr", | ||
| "link", | ||
| "blockquote", | ||
| "list", | ||
| "html", | ||
| "def", | ||
| "table", | ||
| "lheading", | ||
| "escape", | ||
| "tag", | ||
| "reflink", | ||
| "strong", | ||
| "codespan", | ||
| "url", | ||
| ]; | ||
|
|
||
| return commonMarkdownIndicators.some((type) => | ||
| tokens.some((token) => token.type === type), | ||
| ); | ||
| } | ||
|
|
||
| // Wrapper component so that we call isMarkdown only on trigger | ||
| export const MarkdownUrlDetector = ({ | ||
| content, | ||
| parts, | ||
| }: { | ||
| content: string; | ||
| parts: ContentPart[]; | ||
| }) => { | ||
| if (isMarkdown(content)) { | ||
| return <MarkdownRenderer content={content} />; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this still runs on every cell, right?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could make
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This only runs when the cell is clicked and the popout is opened. Radix's popover only renders the content on open. Thanks for the other comments, I agree. And we can also merge this after release for safety. |
||
| } | ||
| return <UrlDetector parts={parts} />; | ||
| }; | ||
|
|
||
| export const UrlDetector = ({ parts }: { parts: ContentPart[] }) => { | ||
| const markup = parts.map((part, idx) => { | ||
| if (part.type === "url") { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import marimo | ||
|
|
||
| __generated_with = "0.17.8" | ||
| app = marimo.App(width="medium") | ||
|
|
||
|
|
||
| @app.cell | ||
| def _(): | ||
| import marimo as mo | ||
| return (mo,) | ||
|
|
||
|
|
||
| @app.cell | ||
| def _(markdown_sample, mo): | ||
| mo.ui.table( | ||
| { | ||
| "heading": ["### Hello" * 8], | ||
| "code_blocks": ["```python\n print('hi')\n```" * 3], | ||
| "lists": ["- item 1\n* item 2\n1. item 3" * 3], | ||
| "markdown_sample": [markdown_sample], | ||
| } | ||
| ) | ||
| return | ||
|
|
||
|
|
||
| @app.cell | ||
| def _(): | ||
| markdown_sample = """ | ||
| # Markdown showcase | ||
|
|
||
| ## Features: | ||
| - **Lists:** | ||
| - Bullet points | ||
| - Numbered lists | ||
|
|
||
| - **Emphasis:** *italic*, **bold**, ***bold italic*** | ||
| - **Links:** [Visit OpenAI](https://www.openai.com) | ||
| - **Images:** | ||
|
|
||
|  | ||
|
|
||
| - **Blockquotes:** | ||
|
|
||
| > Markdown makes writing simple and beautiful! | ||
|
|
||
| Enjoy exploring markdown's versatility! | ||
| """ | ||
| return (markdown_sample,) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| app.run() |
Uh oh!
There was an error while loading. Please reload this page.