| name | utils-reference |
| description | RSSBook utility functions reference. Use this skill when users ask how to use cache, date, ofetch, filter, sort, union, parse, and other utility functions. |
RSSBook Utility Functions Reference
This skill provides detailed usage documentation for all utility functions in RSSBook.
Table of Contents
- Cache
- Date Parsing
- HTTP Requests (ofetch)
- HTML Parsing (load)
- HTML Formatting (formatHTML)
- URL Processing (toAbsoluteURL)
- Feed Parsing (parse)
- Feed Filtering (filter)
- Feed Sorting (sort)
- Feed Merging (union)
- Feed Intersection (intersection)
Cache
Cache utility for storing and reusing data to avoid redundant requests.
Basic Usage
async ({ cache }) => {
const data = await cache.tryGet("cache-key", async () => {
return fetchedData;
});
}
API
| Method | Description |
|---|
tryGet(key, fetcher, maxAgeMs?) | Get cache, execute fetcher if not exists |
get(key) | Get cache value |
set(key, value, maxAgeMs?) | Set cache value |
del(key) | Delete cache |
Example
const data = await cache.tryGet(
`user:${userId}`,
async () => await fetchUser(userId),
5 * 60 * 1000
);
Date Parsing
Universal date parser supporting multiple formats and relative time.
Supported Formats
- ISO 8601:
2024-01-15T10:30:00Z
- Common formats:
2024-01-15, 2024/01/15
- Unix timestamp:
1705312200
- Relative time:
3 days ago, yesterday, 2 hours ago
- Chinese:
今天 (today), 昨天 (yesterday), 前天 (day before yesterday), 周一 (Monday)
Usage
async ({ date }) => {
date("2024-01-15");
date("3 days ago");
date("yesterday 10:30");
date(1705312200);
date("2024-01-15 10:00", +8);
}
HTTP Requests (ofetch)
Enhanced fetch function with automatic retry and type inference.
Usage
async ({ ofetch }) => {
const json = await ofetch("https://api.example.com/data", {
responseType: "json"
});
const html = await ofetch("https://example.com", {
responseType: "text"
});
const data = await ofetch(url, {
headers: { Authorization: "Bearer token" },
responseType: "json"
});
}
Configuration Options
| Option | Description | Default |
|---|
responseType | Response type (json/text/blob) | - |
headers | Request headers | Preset UA |
timeout | Timeout (milliseconds) | 8000 |
retry | Retry count | 2 |
HTML Parsing (load)
Cheerio-based HTML parser providing jQuery-like API.
Usage
async ({ load, ofetch }) => {
const html = await ofetch(url, { responseType: "text" });
const $ = load(html);
const title = $("h1.title").text();
const href = $("a.link").attr("href");
const items = $("article").toArray().map((el) => {
const $el = $(el);
return {
title: $el.find("h2").text().trim(),
link: $el.find("a").attr("href"),
};
});
}
Common Methods
| Method | Description |
|---|
$(selector) | Select elements |
.text() | Get text content |
.html() | Get HTML content |
.attr(name) | Get attribute value |
.find(selector) | Find child elements |
.toArray() | Convert to array |
.first() / .last() | Get first/last element |
.parent() / .children() | Parent/child elements |
HTML Formatting (formatHTML)
Sanitizes and formats HTML content, removing dangerous tags and scripts.
Usage
async ({ formatHTML }) => {
const clean = formatHTML(rawHtml);
const cleanWithUrls = formatHTML(rawHtml, "https://example.com");
}
Features
- Removes dangerous tags like
<script>, <style>
- Preserves safe HTML tags (paragraphs, links, images, etc.)
- Automatically converts relative URLs to absolute URLs
- Cleans unsafe attributes
URL Processing (toAbsoluteURL)
Converts relative URLs to absolute URLs.
Usage
async ({ toAbsoluteURL }) => {
toAbsoluteURL("/path/to/page", "https://example.com");
toAbsoluteURL("../image.png", "https://example.com/blog/post");
toAbsoluteURL("https://other.com/page", "https://example.com");
}
Feed Parsing (parse)
Parses RSS/Atom/JSON Feed content into standard Data format.
Usage
async ({ parse, ofetch }) => {
const xml = await ofetch(feedUrl, { responseType: "text" });
const data = parse(xml);
const jsonData = parse(jsonObject, "raw");
}
Parameters
| Parameter | Type | Description |
|---|
content | string | Feed XML content |
type | "rss" / "atom" / "raw" | Format type (optional) |
Feed Filtering (filter)
Filters Feed items by conditions.
Using Preset Options
import { filter } from "@/utils/feeds";
const filtered = filter(data, {
keywords: {
include: ["tech", "AI"],
exclude: ["ads"],
caseSensitive: false,
},
date: {
after: "2024-01-01",
before: "2024-12-31",
},
author: {
include: ["John"],
exclude: ["bot"],
},
categories: {
include: ["tech"],
exclude: ["entertainment"],
},
limit: {
count: 10,
fromStart: true,
},
});
Using Custom Function
const filtered = filter(data, (item, index) => {
return item.title?.includes("important") && index < 20;
});
Feed Sorting (sort)
Sorts Feed items.
Sort by Date
import { sort } from "@/utils/feeds";
const sorted = sort(data, "date", true);
const sortedAsc = sort(data, "date", false);
Custom Sorting
const sorted = sort(data, (a, b) => {
return a.title.localeCompare(b.title);
});
Feed Merging (union)
Merges multiple Feeds with automatic deduplication.
Usage
import { union } from "@/utils/feeds";
const merged = union(feed1, feed2);
const merged = union(
baseFeed,
[feed1, feed2, feed3],
{ title: "Combined Feed" },
{
hashFn: (item) => item.id || item.link,
}
);
Feed Intersection (intersection)
Gets common items from multiple Feeds.
Usage
import { intersection } from "@/utils/feeds";
const common = intersection(feed1, feed2);
const common = intersection(
baseFeed,
[feed1, feed2],
{ title: "Common Articles" }
);