Consider using `language-` as prefix, not `lang-` #67

Closed
gjtorikian opened this Issue Jul 30, 2012 · 10 comments

Comments

Projects
None yet
6 participants

I had a gfm fence that I wanted colored with highlight.js. Unfortunately, my javascript code was being applied as PHP sometimes, Ruby other times, even though I defined my blocks as ```javascript.

I eventually found out that marked is applying class names using lang-. highlight.js suggests using language- to override its "automatic" heuristics. That's also how HTML5 dictates it to be.

I don't know where lang- comes from. Google Code Prettify also suggests using language (though lang- is supported). I couldn't find requirements for SyntaxHighlighter.

The proposed change would render this code unnecessary:

nodeList = document.getElementsByTagName 'code'

subscribe 'post:textchange', ->
  for node in nodeList when /^lang-/.test node.className
    node.className = node.className.replace 'lang', 'language'
    hljs.highlightBlock node

+1

One more thing: the gfm codeblock should change to:

  fences: /^ *``` *([\w\-]+)? *\n([^\0]+?)\s*``` *(?:\n+|$)/

this allows me to do something like ```no-highlight for my code class (also valid for no-highlight

Owner

chjj commented Aug 1, 2012

Sounds okay to me. The only thing that worries me about this change is legacy code relying on lang-.

Could always do lang- and language- in the same class

/me shrugs

Including both class names sounds perfectly reasonable to me. If this seems crufty, perhaps lang- could be deprecated and removed in a future release.

Owner

chjj commented Aug 1, 2012

Could always do lang- and language- in the same class
Including both class names sounds perfectly reasonable to me.

Seems like it would be a problem for this code (and potentially other pieces of code like this) which doesn't take into account whitespace-separated class names, unless we keep lang- first.

for node in nodeList when /^lang-/.test node.className

edit: Anyway, I think I might just remove lang- anyway. I really don't enjoy supporting legacy features. Although it feels awkward removing something, people who rely on older versions can keep using them I guess, and when they want to upgrade they can upgrade.

edit2: Worth mentioning, this is also not the first time the class name prefix for code has been changed.

adam-p commented Aug 11, 2012

This is related to an issue I created: #54. In that issue I say that Highlight.js uses the bare language name as a class to specify the language, but it can use either the bare name or language- (see the Heuristics section). Either of those is fine with me, or the more flexible approach I suggested in #54.

+1 for #67

prism.js also requires language-

How about making the prefix configurable? To make this work with Highlight.js all you need to do is get rid of the prefix from the marked.js source and it works. But, would of course rather just do something like:

marked.setOptions({
  codeClassPrefix: ""
});

Or something.

@chjj chjj closed this in 8332826 Jan 10, 2013

Nice one. Thanks, @chjj.

Fogetti pushed a commit to Fogetti/marked that referenced this issue May 13, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment