Skip to content

feat: Add support for Pandoc-style table captions - #1105

Open
amiroo54 wants to merge 13 commits into
pulldown-cmark:mainfrom
amiroo54:main
Open

feat: Add support for Pandoc-style table captions#1105
amiroo54 wants to merge 13 commits into
pulldown-cmark:mainfrom
amiroo54:main

Conversation

@amiroo54

@amiroo54 amiroo54 commented Jun 3, 2026

Copy link
Copy Markdown

Currently, there is no native way to add captions to tables. This PR introduces support for table captions inspired by the Pandoc table_caption specification, using the : prefix syntax immediately following a table.

Changes included:

Caption Parsing: Added logic to parse lines beginning with : immediately after a table as a caption node.
Feature Flag: Wrapped the functionality in a new ENABLE_TABLE_CAPTIONS option flag to ensure strict backward compatibility for existing users.

Note on Spec Compliance:

To keep this initial implementation focused and lightweight, I scoped it to support captions after the table using the : prefix. Pandoc’s alternative syntaxes (like Table: Caption before the table) are not included in this PR, but the architecture allows for them to be added later if desired.

@Martin1887

Copy link
Copy Markdown
Collaborator

Thanks! Please add more tests in a spec file like for other features. Also please note the errors in the automated workflows 🙂.

@amiroo54

amiroo54 commented Jun 5, 2026

Copy link
Copy Markdown
Author

Done!

@Martin1887

Copy link
Copy Markdown
Collaborator

Nice, it looks good to me. The only thing that concerns me is that if we are adopting Pandoc spec, I would adopt the full spec allowing also captions in top of tables.

What do you think, @notriddle?

}
line_start.scan_all_space();
ix += line_start.bytes_scanned();
if scan_paragraph_interrupt_no_table(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test case for this feature when table captions and definition lists are enabled at the same time?

I checked, and it works, but these features both use :, which means that the interaction between them should be tested.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Done it.

) {
return None;
}
if scan_ch(&bytes[ix..], b':') == 1 && self.options.contains(Options::ENABLE_TABLE_CAPTIONS)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a tricky test case

```````````````````````````````` example_table_captions
Test|Table
----|-----
Test|row
: Test Caption
Test|ending
.
<table><thead><tr><th>Test</th><th>Table</th></tr></thead><tbody>
<tr><td>Test</td><td>row</td></tr>
<caption>Test Caption</caption>
<tr><td>Test</td><td>ending</td></tr>
</tbody></table>

It reflects the behavior of pandoc, except that <caption> is supposed to be the first child of the table element.

fn parse_table_caption(&mut self, mut ix: usize) -> Option<(usize, TreeIndex)> {
let bytes = self.text.as_bytes();
ix += scan_ch(&bytes[ix..], b':');
ix += scan_whitespace_no_nl(&bytes[ix..]);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that there are no test cases for the zero-spaces caption:

Test|Table
----|-----
Test|row
:Test Caption
Test|ending

That seems like a good thing to test, also.

.
<table><thead><tr><th>Test</th><th>Table</th></tr></thead><tbody>
<tr><td>Test</td><td>row</td></tr>
<caption>Test Caption</caption>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't valid HTML.

According to the standard, you are required to put the <caption> tag as the first child of the <table> element.

I've pushed a commit that fixes this to amiroo54/pulldown-cmark@main...notriddle:pulldown-cmark:html-validation

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, appreciated.

@@ -0,0 +1,123 @@
Run this with `cargo test --features gen-tests suite::table_captions`.

# Table Captions, inspired by Pandoc's table captions

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pandoc supports captions before tables, too. How hard would it be?

@amiroo54

Copy link
Copy Markdown
Author

A quick reminder, isn't this going to be merged?

@Martin1887

Copy link
Copy Markdown
Collaborator

I think that we are both waiting to support for caption before tables to match Pandoc spec 🙂.

@amiroo54

Copy link
Copy Markdown
Author

I was hoping we could do without that. I'm in the middle of some exams and can't work on it for a while. It'll have to wait for the duration I guess.

@notriddle

Copy link
Copy Markdown
Collaborator

I submitted a port of this feature to commonmark-hs, because if we’re going to invent a new syntax, I wanted at least one other implementation of it to exist.

In the past, pulldown-cmark implemented a limited version of footnotes, and we got constant complaints until we fixed it by implementing a more typical version. I don’t want to do that again. If we’re adding this, I want cross-markdown implementation consensus.

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.

3 participants