Skip to content
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

Add path annotation for codeblock #2610

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -12,6 +12,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 @@ -32,6 +33,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 @@ -56,6 +63,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 @@ -89,6 +102,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