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

Add language class to <pre> tags for Prism compatibility #365

Closed
wants to merge 1 commit into from

Conversation

janko
Copy link

@janko janko commented Oct 17, 2019

I'm using Docusaurus, which uses Remarkable under-the-hood. Its default highlighter is higlight.js, but I wanted to use Prism instead.

Prism almost works, the only hiccup is that Prism CSS requires <pre> tags to have the language-* class. Since I didn't have much luck figuring out how to create a plugin for this, I thought we could make this built-in. This is what this PR does.

@janko janko changed the title Add language class to <pre> tags as well for Prism compatibility Add language class to <pre> tags for Prism compatibility Oct 17, 2019
@coveralls
Copy link

Coverage Status

Coverage remained the same at 98.544% when pulling 5da306e on janko:prism-compatibility into c510fce on jonschlinkert:master.

@janko
Copy link
Author

janko commented Oct 19, 2019

I've worked around this by styling the <code> tag directly. Prism doesn't actually require the <pre> tag to have the class, though its themes do rely on it, but that can be tweaked easily.

@janko janko closed this Oct 19, 2019
@janko janko deleted the prism-compatibility branch October 19, 2019 02:09
@psalaets
Copy link

psalaets commented Jan 7, 2023

Sorry to bump an old thread but this is what I'm using with remarkable 2.0.1 and prism 1.29.0.

const prismFriendlyFence: Remarkable.Plugin = (md) => {
  const original = md.renderer.rules.fence;

  md.renderer.rules.fence = function(tokens, idx, options, env, instance) {
    const prefix = options?.langPrefix ? options.langPrefix : '';
    const langName = tokens[idx].params;
    const langClass = langName ? ` class="${prefix + langName}"` : '';

    return original.call(this, tokens, idx, options, env, instance)
      .replace('<pre>', `<pre${langClass}>`);
  };
};

Usage

const md = new Remarkable()
  .use(prismFriendlyFence);

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

Successfully merging this pull request may close these issues.

None yet

3 participants