Skip to content

Add option to fence tables#272

Merged
jugglerchris merged 3 commits into
jugglerchris:mainfrom
J0hannes101:main
May 17, 2026
Merged

Add option to fence tables#272
jugglerchris merged 3 commits into
jugglerchris:mainfrom
J0hannes101:main

Conversation

@J0hannes101
Copy link
Copy Markdown
Contributor

Adds an option to wrap tables inside fences
Example:

fn main() {
    let html = r#"
<table>
  <tr><th>Name</th><th>Age</th><th>City</th></tr>
  <tr><td>Alice</td><td>30</td><td>Berlin</td></tr>
  <tr><td>Bob</td><td>25</td><td>Paris</td></tr>
  <tr><td>Charlie</td><td>35</td><td>London</td></tr>
</table>"#;

    let out = config::plain()
        .fence_tables()
        .string_from_read(html.as_bytes(), 50)
        .unwrap();
    println!("{}", out);
}

This makes tables render correctly in Markdown previews:

───────┬───┬──────
Name   │Age│City  
───────┼───┼──────
Alice  │30 │Berlin
───────┼───┼──────
Bob    │25 │Paris 
───────┼───┼──────
Charlie│35 │London
───────┴───┴──────

instead of:
───────┬───┬──────
Name │Age│City
───────┼───┼──────
Alice │30 │Berlin
───────┼───┼──────
Bob │25 │Paris
───────┼───┼──────
Charlie│35 │London
───────┴───┴──────

@jugglerchris
Copy link
Copy Markdown
Owner

That's a simple clever idea to improve tables when treating it as markdown with proportional fonts! (The long term answer would be to add an option to use markdown table formatting as an alternative to fixed-width cells)

I wonder if it's slightly too simple, though - nested tables are always a bit tricky, and I guess there would be extra ``` around the inner tables? Either way, would you mind adding a unit test or two checking for the correct output?

@J0hannes101
Copy link
Copy Markdown
Contributor Author

I wonder if it's slightly too simple, though - nested tables are always a bit tricky, and I guess there would be extra ``` around the inner tables? Either way, would you mind adding a unit test or two checking for the correct output?

You were right - nested tables added extra ```.
The fix was simply to track the depth of the rendered table and only apply the wrapping on depth 1.
I added tests for both normal and nested tables.

@jugglerchris jugglerchris merged commit eba203f into jugglerchris:main May 17, 2026
4 checks passed
@jugglerchris
Copy link
Copy Markdown
Owner

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants