UNPKG

@wordpress/block-library

Version:
52 lines (51 loc) • 1.98 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_compose = require("@wordpress/compose"); // packages/block-library/src/code/utils.js /** * Escapes ampersands, shortcodes, and links. * * @param {string} content The content of a code block. * @return {string} The given content with some characters escaped. */ function escape(content) { return (0, _wordpress_compose.pipe)(escapeOpeningSquareBrackets, escapeProtocolInIsolatedUrls)(content || ""); } /** * Returns the given content with all opening shortcode characters converted * into their HTML entity counterpart (i.e. [ => &#91;). For instance, a * shortcode like [embed] becomes &#91;embed] * * This function replicates the escaping of HTML tags, where a tag like * <strong> becomes &lt;strong>. * * @param {string} content The content of a code block. * @return {string} The given content with its opening shortcode characters * converted into their HTML entity counterpart * (i.e. [ => &#91;) */ function escapeOpeningSquareBrackets(content) { return content.replace(/\[/g, "&#91;"); } /** * Converts the first two forward slashes of any isolated URL into their HTML * counterparts (i.e. // => &#47;&#47;). For instance, https://youtube.com/watch?x * becomes https:&#47;&#47;youtube.com/watch?x. * * An isolated URL is a URL that sits in its own line, surrounded only by spacing * characters. * * See https://github.com/WordPress/wordpress-develop/blob/5.1.1/src/wp-includes/class-wp-embed.php#L403 * * @param {string} content The content of a code block. * @return {string} The given content with its ampersands converted into * their HTML entity counterpart (i.e. & => &amp;) */ function escapeProtocolInIsolatedUrls(content) { return content.replace(/^(\s*https?:)\/\/([^\s<>"]+\s*)$/m, "$1&#47;&#47;$2"); } exports.escape = escape; //# sourceMappingURL=utils.cjs.map