Kanka Preserve HTML Entities in Summernote

Checks PRE and CODE tags in Summernote and ensures that their contents use HTML entities for proper display.

As of 10.01.2023. See ბოლო ვერსია.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Kanka Preserve HTML Entities in Summernote
// @namespace    http://tampermonkey.net/
// @version      2
// @description  Checks PRE and CODE tags in Summernote and ensures that their contents use HTML entities for proper display.
// @author       Salvatos
// @license      MIT
// @match        https://kanka.io/*
// @match        https://marketplace.kanka.io/*
// @icon         https://www.google.com/s2/favicons?domain=kanka.io
// @run-at       document-end
// ==/UserScript==

// Wait for Summernote to initialize
$('#entry').on('summernote.init', function() {
    // Grab node tree from visual editor
    var masterNode = document.querySelector('#entry + .note-editor .note-editable');

    // Find CODEs and PREs
    masterNode.querySelectorAll(":is(pre, code)").forEach((domObject) => {
        domObject.replaceChildren(domObject.innerHTML);
        // Exclude ' < '  and ' > ' for CSS and comparison operators
        domObject.textContent = domObject.textContent.replace(/ &lt; /g, " < ").replace(/ &gt; /g, " > ");
    });

    // Apply changes to master copy in case of immediate save or switch to code view
    document.getElementById('entry').value = masterNode.innerHTML;
    // BUG: for some reason, this causes changes made in code view to be discarded if you don’t switch back to visual first, so let’s bring back our good old code view save fix...
    $('#entry').on('summernote.change.codeview', function(we, contents, $editable) {
        $(this).val(contents);
    });
});