@@ -12,20 +12,76 @@ import {
1212 UI ,
1313} from "@commontools/builder" ;
1414import { Cell } from "@commontools/runner" ;
15+ import TurndownService from "turndown" ;
16+
17+ // Initialize turndown service
18+ const turndown = new TurndownService ( {
19+ headingStyle : "atx" ,
20+ codeBlockStyle : "fenced" ,
21+ emDelimiter : "*" ,
22+ } ) ;
23+
24+ turndown . addRule ( "removeStyleTags" , {
25+ filter : [ "style" ] ,
26+ replacement : "" ,
27+ } ) ;
1528
1629const sleep = ( ms : number ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
1730
1831const EmailProperties = {
19- id : { type : "string" } ,
20- threadId : { type : "string" } ,
21- labelIds : { type : "array" , items : { type : "string" } } ,
22- snippet : { type : "string" } ,
23- subject : { type : "string" } ,
24- from : { type : "string" } ,
25- date : { type : "string" } ,
26- to : { type : "string" } ,
27- plainText : { type : "string" } ,
28- htmlContent : { type : "string" } ,
32+ id : {
33+ type : "string" ,
34+ title : "Email ID" ,
35+ description : "Unique identifier for the email" ,
36+ } ,
37+ threadId : {
38+ type : "string" ,
39+ title : "Thread ID" ,
40+ description : "Identifier for the email thread" ,
41+ } ,
42+ labelIds : {
43+ type : "array" ,
44+ items : { type : "string" } ,
45+ title : "Labels" ,
46+ description : "Gmail labels assigned to the email" ,
47+ } ,
48+ snippet : {
49+ type : "string" ,
50+ title : "Snippet" ,
51+ description : "Brief preview of the email content" ,
52+ } ,
53+ subject : {
54+ type : "string" ,
55+ title : "Subject" ,
56+ description : "Email subject line" ,
57+ } ,
58+ from : {
59+ type : "string" ,
60+ title : "From" ,
61+ description : "Sender's email address" ,
62+ } ,
63+ date : {
64+ type : "string" ,
65+ title : "Date" ,
66+ description : "Date and time when the email was sent" ,
67+ } ,
68+ to : { type : "string" , title : "To" , description : "Recipient's email address" } ,
69+ plainText : {
70+ type : "string" ,
71+ title : "Plain Text Content" ,
72+ description : "Email content in plain text format (often empty)" ,
73+ } ,
74+ htmlContent : {
75+ type : "string" ,
76+ title : "HTML Content" ,
77+ description : "Email content in HTML format" ,
78+ } ,
79+ markdownContent : {
80+ type : "string" ,
81+ title : "Markdown Content" ,
82+ description :
83+ "Email content converted to Markdown format. Often best for processing email contents." ,
84+ } ,
2985} as const ;
3086
3187const EmailSchema = {
@@ -298,6 +354,22 @@ Accept: application/json
298354 }
299355 }
300356
357+ // Generate markdown content from HTML or plainText
358+ let markdownContent = "" ;
359+ if ( htmlContent ) {
360+ try {
361+ // Convert HTML to markdown using our custom converter
362+ markdownContent = turndown . turndown ( htmlContent ) ;
363+ } catch ( error ) {
364+ console . error ( "Error converting HTML to markdown:" , error ) ;
365+ // Fallback to plainText if HTML conversion fails
366+ markdownContent = plainText ;
367+ }
368+ } else {
369+ // Use plainText as fallback if no HTML content
370+ markdownContent = plainText ;
371+ }
372+
301373 return {
302374 id : messageData . id ,
303375 threadId : messageData . threadId ,
@@ -309,6 +381,7 @@ Accept: application/json
309381 to : extractEmailAddress ( to ) ,
310382 plainText,
311383 htmlContent,
384+ markdownContent,
312385 } ;
313386 } catch ( error ) {
314387 console . error ( "Error processing message part:" , error ) ;
@@ -496,6 +569,7 @@ export default recipe(
496569 < th style = "padding: 10px;" > DATE</ th >
497570 < th style = "padding: 10px;" > SUBJECT</ th >
498571 < th style = "padding: 10px;" > LABEL</ th >
572+ < th style = "padding: 10px;" > CONTENT</ th >
499573 </ tr >
500574 </ thead >
501575 < tbody >
@@ -513,6 +587,14 @@ export default recipe(
513587 ( email ) => email . labelIds . join ( ", " ) ,
514588 ) }
515589 </ td >
590+ < td style = "border: 1px solid black; padding: 10px;" >
591+ < details >
592+ < summary > Show Markdown</ summary >
593+ < pre style = "white-space: pre-wrap; max-height: 300px; overflow-y: auto;" >
594+ { email . markdownContent }
595+ </ pre >
596+ </ details >
597+ </ td >
516598 </ tr >
517599 ) ) }
518600 </ tbody >
0 commit comments