Skip to content

Commit 4e4b0bc

Browse files
Add Syntax Highlighting for more languages in Markdown Component (#11939)
* Add languages for markdown * add changeset * add changeset * Add to stories * Remove console log * Fix code --------- Co-authored-by: gradio-pr-bot <[email protected]>
1 parent 13fdfa6 commit 4e4b0bc

File tree

4 files changed

+85
-7
lines changed

4 files changed

+85
-7
lines changed

.changeset/famous-banks-prove.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@gradio/markdown-code": patch
3+
"gradio": patch
4+
---
5+
6+
fix:Add Syntax Highlighting for more languages in Markdown Component

gradio/components/markdown.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
@document()
2020
class Markdown(Component):
2121
"""
22-
Used to render arbitrary Markdown output. Can also render latex enclosed by dollar signs. As this component does not accept user input,
23-
it is rarely used as an input component.
22+
Used to render arbitrary Markdown output. Can also render latex enclosed by dollar signs as well as code blocks with syntax highlighting.
23+
Supported languages are bash, c, cpp, go, java, javascript, json, php, python, rust, sql, and yaml.
24+
As this component does not accept user input, it is rarely used as an input component.
2425
2526
Demos: blocks_hello, blocks_kinematics
2627
Guides: key-features

js/markdown-code/utils.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ import * as Prism from "prismjs";
55
import "prismjs/components/prism-python";
66
import "prismjs/components/prism-latex";
77
import "prismjs/components/prism-bash";
8+
// Add additional Prism languages here
9+
import "prismjs/components/prism-c";
10+
import "prismjs/components/prism-cpp";
11+
import "prismjs/components/prism-json";
12+
import "prismjs/components/prism-sql";
13+
import "prismjs/components/prism-java";
14+
import "prismjs/components/prism-go";
15+
import "prismjs/components/prism-rust";
16+
import "prismjs/components/prism-php";
17+
import "prismjs/components/prism-yaml";
18+
import "prismjs/components/prism-markup-templating";
819
import GithubSlugger from "github-slugger";
920

10-
// import loadLanguages from "prismjs/components/";
11-
12-
// loadLanguages(["python", "latex"]);
13-
1421
const LINK_ICON_CODE = `<svg class="md-link-icon" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true" fill="currentColor"><path d="m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z"></path></svg>`;
1522

1623
const COPY_ICON_CODE = `
@@ -179,7 +186,6 @@ export function create_marked({
179186
latex_delimiters: { left: string; right: string; display: boolean }[];
180187
}): typeof marked {
181188
const marked = new Marked();
182-
183189
marked.use(
184190
{
185191
gfm: true,

js/markdown/Markdown.stories.svelte

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,68 @@ A --> B
106106
container: true
107107
}}
108108
/>
109+
<Story
110+
name="Markdown Several Coding languages"
111+
args={{
112+
value: `
113+
#### Python
114+
\`\`\`python
115+
def hello_world():
116+
print("Hello, World!")
117+
for i in range(5):
118+
print(f"Count: {i}")
119+
return "欢迎使用Markdown编辑器"
120+
\`\`\`
121+
122+
#### SQL
123+
\`\`\`sql
124+
SELECT u.name, u.email, p.title
125+
FROM users u
126+
LEFT JOIN projects p ON u.id = p.user_id
127+
WHERE u.active = true
128+
AND u.created_date >= '2024-01-01'
129+
ORDER BY u.name ASC, p.created_date DESC;
130+
\`\`\`
131+
132+
#### Rust
133+
\`\`\`rust
134+
use std::collections::HashMap;
135+
136+
#[derive(Debug)]
137+
struct Person {
138+
name: String,
139+
age: u32,
140+
}
141+
142+
impl Person {
143+
fn new(name: &str, age: u32) -> Self {
144+
Person {
145+
name: name.to_string(),
146+
age,
147+
}
148+
}
149+
150+
fn greet(&self) -> String {
151+
format!("Hello, I'm {} and I'm {} years old", self.name, self.age)
152+
}
153+
}
154+
155+
fn main() {
156+
let mut people = HashMap::new();
157+
158+
let alice = Person::new("Alice", 30);
159+
let bob = Person::new("Bob", 25);
160+
161+
people.insert("alice", alice);
162+
people.insert("bob", bob);
163+
164+
for (key, person) in &people {
165+
println!("{}: {}", key, person.greet());
166+
}
167+
}
168+
\`\`\`
169+
`,
170+
show_copy_button: true,
171+
container: true
172+
}}
173+
/>

0 commit comments

Comments
 (0)