Skip to content

Commit 7043097

Browse files
committed
mg-wp-xml: Add support for [gallery] shortcodes
1 parent 48c09f6 commit 7043097

File tree

3 files changed

+110
-6
lines changed

3 files changed

+110
-6
lines changed

packages/mg-wp-api/lib/processor.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,9 @@ const processExcerpt = (html, excerptSelector = false) => {
173173
}
174174
};
175175

176-
const processShortcodes = async ({html}) => {
176+
const processShortcodes = async ({html, options}) => {
177177
const shortcodes = new Shortcodes();
178+
const attachments = options?.attachments ?? null;
178179

179180
shortcodes.add('vc_btn', ({attrs}) => {
180181
let buttonHref = attrs?.link ?? false;
@@ -252,6 +253,47 @@ const processShortcodes = async ({html}) => {
252253
return `<iframe src="${attrs.src}" height="${attrs.height}" style="border:0; width: 100%;" loading="lazy"></iframe>`;
253254
});
254255

256+
if (attachments && attachments.length) {
257+
shortcodes.add('gallery', ({attrs}) => {
258+
// Convert `ids` param to array of images
259+
const images = attrs?.ids.split(',').map((i) => {
260+
let idInt = parseInt(i.trim());
261+
let foundAttachment = _.find(attachments, (item) => {
262+
return parseInt(item.id) === idInt;
263+
});
264+
return foundAttachment;
265+
});
266+
267+
let items = [];
268+
269+
images.forEach((item) => {
270+
items.push({
271+
fileName: basename(item.url),
272+
src: item.url,
273+
alt: item.alt,
274+
width: item.width,
275+
height: item.height
276+
});
277+
});
278+
279+
items = items.map((item, index) => {
280+
return {
281+
...item,
282+
row: Math.floor(index / 3)
283+
};
284+
});
285+
286+
let cardOpts = {
287+
env: {dom: new SimpleDom.Document()},
288+
payload: {
289+
images: items
290+
}
291+
};
292+
293+
return serializer.serialize(galleryCard.render(cardOpts));
294+
});
295+
}
296+
255297
shortcodes.add('sourcecode', ({attrs, content}) => {
256298
let captionString = (attrs?.title) ? `<figcaption>${attrs.title}</figcaption>` : '';
257299
let classString = (attrs?.language) ? `language-${attrs.language}` : '';
@@ -325,7 +367,7 @@ const processContent = async ({html, excerptSelector, featureImageSrc = false, f
325367
allowRemoteScraping = true;
326368
}
327369

328-
html = await processShortcodes({html});
370+
html = await processShortcodes({html, options});
329371

330372
// Drafts can have empty post bodies
331373
if (!html) {

packages/mg-wp-api/test/process.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,63 @@ const hello () => {
766766

767767
expect(convertedHtml).toEqual('<!--kg-card-begin: html--><audio controls src="/path/to/file.mp3" preload="metadata"></audio><!--kg-card-end: html--> <!--kg-card-begin: html--><audio controls src="/path/to/file.ogg" preload="metadata"></audio><!--kg-card-end: html-->');
768768
});
769+
770+
test('Can handle gallery shortcodes', async function () {
771+
let html = `[gallery ids="123,234,345,456" columns="4" size="full"]`;
772+
773+
let options = {
774+
attachments: [
775+
{
776+
id: '123',
777+
url: 'https://example.com.com/wp-content/uploads/2025/02/24/123.jpg',
778+
description: null,
779+
alt: 'Image 123 alt text',
780+
width: 1200,
781+
height: 800
782+
},
783+
{
784+
id: '234',
785+
url: 'https://example.com.com/wp-content/uploads/2025/02/24/234.jpg',
786+
description: null,
787+
alt: '',
788+
width: 1200,
789+
height: 800
790+
},
791+
{
792+
id: '345',
793+
url: 'https://example.com.com/wp-content/uploads/2025/02/24/345.jpg',
794+
description: null,
795+
alt: '',
796+
width: 1200,
797+
height: 800
798+
},
799+
{
800+
id: '456',
801+
url: 'https://example.com.com/wp-content/uploads/2025/02/24/456.jpg',
802+
description: null,
803+
alt: '',
804+
width: 1200,
805+
height: 800
806+
}
807+
]
808+
};
809+
810+
let convertedHtml = await processor.processShortcodes({html, options});
811+
812+
expect(convertedHtml).toEqual('<figure class="kg-card kg-gallery-card kg-width-wide"><div class="kg-gallery-container"><div class="kg-gallery-row"><div class="kg-gallery-image"><img src="https://example.com.com/wp-content/uploads/2025/02/24/123.jpg" width="1200" height="800" loading="lazy" alt="Image 123 alt text"></div><div class="kg-gallery-image"><img src="https://example.com.com/wp-content/uploads/2025/02/24/234.jpg" width="1200" height="800" loading="lazy" alt></div></div><div class="kg-gallery-row"><div class="kg-gallery-image"><img src="https://example.com.com/wp-content/uploads/2025/02/24/345.jpg" width="1200" height="800" loading="lazy" alt></div><div class="kg-gallery-image"><img src="https://example.com.com/wp-content/uploads/2025/02/24/456.jpg" width="1200" height="800" loading="lazy" alt></div></div></div></figure>');
813+
});
814+
815+
test('Will skip gallery shortcodes if no attachments avaliable', async function () {
816+
let html = `[gallery ids="123,234,345,456" columns="4" size="full"]`;
817+
818+
let options = {
819+
attachments: []
820+
};
821+
822+
let convertedHtml = await processor.processShortcodes({html, options});
823+
824+
expect(convertedHtml).toEqual('[gallery ids="123,234,345,456" columns="4" size="full"]');
825+
});
769826
});
770827

771828
describe('wpCDNToLocal', function () {

packages/mg-wp-xml/lib/process.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ const processTags = ($wpTerms) => {
103103
return categories.concat(tags);
104104
};
105105

106-
const preProcessContent = async ({html}) => { // eslint-disable-line no-shadow
106+
const preProcessContent = async ({html, options}) => { // eslint-disable-line no-shadow
107107
// Drafts can have empty post bodies
108108
if (!html) {
109109
return html;
@@ -126,7 +126,7 @@ const preProcessContent = async ({html}) => { // eslint-disable-line no-shadow
126126
html = $html.html();
127127

128128
// Convert shortcodes here to that they don't accidently get wrapped in <p> tags by MarkdownIt
129-
html = await MgWpAPI.process.processShortcodes({html});
129+
html = await MgWpAPI.process.processShortcodes({html, options});
130130

131131
return html;
132132
};
@@ -187,7 +187,8 @@ const processPost = async ($post, users, options) => {
187187
};
188188

189189
post.data.html = await preProcessContent({
190-
html: $($post).children('content\\:encoded').text()
190+
html: $($post).children('content\\:encoded').text(),
191+
options
191192
});
192193

193194
const mdParser = new MarkdownIt({
@@ -291,6 +292,8 @@ const processAttachment = async ($post) => {
291292
let attachmentDesc = $($post).find('content\\:encoded').text() || null;
292293
let attachmentAlt = null;
293294

295+
let meta = await processWPMeta($post);
296+
294297
$($post).find('wp\\:postmeta').each((i, row) => {
295298
let metaKey = $(row).find('wp\\:meta_key').text();
296299
let metaVal = $(row).find('wp\\:meta_value').text();
@@ -304,7 +307,9 @@ const processAttachment = async ($post) => {
304307
id: attachmentKey,
305308
url: attachmentUrl,
306309
description: attachmentDesc,
307-
alt: attachmentAlt
310+
alt: attachmentAlt,
311+
width: meta?._wp_attachment_metadata?.width,
312+
height: meta?._wp_attachment_metadata?.height
308313
};
309314
};
310315

0 commit comments

Comments
 (0)