Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

/p/samcrew/urlfilter/v0

Directory · 4 Files
README.md Open

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/v0?filter=size:XL) - [~~T2~~](/p/samcrew/urlfilter/v0=T1,T2,size:XL) - [**size:XL**](/p/samcrew/urlfilter/v0?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 parameters
  • items: 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

Example