mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-06 22:40:31 -05:00
Changed metadata variables type:
from map[string]interface{} to map[string]string
This commit is contained in:
parent
453d3eb567
commit
d1eb2ea9e2
3 changed files with 31 additions and 11 deletions
|
@ -13,9 +13,9 @@ import (
|
|||
|
||||
var (
|
||||
parsers = []MetadataParser{
|
||||
&JSONMetadataParser{metadata: Metadata{Variables: make(map[string]interface{})}},
|
||||
&TOMLMetadataParser{metadata: Metadata{Variables: make(map[string]interface{})}},
|
||||
&YAMLMetadataParser{metadata: Metadata{Variables: make(map[string]interface{})}},
|
||||
&JSONMetadataParser{metadata: Metadata{Variables: make(map[string]string)}},
|
||||
&TOMLMetadataParser{metadata: Metadata{Variables: make(map[string]string)}},
|
||||
&YAMLMetadataParser{metadata: Metadata{Variables: make(map[string]string)}},
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -28,7 +28,7 @@ type Metadata struct {
|
|||
Template string
|
||||
|
||||
// Variables to be used with Template
|
||||
Variables map[string]interface{}
|
||||
Variables map[string]string
|
||||
}
|
||||
|
||||
// load loads parsed values in parsedMap into Metadata
|
||||
|
@ -40,7 +40,7 @@ func (m *Metadata) load(parsedMap map[string]interface{}) {
|
|||
m.Template, _ = template.(string)
|
||||
}
|
||||
if variables, ok := parsedMap["variables"]; ok {
|
||||
m.Variables, _ = variables.(map[string]interface{})
|
||||
m.Variables, _ = variables.(map[string]string)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -76,6 +76,15 @@ func (j *JSONMetadataParser) Parse(b []byte) ([]byte, error) {
|
|||
if err := decoder.Decode(&m); err != nil {
|
||||
return b, err
|
||||
}
|
||||
if vars, ok := m["variables"].(map[string]interface{}); ok {
|
||||
vars1 := make(map[string]string)
|
||||
for k, v := range vars {
|
||||
if val, ok := v.(string); ok {
|
||||
vars1[k] = val
|
||||
}
|
||||
}
|
||||
m["variables"] = vars1
|
||||
}
|
||||
|
||||
j.metadata.load(m)
|
||||
|
||||
|
@ -120,6 +129,15 @@ func (t *TOMLMetadataParser) Parse(b []byte) ([]byte, error) {
|
|||
if err := toml.Unmarshal(b, &m); err != nil {
|
||||
return markdown, err
|
||||
}
|
||||
if vars, ok := m["variables"].(map[string]interface{}); ok {
|
||||
vars1 := make(map[string]string)
|
||||
for k, v := range vars {
|
||||
if val, ok := v.(string); ok {
|
||||
vars1[k] = val
|
||||
}
|
||||
}
|
||||
m["variables"] = vars1
|
||||
}
|
||||
t.metadata.load(m)
|
||||
return markdown, nil
|
||||
}
|
||||
|
@ -160,10 +178,12 @@ func (y *YAMLMetadataParser) Parse(b []byte) ([]byte, error) {
|
|||
// convert variables (if present) to map[string]interface{}
|
||||
// to match expected type
|
||||
if vars, ok := m["variables"].(map[interface{}]interface{}); ok {
|
||||
vars1 := make(map[string]interface{})
|
||||
vars1 := make(map[string]string)
|
||||
for k, v := range vars {
|
||||
if key, ok := k.(string); ok {
|
||||
vars1[key] = v
|
||||
if val, ok := v.(string); ok {
|
||||
vars1[key] = val
|
||||
}
|
||||
}
|
||||
}
|
||||
m["variables"] = vars1
|
||||
|
|
|
@ -98,7 +98,7 @@ func TestParsers(t *testing.T) {
|
|||
expected := Metadata{
|
||||
Title: "A title",
|
||||
Template: "default",
|
||||
Variables: map[string]interface{}{"name": "value"},
|
||||
Variables: map[string]string{"name": "value"},
|
||||
}
|
||||
compare := func(m Metadata) bool {
|
||||
if m.Title != expected.Title {
|
||||
|
|
|
@ -20,13 +20,13 @@ const (
|
|||
|
||||
type MarkdownData struct {
|
||||
middleware.Context
|
||||
Doc map[string]interface{}
|
||||
Doc map[string]string
|
||||
}
|
||||
|
||||
// Process processes the contents of a page in b. It parses the metadata
|
||||
// (if any) and uses the template (if found).
|
||||
func (md Markdown) Process(c Config, requestPath string, b []byte, ctx middleware.Context) ([]byte, error) {
|
||||
var metadata = Metadata{Variables: make(map[string]interface{})}
|
||||
var metadata = Metadata{Variables: make(map[string]string)}
|
||||
var markdown []byte
|
||||
var err error
|
||||
|
||||
|
@ -166,7 +166,7 @@ func defaultTemplate(c Config, metadata Metadata, requestPath string) []byte {
|
|||
}
|
||||
|
||||
// Title is first line (length-limited), otherwise filename
|
||||
title, _ := metadata.Variables["title"].(string)
|
||||
title, _ := metadata.Variables["title"]
|
||||
|
||||
html := []byte(htmlTemplate)
|
||||
html = bytes.Replace(html, []byte("{{title}}"), []byte(title), 1)
|
||||
|
|
Loading…
Reference in a new issue