diff --git a/internal/build/funcs.go b/internal/build/funcs.go index 1503138..f30aab8 100644 --- a/internal/build/funcs.go +++ b/internal/build/funcs.go @@ -194,6 +194,12 @@ func funcs(s *site.Site, a *Assets, md *site.Markdown, icons Icons) template.Fun "excerpt": func(n int, v any) string { return site.Excerpt(n, fmt.Sprint(v)) }, // termURL "tags" "Open Source" -> /tags/open-source/ (or "" if it has none). "termURL": s.TermURL, + // term "tags" "Linux" -> that term's page (nil if there is none), with + // what content/tags/linux.md says about it. + "term": s.TermPage, + // tagged "tags" "Linux" (collection "products"): the products tagged + // Linux, in the collection's order. + "tagged": s.Tagged, // isExternal: an absolute link to another site. "isExternal": md.External, // Cloudflare's markers that turn Email Address Obfuscation off for a diff --git a/internal/editor/quick.go b/internal/editor/quick.go index 34144a1..9f65a58 100644 --- a/internal/editor/quick.go +++ b/internal/editor/quick.go @@ -8,6 +8,7 @@ import ( "path" "path/filepath" "regexp" + "slices" "sort" "strings" "time" @@ -79,7 +80,8 @@ func (s *Server) apiQuick(w http.ResponseWriter, r *http.Request, sess *Session, } // collectionNames lists the site's collections: folders in content/, and -// any configured in site.yaml. +// any configured in site.yaml. A folder named for a taxonomy (content/tags/) +// describes its terms and isn't one. func collectionNames(siteDir string, cfg *site.Config) []string { seen := map[string]bool{} entries, _ := os.ReadDir(filepath.Join(siteDir, "content")) @@ -93,7 +95,7 @@ func collectionNames(siteDir string, cfg *site.Config) []string { } var out []string for name := range seen { - if slug(name) == name { + if slug(name) == name && !slices.Contains(cfg.Taxonomies, name) { out = append(out, name) } } diff --git a/internal/importer/html2md_test.go b/internal/importer/html2md_test.go index bc96aaa..22cbf5a 100644 --- a/internal/importer/html2md_test.go +++ b/internal/importer/html2md_test.go @@ -68,6 +68,10 @@ func TestShortSummary(t *testing.T) { if got := shortSummary(one); len([]rune(got)) > 160 || !strings.HasSuffix(got, "word…") { t.Errorf("one long sentence: %q (%d)", got, len([]rune(got))) } + dotted := "A black heavyweight tee with the kitten on it. A small mark sits on the left chest. The full mark and example.org run across the back. Sizes S to 4XL, printed to order." + if got := shortSummary(dotted); got != "A black heavyweight tee with the kitten on it. A small mark sits on the left chest. The full mark and example.org run across the back." { + t.Errorf("a full stop inside a word: %q", got) + } if got := shortSummary("Short and sweet."); got != "Short and sweet." { t.Errorf("short: %q", got) } diff --git a/internal/importer/write.go b/internal/importer/write.go index 57a012a..fd8e350 100644 --- a/internal/importer/write.go +++ b/internal/importer/write.go @@ -293,6 +293,8 @@ func Write(items []Item, opt Options) (*Report, error) { var mdImage = regexp.MustCompile(`!\[([^\]]*)\]\(\s*]+)`) +var sentenceEnd = regexp.MustCompile(`[.!?]+(\s+|$)`) + // shortSummary keeps a summary to what search results show (about 160 // characters): whole sentences while they fit, else the first one cut at a // word. WordPress's excerpts are its first 55 words, twice that. @@ -301,12 +303,18 @@ func shortSummary(s string) string { if len([]rune(s)) <= max { return s } + // Whole sentences, one after another: a full stop inside a word + // (example.org, 1.5) doesn't end one. out := "" - for _, sent := range regexp.MustCompile(`[^.!?]+[.!?]+(\s+|$)`).FindAllString(s, -1) { - if len([]rune(out+sent)) > max { + for rest := s; rest != ""; { + end := len(rest) + if loc := sentenceEnd.FindStringIndex(rest); loc != nil { + end = loc[1] + } + if len([]rune(out+rest[:end])) > max { break } - out += sent + out, rest = out+rest[:end], rest[end:] } if out = strings.TrimSpace(out); out != "" { return out diff --git a/internal/packs/bundled_test.go b/internal/packs/bundled_test.go index 6ba227d..0ade0cb 100644 --- a/internal/packs/bundled_test.go +++ b/internal/packs/bundled_test.go @@ -160,14 +160,22 @@ func TestBundledPacksBuildAndPass(t *testing.T) { {"content/products/kit.md", "---\ntitle: Starter kit\nsummary: A jar of starter.\nprice: 24\ncurrency: USD\nweight: 1\nphotos:\n - { image: /media/a.png, alt: A jar }\n - { image: /media/b.png, alt: The card }\nbuy: \"https://buy.stripe.com/test_1\"\noptions:\n - { name: With a basket, price: 39.5, buy: \"https://buy.stripe.com/test_2\" }\n---\nWhat's in the box.\n"}, {"content/products/mug.md", "---\ntitle: Mug\nsummary: A mug.\nprice: 12\ncurrency: EUR\nweight: 2\nsold_out: true\n---\n"}, {"content/products/tee.md", "---\ntitle: Tee\nsummary: A tee.\nprice: 24\nprice_from: true\ncurrency: USD\nweight: 3\ntags: [Linux, Shirts]\nphotos:\n - { image: /media/a.png, alt: The tee }\nchoose: Size\nvariants:\n - { name: S, price: 24, id: v1 }\n - { name: 2XL, price: 26, id: v2 }\n - { name: 3XL, price: 28, id: v3, sold_out: true }\ncheckout: { action: \"https://shop.example/cart/checkout\", field: products, item: \"{id}:{qty}\", join: \",\", fields: { currency: USD } }\n---\n"}, - {"content/products/cap.md", "---\ntitle: Cap\nsummary: A cap.\nprice: 20\ncurrency: USD\nweight: 4\nvariants:\n - { group: Black, name: One size, price: 20, value: \"c1:1\" }\ncheckout: { action: \"https://shop.example/cart/checkout\" }\n---\n"}, + {"content/products/cap.md", "---\ntitle: Cap\nsummary: A cap.\nprice: 20\ncurrency: USD\nweight: 4\ntags: [linux]\nvariants:\n - { group: Black, name: One size, price: 20, value: \"c1:1\" }\ncheckout: { action: \"https://shop.example/cart/checkout\" }\n---\n"}, {"content/cart.md", "---\ntitle: Your cart\nlayout: cart\nsuggest: 3\n---\n"}, + {"content/tags/linux.md", "---\ntitle: Linux things\nlayout: shelf\n---\nFor the penguin people.\n"}, + {"content/tags/kitchen.md", "---\ntitle: Kitchen\n---\nComing soon.\n"}, + {"content/tags/shirts.md", "---\ntitle: Shirts\nsummary: Cotton, mostly.\nlayout: shelf\n---\n"}, + {"content/picks.md", "---\ntitle: Picks\nsummary: Picks.\nsections:\n - products: { heading: Penguin picks, tag: Linux, more: All Linux things }\n---\n"}, {"content/products/print.md", "---\ntitle: Print\nsummary: A print.\nprice: 30\ncurrency: USD\nweight: 5\nvariants:\n - { group: Matte, name: A4, price: 30, buy: \"https://buy.stripe.com/a4\" }\n - { group: Matte, name: A3, price: 40, buy: \"https://buy.stripe.com/a3\", sold_out: true }\n---\n"}, }, map[string][]string{ "products/tee/index.html": {`
`, ``, "Size", `name="products" value="v1:1" required data-cart-variant data-id="v1" data-name="S" data-price="24">`, "2XL $26.00", `value="v3:1" required disabled data-cart-variant`, "3XL sold out", `data-cart-add hidden>Add to cart`, `product-buy-button" type="submit">Buy now`, `