Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fails to read from stdin #584

Open
rayellis4 opened this issue Sep 11, 2018 · 1 comment
Open

Fails to read from stdin #584

rayellis4 opened this issue Sep 11, 2018 · 1 comment
Assignees
Labels

Comments

@rayellis4
Copy link

While the command
showdown makehtml -i foo.md > foo.html
works as expected, the command
showdown makehtml < foo.md > foo.html
produces an empty output file and the response

Enabling option ghCodeBlocks
Enabling option encodeEmails
  ...
Reading data from stdin...
ERROR: Could not read from stdin, reason: The "buffer" argument must be one of type Buffer,   TypedArray, or DataView. Received type number
Run 'showdown <command> -h' for help

under Ubuntu 16.04 (4.4.0-134-generic) running node 10.10.0, showdown 1.8.6.
The problem seems to lie in the file showdown/src/cli/makehtml.cmd.js, where readFromStdIn
calls fs.readSync:

  function readFromStdIn () {
    try {
      var size = fs.fstatSync(process.stdin.fd).size;
      return size > 0 ? fs.readSync(process.stdin.fd, size)[0] : '';
    } catch (e) {
      var err = new Error('Could not read from stdin, reason: ' + e.message);
      messenger.errorExit(err);
    }
  }

According to the nodejs web site, the readSync interface is: fs.readSync(fd, buffer, offset, length, position). (Perhaps it's changed since the code was first written?) I've used the following code successfully, though it ignores issues of character encoding and such. (You can't really convert bytes to a string without a character encoding, if only in the form of an assumption. :-):

  function readFromStdIn () {
    try {
      var size = fs.fstatSync(process.stdin.fd).size;
      var rslt = '';
      if (size > 0) {
         var buf = new Buffer.alloc(size);
         fs.readSync(process.stdin.fd, buf, 0, size, 0);
         rslt = buf.toString();
      }
      return rslt;
    } catch (e) {
      var err = new Error('Could not read from stdin, reason: ' + e.message);
      messenger.errorExit(err);
    }
  }

Hope this helps. I use showdown regularly (among other things, it generates previews for markdown code in my emacs environment) and I greatly appreciate your efforts in developing it and your willingness to share it. Feel free to contact me if you need any further info.

-- Ray Ellis ([email protected])

@tivie tivie self-assigned this Sep 14, 2018
@tivie
Copy link
Member

tivie commented Sep 14, 2018

Thanks for you contribution. The CLI tool is, in fact, undertested, as I haven't had the time to implement proper unit testing for them.

@tivie tivie added the bug label Sep 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants