Skip to content

Commit 500a314

Browse files
authored
Merge pull request #276 from digitalmoksha/add-full-info-string
Add support for full_info_string render option
2 parents 0663e6a + 1e32a9c commit 500a314

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

src/html.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,17 +512,24 @@ impl<'o> HtmlFormatter<'o> {
512512
first_tag += 1;
513513
}
514514

515+
let lang_str = str::from_utf8(&ncb.info[..first_tag]).unwrap();
516+
let info_str = str::from_utf8(&ncb.info[first_tag..]).unwrap().trim();
517+
515518
if self.options.render.github_pre_lang {
516-
pre_attributes.insert(
517-
String::from("lang"),
518-
String::from_utf8(Vec::from(&ncb.info[..first_tag])).unwrap(),
519-
);
519+
pre_attributes.insert(String::from("lang"), lang_str.to_string());
520+
521+
if self.options.render.full_info_string && !info_str.is_empty() {
522+
pre_attributes
523+
.insert(String::from("data-meta"), info_str.trim().to_string());
524+
}
520525
} else {
521-
code_attr = format!(
522-
"language-{}",
523-
str::from_utf8(&ncb.info[..first_tag]).unwrap()
524-
);
526+
code_attr = format!("language-{}", lang_str);
525527
code_attributes.insert(String::from("class"), code_attr);
528+
529+
if self.options.render.full_info_string && !info_str.is_empty() {
530+
code_attributes
531+
.insert(String::from("data-meta"), info_str.to_string());
532+
}
526533
}
527534
}
528535

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ struct Cli {
5858
#[arg(long)]
5959
github_pre_lang: bool,
6060

61+
/// Enable full info strings for code blocks
62+
#[arg(long)]
63+
full_info_string: bool,
64+
6165
/// Enable GitHub-flavored markdown extensions: strikethrough, tagfilter, table, autolink, and tasklist.
6266
/// Also enables --github-pre-lang.
6367
#[arg(long)]
@@ -218,6 +222,7 @@ fn main() -> Result<(), Box<dyn Error>> {
218222
render: ComrakRenderOptions {
219223
hardbreaks: cli.hardbreaks,
220224
github_pre_lang: cli.github_pre_lang || cli.gfm,
225+
full_info_string: cli.full_info_string,
221226
width: cli.width,
222227
unsafe_: cli.unsafe_,
223228
escape: cli.escape,

src/parser/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,21 @@ pub struct ComrakRenderOptions {
398398
/// ```
399399
pub github_pre_lang: bool,
400400

401+
/// Enable full info strings for code blocks
402+
///
403+
/// ```
404+
/// # use comrak::{markdown_to_html, ComrakOptions};
405+
/// let mut options = ComrakOptions::default();
406+
/// assert_eq!(markdown_to_html("``` rust extra info\nfn hello();\n```\n", &options),
407+
/// "<pre><code class=\"language-rust\">fn hello();\n</code></pre>\n");
408+
///
409+
/// options.render.full_info_string = true;
410+
/// let html = markdown_to_html("``` rust extra info\nfn hello();\n```\n", &options);
411+
/// let re = regex::Regex::new(r#"data-meta="extra info""#).unwrap();
412+
/// assert!(re.is_match(&html));
413+
/// ```
414+
pub full_info_string: bool,
415+
401416
/// The wrap column when outputting CommonMark.
402417
///
403418
/// ```

src/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ fn fuzz_doesnt_crash(md: String) {
4040
render: ::ComrakRenderOptions {
4141
hardbreaks: true,
4242
github_pre_lang: true,
43+
full_info_string: true,
4344
width: 80,
4445
unsafe_: true,
4546
escape: false,
@@ -1442,6 +1443,7 @@ fn exercise_full_api<'a>() {
14421443
render: ::ComrakRenderOptions {
14431444
hardbreaks: false,
14441445
github_pre_lang: false,
1446+
full_info_string: false,
14451447
width: 123456,
14461448
unsafe_: false,
14471449
escape: false,

0 commit comments

Comments
 (0)