File size: 8,566 Bytes
7dc28be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// src/markdown-transformer/docsToMarkdown.ts

/**
 * Font families used by the markdown-to-docs direction for code styling.
 * When these are detected on a text run, we render backtick code in markdown.
 */
const CODE_FONT_FAMILIES = new Set(['Roboto Mono', 'Courier New', 'Consolas', 'monospace']);

// --- Main Conversion ---

/**
 * Converts Google Docs JSON structure to a markdown string.
 *
 * Accepts the raw response from `docs.documents.get()`, or a subset with
 * `{ body, lists }` (e.g. when extracting a specific tab).
 *
 * Handles headings, paragraphs, text formatting (bold, italic, strikethrough,
 * underline, links, code), ordered & unordered lists with nesting, tables,
 * and section breaks.
 */
export function docsJsonToMarkdown(docData: { body?: any; lists?: any }): string {
  const body = docData.body;
  if (!body?.content) {
    return '';
  }

  const lists: Record<string, any> = docData.lists ?? {};
  let markdown = '';

  for (const element of body.content as any[]) {
    if (element.paragraph) {
      markdown += convertParagraph(element.paragraph, lists);
    } else if (element.table) {
      markdown += convertTable(element.table);
    } else if (element.sectionBreak) {
      markdown += '\n---\n\n';
    }
  }

  return markdown.trim();
}

// --- Paragraph Conversion ---

function convertParagraph(paragraph: any, lists: Record<string, any>): string {
  // 1. Determine paragraph type
  const headingLevel = getHeadingLevel(paragraph);
  const listInfo = getListInfo(paragraph, lists);

  // 2. Extract text content with inline formatting
  const elements: any[] = paragraph.elements ?? [];
  const text = extractFormattedText(elements);

  // 3. Format based on type
  if (headingLevel && text.trim()) {
    const hashes = '#'.repeat(Math.min(headingLevel, 6));
    return `${hashes} ${text.trim()}\n\n`;
  }

  if (listInfo && text.trim()) {
    const indent = '  '.repeat(listInfo.nestingLevel);
    const marker = listInfo.ordered ? `1.` : `-`;
    return `${indent}${marker} ${text.trim()}\n`;
  }

  if (text.trim()) {
    return `${text.trim()}\n\n`;
  }

  return '\n';
}

// --- Heading Detection ---

function getHeadingLevel(paragraph: any): number | null {
  const styleType = paragraph.paragraphStyle?.namedStyleType;
  if (!styleType) return null;

  if (styleType === 'TITLE') return 1;
  if (styleType === 'SUBTITLE') return 2;

  const match = styleType.match(/^HEADING_(\d)$/);
  return match ? parseInt(match[1], 10) : null;
}

// --- List Detection ---

interface ListInfo {
  ordered: boolean;
  nestingLevel: number;
}

function getListInfo(paragraph: any, lists: Record<string, any>): ListInfo | null {
  if (!paragraph.bullet) return null;

  const nestingLevel: number = paragraph.bullet.nestingLevel ?? 0;
  const listId: string | undefined = paragraph.bullet.listId;
  let ordered = false;

  if (listId && lists[listId]?.listProperties?.nestingLevels) {
    const nestingLevels: any[] = lists[listId].listProperties.nestingLevels;
    const level = nestingLevels[nestingLevel];
    if (level) {
      // glyphType is set for ordered lists (e.g., DECIMAL, ALPHA, ROMAN)
      // glyphSymbol is set for unordered lists (e.g., bullet characters)
      // If glyphType is present and not empty, it's ordered
      if (level.glyphType && level.glyphType !== 'GLYPH_TYPE_UNSPECIFIED') {
        ordered = true;
      }
    }
  }

  return { ordered, nestingLevel };
}

// --- Text Run Conversion ---

function extractFormattedText(elements: any[]): string {
  let result = '';

  for (const element of elements) {
    if (element.textRun) {
      result += convertTextRun(element.textRun);
    }
  }

  return result;
}

