UNPKG

@wordpress/block-library

Version:
1 lines • 3.14 kB
{"version":3,"file":"transforms.mjs","names":[],"sources":["../../src/shortcode/transforms.js"],"sourcesContent":["import { removep, autop } from '@wordpress/autop';\nimport { getBlockTransforms, rawHandler } from '@wordpress/blocks';\nimport { next } from '@wordpress/shortcode';\n\nconst getShortcodeFromTransforms = () =>\n\tgetBlockTransforms( 'from' ).filter(\n\t\t( transform ) =>\n\t\t\ttransform.type === 'shortcode' &&\n\t\t\ttransform.blockName !== 'core/shortcode'\n\t);\n\n// Single-shortcode only: keeps the transform menu honest (one block in, one\n// block out) and avoids unmatched shortcodes silently falling back to freeform.\nconst isSingleShortcode = ( text, tag ) => {\n\tconst trimmed = text.trim();\n\tconst match = next( tag, trimmed );\n\treturn (\n\t\t!! match && match.index === 0 && match.content.length === trimmed.length\n\t);\n};\n\nconst transforms = {\n\tfrom: [\n\t\t{\n\t\t\ttype: 'shortcode',\n\t\t\t// Per \"Shortcode names should be all lowercase and use all\n\t\t\t// letters, but numbers and underscores should work fine too.\n\t\t\t// Be wary of using hyphens (dashes), you'll be better off not\n\t\t\t// using them.\" in https://codex.wordpress.org/Shortcode_API\n\t\t\t// Require that the first character be a letter. This notably\n\t\t\t// prevents footnote markings ([1]) from being caught as\n\t\t\t// shortcodes.\n\t\t\ttag: '[a-z][a-z0-9_-]*',\n\t\t\tattributes: {\n\t\t\t\ttext: {\n\t\t\t\t\ttype: 'string',\n\t\t\t\t\tshortcode: ( attrs, { content } ) => {\n\t\t\t\t\t\treturn removep( autop( content ) );\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\tpriority: 20,\n\t\t},\n\t],\n\t// One `to` transform per registered shortcode-from block. A single transform\n\t// with a dynamic `blocks` list won't work: `isMatch` runs once per\n\t// transform, so all targets would surface (or none) regardless of which\n\t// shortcode tag the block actually contains.\n\tget to() {\n\t\treturn getShortcodeFromTransforms().map( ( fromTransform ) => ( {\n\t\t\ttype: 'block',\n\t\t\tblocks: [ fromTransform.blockName ],\n\t\t\tisMatch: ( { text = '' } ) => {\n\t\t\t\treturn []\n\t\t\t\t\t.concat( fromTransform.tag )\n\t\t\t\t\t.some( ( tag ) => isSingleShortcode( text, tag ) );\n\t\t\t},\n\t\t\ttransform: ( { text = '' } ) => {\n\t\t\t\treturn rawHandler( { HTML: `<p>${ text.trim() }</p>` } );\n\t\t\t},\n\t\t} ) );\n\t},\n};\n\nexport default transforms;\n"],"mappings":";;;;AAIA,MAAM,mCACL,mBAAoB,MAAO,CAAC,CAAC,QAC1B,cACD,UAAU,SAAS,eACnB,UAAU,cAAc,gBAC1B;AAID,MAAM,qBAAsB,MAAM,QAAS;CAC1C,MAAM,UAAU,KAAK,KAAK;CAC1B,MAAM,QAAQ,KAAM,KAAK,OAAQ;CACjC,OACC,CAAC,CAAE,SAAS,MAAM,UAAU,KAAK,MAAM,QAAQ,WAAW,QAAQ;AAEpE;AAEA,MAAM,aAAa;CAClB,MAAM,CACL;EACC,MAAM;EAQN,KAAK;EACL,YAAY,EACX,MAAM;GACL,MAAM;GACN,YAAa,OAAO,EAAE,cAAe;IACpC,OAAO,QAAS,MAAO,OAAQ,CAAE;GAClC;EACD,EACD;EACA,UAAU;CACX,CACD;CAKA,IAAI,KAAK;EACR,OAAO,2BAA2B,CAAC,CAAC,KAAO,mBAAqB;GAC/D,MAAM;GACN,QAAQ,CAAE,cAAc,SAAU;GAClC,UAAW,EAAE,OAAO,SAAU;IAC7B,OAAO,CAAC,CAAC,CACP,OAAQ,cAAc,GAAI,CAAC,CAC3B,MAAQ,QAAS,kBAAmB,MAAM,GAAI,CAAE;GACnD;GACA,YAAa,EAAE,OAAO,SAAU;IAC/B,OAAO,WAAY,EAAE,MAAM,MAAO,KAAK,KAAK,EAAG,MAAM,CAAE;GACxD;EACD,EAAI;CACL;AACD"}