Add support for code tag in rich text editor
See original GitHub issue<code>
tag is not a built-in plugin for wagtail. We use wagtail in-house for our website and would like to be able to write blog posts that include inline code as well as code blocks. I was surprised to find that <code>
is not currently one of the tags available by default. I know that many users of wagtail are non-technical and may not need that particular button, but it would be cool to have out of the box for those of us who are.
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (2 by maintainers)
Top Results From Across the Web
What HTML Tags Does The Rich Text Editor Support?
Below you will find a list of Supported Tags and Attributes that will work within the Rich Text Editor. All tags and attributes...
Read more >how to add code tag in rich text editor? - Stack Overflow
i have rich text editor, in that i want to add code button , on click code button i want HTML code tag,...
Read more >adding html tags like precode in rich text editor - Our Umbraco
Hi,. How can I add Html tags like <pre> <code> in my HTML content. I was able to add <pre><code> in the dropdown...
Read more >Allow HTML tags in rich text editor
We need to allow our users to have source html tags in rich text editor. currently even if the user adds <a href="www.google.com">Click...
Read more >Using the rich text editor for content - Shopify Help Center
Click the Show HTML button to view the HTML code for the content inside the rich text editor. In the HTML view, you...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For now I implemented a
code-block
by defining a customPreformattedTextElementHandler
that keeps all whitespace by temporarily changing the regular expressionWHITESPACE_RE
to match nothing. Not the best approach since it is a module wide constant, but it works:As of #4562 / #5141, inline
code
support is now built-in. This will be released in Wagtail v2.5.We haven’t added
code-block
, because the rich text processing does not currently preserve whitespace in<pre>
tags – it collapses whitespace like with any other content, which is not suitable for code. I think this would be worth supporting, and this issue seems like the right place to keep track of that. Note that this isn’t just an issue for code, but basically any rich text block that uses apre
tag.Here is an illustration of the problem. Thanks @jafacakes2011 for that!
Whether this is fixed in rich text or not, the recommended approach for code blocks is to use a StreamField block, like https://github.com/FlipperPA/wagtailcodeblock, so that block can have a UI that’s more appropriate to code editing, and is easier to render with syntax highlighting.
For a solution to this issue, the change will be similar to #5142. change the HTML to ContentState converter to better handle whitespace collapsing. In this case, that means not doing any collapsing for content that’s within a
<pre>
tag. This aligns with the HTML spec over at https://www.w3.org/TR/html4/struct/text.html#h-9.1.Then, if we introduced built-in support for
code-block
, this would be a similar change to #5141 for blockquote.