( req: NextApiRequest, res: NextApiResponse, )
| 11 | } |
| 12 | |
| 13 | export default async function handler( |
| 14 | req: NextApiRequest, |
| 15 | res: NextApiResponse, |
| 16 | ): Promise<void> { |
| 17 | if (req.method !== "POST") { |
| 18 | res.status(405).json({ |
| 19 | error: "Only POST requests allowed", |
| 20 | }); |
| 21 | return; |
| 22 | } |
| 23 | if ( |
| 24 | !req.headers.authorization || |
| 25 | !req.headers.authorization.includes( |
| 26 | process.env.SERVICE_LEVEL_KEY_SUPABASE ?? "", |
| 27 | ) |
| 28 | ) { |
| 29 | res.status(401).json({ error: "Unauthorized" }); |
| 30 | return; |
| 31 | } |
| 32 | if (!isValidBody<ParseHtmlType>(req.body, ParseHtmlZod)) { |
| 33 | res.status(400).send({ message: "Invalid request body" }); |
| 34 | return; |
| 35 | } |
| 36 | const html = req.body.html; |
| 37 | |
| 38 | let body = cheerio |
| 39 | .load(html, { |
| 40 | xml: { xmlMode: true, recognizeCDATA: true, recognizeSelfClosing: true }, |
| 41 | }) |
| 42 | .root() |
| 43 | .find("body"); |
| 44 | |
| 45 | removeHiddenElements(body); |
| 46 | |
| 47 | const text = body |
| 48 | .text() |
| 49 | .replaceAll(/\t/g, "") |
| 50 | .replaceAll(/\t\t\n/g, "") |
| 51 | .replaceAll(/\n{2,}/g, "\n\n") |
| 52 | .trim(); |
| 53 | |
| 54 | res.status(200).send(text); |
| 55 | } |
| 56 | |
| 57 | export function removeHiddenElements( |
| 58 | body: cheerio.Cheerio<cheerio.Element>, |
nothing calls this directly
no test coverage detected