@tryghost/mg-shortcodes 0.2.13 → 0.2.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -25,6 +25,11 @@ shortcodes.add('link', ({attrs, content}) => {
25
25
  return `<a href="${attrs.url}">${content}</a>`;
26
26
  });
27
27
 
28
+ // [abc color="red"]<p>Full post</p>[def]<p>Free excerpt</p>[/abc]
29
+ shortcodes.addWitSplit('abc', 'def', 0, ({content}) => {
30
+ return content; // <p>Full post</p>
31
+ });
32
+
28
33
  // Unwrap [block] shortcodes
29
34
  shortcodes.unwrap('block');
30
35
 
package/lib/Shortcodes.js CHANGED
@@ -8,6 +8,10 @@ export default class Shortcodes {
8
8
  this.shortcodes.push({name, callback});
9
9
  }
10
10
 
11
+ addWitSplit(name, split, target, callback) {
12
+ this.shortcodes.push({name, split, target, callback});
13
+ }
14
+
11
15
  unwrap(name) {
12
16
  this.shortcodes.push({name, callback: ({content}) => {
13
17
  return `${content} `;
@@ -142,14 +146,24 @@ export default class Shortcodes {
142
146
  return (result) ? result : false;
143
147
  }
144
148
 
149
+ getShortcode(name) {
150
+ if (!name) {
151
+ return false;
152
+ }
153
+
154
+ let foundShortcode = this.shortcodes.filter(n => n.name === name);
155
+
156
+ return foundShortcode[0];
157
+ }
158
+
145
159
  getCallback(name) {
146
160
  if (!name) {
147
161
  return false;
148
162
  }
149
163
 
150
- let thisThing = this.shortcodes.filter(n => n.name === name);
164
+ let foundShortcode = this.shortcodes.filter(n => n.name === name);
151
165
 
152
- return thisThing[0].callback;
166
+ return foundShortcode[0].callback;
153
167
  }
154
168
 
155
169
  process() {
@@ -158,10 +172,18 @@ export default class Shortcodes {
158
172
  let knownShortcode = this.hasKnownShortcode(text);
159
173
 
160
174
  if (knownShortcode) {
161
- // console.log({knownShortcode});
162
- const theCall = this.getCallback(knownShortcode.name);
175
+ const thisShortcode = this.getShortcode(knownShortcode.name);
176
+ const theCall = thisShortcode.callback;
163
177
  const attrs = this.parseAttributes(knownShortcode.match.groups.attrs);
164
- const content = knownShortcode.match.groups.content;
178
+ let content = knownShortcode.match.groups.content;
179
+
180
+ // If the shortcode has a `split` param & is present in the `content`, split the content and return the target
181
+ if (thisShortcode.split && content.includes(`[${thisShortcode.split}]`)) {
182
+ const splitContent = content.split(`[${thisShortcode.split}]`);
183
+ content = splitContent[thisShortcode.target];
184
+ }
185
+
186
+ // content = content?.trim();
165
187
 
166
188
  if (theCall) {
167
189
  text = text.replace(knownShortcode.match[0], theCall({attrs, content}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/mg-shortcodes",
3
- "version": "0.2.13",
3
+ "version": "0.2.15",
4
4
  "repository": "https://github.com/TryGhost/migrate/tree/main/packages/mg-shortcodes",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -20,9 +20,9 @@
20
20
  "access": "public"
21
21
  },
22
22
  "devDependencies": {
23
- "eslint": "8.52.0",
23
+ "eslint": "8.55.0",
24
24
  "jest": "29.7.0",
25
25
  "jest-extended": "4.0.2"
26
26
  },
27
- "gitHead": "726ec1832b8bdd045e32b7b591f441a410565464"
27
+ "gitHead": "e91603ed4e431bba60521d63a070e397d2ccee2f"
28
28
  }