Skip to content

Commit

Permalink
Move getStringArray -> frontmatter.StringArray
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jun 16, 2021
1 parent 1b6ef60 commit 367156b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 30 deletions.
19 changes: 19 additions & 0 deletions frontmatter/frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ func (fm FrontMatter) String(k string, defaultValue string) string {
return defaultValue
}

// StringArray returns m[k] if it's a []string or string array
func (fm FrontMatter) StringArray(k string) []string {
if value, ok := fm[k]; ok {
switch value := value.(type) {
case []string:
return value
case []interface{}:
a := make([]string, len(value))
for i, item := range value {
a[i] = fmt.Sprintf("%s", item)
}
return a
case string:
return []string{value}
}
}
return nil
}

// SortedStringArray returns a sorts list of strings from a
// frontmatter variable that is either a string (in which case it
// is a ws-separated list of words), or a list of strings.
Expand Down
32 changes: 2 additions & 30 deletions plugins/redirect_from.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package plugins

import (
"bytes"
"fmt"
"text/template"
)

Expand Down Expand Up @@ -47,11 +46,7 @@ func (p jekyllRedirectFromPlugin) processRedirectFrom(s Site, ps []Page) (func()
redirects = append(redirects, f)
}
for _, p := range ps {
sources, err := getStringArray(p, "redirect_from")
if err != nil {
return nil, err
}
for _, from := range sources {
for _, from := range p.FrontMatter().StringArray("redirect_from") {
addRedirectFrom(from, p)
}
}
Expand All @@ -64,37 +59,14 @@ func (p jekyllRedirectFromPlugin) processRedirectFrom(s Site, ps []Page) (func()

func (p jekyllRedirectFromPlugin) processRedirectTo(_ Site, ps []Page) error {
for _, p := range ps {
sources, err := getStringArray(p, "redirect_to")
if err != nil {
return err
}
sources := p.FrontMatter().StringArray("redirect_to")
if len(sources) > 0 {
p.SetContent(createRedirectionHTML(sources[0]))
}
}
return nil
}

func getStringArray(p Page, fieldName string) ([]string, error) {
var a []string
if value, ok := p.FrontMatter()[fieldName]; ok {
switch value := value.(type) {
case []string:
a = value
case []interface{}:
a = make([]string, len(value))
for i, item := range value {
a[i] = fmt.Sprintf("%s", item)
}
case string:
a = []string{value}
default:
return nil, fmt.Errorf("unimplemented redirect_from type %T", value)
}
}
return a, nil
}

func createRedirectionHTML(to string) string {
r := redirection{to}
buf := new(bytes.Buffer)
Expand Down

0 comments on commit 367156b

Please sign in to comment.