Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

urlfilter source pure

Package urlfilter provides functionality to filter items based on URL query parameters. It is designed to work with a...

Readme View source

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 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

Overview

Package urlfilter provides functionality to filter items based on URL query parameters. It is designed to work with an avl.Tree structure where each key represents a filter and each value is an avl.Tree containing items associated with that filter.

Functions 1

func ApplyFilters

1func ApplyFilters(u *url.URL, items *bptree.BPTree, paramName string) (string, *bptree.BPTree)
source

ApplyFilters filters items based on the "filter" query parameter in the given URL and generates a Markdown representation of all available filters.

Expected `items` structure:

  • `items` is an *bptree.BPTree where each key is a filter name (e.g., "T1", "size:XL", "on_sale") and each value is an *bptree.BPTree containing the items for that filter.
  • Each item tree uses: Key (string): Unique item identifier Value (any) : Optional associated item data

Example:

Example
 1// Build the main filters tree
 2filters := bptree.NewBPTree32()
 3
 4// Subtree for filter "T1"
 5t1Items := bptree.NewBPTree32()
 6t1Items.Set("item1", nil)
 7t1Items.Set("item2", nil)
 8filters.Set("T1", t1Items)
 9
10// URL with active filter "T1"
11u, _ := url.Parse("/shop?filter=T1")
12
13mdFilters, items := ApplyFilters(u, filters, "filter")
14
15// mdFilters	→ Markdown links for toggling filters
16// items    	→ AVL tree containing the filtered items

Imports 5

Source Files 4