In Short:
I need some HTML tags to work in CKEditor 5

I would be grateful if someone could help me, I've tried many things (see below) but can't get it to work.

The specific tags I need are "<ruby>" and "<rt>".  These tags are used to add simplified Japanese characters above regular Kanji (so young readers who haven't memorized the Kanji yet can read them).
Example: <ruby>Kanji<rt>Hiragana</rt></ruby>
Real Example: <ruby>漢字<rt>かんじ</rt></ruby>

What I've tried.

1.) 
Of course as the "Admin", the Admin has full permissions to use "Full HTML" (Configuration > Content authoring > Text editors and formats).
Just by giving "Full HTML" I would assume ANY HTML would be displayed in the Editor but... no.  When viewing the Edit Page > CKEditor, the text is gone in regular mode but in "Source View" the tags are still there and when viewing the page the tags are working correctly as well.

2.)
"Full HTML" permissions to Admin and specifically selecting checking "Limit allowed HTML tags" and then adding "<ruby> and "<rt>" tags to the "Allowed HTML tags" input box.

After changing this setting and adding the tags, for some reason it actually REMOVED all the tags I had in there previously... ;(  And it doesn't allow (strips them) when entered in "Source View"... this is so... confusing.

3.)
#1 and #2 didn't work so... had to call up my previous life coding experience. I added a function to one of my custom modules which in turn loaded a custom CKEditor 5 plugin.

I tried many versions of the js plugin... coulding get any to work.

Plugin:
Placed in,
C:\wamp\www\thedreamchest\core\modules\ckeditor5\js\plugins\backdrop-ruby\backdrop-ruby.js

js Code Start
===================

(function () {
 'use strict';

 const Plugin = window.CKEDITOR5.core.Plugin;

 class AllowRuby extends Plugin {
   static get requires() {
     return [ window.CKEDITOR5.htmlSupport.GeneralHtmlSupport ];
   }

   init() {
     const editor = this.editor;

     const dataFilter = editor.plugins.get('DataFilter');

     // Explicitly allow ruby and rt
     dataFilter.allowElement('ruby');
     dataFilter.allowElement('rt');

     dataFilter.allowAttributes({
       name: 'ruby',
       attributes: true,
       classes: true,
       styles: true
     });

     dataFilter.allowAttributes({
       name: 'rt',
       attributes: true,
       classes: true,
       styles: true
     });
   }
 }

 window.CKEDITOR5.allowRuby = AllowRuby;

})();

===================
js Code End

===================

Module:
Placed in,
C:\wamp\www\thedreamchest\modules\bs_book\bs_book.module

// Trying to get Ruby tags to show up in the CKEditor 5
// Copied from:
// \core\modules\ckeditor5\ckeditor5.module
// Search: hook_library_info

function bs_book_library_info() {
 $module_path = backdrop_get_path('module', 'ckeditor5');
 $info = system_get_info('module', 'bs_book');
 
 $libraries['backdrop.ckeditor5.ruby'] = array(
   'title' => 'Allows Ruby Tags.',
   'version' => $info['version'],
   'js' => array(
     $module_path . '/js/plugins/backdrop-ruby/backdrop-ruby.js' => array(),
   ),
   //'css' => array(
   //  $module_path . '/css/ckeditor5-maximize.css' => array(),
   //),
 );
 
 return $libraries;
}

 

// copied from:
// https://docs.backdropcms.org/api/backdrop/core%21modules%21ckeditor5%21c...
// OR (the actual file)
// \core\modules\ckeditor5\ckeditor5.module
// seach: ckeditor5_ckeditor5_plugins

function bs_book_ckeditor5_plugins() {
 //$module_path = backdrop_get_path('module', 'ckeditor5');
 //$image_prefix = $module_path . '/icons';

 $plugins['ruby.Ruby'] = array(
   'library' => array('bs_book', 'backdrop.ckeditor5.ruby'),
   'config' => array(
     'maximizeLabel' => t('Ruby'),
   ),
 );
return $plugins;
}

Accepted answer

Yes, you can use TinyMCE on some content and CKEditor5 in others. There is no problem having both enabled on a system.

Comments

I think maybe the problem is with CKEditor stripping the tags - it can be more aggressive than what you would expect from the text format settings.  I copied your examples into an example page with those tags added into the limit settings and it worked when not using a WYSIWYG editor (I got an error from another module, but that is irrelevant to this).

<p>Example: <ruby>Kanji<rt>Hiragana</rt></ruby></p>


<p>Real Example: <ruby>漢字<rt>かんじ</rt></ruby></p>

 

 

You way well find it easier to do this with TinyMCE if working without a WYSIWYG editor is not an option for this.

indigoxela's picture

The specific tags I need are "<ruby>" and "<rt>"

Wow, yeah, that's a pain with CKEditor5. A custom plugin, for sure, but no clue, what the code has to look like.

If TinyMCE is an option for you - that works out of the box. Just add those two tags under Limit allowed HTML tags on your format's configuration page (like admin/config/content/formats/filtered_html or whatever it is).

If you need some dialog to let people set those tags with more convenience, you'd still need a custom editor plugin - but that's less painful than with CKE5.

Thank you both for your suggestions.  Yeah, I'll give TinyMCE a try... better than trying to fight that behemoth CKEditor 5.   The TinyMCE page says it can "co-exist with CKEditor" which is interesting.  I'll just back-up the site and DB before doing anything.

Yes, you can use TinyMCE on some content and CKEditor5 in others. There is no problem having both enabled on a system.

Worked like a charm! Exactly what I envisioned... now I can edit the ruby text right in the editor, sweet:)  Thank you!  For those who may have a similar issue, try TinyMCE, create a new "text format" and call it "Full HTML TinyMCE" (or whatever you like) > assign Admin to be able to use it and then when you edit a piece of content you can select either TinyMCE or CKEditor 5 from the drop down.
 

indigoxela's picture

Out of curiosity: why are these tags only relevant for admins?

Don't "regular" editors on that site also need them under circumstances?

Yes, the editors play nicely, no problem to have them both turned on. But personally, I usually switch to TinyMCE, disable the CKEditor5 module and never look back. 😉

"why are these tags only relevant for admins?"  I'm allowing the editor as well.  That being said, Ruby Text can be a bit of a hassle to edit... easy to accidentally delete a tag or part of a tag and messes everything up.  They have zero HTML skills and opening a Japanese page in source view with all those extra tags is overwhelming... there are a LOT of tags.

indigoxela's picture

Ruby Text can be a bit of a hassle to edit...

Yes, I can imagine that. No idea, how an editor dialog (or whatever) for easier editing of those should look like - in terms of user experience.