UNPKG

@wordpress/blocks

Version:
8 lines (7 loc) 2.4 kB
{ "version": 3, "sources": ["../../../src/api/parser/serialize-raw-block.js"], "sourcesContent": ["/**\n * Internal dependencies\n */\nimport { getCommentDelimitedContent } from '../serializer';\n\n/**\n * @typedef {Object} Options Serialization options.\n * @property {boolean} [isCommentDelimited=true] Whether to output HTML comments around blocks.\n */\n\n/** @typedef {import(\"./\").WPRawBlock} WPRawBlock */\n\n/**\n * Serializes a block node into the native HTML-comment-powered block format.\n * CAVEAT: This function is intended for re-serializing blocks as parsed by\n * valid parsers and skips any validation steps. This is NOT a generic\n * serialization function for in-memory blocks. For most purposes, see the\n * following functions available in the `@wordpress/blocks` package:\n *\n * @see serializeBlock\n * @see serialize\n *\n * For more on the format of block nodes as returned by valid parsers:\n *\n * @see `@wordpress/block-serialization-default-parser` package\n * @see `@wordpress/block-serialization-spec-parser` package\n *\n * @param {WPRawBlock} rawBlock A block node as returned by a valid parser.\n * @param {Options} [options={}] Serialization options.\n *\n * @return {string} An HTML string representing a block.\n */\nexport function serializeRawBlock( rawBlock, options = {} ) {\n\tconst { isCommentDelimited = true } = options;\n\tconst {\n\t\tblockName,\n\t\tattrs = {},\n\t\tinnerBlocks = [],\n\t\tinnerContent = [],\n\t} = rawBlock;\n\n\tlet childIndex = 0;\n\tconst content = innerContent\n\t\t.map( ( item ) =>\n\t\t\t// `null` denotes a nested block, otherwise we have an HTML fragment.\n\t\t\titem !== null\n\t\t\t\t? item\n\t\t\t\t: serializeRawBlock( innerBlocks[ childIndex++ ], options )\n\t\t)\n\t\t.join( '\\n' )\n\t\t.replace( /\\n+/g, '\\n' )\n\t\t.trim();\n\n\treturn isCommentDelimited\n\t\t? getCommentDelimitedContent( blockName, attrs, content )\n\t\t: content;\n}\n"], "mappings": ";AAGA,SAAS,kCAAkC;AA6BpC,SAAS,kBAAmB,UAAU,UAAU,CAAC,GAAI;AAC3D,QAAM,EAAE,qBAAqB,KAAK,IAAI;AACtC,QAAM;AAAA,IACL;AAAA,IACA,QAAQ,CAAC;AAAA,IACT,cAAc,CAAC;AAAA,IACf,eAAe,CAAC;AAAA,EACjB,IAAI;AAEJ,MAAI,aAAa;AACjB,QAAM,UAAU,aACd;AAAA,IAAK,CAAE;AAAA;AAAA,MAEP,SAAS,OACN,OACA,kBAAmB,YAAa,YAAa,GAAG,OAAQ;AAAA;AAAA,EAC5D,EACC,KAAM,IAAK,EACX,QAAS,QAAQ,IAAK,EACtB,KAAK;AAEP,SAAO,qBACJ,2BAA4B,WAAW,OAAO,OAAQ,IACtD;AACJ;", "names": [] }