Skip to content

Commit

Permalink
handle nested code blocks
Browse files Browse the repository at this point in the history
If the contents of the code blocks have leading backticks, extend the
surrounding backticks as appropriate.

I could do something more clever by using `~~~` instead of `` ``` ``,
but it's more complicated (and produces less obvious markdown, imo).

resolves #173
  • Loading branch information
yshavit authored Aug 25, 2024
1 parent 8da4a62 commit e4ec3f8
Showing 1 changed file with 54 additions and 11 deletions.
65 changes: 54 additions & 11 deletions src/fmt_md.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::fmt_md_inlines::{MdInlinesWriter, MdInlinesWriterOptions};
use clap::ValueEnum;
use std::cmp::max;
use std::fmt::Alignment;
use std::ops::Deref;

use crate::link_transform::LinkLabel;
use crate::output::{Block, Output, SimpleWrite};
use crate::str_utils::{pad_to, standard_align, CountingWriter};
use crate::tree::*;
use crate::tree_ref::{ListItemRef, MdElemRef, TableSlice};
use clap::ValueEnum;
use std::borrow::Cow;
use std::cmp::max;
use std::fmt::Alignment;
use std::ops::Deref;

pub struct MdOptions {
pub link_reference_placement: ReferencePlacement,
Expand Down Expand Up @@ -326,7 +326,13 @@ impl<'s, 'md> MdWriterState<'s, 'md> {
} else {
None
};
("```", meta)
let leading_backticks_count = Self::count_longest_opening_backticks(value);
let surround_backticks = if leading_backticks_count < 3 {
Cow::Borrowed("```")
} else {
Cow::Owned("`".repeat(leading_backticks_count + 1))
};
(surround_backticks, meta)
}
CodeVariant::Math { metadata } => {
let meta = if let Some(meta) = metadata {
Expand All @@ -337,24 +343,39 @@ impl<'s, 'md> MdWriterState<'s, 'md> {
} else {
None
};
("$$", meta)
(Cow::Borrowed("$$"), meta)
}
CodeVariant::Toml => ("+++", None),
CodeVariant::Yaml => ("---", None),
CodeVariant::Toml => (Cow::Borrowed("+++"), None),
CodeVariant::Yaml => (Cow::Borrowed("---"), None),
};

out.with_pre_block(|out| {
out.write_str(surround);
out.write_str(&surround);
if let Some(meta) = meta {
out.write_str(&meta);
}
out.write_char('\n');
out.write_str(value);
out.write_char('\n');
out.write_str(surround);
out.write_str(&surround);
});
}

fn count_longest_opening_backticks(contents: &str) -> usize {
let mut max_len = 0;
for line in contents.split('\n') {
let mut len_for_line = 0;
for ch in line.chars() {
if ch != '`' {
break;
}
len_for_line += 1;
}
max_len = max(max_len, len_for_line);
}
max_len
}

fn write_list_item<W: SimpleWrite>(&mut self, out: &mut Output<W>, index: &Option<u32>, item: &'md ListItem) {
let mut counting_writer = CountingWriter::wrap(out);
match index {
Expand Down Expand Up @@ -1160,6 +1181,28 @@ pub mod tests {
---"#},
);
}

#[test]
fn nested_block() {
check_render(
md_elems![CodeBlock {
variant: CodeVariant::Code(None),
value: indoc! {r#"
For example:
```nested
nested contents
```"#}
.to_string(),
}],
indoc! {r#"
````
For example:
```nested
nested contents
```
````"#},
);
}
}

mod inline {
Expand Down

0 comments on commit e4ec3f8

Please sign in to comment.