Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

highlight not defined #375

Closed
wvandaal opened this issue Mar 25, 2014 · 10 comments
Closed

highlight not defined #375

wvandaal opened this issue Mar 25, 2014 · 10 comments

Comments

@wvandaal
Copy link

I'm experiencing a subtle bug when using marked in conjunction with highlight.js. I am using your double-escaping workaround for code highlighting to great effect: optional escape #6, but when I attempt to begin a markdown string with code, as opposed to the other semantic tags, I get an Uncaught ReferenceError: highlight is not defined. Here is the code that I am using:

$(function(){
  var _marked;

  // Set highlighting options for marked
  marked.setOptions({
    highlight: function (code) {
      return hljs.highlightAuto(code).value;
    }
  });

  _marked = marked;

  // Rewrite marked to prevent double-escaping of code blocks
  window.marked = function(text) {
    var tokens = _marked.lexer(text),
        l      = tokens.length;

    for (var i = 0, tok = tokens[i]; i < l; i++) {
      if (tok.type === "code") {
        tok.text = highlight(tok.text, tok.lang);
        tok.escaped = true;
      }
    }

    return _marked.parser(tokens); 
  };
});

As an example, the following markdown will be rendered properly, and without incident:

# Some header
```
puts 'hello'
```

whereas this throws an error:

```
puts 'hello'
```

Any thoughts on what the problem might be here?

@chjj
Copy link
Member

chjj commented Mar 26, 2014

Change:

window.marked = function(text) {

To:

window.marked = function(text, options) {

Change:

tok.text = highlight(tok.text, tok.lang);

To:

tok.text = options.highlight(tok.text, tok.lang);

Change:

for (var i = 0, tok = tokens[i]; i < l; i++) {

To:

for (var i = 0; i < l; i++) {
  tok = tokens[i];

All of this is unnecessary though. If you provide a highlight callback, this is done for you.

@chjj chjj closed this as completed Mar 26, 2014
@wvandaal
Copy link
Author

Your solution leaves me slightly confused: you mention that this is done by providing a highlight callback, however isn't that what this line of my code accomplishes?

  marked.setOptions({
    highlight: function (code) {
      return hljs.highlightAuto(code).value;
    }
  });

@chjj
Copy link
Member

chjj commented Mar 26, 2014

Yep. All of the rest is unnecessary.

@wvandaal
Copy link
Author

Ok, but then why is this still a problem?

@wvandaal
Copy link
Author

My current initialization does the following:

  1. Sets up highlight callback
  2. Rewrites the marked function to call the lexer and parser independently to prevent double-escaping of highlighted code.

the highlight option should already be defined by the time my new function is defined

@chjj
Copy link
Member

chjj commented Mar 26, 2014

Hmm, I think you may have discovered an issue. The non-async highlighting should already prevent double escaping, but it looks like the switch to the renderer broke that. Fixing.

@wvandaal
Copy link
Author

Thanks! Works perfectly now

@chjj
Copy link
Member

chjj commented Mar 26, 2014

Hmm, then again, this shouldn't really affect anything unless the tokens are handled after the code method on the renderer is called. Are you sure you were getting double escaped code using just the highlight option before? I'll have to play around with this some more.

@wvandaal
Copy link
Author

Yeah, initially I tried it with just the highlight callback defined, but I ended up with the code blocks being escaped after they had been highlighted. I switch over to the fix suggested in issue #6, which solved the double escaping but led to this subtle bug in the highlight callback. With the code changes you made yesterday, I was able to remove the workaround suggested in issue #6 and everything worked as expected.

chjj added a commit that referenced this issue Apr 2, 2014
ghost pushed a commit to zergeborg/marked that referenced this issue May 13, 2016
ghost pushed a commit to zergeborg/marked that referenced this issue May 13, 2016
@joshbruce
Copy link
Member

#983

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants