ekalinin / typogr.js

Typography utils for javascript
MIT License
297 stars 20 forks source link

Work better with marked #24

Open runarberg opened 9 years ago

runarberg commented 9 years ago

This package doesn't work well with marked. Say I first run marked on a string of markdown, it will escape quotes to ' or &qout; which means typogr.smartypants won't typeset them. However if I first run typogr.smartypants on raw markdown it will change all underlined second headings to a series of em- and en-dashes, which the marked parser will subsequently miss.

I suggest we either leave lone dashes between newlines alone, or we typeset html encoded quotes along with ascii encoded ones (or even both so that the order of operation won't matter).

ekalinin commented 9 years ago

@runarberg thanks for report. Would you be able to show an example for this issue?

runarberg commented 9 years ago

Sure

example.md

Some basic markdown
-------------------

This text *should* be "turned into" HTML with
[marked](https://github.com/chjj/marked)---a markdown parser and
compiler--- and beautifully typeset with
[typogr](https://github.com/ekalinin/typogr.js)...

...but it isn't.

index.js

var fs = require('fs');

var marked = require('marked');
var typogr = require('typogr');

var result = '<h2 id="some-basic-markdown-">Some basic markdown</h2>\n'
        + '<p>This text <em>should</em> be &#8220;turned into&#8221; <span class="caps">HTML</span> with\n'
        + '<a href="https://github.com/chjj/marked">marked</a>&#8212;a markdown parser and\n'
        + 'compiler&#8212; and beautifully typeset with\n'
        + '<a href="https://github.com/ekalinin/typogr.js">typogr</a>&#8230;</p>\n'
        + '<p>&#8230;but it isn&#8217;t.</p>';

fs.readFile('./example.md', function(err, text) {
    console.log("\ntext -> marked -> typogrify\n");
    var html = marked(String(text));
    console.log(typogr.typogrify(html));

    console.log("\ntext -> typogrify -> marked\n");
    var typeset = typogr.typogrify(String(text));
    console.log(marked(typeset));

    console.log("\nExpecting\n");
    console.log(result);
});

Output

$ node index.js

text -> marked -> typogrify

<h2 id="some-basic-markdown">Some basic markdown</h2>
<p>This text <em>should</em> be &quot;turned into&quot; <span class="caps">HTML</span> with
<a href="https://github.com/chjj/marked">marked</a>&#8212;a markdown parser and
compiler&#8212; and beautifully typeset with
<a href="https://github.com/ekalinin/typogr.js">typogr</a>&#8230;</p>
<p>&#8230;but it isn&#39;t.</p>

text -> typogrify -> marked

<p>Some basic markdown
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>This text <em>should</em> be &#8220;turned into&#8221; <span class="caps">HTML</span> with
<a href="https://github.com/chjj/marked">marked</a>&#8212;a markdown parser and
compiler&#8212; and beautifully typeset with
<a href="https://github.com/ekalinin/typogr.js">typogr</a>&#8230;</p>
<p>&#8230;but it isn&#8217;t.</p>

Expecting

<h2 id="some-basic-markdown-">Some basic markdown</h2>
<p>This text <em>should</em> be &#8220;turned into&#8221; <span class="caps">HTML</span> with
<a href="https://github.com/chjj/marked">marked</a>&#8212;a markdown parser and
compiler&#8212; and beautifully typeset with
<a href="https://github.com/ekalinin/typogr.js">typogr</a>&#8230;</p>
<p>&#8230;but it isn&#8217;t.</p>
colophonemes commented 9 years ago

I had the same issue - I fixed it by using the smartypants option in marked (which does the same thing as the smartypants option in typogr).