syntax-tree/nlcst-to-string

"break" nodes not handled and cause a TypeError exception

staltz opened this issue · 5 comments

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: nlcst-to-string >=2.0.4 (at least, maybe older too)

Steps to reproduce

If there are trailing whitespaces at the end of a line, it seems to get parsed as type: break in Markdown, which nlcst-to-string doesn't know how to convert to string.

Link to code example: https://codesandbox.io/s/remark-debug-forked-hh0ub?file=/src/index.js

Example AST subtree containing breaks:

[ { type: 'text',
    value: 'Mobile: @6RpN4Ztw3jLwzQtHl8XpnnR58LWZTAjwq2vvfyx7zkc=.ed25519',
    position: 
     { start: { line: 3, column: 1, offset: 53 },
       end: { line: 3, column: 62, offset: 114 },
       indent: [] } },
  { type: 'break',
    position: 
     { start: { line: 3, column: 62, offset: 114 },
       end: { line: 4, column: 1, offset: 117 },
       indent: [ 1 ] } },
  { type: 'text',
    value: 'Desktop: @lOUVT+Phkvai9a/cCS/RKo+S9hnPAQdVixms/7ldpPA=.ed25519',
    position: 
     { start: { line: 4, column: 1, offset: 117 },
       end: { line: 4, column: 63, offset: 179 },
       indent: [] } },
  { type: 'break',
    position: 
     { start: { line: 4, column: 63, offset: 179 },
       end: { line: 5, column: 1, offset: 182 },
       indent: [ 1 ] } },
  { type: 'text',
    value: 'Pub: @5XaVcAJ5DklwuuIkjGz4lwm2rOnMHHovhNg7BFFnyJ8=.ed25519',
    position: 
     { start: { line: 5, column: 1, offset: 182 },
       end: { line: 5, column: 59, offset: 240 },
       indent: [] } } ]

Expected behavior

No crash or exception thrown in the JS engine. The stringification should complete and break nodes should be considered empty string.

Actual behavior

Uncaught JS exception:

[TypeError: Cannot read property 'length' of undefined]

Hi! It seems some of the things asked in the template are missing? Please edit your post to fill out everything.

  • Initial checklist (todo)
  • Steps to reproduce
  • Expected behavior
  • Actual behavior

You won’t get any more notifications from me, but I’ll keep on updating this comment, and remove it when done!

If you need it, here’s the original template
<!--
  Bug: please check the needed checkboxes ([ ] -> [x]) and fill out the TODOs.
  Leave the comments as they are: they won’t show on GitHub.

  Some general tips:
  - Is this really a problem?
  - Is this a problem here?
  - Can this be solved in a different way?
-->

### Initial checklist

*   [ ] I read the support docs <!-- https://github.com/syntax-tree/.github/blob/main/support.md -->
*   [ ] I read the contributing guide <!-- https://github.com/syntax-tree/.github/blob/main/contributing.md -->
*   [ ] I agree to follow the code of conduct <!-- https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md -->
*   [ ] I searched issues and couldn’t find anything (or linked relevant results below) <!-- https://github.com/search?q=user%3Asyntax-tree&type=Issues -->

<!--
  Please test using the latest version of the relevant packages to make sure
  your issue has not already been fixed. Also make sure you’re on recent versions
  of Node and npm.
-->

Affected packages and versions: TODO

### Steps to reproduce

<!--
  How did this happen?
  Please provide a minimal, reproducible example:
  https://stackoverflow.com/help/minimal-reproducible-example
  Issues without reproduction steps or code examples may be immediately closed
  as not actionable.

  Here are some starters on codesandbox:
  - remark only (for markdown to markdown): https://codesandbox.io/s/remark-debug-ikwvx
  - remark and rehype (for markdown to html): https://codesandbox.io/s/remark-rehype-debug-4cz8v
  - react-markdown: https://codesandbox.io/s/react-markdown-debug-9n4eg

  Either link to runnable code (not your whole repo) or post the code inline.
-->

1.  TODO
2.  TODO

Link to code example: TODO

### Expected behavior

<!--What should happen?-->

TODO

### Actual behavior

<!--What happens instead?-->

TODO

Thanks,
— bb

Hmm, might this be better suited for mdast-util-to-string? nlcst is natural language nodes (paragraph, sentence, word, etc), whereas mdast includes break? 🤔

Oh, I wasn't sure which part of the pipeline was responsible for this. I'm happy with whatever place you think is the best for the fix.

Can you try mdast-util-to-string and see if that works for your use case?

Released!