├── package.json ├── README.md ├── LICENSE └── index.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wordpress-shortcode-functions-js", 3 | "version": "1.0.0", 4 | "description": "Searches a string for Wordpress shortcodes and calls a mapped function with the attributes as parameters. Return values replace the shortcode in the string.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/websterman/wordpress-shortcode-functions-js.git" 12 | }, 13 | "keywords": [], 14 | "author": "", 15 | "license": "MIT", 16 | "bugs": { 17 | "url": "https://github.com/websterman/wordpress-shortcode-functions-js/issues" 18 | }, 19 | "homepage": "https://github.com/websterman/wordpress-shortcode-functions-js#readme" 20 | } 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wordpress-shortcode-functions-js 2 | Searches a string for Wordpress shortcodes and calls a mapped function with the attributes as parameters. Return values replace the shortcode in the string. 3 | 4 | 5 | Usage: WPShortcodes( string, functionMap ); 6 | 7 | Returns an object with the following structure: 8 | 9 | { 10 | markup: The returned markup 11 | shortcodes : [ 12 | { 13 | code: The shortcode name, 14 | raw: The entire shortcode, 15 | attributes: [ 16 | { 17 | name: The name of the attribute. 18 | value: The value of the attribute. 19 | } 20 | ] 21 | } 22 | ] 23 | } 24 | 25 | The functionMap object should be sorted as such: 26 | 27 | { 28 | 'exampleShortcode': exampleShortcodeFunction 29 | } 30 | 31 | Where the value is a valid function. The shortcode attributes will be passed to the function as an object with the following structure: 32 | 33 | { 34 | name: Attribute name, 35 | value: Attribute value 36 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 websterman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const WPShortcodes = (data, functionMap =[]) => { 2 | 3 | const stripChars = string => { 4 | return string 5 | .replace("=", "") 6 | .replace(/"/g, "") 7 | .replace(/'/g, ""); 8 | }; 9 | 10 | if (!data) { 11 | throw new Error("Must pass data to WPShortcodes"); 12 | return; 13 | } 14 | var markup = data; 15 | 16 | // Picks up all of the shortcodes and turns them into an array. 17 | const shortcodesRaw = data.match(/\[(.*?)?\]/g); 18 | var shortcodes = []; 19 | 20 | // Loops through the shortcode array to find the name and attributes. 21 | for (let shortcode of shortcodesRaw) { 22 | const code = shortcode.match(/(?<=\[)[a-zA-Z0-9]\w*/g).toString(); 23 | let attributes = []; 24 | const attributeString = shortcode 25 | .toString() 26 | .replace(code, "") 27 | .replace("[", "") 28 | .replace("]", ""); 29 | let attributesArray = attributeString.split(" "); 30 | var skipNext = false; 31 | attributesArray.forEach((attribute, index) => { 32 | if (skipNext) { 33 | skipNext = false; 34 | return; 35 | } 36 | if (attributesArray.length === 1 && attribute) { 37 | attributes.push({ name: attribute }); 38 | return; 39 | } 40 | if (attribute === "") { 41 | return; 42 | } 43 | if (attribute === "=") { 44 | attributes[attributes.length - 1] = { 45 | ...attributes[attributes.length - 1], 46 | ...{ value: stripChars(attributesArray[index + 1]) } 47 | }; 48 | skipNext = true; 49 | return; 50 | } 51 | if (attribute[0] === "=") { 52 | attributes[attributes.length - 1] = { 53 | ...attributes[attributes.length - 1], 54 | ...{ value: stripChars(attribute) } 55 | }; 56 | return; 57 | } 58 | if (attribute[attribute.length - 1] === "=") { 59 | attributes.push({ 60 | name: stripChars(attribute), 61 | value: stripChars(attributesArray[index + 1]) 62 | }); 63 | skipNext = true; 64 | 65 | return; 66 | } 67 | if (attribute.indexOf("=") > 0) { 68 | attribute = attribute.split("="); 69 | attributes.push({ 70 | name: attribute[0], 71 | value: stripChars(attribute[1]) 72 | }); 73 | return; 74 | } 75 | if (attribute.length) { 76 | attributes.push({ name: attribute }); 77 | } 78 | }); 79 | 80 | // If our shortcode name is listed in the function map and it points to a valid function, 81 | // run it and replace the shortcode with whatever the function returns. 82 | if (typeof functionMap[code] === 'function') { 83 | let shortCodeMarkup = functionMap[code](attributes); 84 | if (shortCodeMarkup) { 85 | markup = markup.replace(shortcode, shortCodeMarkup); 86 | } 87 | } 88 | // We add the shortcode to our formatted shortcode array, along with the name(code), raw 89 | // shortcode and the attributes. 90 | shortcodes.push({ code: code, raw: shortcode, attributes: attributes }); 91 | } 92 | 93 | // We create an object with the updated string (markup) and the shortcode array and return it all, in case 94 | // someone wants to do something different with the return values. 95 | 96 | const obj = { markup: markup, shortcodes: shortcodes }; 97 | return obj; 98 | }; 99 | 100 | export default WPShortcodes; 101 | --------------------------------------------------------------------------------