Skip to content

Commit 313226e

Browse files
authored
feat: copy attribute for code fences (#2805)
1 parent 8543c29 commit 313226e

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

components/markdown/src/codeblock/fence.rs

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub struct FenceSettings<'a> {
2525
pub highlight_lines: Vec<RangeInclusive<usize>>,
2626
pub hide_lines: Vec<RangeInclusive<usize>>,
2727
pub name: Option<&'a str>,
28+
pub enable_copy: bool,
2829
}
2930

3031
impl<'a> FenceSettings<'a> {
@@ -36,6 +37,7 @@ impl<'a> FenceSettings<'a> {
3637
highlight_lines: Vec::new(),
3738
hide_lines: Vec::new(),
3839
name: None,
40+
enable_copy: false,
3941
};
4042

4143
for token in FenceIter::new(fence_info) {
@@ -46,6 +48,7 @@ impl<'a> FenceSettings<'a> {
4648
FenceToken::HighlightLines(lines) => me.highlight_lines.extend(lines),
4749
FenceToken::HideLines(lines) => me.hide_lines.extend(lines),
4850
FenceToken::Name(n) => me.name = Some(n),
51+
FenceToken::EnableCopy => me.enable_copy = true,
4952
}
5053
}
5154

@@ -61,6 +64,7 @@ enum FenceToken<'a> {
6164
HighlightLines(Vec<RangeInclusive<usize>>),
6265
HideLines(Vec<RangeInclusive<usize>>),
6366
Name(&'a str),
67+
EnableCopy,
6468
}
6569

6670
struct FenceIter<'a> {
@@ -112,6 +116,7 @@ impl<'a> Iterator for FenceIter<'a> {
112116
return Some(FenceToken::Name(n));
113117
}
114118
}
119+
"copy" => return Some(FenceToken::EnableCopy),
115120
lang => {
116121
if tok_split.next().is_some() {
117122
eprintln!("Warning: Unknown annotation {}", lang);

components/markdown/src/codeblock/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ fn opening_html(
1717
pre_style: Option<String>,
1818
pre_class: Option<String>,
1919
line_numbers: bool,
20+
enable_copy: bool,
2021
) -> String {
2122
let mut html = String::from("<pre");
2223
if line_numbers {
2324
html.push_str(" data-linenos");
2425
}
26+
if enable_copy {
27+
html.push_str(" data-copy");
28+
}
2529
let mut classes = String::new();
2630

2731
if let Some(lang) = language {
@@ -112,6 +116,7 @@ impl<'config> CodeBlock<'config> {
112116
highlighter.pre_style(),
113117
highlighter.pre_class(),
114118
fence.line_numbers,
119+
fence.enable_copy,
115120
);
116121
Ok((
117122
Self {

0 commit comments

Comments
 (0)