import { fetch } from "https://esm.town/v/std/fetch"; import { fold } from "https://esm.town/v/jordan/fold"; export const wikitxt = async (req: Request) => { const url = new URL(req.url), article = url.pathname.replace("/", ""), width = parseInt(url.searchParams.get("width")); const wiki = await fetch(`https://en.wikipedia.org/w/api.php?action=query&format=json&titles=${article}&prop=extracts&explaintext`).then(res => res.json()); if (!wiki.query) return new Response("Please input a correct title") const pages = wiki.query.pages; const txt = pages[Object.keys(pages)[0]].extract; const res = width ? fold(txt, width) : fold(txt); return new Response(res); }