@@ -43,16 +43,42 @@ revisionId: ${file.revisionId}
4343---
4444
4545` ;
46- ( ( file . body || { } ) . content || [ ] ) . forEach ( ( item ) => {
46+ file . body ?. content ?. forEach ( ( item ) => {
47+ /**
48+ * Tables
49+ */
50+ if ( item . table ?. tableRows ) {
51+ // Make a blank header
52+ text += "||\n|-\n" ;
53+ item . table . tableRows . forEach ( ( { tableCells } ) => {
54+ const textRows : any [ ] = [ ] ;
55+ tableCells ?. forEach ( ( { content } ) => {
56+ content ?. forEach ( ( { paragraph } ) => {
57+ const styleType =
58+ paragraph ?. paragraphStyle ?. namedStyleType || undefined ;
59+
60+ textRows . push (
61+ paragraph ?. elements ?. map ( ( element ) =>
62+ styleElement ( element , styleType ) ?. replace ( / \s + / g, "" ) . trim ( )
63+ )
64+ ) ;
65+ } ) ;
66+ } ) ;
67+ text += `| ${ textRows . join ( " | " ) } |\n` ;
68+ } ) ;
69+ }
70+
71+ /**
72+ * Paragraphs and lists
73+ */
4774 if ( item . paragraph && item . paragraph . elements ) {
48- const styleType = ( ( item . paragraph || { } ) . paragraphStyle || { } )
49- . namedStyleType ;
50- const bullet = ( item . paragraph || { } ) . bullet || { } ;
51- if ( bullet . listId ) {
52- const listDetails = ( file . lists || { } ) [ bullet . listId ] ;
75+ const styleType =
76+ item ?. paragraph ?. paragraphStyle ?. namedStyleType || undefined ;
77+ const bullet = item . paragraph ? .bullet ;
78+ if ( bullet ? .listId ) {
79+ const listDetails = file . lists ?. [ bullet . listId ] ;
5380 const glyphFormat =
54- ( ( ( listDetails . listProperties || { } ) . nestingLevels || [ ] ) [ 0 ] || { } )
55- . glyphFormat || "" ;
81+ listDetails ?. listProperties ?. nestingLevels ?. [ 0 ] . glyphFormat || "" ;
5682 const padding = " " . repeat ( bullet . nestingLevel || 0 ) ;
5783 if ( [ "[%0]" , "%0." ] . includes ( glyphFormat ) ) {
5884 text += `${ padding } 1. ` ;
@@ -62,43 +88,17 @@ revisionId: ${file.revisionId}
6288 }
6389 item . paragraph . elements . forEach ( ( element ) => {
6490 if ( element . textRun && content ( element ) && content ( element ) !== "\n" ) {
65- if ( styleType === "TITLE" ) {
66- text += `# ${ content ( element ) } ` ;
67- } else if ( styleType === "SUBTITLE" ) {
68- text += `_${ ( content ( element ) || "" ) . trim ( ) } _` ;
69- } else if ( styleType === "HEADING_1" ) {
70- text += `## ${ content ( element ) } ` ;
71- } else if ( styleType === "HEADING_2" ) {
72- text += `### ${ content ( element ) } ` ;
73- } else if ( styleType === "HEADING_3" ) {
74- text += `#### ${ content ( element ) } ` ;
75- } else if ( styleType === "HEADING_4" ) {
76- text += `##### ${ content ( element ) } ` ;
77- } else if ( styleType === "HEADING_5" ) {
78- text += `###### ${ content ( element ) } ` ;
79- } else if ( styleType === "HEADING_6" ) {
80- text += `####### ${ content ( element ) } ` ;
81- } else if (
82- ( element . textRun . textStyle || { } ) . bold &&
83- ( element . textRun . textStyle || { } ) . italic
84- ) {
85- text += `**_${ content ( element ) } _**` ;
86- } else if ( ( element . textRun . textStyle || { } ) . italic ) {
87- text += `_${ content ( element ) } _` ;
88- } else if ( ( element . textRun . textStyle || { } ) . bold ) {
89- text += `**${ content ( element ) } **` ;
90- } else {
91- text += content ( element ) ;
92- }
91+ text += styleElement ( element , styleType ) ;
9392 }
9493 } ) ;
95- text += bullet . listId
94+ text += bullet ? .listId
9695 ? ( text . split ( "\n" ) . pop ( ) || "" ) . trim ( ) . endsWith ( "\n" )
9796 ? ""
9897 : "\n"
9998 : "\n\n" ;
10099 }
101100 } ) ;
101+
102102 const lines = text . split ( "\n" ) ;
103103 const linesToDelete : number [ ] = [ ] ;
104104 lines . forEach ( ( line , index ) => {
@@ -120,10 +120,46 @@ revisionId: ${file.revisionId}
120120 return text . replace ( / \n \s * \n \s * \n / g, "\n\n" ) + "\n" ;
121121} ;
122122
123- const content = ( element : docs_v1 . Schema$ParagraphElement ) => {
124- const textRun = element . textRun || { } ;
125- const text = ( element . textRun || { } ) . content ;
126- if ( ( textRun . textStyle || { } ) . link )
127- return `[${ text } ](${ ( ( textRun . textStyle || { } ) . link || { } ) . url } )` ;
128- return text ;
123+ const styleElement = (
124+ element : docs_v1 . Schema$ParagraphElement ,
125+ styleType ?: string
126+ ) : string | undefined => {
127+ if ( styleType === "TITLE" ) {
128+ return `# ${ content ( element ) } ` ;
129+ } else if ( styleType === "SUBTITLE" ) {
130+ return `_${ ( content ( element ) || "" ) . trim ( ) } _` ;
131+ } else if ( styleType === "HEADING_1" ) {
132+ return `## ${ content ( element ) } ` ;
133+ } else if ( styleType === "HEADING_2" ) {
134+ return `### ${ content ( element ) } ` ;
135+ } else if ( styleType === "HEADING_3" ) {
136+ return `#### ${ content ( element ) } ` ;
137+ } else if ( styleType === "HEADING_4" ) {
138+ return `##### ${ content ( element ) } ` ;
139+ } else if ( styleType === "HEADING_5" ) {
140+ return `###### ${ content ( element ) } ` ;
141+ } else if ( styleType === "HEADING_6" ) {
142+ return `####### ${ content ( element ) } ` ;
143+ } else if (
144+ element . textRun ?. textStyle ?. bold &&
145+ element . textRun ?. textStyle ?. italic
146+ ) {
147+ return `**_${ content ( element ) } _**` ;
148+ } else if ( element . textRun ?. textStyle ?. italic ) {
149+ return `_${ content ( element ) } _` ;
150+ } else if ( element . textRun ?. textStyle ?. bold ) {
151+ return `**${ content ( element ) } **` ;
152+ }
153+
154+ return content ( element ) ;
155+ } ;
156+
157+ const content = (
158+ element : docs_v1 . Schema$ParagraphElement
159+ ) : string | undefined => {
160+ const textRun = element ?. textRun ;
161+ const text = textRun ?. content ;
162+ if ( textRun ?. textStyle ?. link ?. url )
163+ return `[${ text } ]${ textRun . textStyle . link . url } ` ;
164+ return text || undefined ;
129165} ;
0 commit comments