|
| 1 | +/** |
| 2 | + * picture-caption.js | https://theme-next.js.org/docs/tag-plugins/picture-caption |
| 3 | + * Description: Insert an image with caption. |
| 4 | + * Usage: |
| 5 | + * {% picturecaption pictureUrl, captionText %} |
| 6 | + * {% picturecaption pictureUrl, pictureWidth, pictureHeight, captionText, captionUrl, captionIcon, captionPosition, captionAlignment %} |
| 7 | + */ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +module.exports = ctx => function(args) { |
| 12 | + args = args.join(' ').split(','); |
| 13 | + |
| 14 | + const pictureUrl = args[0].trim(); |
| 15 | + if (!pictureUrl) { |
| 16 | + ctx.log.warn('Image URL can NOT be empty in picture-caption tag.'); |
| 17 | + return ''; |
| 18 | + } |
| 19 | + |
| 20 | + // Default value. |
| 21 | + let pictureWidth = 'auto'; |
| 22 | + let pictureHeight = 'auto'; |
| 23 | + let captionText = ''; |
| 24 | + let captionUrl = ''; |
| 25 | + let captionIcon = ''; |
| 26 | + const theme = ctx.theme.config; |
| 27 | + let captionPosition = theme.picture_caption.caption_position; |
| 28 | + let captionAlignment = theme.picture_caption.caption_alignment; |
| 29 | + |
| 30 | + // Determine the invocation method based on the number of parameters. |
| 31 | + if (args.length === 2) { |
| 32 | + captionText = args[1].trim() || 'Picture'; |
| 33 | + } else if (args.length >= 3) { |
| 34 | + // Take parameters from back to front. |
| 35 | + captionAlignment = ['left', 'center', 'right'].includes(args[args.length - 1].trim()) ? args.pop().trim() : 'center'; |
| 36 | + captionPosition = ['top', 'bottom'].includes(args[args.length - 1].trim()) ? args.pop().trim() : 'bottom'; |
| 37 | + captionIcon = args.pop().trim(); // This value can be null. |
| 38 | + captionUrl = args.pop().trim(); // This value can be null. |
| 39 | + // Take parameters from front to back. |
| 40 | + pictureWidth = args[1].trim() || 'auto'; |
| 41 | + pictureHeight = args[2].trim() || 'auto'; |
| 42 | + // Allow the captionText to contain English commas, and extract all characters starting from index 3 to the end. |
| 43 | + captionText = args.slice(3).join(',') || 'Picture'; |
| 44 | + } |
| 45 | + |
| 46 | + const style = `width: ${pictureWidth}; height: ${pictureHeight}; object-fit: contain;`; |
| 47 | + |
| 48 | + const imgTag = `<img src="${pictureUrl}" alt="${captionText || 'Picture'}" style="${style}">`; |
| 49 | + |
| 50 | + // Handle captionUrl and captionIcon, default value is null, 'None/none' or empty string is considered as no link. |
| 51 | + const hascaptionUrl = captionUrl && captionUrl.toLowerCase() !== 'none'; |
| 52 | + const hascaptionIcon = captionIcon && captionIcon.toLowerCase() !== 'none'; |
| 53 | + |
| 54 | + let captionHtml = ''; |
| 55 | + if (captionText) { |
| 56 | + let captionContent = ''; |
| 57 | + if (hascaptionIcon) { |
| 58 | + if (!captionIcon.startsWith('fa')) { captionIcon = 'fa fa-' + captionIcon; } |
| 59 | + captionContent = `<i class="${captionIcon}"></i>${captionText}`; |
| 60 | + } else { |
| 61 | + captionContent = captionText; |
| 62 | + } |
| 63 | + if (hascaptionUrl) { |
| 64 | + captionContent = `<a href="${captionUrl}" target="_blank">${captionContent}</a>`; |
| 65 | + } |
| 66 | + captionHtml = `<div class="picture-caption-caption picture-caption-align-${captionAlignment}">${captionContent}</div>`; |
| 67 | + } |
| 68 | + |
| 69 | + // Determine the order based on captionPosition. |
| 70 | + const content |
| 71 | + = captionPosition === 'bottom' |
| 72 | + ? `${imgTag}${captionHtml}` |
| 73 | + : `${captionHtml}${imgTag}`; |
| 74 | + |
| 75 | + return `<div class="picture-caption-container picture-caption-position-${captionPosition}" style="max-width: ${pictureWidth};">${content}</div>`; |
| 76 | +}; |
0 commit comments