Description
Initial checklist
- I read the support docs
- I read the contributing guide
- I agree to follow the code of conduct
- I searched issues and couldn’t find anything (or linked relevant results below)
Affected packages and versions
mdast-util-to-markdown@2.1.1
, possibly mdast-util-gfm@3.0.0
Link to runnable example
https://github.com/vnphanquang/repro-mdast-util-to-markdown
Steps to reproduce
Reproduction
- Clone the repository: https://github.com/vnphanquang/repro-mdast-util-to-markdown
- Install dependencies (pnpm, yarn, npm)
- Run script:
node index.js
More information
Note
this reproduction does involve mdast-util-gfm
, however stack trace points to mdast-util-to-markdown
.
Given this input:
<table>
<tbody>
<tr>
<td>
<p>Paragraph 1</p>
<p>
<em><strong>Paragraph 2</strong></em>
</p>
</td>
</tr>
</tbody>
</table>
I suspect the key troubling markup is at <em><strong>Paragraph 2</strong></em>
. However, it does require an odd combination of being inside a table
and also having multiple paragraphs in td
to trigger the observed error.
From my test, skipping...
...produces a successful output. That is:
- results[results.length - 1].slice(-1)
+ results[results.length - 1]?.slice(-1)
However, I lack knowledge of the codebase to determine whether this change produces other false positive. Happy to open PR if given direction.
Expected behavior
Should output output.md
Actual behavior
Error thrown as observed in console:
[..truncated]/node_modules/.pnpm/mdast-util-to-markdown@2.1.1/node_modules/mdast-util-to-markdown/lib/util/container-phrasing.js:106
before === results[results.length - 1].slice(-1)
^
TypeError: Cannot read properties of undefined (reading 'slice')
at containerPhrasing ([..truncated]/node_modules/.pnpm/mdast-util-to-markdown@2.1.1/node_modules/mdast-util-to-markdown/lib/util/container-phrasing.js:106:48)
at Object.containerPhrasingBound [as containerPhrasing] ([..truncated]/node_modules/.pnpm/mdast-util-to-markdown@2.1.1/node_modules/mdast-util-to-markdown/lib/index.js:138:10)
at paragraph ([..truncated]/node_modules/.pnpm/mdast-util-to-markdown@2.1.1/node_modules/mdast-util-to-markdown/lib/handle/paragraph.js:16:23)
at containerPhrasing ([..truncated]/node_modules/.pnpm/mdast-util-to-markdown@2.1.1/node_modules/mdast-util-to-markdown/lib/util/container-phrasing.js:50:11)
at Object.containerPhrasingBound [as containerPhrasing] ([..truncated]/node_modules/.pnpm/mdast-util-to-markdown@2.1.1/node_modules/mdast-util-to-markdown/lib/index.js:138:10)
at handleTableCell ([..truncated]/node_modules/.pnpm/mdast-util-gfm-table@2.0.0/node_modules/mdast-util-gfm-table/lib/index.js:216:25)
at handleTableRowAsData ([..truncated]/node_modules/.pnpm/mdast-util-gfm-table@2.0.0/node_modules/mdast-util-gfm-table/lib/index.js:279:23)
at handleTableAsData ([..truncated]/node_modules/.pnpm/mdast-util-gfm-table@2.0.0/node_modules/mdast-util-gfm-table/lib/index.js:255:23)
at Object.handleTable ([..truncated]/node_modules/.pnpm/mdast-util-gfm-table@2.0.0/node_modules/mdast-util-gfm-table/lib/index.js:191:26)
at Object.one [as handle] ([..truncated]/node_modules/.pnpm/zwitch@2.0.4/node_modules/zwitch/index.js:108:17)
Affected runtime and version
node@22.11.0
Affected package manager and version
pnpm@9.12.3
Affected OS and version
Linux 6.6 Arch Linux
Build and bundle tools
other (please specify in steps to reproduce)