Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

extract-html-scripts.ts

19 lines435 Bytes

1import * as cheerio from 'cheerio'
2​
3export function extractHtmlScripts(
4 html: string,
5): Array<{ content?: string; src?: string }> {
6 const $ = cheerio.load(html)
7 const scripts: Array<{ content?: string; src?: string }> = []
8​
9 $('script').each((_, element) => {
10 const src = $(element).attr('src')
11 const content = $(element).html() ?? undefined
12 scripts.push({
13 src,
14 content,
15 })
16 })
17​
18 return scripts