MCPcopy Index your code
hub / github.com/simstudioai/sim / extractTextFromSource

Function extractTextFromSource

apps/sim/lib/webhooks/polling/imap.ts:367–418  ·  view source on GitHub ↗
(source: Buffer)

Source from the content-addressed store, hash-verified

365}
366
367function extractTextFromSource(source: Buffer): { text: string; html: string } {
368 const content = source.toString('utf-8')
369 let text = ''
370 let html = ''
371
372 const parts = content.split(/--[^\r\n]+/)
373
374 for (const part of parts) {
375 const lowerPart = part.toLowerCase()
376
377 if (lowerPart.includes('content-type: text/plain')) {
378 const match = part.match(/\r?\n\r?\n([\s\S]*?)(?=\r?\n--|\r?\n\.\r?\n|$)/i)
379 if (match) {
380 text = match[1].trim()
381 if (lowerPart.includes('quoted-printable')) {
382 text = text
383 .replace(/=\r?\n/g, '')
384 .replace(/=([0-9A-F]{2})/gi, (_, hex) => String.fromCharCode(Number.parseInt(hex, 16)))
385 }
386 if (lowerPart.includes('base64')) {
387 try {
388 text = Buffer.from(text.replace(/\s/g, ''), 'base64').toString('utf-8')
389 } catch {}
390 }
391 }
392 } else if (lowerPart.includes('content-type: text/html')) {
393 const match = part.match(/\r?\n\r?\n([\s\S]*?)(?=\r?\n--|\r?\n\.\r?\n|$)/i)
394 if (match) {
395 html = match[1].trim()
396 if (lowerPart.includes('quoted-printable')) {
397 html = html
398 .replace(/=\r?\n/g, '')
399 .replace(/=([0-9A-F]{2})/gi, (_, hex) => String.fromCharCode(Number.parseInt(hex, 16)))
400 }
401 if (lowerPart.includes('base64')) {
402 try {
403 html = Buffer.from(html.replace(/\s/g, ''), 'base64').toString('utf-8')
404 } catch {}
405 }
406 }
407 }
408 }
409
410 if (!text && !html) {
411 const bodyMatch = content.match(/\r?\n\r?\n([\s\S]+)$/)
412 if (bodyMatch) {
413 text = bodyMatch[1].trim()
414 }
415 }
416
417 return { text, html }
418}
419
420function extractAttachmentsFromSource(
421 source: Buffer,

Callers 1

processEmailsFunction · 0.85

Calls 2

replaceMethod · 0.65
toStringMethod · 0.45

Tested by

no test coverage detected