Syntax Highlighting for HTML block (Add Codemirror)#76773
Syntax Highlighting for HTML block (Add Codemirror)#76773
Conversation
Add codemirror to package.json in the root and in the block-editor package.
Add a code editor component to the block editor package. For now, support CSS, JavaScript and HTML. Enable: Line numbers Highlight History Tabbing
In the HTML block, replace the PlainText block-editor component with the new CodeEditor component (This component also fall backs to PlainText).
|
Size Change: +188 kB (+2.45%) Total Size: 7.88 MB
ℹ️ View Unchanged
|
CodeMirror adds indentation on new lines and may leave trailing spaces. Normalize both before asserting, so the test isn't sensitive to editor-added whitespace.
The aria-label and editor instruction text were not announced because the were on the wrapper, not on the element that is focused (the div with the role set to textbox.
autoCloseTags is not documented where codemirror inline docs claims, but it passes locally.
|
Flaky tests detected in 9d87e8d. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/23586296106
|
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
sirreal
left a comment
There was a problem hiding this comment.
@westonruter and I have spent some time considering this. WordPress Core currently depends on Codemirror v5, we'd hoped to upgrade Core to v6 and expose it as a module.
Any work with Codemirror will need to be very careful about its impact on package sizes. I think will be necessary to use Codemirror as a module. This PR currently has an impact of +187 kB (+56.63%) on the block-editor package which should not be merged as-is.
It's possible that improvements to Core's module system will be necessary for this work to move ahead. 7.0 does include [61587] which will be helpful. #75196 is possible prerequisite.
This PR is a refresh of #60155 from 2024.
That PR is stale and likely not a good starting point. There are some more recent examples of using Codemirror in the editor here:
|
I need everyone who has information about decisions about enabling syntax highligting to add that to the issue, alternatively create a new issue with all the information, it is not possible for other contributors to know what has been discussed if the contexts are in other repositories or in private. |
|
How the script is introduced to Gutenberg, has little effect on how the existing code editors are updated to use it. What I mean is, the component may be moved, but its actual functionality won't change. |
|
I think we should start an effort in Gutenberg to upgrade CodeMirror from v5 to v6. We'll use Gutenberg to update the existing surfaces in core which use v5 to use v6 instead. To go along with that, we can introduce CodeMirror in more places, like here in the HTML block. Then in WordPress 7.1 (🤞🏻) we can have v6 replace v5 in core. |
|
I'll share some ideas I had about the code editor and what it might look like more broadly:
|
Yes but the setting is not available in the block editor yet, it needs to be coded. |
I think it is beyond my capabilities. |
|
I think this is the one where I have the least knowledge
But can we add these to a new issue with clear tasks (checklist) and maybe close the old one to not muddy it.
|
Linting seems like something we can still do, no? The current Custom HTML widget integration in core does linting at least for tags not allowed by Kses. Any linting that would flag invalid HTML would be helpful and should be straightforward to add.
I know that @sirreal has worked on this already, but my understanding is that CodeMirror can be used for this as well. The syntax-highlighted HTML can be rendered to the block in the save function (in some way) or else added as block attributes which can override the static block markup in a dynamic block. The tricky thing is how to deal with markup added to the Code block, like bolded, highlighted, or linked text. |
|
Linting is not included in the scope of the original issue, so I didn't include that package. |

What?
Partial for: #10423
Introduces Codemirror (version 6) as a dependency and adds a new CodeEditor component to the block-editor package.
Adds the CodeEditor component to the HTML block, introducing line numbers and syntax highlighting.
Why?
Syntax highlightning has been requested since 2018 and there has been several attempts to add it.
How?
This PR is a refresh of #60155 from 2024.
Some key differences:
What's not included
Testing Instructions
Testing Instructions for Keyboard
The instructions in the aria-describedby could be improved.
Screen reader announces HTML (this is the aria-label), multiline, and the text from aria-describedby;
"In the editing area, the Tab key enters a tab character. Press Escape then Tab to move focus out of the editor. Use this editor to write HTML."
Screenshots or screencast
Use of AI Tools
Codex wrote the index.js file for the component, based on reading #60155
It also wrote the readme file.