(html: string, characterLimit: number)
| 219 | } |
| 220 | |
| 221 | export function extractHeadline(html: string, characterLimit: number) { |
| 222 | let text = ""; |
| 223 | let start = false; |
| 224 | const parser = new Parser( |
| 225 | { |
| 226 | onopentag: (name) => { |
| 227 | if (name === "p") start = true; |
| 228 | }, |
| 229 | onclosetag: (name) => { |
| 230 | if (name === "p") { |
| 231 | start = false; |
| 232 | parser.pause(); |
| 233 | parser.end(); |
| 234 | } |
| 235 | }, |
| 236 | ontext: (data) => { |
| 237 | if (start) { |
| 238 | text += data; |
| 239 | if (text.length > characterLimit) { |
| 240 | text = text.slice(0, characterLimit); |
| 241 | parser.pause(); |
| 242 | parser.end(); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | }, |
| 247 | { |
| 248 | lowerCaseTags: false, |
| 249 | decodeEntities: true |
| 250 | } |
| 251 | ); |
| 252 | parser.end(html); |
| 253 | return text; |
| 254 | } |
| 255 | |
| 256 | const TITLE_SOURCE_TAGS = ["p", "h1", "h2", "h3", "h4", "h5", "h6"]; |
| 257 |
no test coverage detected