Skip to content

Calling mdast-util-to-markdown's toMarkdown() with table elements throw error Cannot handle unknown node table #67

Closed
@trophygeek

Description

@trophygeek

Initial checklist

Affected package

mdast-util-to-markdown: "^2.1.2"

Steps to reproduce

  1. Pass html that includes a <table> element to toMarkdown()
function htmlToMarkdown(htmlString: string) {
  try {
    const hast = fromHtml(htmlString, {fragment: true});
    const mdast = toMdast(hast);
    return toMarkdown(mdast);  // <-- this line throws error
  } catch (error) {
    console.error('Error converting HTML to Markdown:', error);
    return `Error: ${error instanceof Error ? error.message : String(error)}`;
  }
}

Dependencies:

  "dependencies": {
    "hast-util-from-html": "^2.0.3",
    "hast-util-to-mdast": "^10.1.2",
    "mdast-util-to-markdown": "^2.1.2"
  },

Result: toMarkdown() throws the error:

Cannot handle unknown node table

The README.md has a "List of extensions" sections that seems to indicate that tables should work.

 [syntax-tree/mdast-util-gfm-table](https://github.com/syntax-tree/mdast-util-gfm-table) — GFM tables

But I don't see any table element included in the list of handlers in the code:

export const handle = {

Actual behavior

toMarkdown() throws the error "Cannot handle unknown node table".

Expected behavior

Ideally, tables would be handled but at least update the README to remove it from the list of handlers.

Runtime

Node:22.13.1 Chrome:133.0.6943.54

Package manager

yarn:1.22.22

Operating system

MacOS: Sonoma 14.7.3

Build and bundle tools

Vite

Metadata

Metadata

Assignees

No one assigned

    Labels

    👎 phase/noPost cannot or will not be acted on🙋 no/questionThis does not need any changes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions