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

Allow custom attributes to be applied to pre and code tags #48

Merged
merged 4 commits into from
May 23, 2021

Conversation

andria-dev
Copy link
Contributor

@andria-dev andria-dev commented Apr 11, 2021

Fixes #22

I created a function, getAttributes(attributes), that takes either a string or an object of strings or numbers and outputs a string of HTML attributes. This function is then used to allow the use of the following options: preAttributes and codeAttributes. Usage is as follows:

const syntaxHighlight = require("@11ty/eleventy-plugin-syntaxhighlight");

module.exports = function(eleventyConfig) {
  eleventyConfig.addPlugin(syntaxHighlight, {
    preAttributes: { tabindex: 0 }
  });
};

This will add tabindex="0" to the <pre> tag — allowing code blocks with scrollbars to be keyboard accessible — like so:

<pre class="language-js" tabindex="0"><!-- etcetera --></pre>

Chris Brown added 4 commits April 10, 2021 23:23
Joins the formattedAttributes with space characters to allow for multiple attributes in the attribute map. Also fixes the test to ensure support for multiple attributes.
@zachleat
Copy link
Member

zachleat commented May 23, 2021

Going to merge this but I wonder if we should also change tabindex="0" to be added by default? Maybe in the next major version

I’ll probably also remove the string support as I think the object literal support is probably sufficient (if you disagree please chime in!)

@zachleat
Copy link
Member

Filed the tabindex question at #49

@zachleat zachleat merged commit ccd0adb into 11ty:master May 23, 2021
@zachleat zachleat self-assigned this May 23, 2021
@zachleat zachleat added this to the v3.1.1 milestone May 23, 2021
@chriskirknielsen
Copy link
Contributor

This is fantastic! I added PR #50 to enable dynamic attributes to be generated based on context data — this will for example allow the language to be provided directly as an attribute (see #22 use-case, solved with example below). I hope this isn't already available with this PR as I couldn't get it to work, but please let me know if I'm wrong.

eleventyConfig.addPlugin(pluginSyntaxHighlight, {
	preAttributes: {
		'data-lang': function (context) { return context.language.toUpperCase(); }
	}
});

matijs added a commit to matijs/probablerobot.net that referenced this pull request May 26, 2021
‘@11ty/eleventy-plugin-syntaxhighlight’ recently [added][1] support for
preAttributes that can be used to put a tabindex attributes on a
scrollable (`overflow: auto;`) `<pre>` element. It's not yet the default
setting for these elements, but this sets it through the eleventy
config.

[1]: 11ty/eleventy-plugin-syntaxhighlight#48
matijs added a commit to matijs/probablerobot.net that referenced this pull request May 26, 2021
‘@11ty/eleventy-plugin-syntaxhighlight’ recently [added][1] support for
preAttributes that can be used to put a tabindex attributes on a
scrollable (`overflow: auto;`) `<pre>` element. It's not yet the default
setting for these elements, but this sets it through the eleventy
config.

[1]: 11ty/eleventy-plugin-syntaxhighlight#48
zachleat added a commit to 11ty/eleventy-base-blog that referenced this pull request Jan 24, 2023
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.

Allow to customize attributes applied to the <pre>/<code> tags
3 participants