// Google Docs captures (release 37 S3): a document's `mobilebasic`, `pub` // (also `pub?embedded=true`) or `preview` capture → one `gdoc` draft: the // document's title and its text, headings and list items kept as lines, its // links unwrapped from Google's /url?q= redirector. An `/edit` capture holds // only the editor's JavaScript app → one draft with `noContent: true` whose // text names the `mobilebasic` URL to look for instead. The record is the // whole page (no itemId): a document changes between captures, and each // capture is a version of it. import type { PageExtractInput, PageExtractor, PageRecordDraft } from "../types"; import { findFirst, hasClass, metaContent, pageTitle, parseHtml, renderBlock, textOf, unwrapWaybackHtml, type HtmlElement, } from "./util"; export function isGoogleDocsUrl(url: URL): boolean { return url.hostname.toLowerCase() === "docs.google.com" && /^\/document\//.test(url.pathname); } // The document's id (`/document/d//…`, the older `/document/pub?id=`), // and whether the URL is a published-to-web id (`/document/d/e//pub`), // which has no mobilebasic form. export function googleDocId(url: URL): { id?: string; published?: boolean } { const pub = /^\/document\/d\/e\/([A-Za-z0-9_-]+)/.exec(url.pathname); if (pub) return { id: pub[1], published: true }; const d = /^\/document\/d\/([A-Za-z0-9_-]{10,})/.exec(url.pathname); if (d) return { id: d[1] }; const q = url.searchParams.get("id") ?? url.searchParams.get("docid"); if (q && /^[A-Za-z0-9_-]{10,}$/.test(q)) return { id: q }; return {}; } export function mobilebasicUrl(id: string): string { return `https://docs.google.com/document/d/${id}/mobilebasic`; } const isContents = (e: HtmlElement) => e.attrs.id === "contents" || e.attrs.id === "doc-contents" || hasClass(e, "doc-content"); const DROP = (e: HtmlElement) => e.tag === "style" || e.tag === "script" || e.attrs.id === "header" || e.attrs.id === "footer" || e.attrs.id === "banners"; function docTitle(root: HtmlElement): string | undefined { const own = textOf(findFirst(root, (e) => e.attrs.id === "title" || hasClass(e, "doc-title") || e.attrs.id === "doc-title")) || findFirst(root, (e) => e.tag === "input" && hasClass(e, "docs-title-input"))?.attrs.value?.trim() || metaContent(root, "og:title") || metaContent(root, "name") || (pageTitle(root) ?? "").replace(/\s+-\s+Google (Docs|Drive)$/, ""); return own || undefined; } export function extractGdoc(input: PageExtractInput): PageRecordDraft[] { const html = unwrapWaybackHtml(input.html); const url = new URL(input.capture.originalUrl); const root = parseHtml(html); const title = docTitle(root); const contents = findFirst(root, isContents); const rendered = contents ? renderBlock(contents, url.href, DROP) : null; if (rendered && rendered.text) { const draft: PageRecordDraft = { kind: "gdoc", text: rendered.text, links: rendered.links }; if (title) draft.title = title; if (rendered.media.length > 0) draft.media = rendered.media; return [draft]; } const { id, published } = googleDocId(url); const look = id && !published ? mobilebasicUrl(id) : undefined; const draft: PageRecordDraft = { kind: "gdoc", text: look ? `This capture holds only the Google Docs editor, not the document's text. Look for a capture of ${look}` : "This capture holds no document text.", links: look ? [look] : [], noContent: true, }; if (title) draft.title = title; return [draft]; } export const gdocExtractor: PageExtractor = { name: "gdoc", version: 1, matches(originalUrl) { return isGoogleDocsUrl(originalUrl); }, extract: extractGdoc, };