editor.js/public/assets/json-preview.js
Taly aafab1d395
Use Vite builder (#2300)
* vite builder initial

* save

* add displayName

* add paragraph from npm

* fix postcss apply

* remove some packages, fix tests

* Update cypress.yml

* remove logs

* remove unused packages

* update path to image

* update

* Update index.html

* Update cypress.yml

* Update cypress.yml

* Update cypress.yml

* remove displayName field

* update names

* Update index.ts

* Update index.ts

* update packages

* remove packages

* postcss preserve: true

* Update index.html

* Update editor-modules.d.ts

* use public dir for static

* Update vite.config.js

* update modules type

* Update CHANGELOG.md
2023-04-13 22:25:29 +03:00

46 lines
1.4 KiB
JavaScript

/**
* Module to compose output JSON preview
*/
const cPreview = (function (module) {
/**
* Shows JSON in pretty preview
* @param {object} output - what to show
* @param {Element} holder - where to show
*/
module.show = function(output, holder) {
/** Make JSON pretty */
output = JSON.stringify( output, null, 4 );
/** Encode HTML entities */
output = encodeHTMLEntities( output );
/** Stylize! */
output = stylize( output );
holder.innerHTML = output;
};
/**
* Converts '>', '<', '&' symbols to entities
*/
function encodeHTMLEntities(string) {
return string.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
/**
* Some styling magic
*/
function stylize(string) {
/** Stylize JSON keys */
string = string.replace( /"(\w+)"\s?:/g, '"<span class=sc_key>$1</span>" :');
/** Stylize tool names */
string = string.replace( /"(paragraph|quote|list|header|link|code|image|delimiter|raw|checklist|table|embed|warning)"/g, '"<span class=sc_toolname>$1</span>"');
/** Stylize HTML tags */
string = string.replace( /(&lt;[\/a-z]+(&gt;)?)/gi, '<span class=sc_tag>$1</span>' );
/** Stylize strings */
string = string.replace( /"([^"]+)"/gi, '"<span class=sc_attr>$1</span>"' );
/** Boolean/Null */
string = string.replace( /\b(true|false|null)\b/gi, '<span class=sc_bool>$1</span>' );
return string;
}
return module;
})({});