jonschlinkert / markdown-toc

API and CLI for generating a markdown TOC (table of contents) for a README or any markdown files. Uses Remarkable to parse markdown. Used by NASA/openmct, Prisma, Joi, Mocha, Sass, Prettier, Orbit DB, FormatJS, Raneto, hapijs/code, webpack-flow, docusaurus, release-it, ts-loader, json-server, reactfire, bunyan, husky, react-easy-state, react-snap, chakra-ui, carbon, alfresco, repolinter, Assemble, Verb, and thousands of other projects.
https://github.com/jonschlinkert
MIT License
1.64k stars 706 forks source link

[markdown-pdf] crashes with 'Invalid non-string/buffer chunk' #117

Open fatso83 opened 6 years ago

fatso83 commented 6 years ago

Created the following minimal script which works fine when I uncomment the toc.plugin line, but fails otherwise with TypeError: Invalid non-string/buffer chunk. After browsing the documentation of Remarkable and this plugin, I can't seem to find that I am doing something wrong ...

var markdownpdf = require("markdown-pdf"),
  through2 = require("through2"),
  toc = require("markdown-toc"),
  fs = require("fs");

var args = process.argv.slice(2);
var file = args[0];
var outputFile = file + ".pdf";

fs
  .createReadStream(file)
  .pipe(
    markdownpdf({
      remarkable: {
        plugins: [
          // enabling this results in 'TypeError: Invalid non-string/buffer chunk'
          toc.plugin({})
        ]
      }
    })
  )
  .pipe(fs.createWriteStream(outputFile));
Output from command with stacktrace ```javascript node non-working.js ../README.md events.js:182 throw er; // Unhandled 'error' event ^ TypeError: Invalid non-string/buffer chunk at chunkInvalid (/home/carlerik/dev/holmevann/scripts/node_modules/readable-stream/lib/_stream_readable.js:306:10) at readableAddChunk (/home/carlerik/dev/holmevann/scripts/node_modules/readable-stream/lib/_stream_readable.js:260:31) at DestroyableTransform.Readable.push (/home/carlerik/dev/holmevann/scripts/node_modules/readable-stream/lib/_stream_readable.js:245:10) at DestroyableTransform.Transform.push (/home/carlerik/dev/holmevann/scripts/node_modules/readable-stream/lib/_stream_transform.js:148:32) at DestroyableTransform.flush [as _flush] (/home/carlerik/dev/holmevann/scripts/node_modules/markdown-pdf/index.js:77:12) at DestroyableTransform.prefinish (/home/carlerik/dev/holmevann/scripts/node_modules/readable-stream/lib/_stream_transform.js:138:10) at emitNone (events.js:105:13) at DestroyableTransform.emit (events.js:207:7) at prefinish (/home/carlerik/dev/holmevann/scripts/node_modules/readable-stream/lib/_stream_writable.js:609:14) at finishMaybe (/home/carlerik/dev/holmevann/scripts/node_modules/readable-stream/lib/_stream_writable.js:617:5) ```

These are my dependencies:

    "markdown-pdf": "^8.1.1",
    "markdown-toc": "^1.2.0",
    "markdown2pdf": "^0.2.1",
    "through2": "^2.0.3"
doowb commented 6 years ago

markdown-toc returns an object with a .content property that contains the results from rendering the toc. It looks like markdown-pdf is expecting a string or buffer to be returned since that's what it's pushing into the stream.

The only option I'm thinking of at this time is to create a new option for mardown-toc that will return the .content instead of the object. If someone could do a PR with that (or any additional ideas), I'm open to reviewing and discussing it.

fatso83 commented 6 years ago

@doowb Does that mean I should file the report to markdown-pdf instead? I wasn't sure which party is non-conforming to some standard/convention.

doowb commented 6 years ago

@fatso83 if you'd like to open an issue at markdown-pdf you could do that to request that they check the output of .render and if it's an object use .content. But, I think it would be better to update it here by adding an option, then you can use that option when registering your plugin.