From 9defa29f85531672ced6a41924c29e22b2b88b88 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Wed, 22 Nov 2023 16:19:33 +0000 Subject: [PATCH] Add support for shortcodes with if/else results --- packages/mg-shortcodes/README.md | 5 ++ packages/mg-shortcodes/lib/Shortcodes.js | 32 ++++++++-- packages/mg-shortcodes/test/shortcode.test.js | 58 +++++++++++++++++++ 3 files changed, 90 insertions(+), 5 deletions(-) diff --git a/packages/mg-shortcodes/README.md b/packages/mg-shortcodes/README.md index 2356e498a..939b785b7 100644 --- a/packages/mg-shortcodes/README.md +++ b/packages/mg-shortcodes/README.md @@ -25,6 +25,11 @@ shortcodes.add('link', ({attrs, content}) => { return `${content}`; }); +// [abc color="red"]

Full post

[def]

Free excerpt

[/abc] +shortcodes.addWitSplit('abc', 'def', 0, ({content}) => { + return content; //

Full post

+}); + // Unwrap [block] shortcodes shortcodes.unwrap('block'); diff --git a/packages/mg-shortcodes/lib/Shortcodes.js b/packages/mg-shortcodes/lib/Shortcodes.js index 9ff3a1759..0564bfb76 100644 --- a/packages/mg-shortcodes/lib/Shortcodes.js +++ b/packages/mg-shortcodes/lib/Shortcodes.js @@ -8,6 +8,10 @@ export default class Shortcodes { this.shortcodes.push({name, callback}); } + addWitSplit(name, split, target, callback) { + this.shortcodes.push({name, split, target, callback}); + } + unwrap(name) { this.shortcodes.push({name, callback: ({content}) => { return `${content} `; @@ -142,14 +146,24 @@ export default class Shortcodes { return (result) ? result : false; } + getShortcode(name) { + if (!name) { + return false; + } + + let foundShortcode = this.shortcodes.filter(n => n.name === name); + + return foundShortcode[0]; + } + getCallback(name) { if (!name) { return false; } - let thisThing = this.shortcodes.filter(n => n.name === name); + let foundShortcode = this.shortcodes.filter(n => n.name === name); - return thisThing[0].callback; + return foundShortcode[0].callback; } process() { @@ -158,10 +172,18 @@ export default class Shortcodes { let knownShortcode = this.hasKnownShortcode(text); if (knownShortcode) { - // console.log({knownShortcode}); - const theCall = this.getCallback(knownShortcode.name); + const thisShortcode = this.getShortcode(knownShortcode.name); + const theCall = thisShortcode.callback; const attrs = this.parseAttributes(knownShortcode.match.groups.attrs); - const content = knownShortcode.match.groups.content; + let content = knownShortcode.match.groups.content; + + // If the shortcode has a `split` param & is present in the `content`, split the content and return the target + if (thisShortcode.split && content.includes(`[${thisShortcode.split}]`)) { + const splitContent = content.split(`[${thisShortcode.split}]`); + content = splitContent[thisShortcode.target]; + } + + // content = content?.trim(); if (theCall) { text = text.replace(knownShortcode.match[0], theCall({attrs, content})); diff --git a/packages/mg-shortcodes/test/shortcode.test.js b/packages/mg-shortcodes/test/shortcode.test.js index 6013c9f76..edd86ec70 100644 --- a/packages/mg-shortcodes/test/shortcode.test.js +++ b/packages/mg-shortcodes/test/shortcode.test.js @@ -515,4 +515,62 @@ describe('Shortcode processing', function () { expect(parsed).toEqual('Hello world '); }); + + test('Can handle shortcodes with splits', function () { + const shortcodes = new Shortcodes(); + + shortcodes.addWitSplit('premium_content', 'premelse', 0, ({attrs, content}) => { + return `
${content}
`; + }); + + shortcodes.add('plan_setup', () => { + return ''; + }); + + const html = `[premium_content color="red"] +

Full post

+ [premelse] +

Free excerpt

+ [/premium_content] + [premium_content plan="unregistered," type="show"] + [plan_setup id="2" hide_title="true"] + [/premium_content]`; + + let parsed = shortcodes.parse(html).trim(); + + expect(parsed).toInclude('
'); + expect(parsed).toInclude('

Full post

'); + expect(parsed).not.toInclude('

Free excerpt

'); + expect(parsed).not.toInclude('premium_content'); + expect(parsed).not.toInclude('plan_setup'); + }); + + test('Can handle shortcodes with splits and return other part', function () { + const shortcodes = new Shortcodes(); + + shortcodes.addWitSplit('premium_content', 'premelse', 1, ({attrs, content}) => { + return `
${content}
`; + }); + + shortcodes.add('plan_setup', () => { + return ''; + }); + + const html = `[premium_content color="red"] +

Full post

+ [premelse] +

Free excerpt

+ [/premium_content] + [premium_content plan="unregistered," type="show"] + [plan_setup id="2" hide_title="true"] + [/premium_content]`; + + let parsed = shortcodes.parse(html).trim(); + + expect(parsed).toInclude('
'); + expect(parsed).toInclude('

Free excerpt

'); + expect(parsed).not.toInclude('

Full post

'); + expect(parsed).not.toInclude('premium_content'); + expect(parsed).not.toInclude('plan_setup'); + }); });