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

Use marked highlight option instead of overriding code #114

Closed
dyllan-to-you opened this issue Jan 27, 2022 · 3 comments
Closed

Use marked highlight option instead of overriding code #114

dyllan-to-you opened this issue Jan 27, 2022 · 3 comments

Comments

@dyllan-to-you
Copy link
Contributor

dyllan-to-you commented Jan 27, 2022

if (!Object.prototype.hasOwnProperty.call(renderer, 'code')) {
renderer.code = (code, languageName) => {
// if the given language is not available in highlight.js, fall back to plaintext
const language = languageName && hljs.getLanguage(languageName) ? languageName : 'plaintext';
return `<pre><code class="hljs ${language}">${hljs.highlight(code, { language }).value}</code></pre>`;
};
}

Hi! I was wondering why code highlighting was implemented as an override to the code function of the marked renderer, versus utilizing the highlighted option in marked?

I believe setting the options such that

highlight: function(code, lang) {
    const hljs = require('highlight.js');
    const language = hljs.getLanguage(lang) ? lang : 'plaintext';
    return hljs.highlight(code, { language }).value;
  },
langPrefix: 'hljs '

would have the same result.

My current goal is to override the code renderer, and I do so by creating a new class that extends marked.Renderer and defining a new code method within there. The current implementation of the marked integration, however, ends up overriding my custom class. The alternative method

const renderer = new marked.Rendere0;
renderer.code = (code, infostring, escaped) => { /* do stuff */ }

does not allow me to easily fall back to the default handler.

If this change is OK I can submit a PR.

@simonhaenisch
Copy link
Owner

Hey, thanks for bringing this up... I was not aware of the highlight option, maybe that wasn't around yet when I implemented this? I started out with marked@0.3 😅

This should also allow to simplify the code base, but yeah I'm also planning to change the whole implementation around marked because there's now the use method to allow extensions, which I want to expose properly (will have to be a new major version though I think)... currently don't really have much time to work on this though. So thanks for the PR already, will have a look tomorrow.

@dyllan-to-you
Copy link
Contributor Author

dyllan-to-you commented Jan 27, 2022

Heh that's fair. It does look like highlight is pretty new, and yeah utilizing that use method sounds like it would provide a nicer API!

Glad I could help!

@simonhaenisch
Copy link
Owner

I am mistaken, it was actually available in 0.3.0 already... must have just missed it (:

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

2 participants