Skip to content

Some highlighters require class on a wrapping element to render properly - #418

Closed
meeDamian wants to merge 3 commits into
markedjs:masterfrom
meeDamian:master
Closed

meeDamian wants to merge 3 commits into
markedjs:masterfrom
meeDamian:master

Conversation

@meeDamian

Copy link
Copy Markdown

This is one of the possible solutions. Any thoughts?

Review on Reviewable

@lifeisfoo

Copy link
Copy Markdown

👍

@lifeisfoo

Copy link
Copy Markdown

I hope this will be merged soon.
E.g. without it isn't possible to add line numbers with prism http://prismjs.com/plugins/line-numbers/

@meeDamian

Copy link
Copy Markdown
Author

@lifeisfoo Until then you can temporarily add this dependency to your package.json instead:

{
  "dependencies": {
    "marked": "chester1000/marked",

    "...": "..."
  }
}

remember to npm install afterwards.

@hemp

hemp commented Jul 18, 2014

Copy link
Copy Markdown

👍 This helped me make sure the styles are correct when using marked and highlightjs.

@aymericbeaumet

Copy link
Copy Markdown

Thanks for the feature 👍 This is exactly what I needed.

Though I think it would be nice to also support an array of string method in order to permit several classes definition.

@meeDamian

Copy link
Copy Markdown
Author

Since simply passing classesArray.join(' ') would work great, I'm not sure if additional check in a library is necessary here.

If more people will be pro-Array, though, I can edit my PR to include it.

@aymericbeaumet

Copy link
Copy Markdown

It could indeed be done as you said, my proposal was just for convenience.

hyponymous added a commit to hyponymous/marked that referenced this pull request Sep 1, 2014
hyponymous added a commit to hyponymous/marked that referenced this pull request Sep 1, 2014
@chikathreesix

Copy link
Copy Markdown

This is what I needed!
But I could actually do this with following code just as a workaround.
I know this is not a nice way, so I hope this pull request will be merged soon!

var marked = require('marked');
var hljs = require('highlight.js');
var renderer = new marked.Renderer();

renderer.code = function(code, language){
  return '<pre><code class="hljs ' + language + '">' + 
    hljs.highlight(language, code).value +
    '</code></pre>';
};

console.log(marked(codeStr, { renderer: renderer }));

@aymericbeaumet

Copy link
Copy Markdown

@chikathreesix I actually ended up with the same solution 👍 Far more flexible

@chikathreesix

Copy link
Copy Markdown

Cool 👍

@nickcmaynard

Copy link
Copy Markdown

+1

@blairmorris

Copy link
Copy Markdown

+1 for sure.

@codeboyim

Copy link
Copy Markdown

I set langPrefix:'hljs ' in marked.setOptions to work around

@meeDamian

Copy link
Copy Markdown
Author

Updated my repo to the latest codebase. Still can't get why it can't be merged.

@xdumaine

Copy link
Copy Markdown

Bump. @chjj?

@stoffeastrom

Copy link
Copy Markdown

Please merge!

tiziano88 added a commit to tiziano88/elm-markdown that referenced this pull request Apr 25, 2016
Currently the class is not added by marked nor by highlight.js itself,
and the rendered code will lack attributes such as background colour and
others that are defined by highlight.js styles on that particular class.

This workaround is based on
markedjs/marked#418 (comment).
@Huxpro

Huxpro commented May 4, 2016

Copy link
Copy Markdown

Please merge!

slayerfat added a commit to slayerfat/slayerfat.com.ve that referenced this pull request May 7, 2016
@akuma

akuma commented Jun 11, 2016

Copy link
Copy Markdown

Please merge!

@tzi

tzi commented Oct 19, 2016

Copy link
Copy Markdown

Please :)

@sovanna

sovanna commented Oct 24, 2016

Copy link
Copy Markdown

it could help a lot of people... instead, we use workaround ..

@wells

wells commented Jan 30, 2017

Copy link
Copy Markdown

@chjj Please merge this.

@ee7klt

ee7klt commented Feb 26, 2017

Copy link
Copy Markdown

@chjj please merge ;)

@joshbruce joshbruce added this to the 0.4.0 - No known defects milestone Dec 25, 2017
@joshbruce

Copy link
Copy Markdown
Member

#983

@joshbruce joshbruce removed this from the 0.4.0 - No known defects milestone Apr 4, 2018
@joshbruce

Copy link
Copy Markdown
Member

Closing due to merge conflicts and not in line with recent changes to docs.

@joshbruce joshbruce closed this Apr 5, 2018
@lyswhut

lyswhut commented Dec 9, 2018 •

Copy link
Copy Markdown

I set langPrefix:'hljs ' in marked.setOptions to work around

When the language is undefined, langPrefix does not work, such as:

```
console.log('hahaha')
```

Will be rendered as:

<pre><code><span class="hljs-built_in">console</span>.log(<span class="hljs-string">'hahaha'</span>)</code></pre>

The code block will be missing the 'hljs' class.
My solution is:

const marked = require('marked')
const hljs = require('highlight.js')
const renderer = new marked.Renderer()
const doRenderCode = (code, lang) => lang ? hljs.highlight(lang, code, true).value : hljs.highlightAuto(code).value
renderer.code = (code, lang) => `<pre><code class="hljs ${lang || ''}">${doRenderCode(code, lang)}</code></pre>`
const markedOptions = {
   Renderer
   // langPrefix: 'hljs ',
   // highlight: (code, lang) => lang ? hljs.highlight(lang, code, true).value : hljs.highlightAuto(code).value
}
console.log(marked(markdownStr, markedOptions))

Input markdown string:

```
console.log('hahaha')
```

```js
console.log('hahaha')
```

Will be rendered as:

<pre><code class="hljs "><span class="hljs-built_in">console</span>.log(<span class="hljs-string">'hahaha'</span>)</code></pre>
<pre><code class="hljs js"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">'hahaha'</span>)</code></pre>

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.