Skip to content

Commit

Permalink
feat: Support for [gallery] shortcodes with no specific IDs
Browse files Browse the repository at this point in the history
In WordPress, a gallery shortcode with no files will create a gallery of all attached media.
This commit mimics that behavior.

Ref: #68
  • Loading branch information
ashishb committed Nov 24, 2024
1 parent 7266f08 commit 6b550e8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ func (g Generator) newHugoPage(pageURL *url.URL, page wpparser.CommonFields) (*h
g.imageURLProvider,
*pageURL, page.Author, page.Title, page.PublishDate,
page.PublishStatus == wpparser.PublishStatusDraft || page.PublishStatus == wpparser.PublishStatusPending,
page.Categories, page.Tags, page.Footnotes, page.Content, page.GUID, page.FeaturedImageID, page.PostFormat)
page.Categories, page.Tags, g.wpInfo.GetAttachmentsForPost(page.PostID),
page.Footnotes, page.Content, page.GUID, page.FeaturedImageID, page.PostFormat)
}

func (g Generator) downloadPageMedia(outputMediaDirPath string, p *hugopage.Page, pageURL *url.URL) error {
Expand Down
17 changes: 13 additions & 4 deletions src/wp2hugo/internal/hugogenerator/hugopage/hugo_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ const (
type Page struct {
// This is the original URL of the page from the WordPress site
absoluteURL url.URL
metadata map[string]any
markdown string
attachments []wpparser.AttachmentInfo

metadata map[string]any
markdown string
}

const _WordPressMoreTag = "<!--more-->"
Expand Down Expand Up @@ -69,7 +71,8 @@ var _hugoAudioLinks = regexp.MustCompile(`{{< audio.*?src="([^\"]+?)".*? >}}`)
var _hugoParallaxBlurLinks = regexp.MustCompile(`{{< parallaxblur.*?src="([^\"]+?)".*? >}}`)

func NewPage(provider ImageURLProvider, pageURL url.URL, author string, title string, publishDate *time.Time,
isDraft bool, categories []string, tags []string, footnotes []wpparser.Footnote,
isDraft bool, categories []string, tags []string, attachments []wpparser.AttachmentInfo,
footnotes []wpparser.Footnote,
htmlContent string, guid *rss.GUID, featuredImageID *string, postFormat *string) (*Page, error) {
metadata, err := getMetadata(provider, pageURL, author, title, publishDate, isDraft, categories, tags, guid,
featuredImageID, postFormat)
Expand All @@ -79,6 +82,7 @@ func NewPage(provider ImageURLProvider, pageURL url.URL, author string, title st
page := Page{
absoluteURL: pageURL,
metadata: metadata,
attachments: attachments,
}
// htmlContent is the HTML content of the page that will be
// transformed to Markdown
Expand Down Expand Up @@ -259,11 +263,16 @@ func (page *Page) getMarkdown(provider ImageURLProvider, htmlContent string, foo
msg := ""
return &msg, nil
}
attachmentIDs := make([]string, 0, len(page.attachments))
for _, attachment := range page.attachments {
attachmentIDs = append(attachmentIDs, attachment.PostID)
}

converter := getMarkdownConverter()
htmlContent = improvePreTagsWithCode(htmlContent)
htmlContent = replaceCaptionWithFigure(htmlContent)
htmlContent = replaceAudioShortCode(htmlContent)
htmlContent = replaceGalleryWithFigure(provider, htmlContent)
htmlContent = replaceGalleryWithFigure(provider, attachmentIDs, htmlContent)
htmlContent = replaceAWBWithParallaxBlur(provider, htmlContent)
htmlContent = strings.Replace(htmlContent, _WordPressMoreTag, _customMoreTag, 1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestManualLineBreaks(t *testing.T) {
func testMarkdownExtractor(t *testing.T, htmlInput string, markdownOutput string) {
url1, err := url.Parse("https://example.com")
assert.Nil(t, err)
page, err := NewPage(nil, *url1, "author", "Title", nil, false, nil, nil, nil, htmlInput, nil, nil, nil)
page, err := NewPage(nil, *url1, "author", "Title", nil, false, nil, nil, nil, nil, htmlInput, nil, nil, nil)
assert.Nil(t, err)
md, err := page.getMarkdown(nil, htmlInput, nil)
assert.Nil(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ var galleryWithNoIDsErr = errors.New("no image IDs found in gallery shortcode")

// Converts the WordPress's caption shortcode to Hugo shortcode "figure"
// https://adityatelange.github.io/hugo-PaperMod/posts/papermod/papermod-faq/#centering-image-in-markdown
func replaceGalleryWithFigure(provider ImageURLProvider, htmlData string) string {
func replaceGalleryWithFigure(provider ImageURLProvider, attachmentIDs []string, htmlData string) string {
log.Debug().
Msg("Replacing gallery with figures")

htmlData = replaceAllStringSubmatchFunc(_GalleryRegEx, htmlData,
func(groups []string) string {
info, err := galleryReplacementFunction(provider, groups[1])
info, err := galleryReplacementFunction(provider, attachmentIDs, groups[1])
if err != nil {
return fmt.Sprintf("[gallery %s]", info) // Return the original shortcode
}
Expand All @@ -43,7 +43,7 @@ func replaceGalleryWithFigure(provider ImageURLProvider, htmlData string) string
return htmlData
}

func galleryReplacementFunction(provider ImageURLProvider, galleryInfo string) (string, error) {
func galleryReplacementFunction(provider ImageURLProvider, attachmentIDs []string, galleryInfo string) (string, error) {
var output strings.Builder

// Find columns layout
Expand All @@ -56,10 +56,17 @@ func galleryReplacementFunction(provider ImageURLProvider, galleryInfo string) (
// Find image IDs
ids := _idRegEx.FindStringSubmatch(galleryInfo)
if len(ids) == 0 {
log.Warn().
Str("galleryInfo", galleryInfo).
Msg("No image IDs found in gallery shortcode")
return "", galleryWithNoIDsErr
if len(attachmentIDs) > 0 {
ids = []string{"", strings.Join(attachmentIDs, ",")}
log.Info().
Str("galleryInfo", galleryInfo).
Strs("attachmentIDs", attachmentIDs).
Msg("No image IDs found in gallery shortcode, fallback to page attachments")
} else {
log.Warn().
Msg("No image IDs found in gallery shortcode and no page attachments")
return "", galleryWithNoIDsErr
}
}

idsArray := strings.Split(ids[1], ",")
Expand Down

0 comments on commit 6b550e8

Please sign in to comment.