mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-29 15:52:36 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The Google Workspace connector gives agents Google tools through the tool gateway > - Google's hosted Workspace MCP servers are a Developer Preview, so Paperclip launches on its own tools over Google's REST APIs, in `server/src/google-workspace-rest-tools/` > - Gmail and Calendar are done there; Docs and Slides have no tools yet > - This pull request adds `read_doc`, `update_doc`, `read_presentation` and `update_presentation` with the same names and inputs as Google's MCP tools > - The benefit is that the Docs and Slides services can switch to `paperclip_rest` once their API is enabled, and reviewers see a full document in Paperclip ## Linked Issues or Issue Description **Subsystem affected** Google Workspace REST tool set (`server/src/google-workspace-rest-tools/`), Docs and Slides services. **Problem or motivation** The REST tool set has no Docs or Slides tools. The allowlist (`toolScopes.docs`, `toolScopes.slides`) names two tools for each service. A service cannot switch to `paperclip_rest` until all of its allowlisted tools exist. **Proposed solution** Add the four tools in new `docs/` and `slides/` folders. Each tool makes exactly one REST call. The read tools return the whole file as Markdown-style text plus an index structure for edits. The update tools post `batchUpdate` requests in order and require the revision ID that the caller read. **Alternatives considered** Return the raw `documents.get` JSON. It is 10 to 20 times larger than the text, so ordinary documents go over the result size limit, and it is hard to compare with Google Docs. Reorder edit requests by index. That changes what dependent requests do, and Google's tool does not do it. Refs #22000, #22014, #22017 ## What Changed - `docs/`: `read_doc` (`documents.get` with `includeTabsContent=true`) and `update_doc` (`documents.batchUpdate`), a renderer for every tab, child tab, heading, list (real numbering), table, link, header, footer, footnote, chip, suggestion and comment, and an index structure where each paragraph's `text.length` equals `endIndex - startIndex`. - `slides/`: `read_presentation` (`presentations.get`; with `fields` it returns Google's resource as is) and `update_presentation` (`presentations.batchUpdate`), a renderer for every slide's text, tables, groups, media and speaker notes. - `documents-shared.ts`: the 250,000-byte limit on the final serialized result (an error, never a cut-short result), the revision-conflict error, comment rendering, and argument checks. - Update tools require `writeControl.requiredRevisionId` and refuse `targetRevisionId`. A revision conflict returns "nothing was changed; read it again". Writes are never retried. - `google-http.ts`: allows `docs.googleapis.com` only under `/v1/documents/` and `slides.googleapis.com` only under `/v1/presentations/`. - `tools.ts`, `probe.ts`: register the services. Docs and Slides have no network probe, because every read needs a file ID. - Update tools accept only a closed list of batch request kinds (`docs/request-kinds.ts`, `slides/request-kinds.ts`). Kinds and forms that read another file are refused by name: `insertRichLink`, the Sheets chart kinds, `importSlides`, `createAudio`, Drive videos, and images from Drive or Docs links. Unknown kinds are refused too. - `read_doc` always requests `SUGGESTIONS_INLINE` and falls back to an explicit `PREVIEW_WITHOUT_SUGGESTIONS` read for view-only users; the result says which view it used. - Tables with merged cells are placed on the full grid; covered cells read `(merged left)` or `(merged up)`. - Revision conflicts are recognized from Google's `error.status` and `error.details` first, with the message as a fallback. - Annotations: `update_doc` and `update_presentation` are destructive and not idempotent. **Calendar `update_event` is now also marked destructive** (it overwrites fields, removes attendees and replaces reminders). Google's references mark both non-destructive; the README records this. - `README.md`: endpoint and scope table with Google's documentation links, the result format, what cannot be shown, update order and revision rules, and every difference from Google's tools. ## Verification - `cd server && npx vitest run src/google-workspace-rest-tools`: 33 files, 573 tests pass (169 new in `docs/` and `slides/`, plus host, probe and allowlist tests). - `cd server && pnpm typecheck`: passes. - `pnpm verify`: exit 2 (README is a broad input), so required CI supplies broad coverage. - `src/services/google-workspace-rest-dispatch.test.ts` fails the same way on `master` (it still expects Gmail to be incomplete after #22014). This PR does not touch it. ## Risks - Low risk. Nothing calls these tools until the Docs or Slides transport is set to `paperclip_rest`; both stay `google_mcp`. - The inner format of `content` differs from Google's tool, which Google does not publish. The README lists this. - Revision-conflict detection matches Google's 400 message on "revision". Any 400 rejects the whole batch, so nothing changes in either case. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, in Claude Code with tool use and code execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
493 lines
20 KiB
TypeScript
493 lines
20 KiB
TypeScript
import { markdownTable, OBJECT_PLACEHOLDER, plural, renderComments, tableGrid, type TableCellText } from "../documents-shared.js";
|
|
import type {
|
|
RestDocsLink,
|
|
RestDocument,
|
|
RestDocumentTab,
|
|
RestEmbeddedObject,
|
|
RestNestingLevel,
|
|
RestParagraph,
|
|
RestParagraphElement,
|
|
RestSegment,
|
|
RestStructuralElement,
|
|
RestSuggestible,
|
|
RestTab,
|
|
} from "./document.js";
|
|
|
|
export interface DocsParagraphView {
|
|
type: "paragraph";
|
|
startIndex: number;
|
|
endIndex: number;
|
|
text: string;
|
|
style?: string;
|
|
list?: { listId: string; nestingLevel: number };
|
|
}
|
|
|
|
export interface DocsTableCellView {
|
|
startIndex: number;
|
|
endIndex: number;
|
|
rowSpan?: number;
|
|
columnSpan?: number;
|
|
content: DocsElementView[];
|
|
}
|
|
|
|
export interface DocsTableView {
|
|
type: "table";
|
|
startIndex: number;
|
|
endIndex: number;
|
|
rows: number;
|
|
columns: number;
|
|
cells: DocsTableCellView[][];
|
|
}
|
|
|
|
export interface DocsTableOfContentsView {
|
|
type: "tableOfContents";
|
|
startIndex: number;
|
|
endIndex: number;
|
|
content: DocsElementView[];
|
|
}
|
|
|
|
export interface DocsSectionBreakView {
|
|
type: "sectionBreak";
|
|
startIndex: number;
|
|
endIndex: number;
|
|
}
|
|
|
|
export type DocsElementView = DocsParagraphView | DocsTableView | DocsTableOfContentsView | DocsSectionBreakView;
|
|
|
|
export interface DocsSegmentView {
|
|
segmentId: string;
|
|
kind?: string;
|
|
footnoteNumber?: string;
|
|
content: DocsElementView[];
|
|
}
|
|
|
|
export interface DocsTabView {
|
|
tabId: string;
|
|
title: string;
|
|
parentTabId?: string;
|
|
nestingLevel?: number;
|
|
body: DocsElementView[];
|
|
headers: DocsSegmentView[];
|
|
footers: DocsSegmentView[];
|
|
footnotes: DocsSegmentView[];
|
|
}
|
|
|
|
export interface DocsContent {
|
|
documentId: string;
|
|
title: string;
|
|
revisionId?: string;
|
|
suggestionsViewMode: SuggestionsView;
|
|
text: string;
|
|
tabs: DocsTabView[];
|
|
notes: string[];
|
|
[key: string]: unknown;
|
|
}
|
|
|
|
interface Tally {
|
|
images: number;
|
|
drawings: number;
|
|
charts: number;
|
|
equations: number;
|
|
dropdowns: number;
|
|
}
|
|
|
|
interface TabContext {
|
|
tab: RestDocumentTab;
|
|
tally: Tally;
|
|
counters: Map<string, number[]>;
|
|
footnoteNumbers: Map<string, string>;
|
|
}
|
|
|
|
const ORDERED_GLYPHS = new Set(["DECIMAL", "ZERO_DECIMAL", "ALPHA", "UPPER_ALPHA", "ROMAN", "UPPER_ROMAN"]);
|
|
const HEADING_PREFIX: Record<string, string> = {
|
|
TITLE: "# ",
|
|
SUBTITLE: "## ",
|
|
HEADING_1: "# ",
|
|
HEADING_2: "## ",
|
|
HEADING_3: "### ",
|
|
HEADING_4: "#### ",
|
|
HEADING_5: "##### ",
|
|
HEADING_6: "###### ",
|
|
};
|
|
|
|
const indexOf = (value: number | undefined) => value ?? 0;
|
|
|
|
function letters(value: number): string {
|
|
let result = "";
|
|
for (let n = value; n > 0; n = Math.floor((n - 1) / 26)) result = String.fromCharCode(97 + ((n - 1) % 26)) + result;
|
|
return result || "0";
|
|
}
|
|
|
|
function roman(value: number): string {
|
|
if (value <= 0 || value >= 4000) return String(value);
|
|
const table: Array<[number, string]> = [[1000, "m"], [900, "cm"], [500, "d"], [400, "cd"], [100, "c"], [90, "xc"], [50, "l"], [40, "xl"], [10, "x"], [9, "ix"], [5, "v"], [4, "iv"], [1, "i"]];
|
|
let rest = value;
|
|
let result = "";
|
|
for (const [amount, symbol] of table) {
|
|
while (rest >= amount) {
|
|
result += symbol;
|
|
rest -= amount;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export function formatListNumber(value: number, glyphType: string | undefined): string {
|
|
switch (glyphType) {
|
|
case "ZERO_DECIMAL": return value < 10 ? `0${value}` : String(value);
|
|
case "ALPHA": return letters(value);
|
|
case "UPPER_ALPHA": return letters(value).toUpperCase();
|
|
case "ROMAN": return roman(value);
|
|
case "UPPER_ROMAN": return roman(value).toUpperCase();
|
|
default: return String(value);
|
|
}
|
|
}
|
|
|
|
export function listMarker(levels: readonly RestNestingLevel[], counters: number[], level: number): string {
|
|
const spec = levels[level] ?? {};
|
|
const previous = counters[level];
|
|
counters.length = level + 1;
|
|
counters[level] = previous === undefined ? (spec.startNumber ?? 1) : previous + 1;
|
|
if (!spec.glyphType || !ORDERED_GLYPHS.has(spec.glyphType)) return "-";
|
|
const format = spec.glyphFormat || `%${level}.`;
|
|
return format.replace(/%(\d)/g, (_match, digit: string) => {
|
|
const at = Number(digit);
|
|
return formatListNumber(counters[at] ?? levels[at]?.startNumber ?? 1, levels[at]?.glyphType);
|
|
});
|
|
}
|
|
|
|
export function linkTarget(link: RestDocsLink | undefined): string | undefined {
|
|
if (!link) return undefined;
|
|
if (link.url) return link.url;
|
|
const heading = link.heading?.id ?? link.headingId;
|
|
if (heading) return `#heading=${heading}`;
|
|
const bookmark = link.bookmark?.id ?? link.bookmarkId;
|
|
if (bookmark) return `#bookmark=${bookmark}`;
|
|
if (link.tabId) return `#tab=${link.tabId}`;
|
|
return undefined;
|
|
}
|
|
|
|
function embeddedLabel(object: RestEmbeddedObject | undefined, tally: Tally): string {
|
|
const label = (object?.title || object?.description || "").replace(/\s+/g, " ").trim();
|
|
const suffix = label ? `: ${label}` : "";
|
|
if (object?.linkedContentReference?.sheetsChartReference) {
|
|
tally.charts += 1;
|
|
return `[chart${suffix}]`;
|
|
}
|
|
if (object?.embeddedDrawingProperties) {
|
|
tally.drawings += 1;
|
|
return `[drawing${suffix}]`;
|
|
}
|
|
tally.images += 1;
|
|
return `[image${suffix}]`;
|
|
}
|
|
|
|
function suggestible(element: RestParagraphElement): RestSuggestible | undefined {
|
|
return element.textRun ?? element.autoText ?? element.pageBreak ?? element.columnBreak ?? element.footnoteReference
|
|
?? element.horizontalRule ?? element.equation ?? element.inlineObjectElement ?? element.person ?? element.richLink
|
|
?? element.dateElement ?? element.dropdown;
|
|
}
|
|
|
|
function inlineText(element: RestParagraphElement, context: TabContext): string {
|
|
if (element.textRun) return element.textRun.content ?? "";
|
|
if (element.autoText) {
|
|
if (element.autoText.type === "PAGE_NUMBER") return "[page number]";
|
|
if (element.autoText.type === "PAGE_COUNT") return "[page count]";
|
|
return "[auto text]";
|
|
}
|
|
if (element.pageBreak) return "[page break]";
|
|
if (element.columnBreak) return "[column break]";
|
|
if (element.footnoteReference) {
|
|
const number = element.footnoteReference.footnoteNumber ?? "?";
|
|
if (element.footnoteReference.footnoteId) context.footnoteNumbers.set(element.footnoteReference.footnoteId, number);
|
|
return `[^${number}]`;
|
|
}
|
|
if (element.horizontalRule) return "---";
|
|
if (element.equation) {
|
|
context.tally.equations += 1;
|
|
return "[equation]";
|
|
}
|
|
if (element.inlineObjectElement) {
|
|
const id = element.inlineObjectElement.inlineObjectId ?? "";
|
|
return embeddedLabel(context.tab.inlineObjects?.[id]?.inlineObjectProperties?.embeddedObject, context.tally);
|
|
}
|
|
if (element.person) {
|
|
const { name, email } = element.person.personProperties ?? {};
|
|
if (name && email) return `${name} <${email}>`;
|
|
return name || email || "[person]";
|
|
}
|
|
if (element.richLink) {
|
|
const { title, uri } = element.richLink.richLinkProperties ?? {};
|
|
return uri ? `[${title || uri}](${uri})` : title || "[link]";
|
|
}
|
|
if (element.dateElement) return element.dateElement.dateElementProperties?.displayText || "[date]";
|
|
if (element.dropdown) {
|
|
context.tally.dropdowns += 1;
|
|
return "[dropdown]";
|
|
}
|
|
return "";
|
|
}
|
|
|
|
function renderInline(element: RestParagraphElement, context: TabContext): string {
|
|
const raw = inlineText(element, context);
|
|
const trailing = raw.match(/\n*$/)![0];
|
|
let body = raw.slice(0, raw.length - trailing.length);
|
|
if (!body) return raw;
|
|
const target = linkTarget(element.textRun?.textStyle?.link);
|
|
if (target) body = `[${body}](${target})`;
|
|
const marks = suggestible(element);
|
|
if (marks?.suggestedInsertionIds?.length) body = `{+${body}+}`;
|
|
else if (marks?.suggestedDeletionIds?.length) body = `{-${body}-}`;
|
|
return body + trailing;
|
|
}
|
|
|
|
export function paragraphRawText(paragraph: RestParagraph): string {
|
|
return (paragraph.elements ?? []).map((element) => {
|
|
if (element.textRun) return element.textRun.content ?? "";
|
|
const length = Math.max(1, indexOf(element.endIndex) - indexOf(element.startIndex));
|
|
return OBJECT_PLACEHOLDER.repeat(length);
|
|
}).join("");
|
|
}
|
|
|
|
function paragraphLine(paragraph: RestParagraph, context: TabContext): string {
|
|
let text = (paragraph.elements ?? []).map((element) => renderInline(element, context)).join("");
|
|
text = text.replace(/\n$/, "").replace(/\u000b/g, "\n");
|
|
for (const id of paragraph.positionedObjectIds ?? []) {
|
|
const label = embeddedLabel(context.tab.positionedObjects?.[id]?.positionedObjectProperties?.embeddedObject, context.tally);
|
|
text = text ? `${text} ${label}` : label;
|
|
}
|
|
if (paragraph.bullet) {
|
|
const listId = paragraph.bullet.listId ?? "";
|
|
const level = paragraph.bullet.nestingLevel ?? 0;
|
|
const counters = context.counters.get(listId) ?? [];
|
|
context.counters.set(listId, counters);
|
|
const levels = context.tab.lists?.[listId]?.listProperties?.nestingLevels ?? [];
|
|
return `${" ".repeat(level)}${listMarker(levels, counters, level)} ${text}`;
|
|
}
|
|
const prefix = HEADING_PREFIX[paragraph.paragraphStyle?.namedStyleType ?? ""];
|
|
return prefix && text ? `${prefix}${text}` : text;
|
|
}
|
|
|
|
function cellText(content: readonly RestStructuralElement[], context: TabContext): string {
|
|
const parts: string[] = [];
|
|
for (const element of content) {
|
|
if (element.paragraph) parts.push(paragraphLine(element.paragraph, context));
|
|
else if (element.table) {
|
|
const rows = tableGrid(tableCells(element, context), element.table.columns ?? 0).map((cells) => cells.join(" / "));
|
|
parts.push(`[table: ${rows.join("; ")}]`);
|
|
} else if (element.tableOfContents) parts.push(cellText(element.tableOfContents.content ?? [], context));
|
|
}
|
|
while (parts.length > 1 && parts[parts.length - 1] === "") parts.pop();
|
|
return parts.join("\n");
|
|
}
|
|
|
|
function tableCells(element: RestStructuralElement, context: TabContext): TableCellText[][] {
|
|
return (element.table?.tableRows ?? []).map((row) => (row.tableCells ?? []).map((cell) => ({
|
|
text: cellText(cell.content ?? [], context),
|
|
rowSpan: cell.tableCellStyle?.rowSpan,
|
|
columnSpan: cell.tableCellStyle?.columnSpan,
|
|
})));
|
|
}
|
|
|
|
function tableLines(element: RestStructuralElement, context: TabContext): string[] {
|
|
return markdownTable(tableCells(element, context), element.table?.columns ?? 0);
|
|
}
|
|
|
|
export function renderContent(content: readonly RestStructuralElement[], context: TabContext): string[] {
|
|
const lines: string[] = [];
|
|
let afterTable = false;
|
|
for (const element of content) {
|
|
if (element.paragraph) {
|
|
const line = paragraphLine(element.paragraph, context);
|
|
if (afterTable && line !== "") lines.push("");
|
|
lines.push(line);
|
|
afterTable = false;
|
|
} else if (element.table) {
|
|
if (lines.length > 0 && lines[lines.length - 1] !== "") lines.push("");
|
|
lines.push(...tableLines(element, context));
|
|
afterTable = true;
|
|
} else if (element.tableOfContents) {
|
|
lines.push("[Table of contents]", ...renderContent(element.tableOfContents.content ?? [], context));
|
|
afterTable = false;
|
|
} else if (element.sectionBreak && indexOf(element.startIndex) > 0) {
|
|
lines.push("[section break]");
|
|
afterTable = false;
|
|
}
|
|
}
|
|
return lines;
|
|
}
|
|
|
|
function structure(content: readonly RestStructuralElement[]): DocsElementView[] {
|
|
const views: DocsElementView[] = [];
|
|
for (const element of content) {
|
|
const startIndex = indexOf(element.startIndex);
|
|
const endIndex = indexOf(element.endIndex);
|
|
if (element.paragraph) {
|
|
const style = element.paragraph.paragraphStyle?.namedStyleType;
|
|
const bullet = element.paragraph.bullet;
|
|
views.push({
|
|
type: "paragraph",
|
|
startIndex,
|
|
endIndex,
|
|
text: paragraphRawText(element.paragraph),
|
|
...(style && style !== "NORMAL_TEXT" ? { style } : {}),
|
|
...(bullet ? { list: { listId: bullet.listId ?? "", nestingLevel: bullet.nestingLevel ?? 0 } } : {}),
|
|
});
|
|
} else if (element.table) {
|
|
views.push({
|
|
type: "table",
|
|
startIndex,
|
|
endIndex,
|
|
rows: element.table.rows ?? element.table.tableRows?.length ?? 0,
|
|
columns: element.table.columns ?? element.table.tableRows?.[0]?.tableCells?.length ?? 0,
|
|
cells: (element.table.tableRows ?? []).map((row) => (row.tableCells ?? []).map((cell) => {
|
|
const rowSpan = cell.tableCellStyle?.rowSpan;
|
|
const columnSpan = cell.tableCellStyle?.columnSpan;
|
|
return {
|
|
startIndex: indexOf(cell.startIndex),
|
|
endIndex: indexOf(cell.endIndex),
|
|
...(rowSpan !== undefined && rowSpan !== 1 ? { rowSpan } : {}),
|
|
...(columnSpan !== undefined && columnSpan !== 1 ? { columnSpan } : {}),
|
|
content: structure(cell.content ?? []),
|
|
};
|
|
})),
|
|
});
|
|
} else if (element.tableOfContents) {
|
|
views.push({ type: "tableOfContents", startIndex, endIndex, content: structure(element.tableOfContents.content ?? []) });
|
|
} else if (element.sectionBreak) {
|
|
views.push({ type: "sectionBreak", startIndex, endIndex });
|
|
}
|
|
}
|
|
return views;
|
|
}
|
|
|
|
const HEADER_KINDS: Array<[keyof NonNullable<RestDocumentTab["documentStyle"]>, string]> = [
|
|
["defaultHeaderId", "default"], ["firstPageHeaderId", "first page"], ["evenPageHeaderId", "even pages"],
|
|
["defaultFooterId", "default"], ["firstPageFooterId", "first page"], ["evenPageFooterId", "even pages"],
|
|
];
|
|
|
|
function segmentKind(tab: RestDocumentTab, id: string): string | undefined {
|
|
return HEADER_KINDS.find(([field]) => tab.documentStyle?.[field] === id)?.[1];
|
|
}
|
|
|
|
function flattenTabs(tabs: readonly RestTab[], path: string[] = []): Array<{ tab: RestTab; path: string[] }> {
|
|
return tabs.flatMap((tab) => {
|
|
const here = [...path, tab.tabProperties?.title || "Untitled tab"];
|
|
return [{ tab, path: here }, ...flattenTabs(tab.childTabs ?? [], here)];
|
|
});
|
|
}
|
|
|
|
function numericOrder(value: string | undefined): number {
|
|
const parsed = Number(value);
|
|
return Number.isFinite(parsed) ? parsed : Number.MAX_SAFE_INTEGER;
|
|
}
|
|
|
|
interface RenderedTab {
|
|
view: DocsTabView;
|
|
lines: string[];
|
|
}
|
|
|
|
function renderTab(tab: RestTab, tally: Tally): RenderedTab {
|
|
const documentTab = tab.documentTab ?? {};
|
|
const context: TabContext = { tab: documentTab, tally, counters: new Map(), footnoteNumbers: new Map() };
|
|
const lines = renderContent(documentTab.body?.content ?? [], context);
|
|
const segments = (kind: "Header" | "Footer", record: Record<string, RestSegment> | undefined): DocsSegmentView[] =>
|
|
Object.entries(record ?? {}).map(([id, segment]) => {
|
|
const label = segmentKind(documentTab, id);
|
|
const segmentLines = renderContent(segment.content ?? [], { ...context, counters: new Map() });
|
|
while (segmentLines.length && segmentLines[segmentLines.length - 1] === "") segmentLines.pop();
|
|
if (lines.length && lines[lines.length - 1] !== "") lines.push("");
|
|
lines.push(`[${kind}${label ? ` (${label})` : ""}]`, ...segmentLines);
|
|
return { segmentId: id, ...(label ? { kind: label } : {}), content: structure(segment.content ?? []) };
|
|
});
|
|
const headers = segments("Header", documentTab.headers);
|
|
const footers = segments("Footer", documentTab.footers);
|
|
const footnoteEntries = Object.entries(documentTab.footnotes ?? {})
|
|
.map(([id, segment]) => ({ id, segment, number: context.footnoteNumbers.get(id) }))
|
|
.sort((a, b) => numericOrder(a.number) - numericOrder(b.number));
|
|
const footnotes = footnoteEntries.map(({ id, segment, number }) => {
|
|
const footnoteLines = renderContent(segment.content ?? [], { ...context, counters: new Map() })
|
|
.map((line) => line.trim()).filter((line) => line !== "");
|
|
if (lines.length && lines[lines.length - 1] !== "" && !lines[lines.length - 1]!.startsWith("[^")) lines.push("");
|
|
lines.push(`[^${number ?? "?"}]: ${footnoteLines.join(" ")}`);
|
|
return { segmentId: id, ...(number ? { footnoteNumber: number } : {}), content: structure(segment.content ?? []) };
|
|
});
|
|
const properties = tab.tabProperties ?? {};
|
|
return {
|
|
lines,
|
|
view: {
|
|
tabId: properties.tabId ?? "",
|
|
title: properties.title ?? "",
|
|
...(properties.parentTabId ? { parentTabId: properties.parentTabId } : {}),
|
|
...(properties.nestingLevel ? { nestingLevel: properties.nestingLevel } : {}),
|
|
body: structure(documentTab.body?.content ?? []),
|
|
headers,
|
|
footers,
|
|
footnotes,
|
|
},
|
|
};
|
|
}
|
|
|
|
function trimBlankEdges(lines: string[]): string[] {
|
|
let start = 0;
|
|
let end = lines.length;
|
|
while (start < end && lines[start] === "") start += 1;
|
|
while (end > start && lines[end - 1] === "") end -= 1;
|
|
return lines.slice(start, end);
|
|
}
|
|
|
|
export function tallyNotes(tally: Tally): string[] {
|
|
const notes: string[] = [];
|
|
if (tally.images) notes.push(`${plural(tally.images, "image is", "images are")} shown as [image] with its title or alt text; the picture itself is not included.`);
|
|
if (tally.drawings) notes.push(`${plural(tally.drawings, "drawing is", "drawings are")} shown as [drawing]; the Docs API does not return drawing content.`);
|
|
if (tally.charts) notes.push(`${plural(tally.charts, "chart is", "charts are")} shown as [chart]; chart data is not included.`);
|
|
if (tally.equations) notes.push(`${plural(tally.equations, "equation is", "equations are")} shown as [equation]; the Docs API does not return equation content.`);
|
|
if (tally.dropdowns) notes.push(`${plural(tally.dropdowns, "dropdown is", "dropdowns are")} shown as [dropdown]; the Docs API does not return the selected value.`);
|
|
return notes;
|
|
}
|
|
|
|
export type SuggestionsView = "SUGGESTIONS_INLINE" | "PREVIEW_WITHOUT_SUGGESTIONS";
|
|
|
|
export interface RenderDocumentOptions {
|
|
commentsRequested?: boolean;
|
|
suggestionsView?: SuggestionsView;
|
|
}
|
|
|
|
export const PREVIEW_NOTE = "Pending suggested edits are not shown: your access to this document does not include suggestions, so it was read in Google's PREVIEW_WITHOUT_SUGGESTIONS view. Google requires edits to be based on the SUGGESTIONS_INLINE view, so these indices may not be valid for update_doc.";
|
|
export const INLINE_NOTE = "Read in Google's SUGGESTIONS_INLINE view: pending suggested edits are included, marked {+inserted+} and {-deleted-}, and the indices are valid for update_doc.";
|
|
export const COMMENTS_UNAVAILABLE_NOTE = "Comments were asked for but are not available with your access to this document.";
|
|
|
|
export function renderDocument(document: RestDocument, options: RenderDocumentOptions = {}): DocsContent {
|
|
const tally: Tally = { images: 0, drawings: 0, charts: 0, equations: 0, dropdowns: 0 };
|
|
const flat = flattenTabs(document.tabs ?? []);
|
|
const rendered = flat.map(({ tab, path }) => ({ path, ...renderTab(tab, tally) }));
|
|
const sections = rendered.map(({ path, lines }) => {
|
|
const body = trimBlankEdges(lines);
|
|
return rendered.length > 1 ? [`=== Tab: ${path.join(" / ")} ===`, ...body] : body;
|
|
});
|
|
const lines = sections.flatMap((section, index) => (index === 0 ? section : ["", ...section]));
|
|
const view = options.suggestionsView ?? "SUGGESTIONS_INLINE";
|
|
const preview = view === "PREVIEW_WITHOUT_SUGGESTIONS";
|
|
if (options.commentsRequested && preview) lines.push("", "[Comments]", "Not available with your access to this document.");
|
|
else if (options.commentsRequested) {
|
|
const comments = document.comments ?? [];
|
|
const tabTitles = new Map<string, string>();
|
|
for (const { tab, path } of flat) {
|
|
for (const anchorId of Object.keys(tab.documentTab?.commentAnchors ?? {})) tabTitles.set(anchorId, path.join(" / "));
|
|
}
|
|
lines.push("", "[Comments]", ...(comments.length
|
|
? renderComments(comments, (thread) => (flat.length > 1 && thread.anchorId ? tabTitles.get(thread.anchorId) : undefined))
|
|
: ["No comments."]));
|
|
}
|
|
const notes = [preview ? PREVIEW_NOTE : INLINE_NOTE, ...(preview && options.commentsRequested ? [COMMENTS_UNAVAILABLE_NOTE] : []), ...tallyNotes(tally)];
|
|
return {
|
|
documentId: document.documentId ?? "",
|
|
title: document.title ?? "",
|
|
...(document.revisionId ? { revisionId: document.revisionId } : {}),
|
|
suggestionsViewMode: view,
|
|
text: trimBlankEdges(lines).join("\n"),
|
|
tabs: rendered.map(({ view }) => view),
|
|
notes,
|
|
};
|
|
}
|