Optimizes web fetching by using Cloudflare's Markdown for Agents, reducing token consumption by ~80%
Cloudflare 推出 Markdown for Agents 功能:
在需要网页抓取时,使用优化后的 fetch 函数:
javascript
const { optimizedFetch } = require(./markdown-fetch);
const result = await optimizedFetch(https://example.com);
// result.markdown - Markdown 内容(如果有)
// result.html - HTML 内容(备用)
// result.tokensSaved - 节省的 tokens(如果有)
javascript
async function optimizedFetch(url, options = {}) {
const headers = {
Accept: text/markdown, text/html,
...options.headers
};
const response = await fetch(url, { ...options, headers });
const contentType = response.headers.get(content-type);
const xMarkdownTokens = response.headers.get(x-markdown-tokens);
let result = {
url,
contentType,
tokensSaved: xMarkdownTokens ? parseInt(xMarkdownTokens) : null
};
if (contentType.includes(text/markdown)) {
result.markdown = await response.text();
result.format = markdown;
} else {
result.html = await response.text();
result.format = html;
}
return result;
}
| Content-Type | 处理方式 |
|---|---|
| text/markdown | 直接使用,跳过 HTML 解析 |
| text/html |
如果响应中有 x-markdown-tokens header,记录到日志:
javascript
if (result.tokensSaved) {
console.log([Markdown Fetch] Token 节省: ${result.tokensSaved});
}
找一个 Cloudflare 托管的网站测试:
bash
curl -H Accept: text/markdown, text/html https://cloudflare-example.com
确认收到 content-type: text/markdown 响应。
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 markdown-fetch-1776420072 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 markdown-fetch-1776420072 技能
skillhub install markdown-fetch-1776420072
文件大小: 3.63 KB | 发布时间: 2026-4-17 18:59