Problem/Motivation
I used CKEditor 4 module of Core in the past. I switched to CKEditor 5. It works well, but leads to a major regression.
The HTML is now saved as a single line. This means that revision comparison (via Diff module) have become impossible. Every change you make leads to the single line HTML being different, and the entire line being highlighted as a difference. Good luck figuring out what has changed then.
I looked at CKEditor configuration, Diff configuration, but nothing seems to be there to solve this. Also, I checked the database, and the HTML is now really saved as a single line there, while before it was stored in multiple lines, matching what CKEditor shows when viewing the source of the content.
Steps to reproduce
Use CKEditor 5, create a node, then change the node, and compare the revisions with the Diff module. Or, just check the stored HTML of the node body and its revisions.
Proposed resolution
Store in the database the multiline HTML that is also shown by CKEditor in its Source view, as it did with CKEditor 4.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Issue fork drupal-3351660
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
dhendriks commentedAlmost two months later, no replies. Can somebody at least confirm that we consider this a bug?
Comment #3
damienmckennaI can confirm this happens, and that it is annoying.
Comment #4
damienmckennaI found the same question on StackOverflow, but no answer yet: https://stackoverflow.com/questions/71733317/ckeditor5-html-output-forma...
Comment #5
damienmckennaThere's also an official feature request about this: https://github.com/ckeditor/ckeditor5/issues/8668
Comment #6
damienmckennaOne of the suggestions in the github issue was to use a 3rd party formatter.
Comment #7
wim leersI just realized that we could totally do this in our own
DrupalHtmlEngineplugin 🤔This very simplistic patch already improves
Clearly too naïve, more work needed. But this at least is a starting point :)
Patch:
(followed by
yarn --cwd ./core run build:ckeditor5)Comment #8
damienmckennaThanks for putting that together.
Might this be a better approach?
Comment #9
wim leersSure! 😄
Maybe better still: leading whitespace, to show the tag nesting level? OTOH, if something gets wrapped or unwrapped in some tag, that would then cause many lines to change, versus #7 and #8 which would show that indeed the text inside tags did not change 🤔
I defer to all of you users of the Diff module! 😊
Comment #10
quicksketchIn Backdrop-land we ended up porting the code used by CKEditor's Source plugin. The exact output shown while in source mode is what I would expect to be saved to the field.
That port exists in Backdrop here: ckeditor5.formatter.js.
When the content is saved to the field, we run the formatter, like this:
I also considered/tried building the formatting into the HTML engine (we ported
DrupalHtmlEngineinto our module as well). But I ran into a different problem in that when using CKEditor's "Source" button, the internal formatter expects all the HTML to be on one line. If you run the formatter on already-formatted HTML, you end up with lots of issues like double-indented lines and extra new lines added when viewing the source within CKEditor.Comment #11
wim leersThanks so much for letting us know that, @quicksketch! 🙏😊
I know this is a long stretch, but … any chance you'd be interested in creating a merge request against Drupal core, to port this wonderful logic from Backdrop? 😄 (It's so cool to have contributions flowing in both directions! I hope you like what you've found in CKEditor 5 — I definitely missed working with you this time around: you were so instrumental in getting CKEditor 4 in Drupal core!)