API design can be better


#1

marked

marked('I am using __markdown__.');

commonmark

new commonmark.HtmlRenderer().render(
  new commonmark.Parser().parse("Hello *world*")
);

Complicated usage forcing me to create helper in my blog. It feels awkward. Can we do better? Another way is to extract helper into separate npm module, but it feels weird too for various reasons.


#2

I’d have no objections to providing a simple helper function in the JS library.
(We have one like this in cmark already.) Put an issue on the tracker?


#4

I created draft of better commonmark api as an commonmark-helpers module. take a look at usage examples:

var md = require('commonmark-helpers');
var input = [
  '# title',
  '## title 2',
  'paragraph',
  '![](imgsrc)',
  '> BlockQuote'
].join('\n\n');

// helpers above commonmark API
function isHeader(event) { return event.entering && md.isHeader(event) };
function isParagraph(event) { return event.entering && md.isParagraph(event) };
function isBlockQuote(event) { return event.entering && md.isBlockQuote(event) };
function isImage(event) { return event.entering && md.isImage(event) };

md.html(`*italic*`);        // <p><em>italic</em></p>\n
md.text('**`plaintext`**'); // plaintext

function custom(event) { return event.entering && event.node.type === 'Header'; }
md.text(md.match(input, custom));   // title
md.text(md.match(input, isHeader)); // title

md.text(md.match(input, (event)=> md.isLevel(event, 2)));  // title 2
md.text(md.match(input, isParagraph));  // paragraph
md.text(md.match(input, isBlockQuote)); // BlockQuote
md.match(input, isImage).destination;   // imgsrc

#5

What does anybody think about commonmark-helpers?