mapbox / retext-mapbox-standard

Enforce Mapbox rules about language.
Other
139 stars 15 forks source link

Correctly ignore liquid template sections #6

Closed tmcw closed 8 years ago

tmcw commented 8 years ago

Our docs use {% highlight json %} style sections, and mdast / retext do not currently handle this, so code within these blocks causes false-positives.

wooorm commented 8 years ago

I’d say this should be implemented as a remark plugin. By default, mdast-util-to-nlcst (which is now wrapped in remark-retext) does not transform nodes without children to natural language nodes. This means that inlineCode, which has a value, is ignored when running, for example, retext-equality.

Could you link some examples of how and where these tags occur?

tmcw commented 8 years ago

Here's an example from our documentation:

Let’s make the neighborhood boundaries only show past zoom 15, but have the fill show all the time:

{% highlight scss %}
#neighborhoods {
  polygon-fill: #000; // Apply this at all zoom levels
  [zoom > 14]{ // Things to only apply past zoom 14
    line-color:#fff;
    line-width:0.5;
    line-opacity: 0.75
  }
}
{% endhighlight %}

This works almost exactly the same as our conditional formatting above, yet here we have written a rule for zoom levels only.
wooorm commented 8 years ago

Thanks for checking. Are those the only format? {% ... %}? I remember searching a while back for some formal grammar, I found lots of extensions (e.g., if, else, loops), are you using those at mapbox?

tmcw commented 8 years ago

Unfortunately liquid syntax is expansive: https://github.com/Shopify/liquid/wiki/Liquid-for-Designers

For the retext use case, it makes sense to ignore all liquid tags and the content in them, but for remark in general (if people are porting from Jekyll to something remark-based) it'd be useful for liquid-style highlight blocks to be interpreted the same way as fenced code blocks.

wooorm commented 8 years ago

Oh, darn...

Would it maybe be better to preprocess the file before passing it to remark? This depends on whether those tags can produce markdown and thus HTML: {{ '*' }}foo{{ '*' }} to <em>foo</em>, when compiling to HTML, or is this not the case?

tmcw commented 8 years ago

Yep, for my usecase censoring liquid tags will do just fine, and the general case is probably way too much work for benefit. I'll do that, it's probably just a few regexps away :)

tmcw commented 8 years ago

It isn't pretty, but https://github.com/mapbox/retext-mapbox-standard/blob/master/lib/strip_liquid.js does the trick.