Skip to content

Commit

Permalink
feat: extract links for Hugo shortcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishb committed Mar 17, 2024
1 parent a999195 commit eaaa56c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/wp2hugo/internal/hugogenerator/hugo_gen_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ func writePage(outputMediaDirPath string, pagePath string, page wpparser.CommonF
prefixes = append(prefixes, fmt.Sprintf("http://www.%s", pageURL.Host))

if downloadMedia {
log.Debug().
Int("links", len(links)).
Msg("Downloading media files")
for _, link := range links {
for _, prefix := range prefixes {
link = strings.TrimPrefix(link, prefix)
Expand Down
16 changes: 13 additions & 3 deletions src/wp2hugo/internal/hugogenerator/hugopage/hugo_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ type Page struct {
HTMLContent string
}

var _imageLinks = regexp.MustCompile(`!\[.*?]\((.+?)\)`)
var _markdownImageLinks = regexp.MustCompile(`!\[.*?]\((.+?)\)`)

// Extracts "src" from Hugo figure shortcode
// {{< figure align=aligncenter width=905 src="/wp-content/uploads/2023/01/Stollemeyer-castle-1024x768.jpg" alt="" >}}
var _hugoFigureLinks = regexp.MustCompile(`{{< figure.*?src="(.+?)".*? >}}`)

func (page Page) getRelativeURL() string {
return page.AbsoluteURL.Path
Expand All @@ -57,12 +61,18 @@ func (page Page) WPImageLinks() ([]string, error) {
if err != nil {
return nil, err
}
arr1 := getMarkdownLinks(_markdownImageLinks, *markdown)
arr2 := getMarkdownLinks(_hugoFigureLinks, *markdown)
return append(arr1, arr2...), nil
}

func getMarkdownLinks(regex *regexp.Regexp, markdown string) []string {
var links []string
matches := _imageLinks.FindAllStringSubmatch(*markdown, -1)
matches := regex.FindAllStringSubmatch(markdown, -1)
for _, match := range matches {
links = append(links, match[1])
}
return links, nil
return links
}

func (page Page) writeMetadata(w io.Writer) error {
Expand Down

0 comments on commit eaaa56c

Please sign in to comment.