Skip to content

Fix tables - #57

Merged
letmutex merged 3 commits into
letmutex:mainfrom
bjones1:fix-tables
Nov 14, 2025
Merged

letmutex merged 3 commits into
letmutex:mainfrom
bjones1:fix-tables

Conversation

@bjones1

@bjones1 bjones1 commented Nov 9, 2025

Copy link
Copy Markdown
Contributor

Tables can include inline markdown; these changes add support for this feature.

@letmutex

letmutex commented Nov 11, 2025 •

Copy link
Copy Markdown
Owner

Refactor: replace options and handlers with html_to_markdown.

This commit likely introduces an anti-pattern change? I think we should not expose the whole converter to the element handlers, because these functions previously had only minimal dependencies.

@bjones1

bjones1 commented Nov 11, 2025 •

Copy link
Copy Markdown
Contributor Author

Thanks for your feedback! The rationale for this change comes from the requirement to support inline Markdown in tables; without passing html_to_markdown, the code has no way to translate table cell content from HTML to Markdown.

Code blocks and lists represent a simpler case: <code> and <li> tag handlers can translate Markdown and return it in a string to be assembled by the parent <ul>, <ol>, or <pre> tag handlers by an intelligent 1-D concatenation of these strings. For example, the <ul> and <li> tag handler simply adds a number or bullet to the first line of each string, then indents the remaining lines.

In contrast, the <td> and <th> tag handler needs to provide 2-D data (row, column) that doesn't easily map to a flat set of strings. So, the <table> tag handler needs a way iterate over (row, column), then re-translate Markdown at the appropriate coordinates. Re-translating requires the html_to_markdown struct. For example, here's the logic for processing a <tr> tag:

https://github.com/letmutex/htmd/pull/57/files#diff-de1b366ee926c91170233e4e151a0f15e29aca58fec41088ab78cbd69a287576R71-R82

                    "tr" => {
                        // If no thead is found, use the first row as headers
                        if !has_thead && headers.is_empty() {
                            headers = extract_row_cells(&child, "th", element.html_to_markdown);
                            if headers.is_empty() {
                                let cells =
                                    extract_row_cells(&child, "td", element.html_to_markdown);
                                if !cells.is_empty() {
                                    headers = cells;
                                }
                            }
                            has_thead = !headers.is_empty();
                        } else {
                            let row_cells =
                                extract_row_cells(&child, "td", element.html_to_markdown);
                            if !row_cells.is_empty() {
                                rows.push(row_cells);
                            }
                        }
                    }

Inside extract_row_cells, the code invokes get_node_content(), which requires the html_to_markdown parameter; without this parameter, this code can't translate table cell contents:

https://github.com/letmutex/htmd/pull/57/files#diff-de1b366ee926c91170233e4e151a0f15e29aca58fec41088ab78cbd69a287576R162-R175

fn extract_row_cells(
    row_node: &Rc<markup5ever_rcdom::Node>,
    cell_tag: &str,
    html_to_markdown: &HtmlToMarkdown,
) -> Vec<String> {
    let mut cells = Vec::new();

    for cell_node in get_node_children(row_node) {
        if let NodeData::Element { name, .. } = &cell_node.data
            && name.local.as_ref() == cell_tag
        {
            let cell_content = get_node_content(&cell_node, html_to_markdown)
                .trim()
                .to_string();
            cells.push(cell_content);
        }
    }

    cells
}

@bjones1

bjones1 commented Nov 12, 2025

Copy link
Copy Markdown
Contributor Author

Another thought: the current approach for the <li> tag is inefficient: for each list item, it has to look at the parent <ol> or <ul> tag, count the total number of list items (for an <ol>), then format the number -- for each list item. This means it's taken an $O(n^2)$ time for what should be an $O(n)$ task A better and faster approach: defer this processing to the <ol> / <ul> tag handler, where that handler can count the items once, then generate them all by calling get_node_content(&li_node, html_to_markdown) on each <li> instance, which is an $O(n)$ time algorithm.

@letmutex

Copy link
Copy Markdown
Owner

Oh, I see, the current design doesn't allow us to easily get the markdown of a child node in the handler. Perhaps we can keep the signature of these functions for now and call walk_node directly in the table handler. We should have a better API for this, later.

@letmutex

letmutex commented Nov 13, 2025 •

Copy link
Copy Markdown
Owner

defer this processing to the

    /
      tag handler, where that handler can count the items once

Good idea.

@bjones1

bjones1 commented Nov 13, 2025

Copy link
Copy Markdown
Contributor Author

Oh, I see, the current design doesn't allow us to easily get the markdown of a child node in the handler. Perhaps we can keep the signature of these functions for now and call walk_node directly in the table handler. We should have a better API for this, later.

That's a good idea. The challenges here are in what the code knows during the traversal. Thoughts on this:

  1. During the initial traversal, as the code descends the DOM, a (new) pre-tag handler would need to mark nodes such as <table>, <ul>, <ol>, and <pre> as potentially translatable to Markdown.
  2. As the code finds (or doesn't find) accompanying <li>, <pre>, <tbody>, <thead>, etc. tags, it would use info from the parent (this is a list context, or a code block context, or a table context) to place data in a special data structure (an enum with CodeBlock(String), ListItem(String), and TableData(String), etc.).
  3. The existing tag handlers would then use this returned data to construct the appropriate Markdown translation.

But this is a sizable rewrite; for now, I'm happy with the current implementation.

@letmutex

Copy link
Copy Markdown
Owner

Agreed, merging these changes now.

@letmutex
letmutex merged commit 56c5019 into letmutex:main Nov 14, 2025
@bjones1

bjones1 commented Nov 14, 2025

Copy link
Copy Markdown
Contributor Author

Thanks!

@bjones1
bjones1 deleted the fix-tables branch September 3, 2026 11:39
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