function convertTextRun(textRun: any): string {
  let text: string = textRun.content ?? '';
  const style = textRun.textStyle;

  if (!style) return text;

  // Detect code-styled text (monospace font) -- wrap in backticks and skip
  // other formatting since markdown code spans don't support nested formatting.
  if (isCodeStyled(style)) {
    const trimmed = text.replace(/\n$/, '');
    if (trimmed) {
      return `\`${trimmed}\`${text.endsWith('\n') ? '\n' : ''}`;
    }
    return text;
  }

  // Strip trailing newline before applying formatting markers, then re-add.
  // This prevents markers from wrapping the newline (e.g., "**text\n**").
  const trailingNewline = text.endsWith('\n');
  const content = trailingNewline ? text.slice(0, -1) : text;

  if (!content) return text;

  let formatted = content;

  // Apply inline formatting (bold + italic combined, or individually)
  if (style.bold && style.italic) {
    formatted = `***${formatted}***`;
  } else if (style.bold) {
    formatted = `**${formatted}**`;
  } else if (style.italic) {
    formatted = `*${formatted}*`;
  }

  if (style.strikethrough) {
    formatted = `~~${formatted}~~`;
  }

  if (style.underline && !style.link) {
    formatted = `<u>${formatted}</u>`;
  }

  if (style.link?.url) {
    formatted = `[${formatted}](${style.link.url})`;
  }

  return formatted + (trailingNewline ? '\n' : '');
}

function isCodeStyled(style: any): boolean {
  const fontFamily = style.weightedFontFamily?.fontFamily;
  return typeof fontFamily === 'string' && CODE_FONT_FAMILIES.has(fontFamily);
}

// --- Table Conversion ---

function convertTable(table: any): string {
  if (!table.tableRows || table.tableRows.length === 0) {
    return '';
  }

  // Detect code block tables (1x1 table with monospace font or gray background)
  if (isCodeBlockTable(table)) {
    return convertCodeBlockTable(table);
  }

  let markdown = '\n';
  let isFirstRow = true;

  for (const row of table.tableRows) {
    if (!row.tableCells) continue;

    let rowText = '|';
    for (const cell of row.tableCells) {
      const cellText = extractCellText(cell);
      rowText += ` ${cellText} |`;
    }
    markdown += rowText + '\n';

    // Add header separator after the first row
    if (isFirstRow) {
      let separator = '|';
      for (let i = 0; i < row.tableCells.length; i++) {
        separator += ' --- |';
      }
      markdown += separator + '\n';
      isFirstRow = false;
    }
  }

  return markdown + '\n';
}

/**
 * Detects if a table is a code block (1x1 table with monospace font or gray background).
 * Google Docs "Code Block" building blocks are represented as styled 1x1 tables.
 */
function isCodeBlockTable(table: any): boolean {
  // Must be a 1x1 table
  if (!table.tableRows || table.tableRows.length !== 1) return false;
  const row = table.tableRows[0];
  if (!row.tableCells || row.tableCells.length !== 1) return false;

  const cell = row.tableCells[0];

  // Check for gray/colored background on the cell
  const cellStyle = cell.tableCellStyle;
  if (cellStyle?.backgroundColor?.color?.rgbColor) {
    const bg = cellStyle.backgroundColor.color.rgbColor;
    // Detect light gray backgrounds (typical of code blocks)
    // Allow a range of light grays
    const r = bg.red ?? 0;
    const g = bg.green ?? 0;
    const b = bg.blue ?? 0;
    if (r > 0.85 && g > 0.85 && b > 0.85 && r < 1 && g < 1 && b < 1) {
      return true;
    }
  }

  // Check for monospace font in cell content
  if (cell.content) {
    for (const element of cell.content) {
      if (element.paragraph?.elements) {
        for (const pe of element.paragraph.elements) {
          if (pe.textRun?.textStyle) {
            if (isCodeStyled(pe.textRun.textStyle)) {
              return true;
            }
          }
        }
      }
    }
  }

  return false;
}

/**
 * Converts a code block table (1x1 table) to a fenced markdown code block.
 */
function convertCodeBlockTable(table: any): string {
  const cell = table.tableRows[0].tableCells[0];
  let codeText = '';

  if (cell.content) {
    for (const element of cell.content) {
      if (element.paragraph?.elements) {
        for (const pe of element.paragraph.elements) {
          if (pe.textRun?.content) {
            codeText += pe.textRun.content;
          }
        }
      }
    }
  }

  // Remove trailing newline (cells always end with one)
  if (codeText.endsWith('\n')) {
    codeText = codeText.slice(0, -1);
  }

  return '\n```\n' + codeText + '\n```\n\n';
}

function extractCellText(cell: any): string {
  let text = '';
  if (!cell.content) return text;

  for (const element of cell.content) {
    if (element.paragraph?.elements) {
      for (const pe of element.paragraph.elements) {
        if (pe.textRun?.content) {
          text += pe.textRun.content.replace(/\n/g, ' ').trim();
        }
      }
    }
  }

  return text;
}