repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
8197cc8
parent
98e17c3
author
Christopher Hoelter
date
2023-09-05 23:23:17 +0000 UTC
Consistent ordering of nav items (#40)

* Ensuring consistent ordering of nav items

If nav items are declared in the yaml without the "-" prefix ordering
was random of those items and a page refresh could change where they
are. This ensures ordering is preserved in that situation.
3 files changed,  +29, -14
M go.mod
M go.sum
M go.mod
+1, -1
 1@@ -32,6 +32,7 @@ require (
 2 	go.uber.org/zap v1.24.0
 3 	golang.org/x/crypto v0.6.0
 4 	golang.org/x/exp v0.0.0-20221211140036-ad323defaf05
 5+	gopkg.in/yaml.v2 v2.4.0
 6 )
 7 
 8 require (
 9@@ -112,5 +113,4 @@ require (
10 	golang.org/x/text v0.7.0 // indirect
11 	google.golang.org/protobuf v1.28.1 // indirect
12 	gopkg.in/ini.v1 v1.67.0 // indirect
13-	gopkg.in/yaml.v2 v2.4.0 // indirect
14 )
M go.sum
+5, -2
 1@@ -21,6 +21,7 @@ github.com/aymanbagabas/go-osc52 v1.2.1/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZ
 2 github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
 3 github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
 4 github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
 5+github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
 6 github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
 7 github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
 8 github.com/caarlos0/sshmarshal v0.1.0 h1:zTCZrDORFfWh526Tsb7vCm3+Yg/SfW/Ub8aQDeosk0I=
 9@@ -124,8 +125,6 @@ github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa02
10 github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
11 github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
12 github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
13-github.com/kolesa-team/go-webp v1.0.2 h1:XCrWqxI7tNOI3dr0YufD9TUb+54vBDogg9KsHH7q5Lc=
14-github.com/kolesa-team/go-webp v1.0.2/go.mod h1:oMvdivD6K+Q5qIIkVC2w4k2ZUnI1H+MyP7inwgWq9aA=
15 github.com/kolesa-team/go-webp v1.0.4 h1:wQvU4PLG/X7RS0vAeyhiivhLRoxfLVRlDq4I3frdxIQ=
16 github.com/kolesa-team/go-webp v1.0.4/go.mod h1:oMvdivD6K+Q5qIIkVC2w4k2ZUnI1H+MyP7inwgWq9aA=
17 github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
18@@ -205,10 +204,12 @@ github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTK
19 github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw=
20 github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0=
21 github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
22+github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
23 github.com/pkg/sftp v1.13.5 h1:a3RLUqkyjYRtBTZJZ1VRrKbN3zhuPLlUc3sphVz81go=
24 github.com/pkg/sftp v1.13.5/go.mod h1:wHDZ0IZX6JcBYRK1TH9bcVq8G7TLpVHYIGJRFnmPfxg=
25 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
26 github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
27+github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
28 github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
29 github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b h1:0LFwY6Q3gMACTjAbMZBjXAqTOzOwFaj2Ld6cjeQ7Rig=
30 github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
31@@ -272,6 +273,7 @@ go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
32 go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
33 go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
34 go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
35+go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
36 go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
37 go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
38 go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
39@@ -366,6 +368,7 @@ google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw
40 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
41 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
42 gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
43+gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
44 gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
45 gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
46 gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
M shared/mdparser.go
+23, -11
 1@@ -18,6 +18,7 @@ import (
 2 	"github.com/yuin/goldmark/renderer"
 3 	ghtml "github.com/yuin/goldmark/renderer/html"
 4 	"github.com/yuin/goldmark/util"
 5+	yaml "gopkg.in/yaml.v2"
 6 )
 7 
 8 type Link struct {
 9@@ -67,29 +68,38 @@ func toBool(obj interface{}) bool {
10 	return obj.(bool)
11 }
12 
13-func toLinks(obj interface{}) ([]Link, error) {
14+func toLinks(orderedMetaData yaml.MapSlice) ([]Link, error) {
15+	var navData interface{}
16+	for i := 0; i < len(orderedMetaData); i++ {
17+		var item = orderedMetaData[i]
18+		if item.Key == "nav" {
19+			navData = item.Value
20+			break
21+		}
22+	}
23+
24 	links := []Link{}
25-	if obj == nil {
26+	if navData == nil {
27 		return links, nil
28 	}
29 
30-	addLinks := func(raw map[interface{}]interface{}) {
31-		for k, v := range raw {
32+	addLinks := func(raw yaml.MapSlice) {
33+		for _, k := range raw {
34 			links = append(links, Link{
35-				Text: k.(string),
36-				URL:  v.(string),
37+				Text: k.Key.(string),
38+				URL:  k.Value.(string),
39 			})
40 		}
41 	}
42 
43-	switch raw := obj.(type) {
44-	case map[interface{}]interface{}:
45+	switch raw := navData.(type) {
46+	case yaml.MapSlice:
47 		addLinks(raw)
48 	case []interface{}:
49 		for _, v := range raw {
50 			switch linkRaw := v.(type) {
51-			case map[interface{}]interface{}:
52-				addLinks(v.(map[interface{}]interface{}))
53+			case yaml.MapSlice:
54+				addLinks(v.(yaml.MapSlice))
55 			default:
56 				return links, fmt.Errorf("unsupported type for `nav` link item (%T), looking for map (`text: href`)", linkRaw)
57 			}
58@@ -264,7 +274,9 @@ func ParseText(text string, linkify Linkify) (*ParsedText, error) {
59 	}
60 	parsed.MetaData.PublishAt = publishAt
61 
62-	nav, err := toLinks(metaData["nav"])
63+	orderedMetaData := meta.GetItems(context)
64+
65+	nav, err := toLinks(orderedMetaData)
66 	if err != nil {
67 		return &parsed, err
68 	}