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

'class' attribute value in preAttributes and codeAttributes doesn't work #52

Closed
handreistoicescu opened this issue Jun 3, 2021 · 5 comments
Labels
bug Something isn't working
Milestone

Comments

@handreistoicescu
Copy link

handreistoicescu commented Jun 3, 2021

Description

When a user adds a class attribute value to preAttributes or codeAttributes, nothing happens:

eleventyConfig.addPlugin(pluginSyntaxHighlight, {
    preAttributes: {
      class: "hey-this-is-a-custom-class-name",
    },
    codeAttributes: "this-is-also-custom-class-name",
});

Resulting DOM:

Screenshot 2021-06-03 at 23 48 32

I assume this is because <pre> and <code> already have a class name that denotes the language being used (e.g. language-js).

Possible solutions

  1. Maybe we can concatenate the new class names to the existing one, taking care not to overwrite the originals
  2. ...or add the possibility for the user to add a wrapper element over the <pre>, because the scenario (at least in my case) is to add some layout styling to the code block (say, max-width):
eleventyConfig.addPlugin(pluginSyntaxHighlight, {
    wrapper: {
      element: "div",
      attributes: {
         // here be attributes
      }
    },
});
@handreistoicescu
Copy link
Author

@zachleat I'd be happy to start working on this, but first it'd be awesome to get your input, so I'd be on the right path ✨

@WickyNilliams
Copy link

How about a function like:

eleventyConfig.addPlugin(pluginSyntaxHighlight, {
    wrap: inner => `<div class="a-custom-wrapper">${inner}</div>`
});

This is flexible, can be as simple or complex as you like, and would not complicate the plugin

@zachleat
Copy link
Member

zachleat commented Jun 28, 2022

This was included as an addendum to #50. If you supply a class attribute to preAttributes or codeAttributes it will override the built-in class="language-${language}" class added to <pre> or <code> respectively.

@zachleat zachleat added this to the 4.1.0 milestone Jun 28, 2022
@zachleat zachleat added the bug Something isn't working label Jun 28, 2022
@AleksandrHovhannisyan
Copy link

AleksandrHovhannisyan commented Jun 28, 2022

If you supply a class attribute to preAttributes or codeAttributes it will override the built-in class="language-${language}" class added to <pre> or <code> respectively.

@zachleat Out of curiosity, is there a way to preserve the default class name and append the custom class name? If I migrate back to this plugin in the future, I'd have a use case for this: contextual styling for a particular language but also some custom utility classes.

Edit: oh, you beat me to it: #66 (comment)

@zachleat
Copy link
Member

No but it's easy enough to add it back in with the provided callback! The language is passed to the callback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants