Skip to content

Commit

Permalink
Node -> MdqNode unit tests plus fixes
Browse files Browse the repository at this point in the history
Big-ish chunk of work, so I'm putting up a checkpoint.

- Create a framework for testing `mdast::Node` -> `MdqNode` conversions.
  It includes a check (ignored for now) that we've covered all the
  `Node` branches
- Add a bunch of tests
- Refactor and finish implementation of link and image reference
  • Loading branch information
yshavit authored Jun 12, 2024
1 parent 5ef82a4 commit 3478b88
Show file tree
Hide file tree
Showing 6 changed files with 668 additions and 77 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ jobs:
- uses: actions/checkout@v4
- name: cargo test
run: cargo test --verbose
- name: list ignored tests
run: |
(find . -name '*.rs' -exec grep --fixed-strings -Hno '#[ignore]' {} \; || true) | sed -E 's/^([^:]+):([^:]+):.*/::warning file=\1,line=\2,title=Ignored test::Regex indicates this test is probably ignored/'
fmt:
needs: build
Expand Down
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ serde_yaml = "0.9"
regex = "1.10.4"
log = "0.4.21"
indoc = "2"
lazy_static = "1.4.0"
7 changes: 5 additions & 2 deletions src/fmt_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::borrow::Borrow;
use markdown::mdast::AlignKind;
use serde_json::{json, Map, Value};

use crate::tree::{CodeOpts, CodeVariant, Inline, MdqNode};
use crate::tree::{CodeOpts, CodeVariant, Inline, Link, MdqNode};

const BODY_KEY: &str = "body";

Expand Down Expand Up @@ -140,7 +140,10 @@ impl TextOnly {
Self::build_string(out, child);
}
}
Inline::Image { title, .. } => {
Inline::Image {
link: Link { title, .. },
..
} => {
out.push_str("<image");
if let Some(title) = title {
out.push_str(": ");
Expand Down
65 changes: 45 additions & 20 deletions src/fmt_md.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::Write;
use crate::fmt_str::{pad_to, standard_align};
use crate::output::Block::Inlined;
use crate::output::{Block, Output};
use crate::tree::{CodeVariant, Inline, InlineVariant, MdqNode, SpanVariant};
use crate::tree::{CodeVariant, Inline, InlineVariant, Link, LinkReference, MdqNode, SpanVariant};

pub fn write_md<N, W>(out: &mut Output<W>, nodes: &[N])
where
Expand Down Expand Up @@ -241,31 +241,56 @@ where
out.write_str(value);
out.write_str(surround);
}
Inline::Link { url, text, title, .. } => {
out.write_char('[');
write_line(out, text);
out.write_str("](");
out.write_str(url);
if let Some(title) = title {
out.write_str(" \"");
escape_title_to(out, title);
out.write_char('"');
}
out.write_char(')');
// TODO reference-style (non-inline) images
Inline::Link { text, link } => {
write_link_inline(out, link, |out| write_line(out, text));
}
Inline::Image { alt, link } => {
out.write_char('!');
write_link_inline(out, link, |out| out.write_str(alt));
}
Inline::Image { url, alt, title, .. } => {
out.write_str("![");
out.write_str(alt);
out.write_str("](");
out.write_str(url);
if let Some(title) = title {
}
}

/// Writes the inline portion of the link, which may be the full link if it was originally inlined.
///
/// Examples:
///
/// ```
/// [an inline link](https://example.com)
/// [a referenced link][1]
/// ```
///
/// The `contents` function is what writes e.g. `an inline link` above. It's a function because it may be a recursive
/// call into [write_line] (for links) or just simple text (for image alts).
fn write_link_inline<W, F>(out: &mut Output<W>, link: &Link, contents: F)
where
W: Write,
F: FnOnce(&mut Output<W>),
{
out.write_str("![");
contents(out);
out.write_char(']');
match &link.reference {
LinkReference::Inline => {
out.write_char('(');
out.write_str(&link.url);
if let Some(title) = &link.title {
out.write_str(" \"");
escape_title_to(out, title);
out.write_char('"');
}
out.write_char(')');
// TODO reference-style (non-inline) images
}
LinkReference::Full(identifier) => {
out.write_char('[');
out.write_str(identifier);
out.write_char(']');
}
LinkReference::Collapsed => {
out.write_str("[]");
}
LinkReference::Shortcut => {
// nothing
}
}
}
Expand Down
Loading

0 comments on commit 3478b88

Please sign in to comment.