docsutil

package
v0.14.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package docsutil provides utilities for working with the Google Docs API.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidURL is returned when a URL cannot be parsed.
	ErrInvalidURL = errors.New("invalid Google Docs URL")

	// ErrEmptyInput is returned when input is empty.
	ErrEmptyInput = errors.New("input cannot be empty")
)

Functions

func BuildDocumentURL

func BuildDocumentURL(documentID string) string

BuildDocumentURL constructs a Google Docs URL from a document ID.

func EnrichImagesWithURIs

func EnrichImagesWithURIs(content *DocumentContent, doc *docs.Document)

EnrichImagesWithURIs populates image URIs from the document's inline objects.

func ExtractPlainText

func ExtractPlainText(doc *docs.Document) string

ExtractPlainText extracts all text from a document as a single string.

func ExtractTextByParagraph

func ExtractTextByParagraph(doc *docs.Document) []string

ExtractTextByParagraph extracts text organized by paragraphs.

func IsDocumentURL

func IsDocumentURL(s string) bool

IsDocumentURL checks if the given string is a Google Docs document URL.

func NormalizeDocumentInput

func NormalizeDocumentInput(urlOrID string) (string, error)

NormalizeDocumentInput accepts either a document ID or URL and returns the document ID. This is useful for APIs that accept either format.

func ParseDocumentURL

func ParseDocumentURL(urlOrID string) (string, error)

ParseDocumentURL extracts the document ID from a Google Docs URL. Returns the document ID or an error if the URL is invalid.

func Scopes

func Scopes() []string

Scopes returns the OAuth2 scopes for Google Docs API access.

func ScopesReadWrite

func ScopesReadWrite() []string

ScopesReadWrite returns the OAuth2 scopes for read-write Google Docs API access.

Types

type Block added in v0.13.0

type Block struct {
	// Location is "body/p/<n>" for a paragraph or
	// "body/table/<n>/r/<row>/c/<col>" for a table cell, where n is the index
	// of the structural element in the body.
	Location string    `json:"location"`
	Segments []Segment `json:"segments"`
	// Original is the original and deletion segments concatenated.
	Original string `json:"original"`
	// Accepted is the original and insertion segments concatenated.
	Accepted string `json:"accepted"`
}

Block is one paragraph or table cell with its segments in document order.

type DocumentContent

type DocumentContent struct {
	Title      string            `json:"title"`
	DocumentID string            `json:"document_id"`
	Sections   []SectionContent  `json:"sections"`
	Images     []ImageContent    `json:"images,omitempty"`
	Tables     []TableContent    `json:"tables,omitempty"`
	Lists      []ListContent     `json:"lists,omitempty"`
	Headers    map[string]string `json:"headers,omitempty"`
	Footers    map[string]string `json:"footers,omitempty"`
}

DocumentContent represents extracted content from a Google Doc.

func ExtractDocumentContent

func ExtractDocumentContent(doc *docs.Document) *DocumentContent

ExtractDocumentContent extracts all text and structural content from a document.

type ImageContent

type ImageContent struct {
	ObjectID    string `json:"object_id"`
	ContentURI  string `json:"content_uri"`
	SourceURI   string `json:"source_uri,omitempty"`
	Title       string `json:"title,omitempty"`
	Description string `json:"description,omitempty"`
}

ImageContent represents an embedded image.

type ListContent

type ListContent struct {
	ListID string   `json:"list_id"`
	Items  []string `json:"items"`
}

ListContent represents a list in the document.

type SectionContent

type SectionContent struct {
	Type    string `json:"type"` // "heading", "paragraph", "list_item"
	Level   int    `json:"level,omitempty"`
	Text    string `json:"text"`
	StyleID string `json:"style_id,omitempty"`
}

SectionContent represents a section of text content.

type Segment added in v0.13.0

type Segment struct {
	Kind          SegmentKind `json:"kind"`
	Text          string      `json:"text"`
	SuggestionIDs []string    `json:"suggestion_ids,omitempty"`
}

Segment is a run of text of one kind with the suggestions that affect it.

type SegmentKind added in v0.13.0

type SegmentKind string

SegmentKind classifies a run of text relative to pending suggestions.

const (
	// SegmentOriginal is text no suggestion touches.
	SegmentOriginal SegmentKind = "original"
	// SegmentInsertion is text a suggestion would add.
	SegmentInsertion SegmentKind = "insertion"
	// SegmentDeletion is text a suggestion would remove.
	SegmentDeletion SegmentKind = "deletion"
)

type Service

type Service struct {
	DocsService      *docs.Service
	DocumentsService *docs.DocumentsService
	// contains filtered or unexported fields
}

Service wraps the Google Docs API service.

func NewService

func NewService(ctx context.Context, httpClient *http.Client) (*Service, error)

NewService creates a new Docs service from an authenticated HTTP client.

func (*Service) GetDocument

func (s *Service) GetDocument(ctx context.Context, documentID string) (*docs.Document, error)

GetDocument retrieves a document by ID.

func (*Service) GetDocumentWithSuggestions

func (s *Service) GetDocumentWithSuggestions(ctx context.Context, documentID, suggestionsViewMode string) (*docs.Document, error)

GetDocumentWithSuggestions retrieves a document with the specified suggestions view mode. Mode can be: DEFAULT_FOR_CURRENT_ACCESS, SUGGESTIONS_INLINE, PREVIEW_SUGGESTIONS_ACCEPTED, PREVIEW_WITHOUT_SUGGESTIONS

type SuggestionContent added in v0.13.0

type SuggestionContent struct {
	DocumentID    string   `json:"document_id"`
	Blocks        []Block  `json:"blocks"`
	SuggestionIDs []string `json:"suggestion_ids"` // distinct, document order
}

SuggestionContent is a document's text split by pending suggestions.

func ExtractSuggestionContent added in v0.13.0

func ExtractSuggestionContent(doc *docs.Document) *SuggestionContent

ExtractSuggestionContent splits the body of a document fetched with suggestions view mode SUGGESTIONS_INLINE into blocks of original, inserted, and deleted text. The Docs API does not report who made a suggestion, so blocks carry no author.

Document order is preserved: concatenating the original and deletion segments of the blocks yields the original text, and concatenating the original and insertion segments yields the text as if every suggestion were accepted. A run that is both suggested for insertion and for deletion is classified as an insertion.

Every paragraph and table cell yields a block, including those without suggestions. The input is not modified.

type TableContent

type TableContent struct {
	Rows    int        `json:"rows"`
	Columns int        `json:"columns"`
	Cells   [][]string `json:"cells"`
}

TableContent represents a table in the document.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL