From b90317efc157cc0b53ec0d2df90458f75e075370 Mon Sep 17 00:00:00 2001 From: Hugues Morisset Date: Sun, 4 Aug 2024 20:26:14 +0200 Subject: [PATCH 1/3] Add path annotation for codeblock --- components/markdown/src/codeblock/fence.rs | 13 +++++++++++++ components/markdown/src/codeblock/mod.rs | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/components/markdown/src/codeblock/fence.rs b/components/markdown/src/codeblock/fence.rs index 3658ce54ed..d726d31a87 100644 --- a/components/markdown/src/codeblock/fence.rs +++ b/components/markdown/src/codeblock/fence.rs @@ -24,6 +24,7 @@ pub struct FenceSettings<'a> { pub line_number_start: usize, pub highlight_lines: Vec>, pub hide_lines: Vec>, + pub path: Option<&'a str>, } impl<'a> FenceSettings<'a> { @@ -34,6 +35,7 @@ impl<'a> FenceSettings<'a> { line_number_start: 1, highlight_lines: Vec::new(), hide_lines: Vec::new(), + path: None, }; for token in FenceIter::new(fence_info) { @@ -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::Path(p) => me.path = Some(p), } } @@ -57,6 +60,7 @@ enum FenceToken<'a> { InitialLineNumber(usize), HighlightLines(Vec>), HideLines(Vec>), + Path(&'a str), } struct FenceIter<'a> { @@ -103,7 +107,16 @@ impl<'a> Iterator for FenceIter<'a> { let ranges = Self::parse_ranges(tok_split.next()); return Some(FenceToken::HideLines(ranges)); } + "path" => { + if let Some(p) = tok_split.next() { + return Some(FenceToken::Path(p)); + } + } lang => { + if tok_split.next().is_some() { + eprintln!("Warning: Unknown annotation {}", lang); + continue; + } return Some(FenceToken::Language(lang)); } } diff --git a/components/markdown/src/codeblock/mod.rs b/components/markdown/src/codeblock/mod.rs index 5e297926ff..637582fd75 100644 --- a/components/markdown/src/codeblock/mod.rs +++ b/components/markdown/src/codeblock/mod.rs @@ -12,6 +12,7 @@ pub(crate) use fence::FenceSettings; fn opening_html( language: Option<&str>, + path: Option<&str>, pre_style: Option, pre_class: Option, line_numbers: bool, @@ -32,6 +33,12 @@ fn opening_html( html.push('"'); } + if let Some(path) = path { + html.push_str(" data-path=\""); + html.push_str(path); + html.push('"'); + } + if let Some(styles) = pre_style { html.push_str(" style=\""); html.push_str(styles.as_str()); @@ -56,6 +63,12 @@ fn opening_html( html.push_str(lang); html.push('"'); } + + if let Some(path) = path { + html.push_str(" data-path=\""); + html.push_str(path); + html.push('"'); + } html.push('>'); html } @@ -89,6 +102,7 @@ impl<'config> CodeBlock<'config> { let html_start = opening_html( fence.language, + fence.path, highlighter.pre_style(), highlighter.pre_class(), fence.line_numbers, From d73d4a22fb675f12bc2b7a73909208b84e6e9cc5 Mon Sep 17 00:00:00 2001 From: Hugues Morisset Date: Sun, 11 Aug 2024 17:34:52 +0200 Subject: [PATCH 2/3] Doc --- .../documentation/content/syntax-highlighting.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/content/documentation/content/syntax-highlighting.md b/docs/content/documentation/content/syntax-highlighting.md index e84eb16539..3314e63013 100644 --- a/docs/content/documentation/content/syntax-highlighting.md +++ b/docs/content/documentation/content/syntax-highlighting.md @@ -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; @@ -311,6 +311,16 @@ highlight(code); ``` ```` +- `path` to specify a path the code block is associated with. + +```` +```rust,path=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 From cf0130f6522b641441f3396fc379ec446db02a34 Mon Sep 17 00:00:00 2001 From: Hugues Morisset Date: Mon, 26 Aug 2024 21:41:28 +0200 Subject: [PATCH 3/3] Rename path to name --- components/markdown/src/codeblock/fence.rs | 14 +++++++------- components/markdown/src/codeblock/mod.rs | 16 ++++++++-------- .../documentation/content/syntax-highlighting.md | 4 ++-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/components/markdown/src/codeblock/fence.rs b/components/markdown/src/codeblock/fence.rs index d726d31a87..eb3208613b 100644 --- a/components/markdown/src/codeblock/fence.rs +++ b/components/markdown/src/codeblock/fence.rs @@ -24,7 +24,7 @@ pub struct FenceSettings<'a> { pub line_number_start: usize, pub highlight_lines: Vec>, pub hide_lines: Vec>, - pub path: Option<&'a str>, + pub name: Option<&'a str>, } impl<'a> FenceSettings<'a> { @@ -35,7 +35,7 @@ impl<'a> FenceSettings<'a> { line_number_start: 1, highlight_lines: Vec::new(), hide_lines: Vec::new(), - path: None, + name: None, }; for token in FenceIter::new(fence_info) { @@ -45,7 +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::Path(p) => me.path = Some(p), + FenceToken::Name(n) => me.name = Some(n), } } @@ -60,7 +60,7 @@ enum FenceToken<'a> { InitialLineNumber(usize), HighlightLines(Vec>), HideLines(Vec>), - Path(&'a str), + Name(&'a str), } struct FenceIter<'a> { @@ -107,9 +107,9 @@ impl<'a> Iterator for FenceIter<'a> { let ranges = Self::parse_ranges(tok_split.next()); return Some(FenceToken::HideLines(ranges)); } - "path" => { - if let Some(p) = tok_split.next() { - return Some(FenceToken::Path(p)); + "name" => { + if let Some(n) = tok_split.next() { + return Some(FenceToken::Name(n)); } } lang => { diff --git a/components/markdown/src/codeblock/mod.rs b/components/markdown/src/codeblock/mod.rs index 637582fd75..24a9dd6c0b 100644 --- a/components/markdown/src/codeblock/mod.rs +++ b/components/markdown/src/codeblock/mod.rs @@ -12,7 +12,7 @@ pub(crate) use fence::FenceSettings; fn opening_html( language: Option<&str>, - path: Option<&str>, + name: Option<&str>, pre_style: Option, pre_class: Option, line_numbers: bool, @@ -33,9 +33,9 @@ fn opening_html( html.push('"'); } - if let Some(path) = path { - html.push_str(" data-path=\""); - html.push_str(path); + if let Some(name) = name { + html.push_str(" data-name=\""); + html.push_str(name); html.push('"'); } @@ -64,9 +64,9 @@ fn opening_html( html.push('"'); } - if let Some(path) = path { - html.push_str(" data-path=\""); - html.push_str(path); + if let Some(name) = name { + html.push_str(" data-name=\""); + html.push_str(name); html.push('"'); } html.push('>'); @@ -102,7 +102,7 @@ impl<'config> CodeBlock<'config> { let html_start = opening_html( fence.language, - fence.path, + fence.name, highlighter.pre_style(), highlighter.pre_class(), fence.line_numbers, diff --git a/docs/content/documentation/content/syntax-highlighting.md b/docs/content/documentation/content/syntax-highlighting.md index 3314e63013..da18fa4cf5 100644 --- a/docs/content/documentation/content/syntax-highlighting.md +++ b/docs/content/documentation/content/syntax-highlighting.md @@ -311,10 +311,10 @@ highlight(code); ``` ```` -- `path` to specify a path the code block is associated with. +- `name` to specify a name the code block is associated with. ```` -```rust,path=mod.rs +```rust,name=mod.rs use highlighter::highlight; let code = "..."; highlight(code);