~gjabell/bmk

0c1781352156b3c1b74be66180c5685bb2bd3798 — Galen Abell 1 year, 4 months ago d4492c7
Remove UTM params from url
1 files changed, 31 insertions(+), 5 deletions(-)

M bookmark/bookmark.go
M bookmark/bookmark.go => bookmark/bookmark.go +31 -5
@@ 18,6 18,7 @@ package bookmark
import (
	"fmt"
	"io/ioutil"
	"net/url"
	"os"
	"sort"
	"strconv"


@@ 27,6 28,10 @@ import (
	"gopkg.in/yaml.v2"
)

// list of possible utm parameters
var utmParams = []string{"utm_source", "utm_medium", "utm_campaign", "utm_term",
	"utm_content", "utm_brand"}

// Bookmark represents a single bookmark with a URL and optional tags and description
type Bookmark struct {
	URL  string   `yaml:"url"`


@@ 59,16 64,37 @@ func Add(filename string, bookmark Bookmark) error {
		return err
	}

	// normalize tags
	for i, t := range bookmark.Tags {
		bookmark.Tags[i] = strings.ToLower(t)
	}
	sort.Strings(bookmark.Tags)
	bookmark.Tags = normalizeTags(bookmark.Tags)

	bookmark.URL = stripUTM(bookmark.URL)

	lst = append(lst, bookmark)

	return save(filename, lst)
}

func normalizeTags(tags []string) []string {
	for i, t := range tags {
		tags[i] = strings.ToLower(t)
	}

	sort.Strings(tags)

	return tags
}

func stripUTM(us string) string {
	u, _ := url.Parse(us)

	q := u.Query()
	for _, p := range utmParams {
		delete(q, p)
	}
	u.RawQuery = q.Encode()

	return u.String()
}

// List returns a list of bookmarks that match the given filter or indices
// Either filter or indices will be relevant, but not both
func List(filename string, filter Filter) (Bookmarks, error) {