-
Notifications
You must be signed in to change notification settings - Fork 11
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 slug and name to <pre> wrapper element #208
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it! 👍
The only thing that gives me pause is that perhaps it would be better if the |
If it's added as a span, then should it be opt-in? My only reservation about it being in the DOM is that we now have to style it. The current styling we introduce is minimal (hljs + layout for line numbers) but if we style how this span looks then users would need to reset its styles for their own themes or hide it entirely if they don't want it. |
What if we |
+1 for |
I suppose it depends on the use case. I was thinking that the language name would be displayed instead of the language slug. But then what purpose does having the language slug as an attribute? If both were added as hidden |
I think two |
Oh hold on. Adding inline elements will cause problems when the block is rendered and the HTML is served in RSS feeds or email clients, where the styles may get stripped out. We'd have to make sure that the <pre class="wp-block-code" aria-describedby="shcb-language-3" data-shcb-language-name="JavaScript" data-shcb-language-slug="javascript"><div><code class="hljs language-javascript shcb-code-table"><span class='shcb-loc'><span><span class="hljs-keyword">if</span> ( a < b ) {
</span></span><mark class='shcb-loc'><span> alert( <span class="hljs-string">'A is less than B!'</span> );
</span></mark><span class='shcb-loc'><span>}
</span></span></code></div><small class="shcb-language" id="shcb-language-3"><span class="shcb-language__label">Code language:</span> <span class="shcb-language__name">JavaScript</span> <span class="shcb-language__paren">(</span><span class="shcb-language__slug">javascript</span><span class="shcb-language__paren">)</span></small></pre> When styles are present, the language is hidden by default: When styles are removed: And when custom styles are added to display the language info in the desired location: For example: .wp-block-code {
position: relative;
}
.wp-block-code .shcb-language {
clip: initial;
width: auto;
height: auto;
margin: auto;
clip-path: none;
top: 6px;
right: 6px;
line-height: 1em;
white-space: normal;
font-family: sans-serif;
font-size: 12px;
font-weight: bold;
}
.wp-block-code .shcb-language__label,
.wp-block-code .shcb-language__paren,
.wp-block-code .shcb-language__slug {
display: none;
} |
For maximum styling flexibility, the language should probably be added to |
Updated #208 (comment) to show how |
.shcb-language { | ||
border: 0; | ||
clip: rect(1px, 1px, 1px, 1px); | ||
-webkit-clip-path: inset(50%); | ||
clip-path: inset(50%); | ||
height: 1px; | ||
margin: -1px; | ||
overflow: hidden; | ||
padding: 0; | ||
position: absolute; | ||
width: 1px; | ||
word-wrap: normal; | ||
word-break: normal; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is adapted from the ubiquitous screen-reader-text
styles used in WordPress.
Ah this is a lot better! I like how even the parentheses are surrounded by classes so they can be visually hidden. Which properties need to be reset in |
Fixes #37.
This PR adds two new data attributes to the
pre.wp-block-code
element:data-language-slug
: The identifier for the language.data-language-name
: The translated name (label) for the language.Given this
post_content
:Before:
After: