Last active
February 24, 2026 20:23
-
-
Save mcmullengreg/244b93fe0c7dcd105a273077058bd0c9 to your computer and use it in GitHub Desktop.
Shortcode Macro build-out. Default loads in all of the formats associated with shorcodes, then process the HTML passed in. Form builds out our form Embed code to be included on a page. Substring-start-end, just passes in a string with start/end position to get the value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #* | |
| This page serves as a template and guide to developing and/or editing new shortcodes | |
| Shortcodes should all be stored inside of: _cms/macros/shortcodes/{name} | |
| The shortcode, file and macro names should match exactly. The name is used as a check to: | |
| 1. Import the macro file | |
| 2. "Name" the macro itself | |
| 3. Trigger a check for which macro to run (inside of do_shortcode) | |
| i.e. #macro form ; shortcode is [form id=""]; filename is form | |
| Basic structure: | |
| - Import the find_shortcode file, this allows you to search for a named shortcode. | |
| - Write your macro/shortcode | |
| - Use $contentString (or similar) to pass in the wysiwyg editor content. This is all that's needed to be passed in | |
| - Ensure you write out to $wysiwyg at the end of it all | |
| - Update do_shortcode with your new entry | |
| *# | |
| #macro ( _template $contentString ) | |
| ## Gets the starting position of the shortcode | |
| ## Uses the curly brace, due to processing occurring higher in the chain | |
| #set ( $startPosPar = $contentString.indexOf('{_template') ) | |
| ## IndexOf uses -1 if an item is not found; 0 or above if it is | |
| #if ( $startPosPar > 0 ) | |
| ## Uses the String by Start and End macro to get the start and end of the shortcode | |
| #set ( $shortCode = "#strByStartEnd($contentString, '{form', '}')" ) | |
| ## Sets the index of the closing brace for the shortcode | |
| #set ( $endPos = $contentString.indexOf('}', $startPosPar) ) | |
| #set ( $endPos = $endPos + 1 ) | |
| ## Gets the full shortcode entity for additional processing | |
| #set ( $fullShortCode = $contentString.substring($startPosPar, $endPos) ) | |
| ## Start processing additional attributes (use as many as needed for your shortcode) | |
| ## The ending attribute, typically a double quote (") | |
| #set ( $endAtt = '"' ) | |
| ## Your attributes, could be done differently, but this ensures we know what we get | |
| #set ( $attStart1 = 'att1="' ) | |
| #set ( $attVal1 = "#strByStartEnd($shortCode, $attStart1, $endAtt)" ) | |
| #set ( $attStart2 = 'att2="' ) | |
| #set ( $attVal2 = "#strByStartEnd($shortCode, $attStart2, $endAtt)" ) | |
| ## After getting all of your values, you can then start building whatever code is needed. It's recommend that you build it as a string, and replace whatever values are needed. | |
| ## The code {att1}, {att2} are placeholders for the real content we want dropped in. | |
| #set ( $embedCode = '<div id="{att1}">{att2}</div>' ) | |
| ## If you have multiple instances of a replace, use replaceAll | |
| #set ( $embedCode = $embedCode.replace("\{att1\}", $attVal1) ) | |
| #set ( $embedCode = $embedCode.replace("\{att2\}", $attVal2) ) | |
| ## Replace the HTML content with your updated $embedCode | |
| #set ( $wysiwyg = $contentString.replace($fullShortCode, $embedCode) ) | |
| ## Don't forget to update do_shortcode with a new elseif entry | |
| #else ## Nothing was found, just pass it through (ensures we don't try to process something that doesn't need it ). Also sets the $wysiwyg variable to be output on the desired content format | |
| #set ( $wysiwyg = $contentString ) | |
| #end | |
| #end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Import all known shortcode Formats | |
| #set ( $folder = $_.locateFolder("macros/shortcodes", "_cms") ) | |
| #set ( $codes = [] ) | |
| #set ( $shortcodes = [] ) | |
| #foreach ( $child as $folder.children ) | |
| #set ( $formatPath = "site://_cms/" + $child.path ) | |
| #if ( $child.name != "default" || $child.name != "_template" ) | |
| #import($formatPath) | |
| #set ( $null = $codes.add($child.name) ) | |
| #end | |
| #end | |
| #macro ( findCodes $contentString ) | |
| #set ( $startPosPar = $contentString.indexOf('[') ) | |
| #if ( $startPosPar > 0 ) | |
| #set ( $scFull = "#strByStartEnd($contentString, '[', ']')" ) | |
| #set ( $scName = $scFull.split("\s") ) | |
| #set ( $scName = $scName[0] ) | |
| #if ( $codes.contains($scName) ) | |
| #set ( $replace = "["+$scFull+"]" ) | |
| #set ( $new = "{"+$scFull+"}" ) | |
| #set ( $contentReplace = $contentString.replace($replace,$new) ) | |
| #set ( $void = $shortcodes.add($new) ) | |
| #set ( $wysiwyg = $contentReplace ) | |
| #findCodes($wysiwyg) | |
| #else | |
| #foreach ( $item in $shortcodes ) | |
| #do_shortcode($item, $wysiwyg) | |
| #end | |
| #end | |
| #else | |
| #foreach ( $item in $shortcodes ) | |
| #do_shortcode($item, $wysiwyg) | |
| #end | |
| #end | |
| #end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #* | |
| * Macro do_shortcode ( $shortcodeName, $content ) | |
| * Processes the actual shortcode, and attaches the macro to it | |
| *# | |
| #macro ( do_shortcode $shortcodeName, $content ) | |
| #if ( $shortcodeName.contains("form") ) | |
| #form($content) | |
| #elseif ( $shortcodeName.contains("nothing") ) | |
| #nothing($content) | |
| ## This entry is an example, and should never be uncommented | |
| ##elseif ( $shortcodeName.contains("_template") ) | |
| ##_template($content) | |
| #else | |
| #set ( $wysiwyg = $content ) | |
| #end | |
| #end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #* | |
| Macro to get a substring by a start and end string, e.g. | |
| Full String Start String End String Return | |
| [getBlock settings] [getBlock ] settings | |
| attribute="your/path" attribute=" " your/path | |
| *# | |
| #macro ( strByStartEnd $contentString $startString $endString )## | |
| #set ( $startPos = $contentString.indexOf($startString) )## | |
| #if ( $startPos > 0 )## | |
| #set ( $startPosExc = $startPos + $startString.length() )## | |
| $contentString.substring($startPosExc, $contentString.indexOf($endString, $startPosExc))## | |
| #end## | |
| #end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import ( "site://_cms/macros/shortcodes/default" ) | |
| #set ( $wysiwyg = $currentPage.getStructuredDataNode("wysiwyg").textValue ) | |
| #findCodes($wysiwyg) | |
| ${wysiwyg} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment