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

handle backticks within inline code blocks #172

Merged
merged 4 commits into from
Aug 11, 2024
Merged
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/yshavit/mdq"

[dependencies]
clap = { version = "4.5.7", features = ["derive"] }
markdown = "1.0.0-alpha.16"
markdown = "1.0.0-alpha.19"
paste = "1.0"
regex = "1.10.4"
serde = { version = "1", features = ["derive"] }
Expand Down
136 changes: 129 additions & 7 deletions src/fmt_md_inlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
};
use serde::Serialize;
use std::borrow::Cow;
use std::cmp::max;
use std::collections::{HashMap, HashSet};

#[derive(Debug, Copy, Clone)]
Expand Down Expand Up @@ -128,15 +129,29 @@
out.write_str(surround);
}
Inline::Text(Text { variant, value }) => {
let surround = match variant {
TextVariant::Plain => "",
TextVariant::Code => "`",
TextVariant::Math => "$",
TextVariant::Html => "",
let (surround_ch, surround_space) = match variant {
TextVariant::Plain => (Cow::Borrowed(""), false),
TextVariant::Math => (Cow::Borrowed("$"), false),
TextVariant::Html => (Cow::Borrowed(""), false),
TextVariant::Code => {
let backticks_info = BackticksInfo::from(value);
let surround_ch = if backticks_info.count == 0 {
Cow::Borrowed("`")
} else {
Cow::Owned("`".repeat(backticks_info.count + 1))
};
(surround_ch, backticks_info.at_either_end)
}
};
out.write_str(surround);
out.write_str(&surround_ch);
if surround_space {
out.write_char(' ');
}
out.write_str(value);
out.write_str(surround);
if surround_space {
out.write_char(' ');
}
out.write_str(&surround_ch);
}
Inline::Link(link) => self.write_linklike(out, link),
Inline::Image(image) => self.write_linklike(out, image),
Expand Down Expand Up @@ -225,6 +240,32 @@
}
}

struct BackticksInfo {
count: usize,
at_either_end: bool,
}

impl From<&String> for BackticksInfo {
fn from(s: &String) -> Self {
let mut overall_max = 0;
let mut current_stretch = 0;
for c in s.chars() {
match c {
'`' => current_stretch += 1,
_ => {
if current_stretch > 0 {
overall_max = max(current_stretch, overall_max);
current_stretch = 0;
}
}
}
}
let count = max(current_stretch, overall_max);
let at_either_end = s.starts_with('`') || s.ends_with('`');
Self { count, at_either_end }
}
}

enum TitleQuote {
Double,
Single,
Expand Down Expand Up @@ -276,6 +317,9 @@
mod tests {
use super::*;
use crate::output::Output;
use crate::tree::ReadOptions;
use crate::unwrap;
use crate::utils_for_test::get_only;

mod title_quoting {
use super::*;
Expand Down Expand Up @@ -318,4 +362,82 @@
assert_eq!(&actual, expected);
}
}

mod inline_code {
use super::*;

#[test]
fn round_trip_no_backticks() {
round_trip_inline("hello world");
}

#[test]
fn round_trip_one_backtick() {
round_trip_inline("hello ` world");
}

#[test]
#[ignore] // #171

Check warning on line 380 in src/fmt_md_inlines.rs

View workflow job for this annotation

GitHub Actions / test

Ignored test

Regex indicates this test is probably ignored
fn round_trip_one_backtick_at_start() {
round_trip_inline("`hello");
}

#[test]
#[ignore] // #171

Check warning on line 386 in src/fmt_md_inlines.rs

View workflow job for this annotation

GitHub Actions / test

Ignored test

Regex indicates this test is probably ignored
fn round_trip_one_backtick_at_end() {
round_trip_inline("hello `");
}

#[test]
fn round_trip_three_backticks() {
round_trip_inline("hello ``` world");
}

#[test]
#[ignore] // #171

Check warning on line 397 in src/fmt_md_inlines.rs

View workflow job for this annotation

GitHub Actions / test

Ignored test

Regex indicates this test is probably ignored
fn round_trip_three_backticks_at_end() {
round_trip_inline("hello `");
}

#[test]
#[ignore] // #171

Check warning on line 403 in src/fmt_md_inlines.rs

View workflow job for this annotation

GitHub Actions / test

Ignored test

Regex indicates this test is probably ignored
fn round_trip_three_backticks_at_start() {
round_trip_inline("`hello");
}

#[test]
fn round_trip_surrounding_whitespace() {
round_trip_inline(" hello ");
}

#[test]
fn round_trip_backtick_and_surrounding_whitespace() {
round_trip_inline(" hello`world ");
}

fn round_trip_inline(inline_str: &str) {
round_trip(&Inline::Text(Text {
variant: TextVariant::Code,
value: inline_str.to_string(),
}));
}
}

/// Not a pure unit test; semi-integ. Checks that writing an inline to markdown and then parsing
/// that markdown results in the original inline.
fn round_trip(orig: &Inline) {
let mut output = Output::new(String::new());
let mut writer = MdInlinesWriter::new(MdInlinesWriterOptions {
link_format: LinkTransform::Keep,
});
writer.write_inline_element(&mut output, &orig);
let md_str = output.take_underlying().unwrap();

let ast = markdown::to_mdast(&md_str, &markdown::ParseOptions::gfm()).unwrap();
let md_tree = MdElem::read(ast, &ReadOptions::default()).unwrap();

unwrap!(&md_tree[0], MdElem::Paragraph(p));
let parsed = get_only(&p.body);
assert_eq!(parsed, orig);
}
}
22 changes: 22 additions & 0 deletions src/utils_for_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
#[cfg(test)]
pub use test_utils::*;

// We this file's contents from prod by putting them in a submodule guarded by cfg(test), but then "pub use" it to
// export its contents.
#[cfg(test)]
mod test_utils {
use std::fmt::Debug;

pub fn get_only<T: Debug, C: IntoIterator<Item = T>>(col: C) -> T {
let mut iter = col.into_iter();
let Some(result) = iter.next() else {
panic!("expected an element, but was empty");
};
match iter.next() {
None => result,
Some(extra) => {
let mut all = Vec::new();
all.push(result);
all.push(extra);
all.extend(iter);
panic!("expected exactly one element, but found {}: {all:?}", all.len());
}
}
}

/// Turn a pattern match into an `if let ... { else panic! }`.
#[macro_export]
macro_rules! unwrap {
Expand Down
Loading