Kanka Preserve HTML Entities in Summernote

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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);
    });
});