README.md
2.31 Kb ยท 74 lines
urlfilter
- URL-based filtering
Filter items using URL query parameters with toggleable markdown links. Works with AVL tree structures where each filter contains its associated items.
Given filters ["T1", "T2", "size:XL"]
and URL /shop?filter=T1,size:XL
, it generates toggle links:
- T1 (active, click to remove)
T2(inactive, click to add)- size:XL (active, click to remove)
Markdown output:
1[**T1**](/p/samcrew/urlfilter?filter=size:XL) - [~~T2~~](/p/samcrew/urlfilter=T1,T2,size:XL) - [**size:XL**](/p/samcrew/urlfilter?filter=T1)
Rendered as:
T1 - T2 - size:XL
Usage
The package expects a two-level AVL tree structure:
- Top level: Filter names as keys (e.g., "T1", "size:XL", "on_sale")
- Second level: Item trees containing the actual items for each filter
1// Build the main filters tree
2filters := avl.NewTree()
3
4// Subtree for filter "T1"
5t1Items := avl.NewTree()
6t1Items.Set("key1", "item1")
7t1Items.Set("key2", "item2")
8filters.Set("T1", t1Items)
9
10// Subtree for filter "size:XL"
11t2Items := avl.NewTree()
12t2Items.Set("key3", "item3")
13filters.Set("T2", t2Items)
14
15// URL with active filter "T1"
16u, _ := url.Parse("/shop?filter=T1")
17
18// Apply filtering
19mdLinks, filteredItems := urlfilter.ApplyFilters(u, filters, "filter") // "filter" for /shop?*filter*=T1
20
21// mdLinks โ Markdown links for toggling filters
22// filteredItems โ AVL tree containing only filtered items
API
1func ApplyFilters(u *url.URL, items *avl.Tree, paramName string) (string, *avl.Tree)
Parameters:
u
: URL containing query parametersitems
: Two-level AVL tree (filters โ item trees)paramName
: Query parameter name (e.g., "filter" for /shop?filter=T1)
URL Format:
- Single filter:
?filter=T1
- Multiple filters:
?filter=T1,size:XL,on_sale
- Filter names are comma-separated
Returns:
- Markdown links: Toggleable filter links with formatting
- Filtered items: AVL tree containing items from active filters
- If no filters active: returns all items
- Item keys are preserved, values show which filter matched