UNPKG

@wordpress/block-library

Version:
8 lines (7 loc) 2.23 kB
{ "version": 3, "sources": ["../../src/code/utils.js"], "sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { pipe } from '@wordpress/compose';\n\n/**\n * Escapes ampersands, shortcodes, and links.\n *\n * @param {string} content The content of a code block.\n * @return {string} The given content with some characters escaped.\n */\nexport function escape( content ) {\n\treturn pipe(\n\t\tescapeOpeningSquareBrackets,\n\t\tescapeProtocolInIsolatedUrls\n\t)( content || '' );\n}\n\n/**\n * Returns the given content with all opening shortcode characters converted\n * into their HTML entity counterpart (i.e. [ => &#91;). For instance, a\n * shortcode like [embed] becomes &#91;embed]\n *\n * This function replicates the escaping of HTML tags, where a tag like\n * <strong> becomes &lt;strong>.\n *\n * @param {string} content The content of a code block.\n * @return {string} The given content with its opening shortcode characters\n * converted into their HTML entity counterpart\n * (i.e. [ => &#91;)\n */\nfunction escapeOpeningSquareBrackets( content ) {\n\treturn content.replace( /\\[/g, '&#91;' );\n}\n\n/**\n * Converts the first two forward slashes of any isolated URL into their HTML\n * counterparts (i.e. // => &#47;&#47;). For instance, https://youtube.com/watch?x\n * becomes https:&#47;&#47;youtube.com/watch?x.\n *\n * An isolated URL is a URL that sits in its own line, surrounded only by spacing\n * characters.\n *\n * See https://github.com/WordPress/wordpress-develop/blob/5.1.1/src/wp-includes/class-wp-embed.php#L403\n *\n * @param {string} content The content of a code block.\n * @return {string} The given content with its ampersands converted into\n * their HTML entity counterpart (i.e. & => &amp;)\n */\nfunction escapeProtocolInIsolatedUrls( content ) {\n\treturn content.replace(\n\t\t/^(\\s*https?:)\\/\\/([^\\s<>\"]+\\s*)$/m,\n\t\t'$1&#47;&#47;$2'\n\t);\n}\n"], "mappings": ";AAGA,SAAS,YAAY;AAQd,SAAS,OAAQ,SAAU;AACjC,SAAO;AAAA,IACN;AAAA,IACA;AAAA,EACD,EAAG,WAAW,EAAG;AAClB;AAeA,SAAS,4BAA6B,SAAU;AAC/C,SAAO,QAAQ,QAAS,OAAO,OAAQ;AACxC;AAgBA,SAAS,6BAA8B,SAAU;AAChD,SAAO,QAAQ;AAAA,IACd;AAAA,IACA;AAAA,EACD;AACD;", "names": [] }