Skip to content

Add language block attribute to code block (to help highlighters) - #76641

Closed
carolinan wants to merge 17 commits into
trunkfrom
update/code-block-language-class
Closed

carolinan wants to merge 17 commits into
trunkfrom
update/code-block-language-class

Conversation

@carolinan

@carolinan carolinan commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

What?

Closes #10200

Introduce a language block attribute for core/code blocks.
The attribute value is used to add a language-slug CSS class on the <code> element: <code class="language-php">

The attribute is set by:

  • A "Code Language" inspector control (TextControl) where the user can type in whatever.
  • Detecting markdown language definitions such as ```php when the user adds content in the code block.
    Finally, it tries to detect the language when transforming to the code block.

Note: there is no attempt to validate that it is a coding language, type "hey" or '''' if you want 🤷‍♀️.

Why?

When using markdown to define the coding language, the reporter expects it to also add the CSS class with the language to the
<code> element. See the issue.
This helps developers / advanced users and third-party code highlighters identify the coding language.

Testing Instructions

Updated Friday March 20.

TYPE in three backticks into a paragraph block (the default block), press Enter.
Observe that it turns into a code block.

TYPE in three backticks followed by word into a paragraph block (the default block), press Enter.
Observe that it turns into a code block.

PASTE three backticks followed by a word into a paragraph block.
```php
Manually transform that paragraph to a code block using the option in the toolbar

Create a new code block and type in three backticks followed by a word.

Confirm that:
The backticks are no longer visible in the code blocks.
The language input field in the block settings sidebar contains the correct word.
The CSS class name is added to the code block: language-word.

Create a new code block and type in three backticks followed by a sentence without a line break:
Confirm that this does not trigger the language detection.

Use of AI Tools

Copilot essentially wrote all of it. It initially tried something more complex so I tried to limit it to a plain text input instead of the custom inspector control it suggested.

Introduce a language attribute for core/code blocks and expose a Code Language inspector control (TextControl) to set it.
Parse fenced code blocks from pasted/edited content and normalize language slugs (sanitizeLanguage, parseFencedCode, toHTMLStr).
Persist the language as a language-<slug> class on the <code> element in save output.
Update block transforms to extract language from <code class="language-..."> and to create blocks from fenced code, and update docs to document the new attribute.
@carolinan carolinan added [Type] Enhancement A suggestion for improvement. [Block] Code Affects the Code Block labels Mar 18, 2026
@github-actions github-actions Bot added the [Package] Block library /packages/block-library label Mar 18, 2026
@github-actions

github-actions Bot commented Mar 18, 2026 •

Copy link
Copy Markdown

Size Change: +637 B (+0.01%)

Total Size: 7.68 MB

📦 View Changed
Filename Size Change
build/scripts/block-library/index.min.js 334 kB +637 B (+0.19%)

compressed-size-action

@github-actions

github-actions Bot commented Mar 18, 2026 •

Copy link
Copy Markdown

Flaky tests detected in c436058.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/23331767410
📝 Reported issues:

@carolinan
carolinan requested a review from Copilot March 19, 2026 04:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a language attribute to the core Code block so Gutenberg can preserve/detect a language slug and emit a language-<slug> class on the saved <code> element to support syntax highlighters.

Changes:

  • Adds a language block attribute (default "") for core/code.
  • Introduces parsing/sanitization helpers to detect fenced-markdown openings (e.g. ```php) and extract language/content.
  • Updates transforms, editor UI (Inspector control), and save markup to persist and render the language as a CSS class.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
test/integration/fixtures/blocks/core__code.json Updates fixture to include the new language attribute.
packages/block-library/src/code/utils.js Adds helpers for language sanitization and fenced-code parsing.
packages/block-library/src/code/transforms.js Enhances paragraph/raw HTML transforms to detect and retain language.
packages/block-library/src/code/save.js Outputs language-<slug> class on <code> when language is set.
packages/block-library/src/code/edit.js Adds Inspector TextControl for language and detects fenced markdown during editing.
packages/block-library/src/code/block.json Declares new language attribute for the block.
docs/reference-guides/core-blocks.md Documents the new language attribute for core/code.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/block-library/src/code/utils.js
Comment thread packages/block-library/src/code/utils.js Outdated
Comment thread packages/block-library/src/code/utils.js Outdated
Comment thread packages/block-library/src/code/transforms.js
carolinan and others added 6 commits March 19, 2026 10:05
Allow optional spaces after the backticks

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Previously, some attributes like border styles did not work when transforming to a code block.
Courtesy of Copilot
@carolinan
carolinan marked this pull request as ready for review March 20, 2026 01:43
@github-actions

github-actions Bot commented Mar 20, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @davidbhayes.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: davidbhayes.

Co-authored-by: carolinan <poena@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: juanmaguitar <juanmaguitar@git.wordpress.org>
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: designsimply <designsimply@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ramonjd

ramonjd commented Mar 20, 2026

Copy link
Copy Markdown
Member

I think this is a good idea, and it works according to instructions.

The only thing I noticed is that, outside a parent block, the backticks convert to a block immediately. I think here in the transforms?

Kapture.2026-03-20.at.16.53.09.mp4

@carolinan

carolinan commented Mar 20, 2026 •

Copy link
Copy Markdown
Contributor Author

@ramonjd I could change the transform to type enter instead of input, but it is a change to existing behavior.

@carolinan

Copy link
Copy Markdown
Contributor Author

I prefer the previous version, but at least it can be tested.

@ramonjd

ramonjd commented Jun 2, 2026

Copy link
Copy Markdown
Member

I could change the transform to type enter instead of input, but it is a change to existing behavior.

I just tested, sorry for the delay in getting back to you. I personally feel the enter is reasonable compromise, if it means the code block + language work consistently together.

But if you think it's a disruptive change in behaviour, I'll defer to your wisdom.


export default function save( { attributes } ) {
const { content, language } = attributes;
const codeClassName = language ? `language-${ language }` : undefined;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I just saw this example is in the HTML spec... nice!

@juanmaguitar

juanmaguitar commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Hi @carolinan 👋

About the conflict with docs/reference-guides/core-blocks.md.

This PR was opened before #77612 was merged (Jun 7), which restructured the generated block docs: docs/reference-guides/core-blocks.md was removed in favor of an overview index at docs/reference-guides/core-blocks/README.md plus a per-block README.md (packages/block-library/src/<block>/README.md). Your branch still edits the now-deleted core-blocks.md, hence the conflict.

Resolution (tested locally, clean):

  1. Merge the latest trunk into your branch.
  2. On the conflict, drop the changes to core-blocks.md (accept its deletion).
  3. Regenerate:
    npm run docs:build
  4. Commit the regenerated files (the per-block README.md plus docs/reference-guides/core-blocks/README.md).

@carolinan carolinan closed this Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Code Affects the Code Block [Package] Block library /packages/block-library [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code block: add language support to retain language information when pasting markdown

4 participants