Skip to content

Commit

Permalink
Add path annotation for codeblock (#2610)
Browse files Browse the repository at this point in the history
* Add path annotation for codeblock

* Doc

* Rename path to name
  • Loading branch information
izissise authored Dec 26, 2024
1 parent 0a43f61 commit a8d0ddf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
13 changes: 13 additions & 0 deletions components/markdown/src/codeblock/fence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct FenceSettings<'a> {
pub line_number_start: usize,
pub highlight_lines: Vec<RangeInclusive<usize>>,
pub hide_lines: Vec<RangeInclusive<usize>>,
pub name: Option<&'a str>,
}

impl<'a> FenceSettings<'a> {
Expand All @@ -34,6 +35,7 @@ impl<'a> FenceSettings<'a> {
line_number_start: 1,
highlight_lines: Vec::new(),
hide_lines: Vec::new(),
name: None,
};

for token in FenceIter::new(fence_info) {
Expand All @@ -43,6 +45,7 @@ impl<'a> FenceSettings<'a> {
FenceToken::InitialLineNumber(l) => me.line_number_start = l,
FenceToken::HighlightLines(lines) => me.highlight_lines.extend(lines),
FenceToken::HideLines(lines) => me.hide_lines.extend(lines),
FenceToken::Name(n) => me.name = Some(n),
}
}

Expand All @@ -57,6 +60,7 @@ enum FenceToken<'a> {
InitialLineNumber(usize),
HighlightLines(Vec<RangeInclusive<usize>>),
HideLines(Vec<RangeInclusive<usize>>),
Name(&'a str),
}

struct FenceIter<'a> {
Expand Down Expand Up @@ -103,7 +107,16 @@ impl<'a> Iterator for FenceIter<'a> {
let ranges = Self::parse_ranges(tok_split.next());
return Some(FenceToken::HideLines(ranges));
}
"name" => {
if let Some(n) = tok_split.next() {
return Some(FenceToken::Name(n));
}
}
lang => {
if tok_split.next().is_some() {
eprintln!("Warning: Unknown annotation {}", lang);
continue;
}
return Some(FenceToken::Language(lang));
}
}
Expand Down
14 changes: 14 additions & 0 deletions components/markdown/src/codeblock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(crate) use fence::FenceSettings;

fn opening_html(
language: Option<&str>,
name: Option<&str>,
pre_style: Option<String>,
pre_class: Option<String>,
line_numbers: bool,
Expand All @@ -33,6 +34,12 @@ fn opening_html(
html.push('"');
}

if let Some(name) = name {
html.push_str(" data-name=\"");
html.push_str(name);
html.push('"');
}

if let Some(styles) = pre_style {
html.push_str(" style=\"");
html.push_str(styles.as_str());
Expand All @@ -57,6 +64,12 @@ fn opening_html(
html.push_str(lang);
html.push('"');
}

if let Some(name) = name {
html.push_str(" data-name=\"");
html.push_str(name);
html.push('"');
}
html.push('>');
html
}
Expand Down Expand Up @@ -95,6 +108,7 @@ impl<'config> CodeBlock<'config> {

let html_start = opening_html(
fence.language,
fence.name,
highlighter.pre_style(),
highlighter.pre_class(),
fence.line_numbers,
Expand Down
12 changes: 11 additions & 1 deletion docs/content/documentation/content/syntax-highlighting.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ highlight(code);

- `hide_lines` to hide lines. You must specify a list of inclusive ranges of lines to hide,
separated by ` ` (whitespace). Ranges are 1-indexed.

````
```rust,hide_lines=1-2
use highlighter::highlight;
Expand All @@ -311,6 +311,16 @@ highlight(code);
```
````

- `name` to specify a name the code block is associated with.

````
```rust,name=mod.rs
use highlighter::highlight;
let code = "...";
highlight(code);
```
````

## Styling codeblocks

Depending on the annotations used, some codeblocks will be hard to read without any CSS. We recommend using the following
Expand Down

0 comments on commit a8d0ddf

Please sign in to comment.