| 30 | } |
| 31 | |
| 32 | func findDocsWriteMarkdownOrphans( |
| 33 | ctx context.Context, |
| 34 | driveSvc *drive.Service, |
| 35 | docsSvc *docs.Service, |
| 36 | docID string, |
| 37 | markdown preparedMarkdown, |
| 38 | tabQuery string, |
| 39 | wholeDocument bool, |
| 40 | ) ([]docsWriteOrphanComment, string, error) { |
| 41 | doc, err := docsSvc.Documents.Get(docID).Context(ctx).IncludeTabsContent(true).Do() |
| 42 | if err != nil { |
| 43 | if isDocsNotFound(err) { |
| 44 | return nil, "", fmt.Errorf("doc not found or not a Google Doc (id=%s)", docID) |
| 45 | } |
| 46 | return nil, "", err |
| 47 | } |
| 48 | doc, err = requireRawResponse(doc, "doc not found") |
| 49 | if err != nil { |
| 50 | return nil, "", err |
| 51 | } |
| 52 | |
| 53 | targetTabID, err := docsWriteOrphanTargetTabID(doc, tabQuery, wholeDocument) |
| 54 | if err != nil { |
| 55 | return nil, "", err |
| 56 | } |
| 57 | comments, _, err := listDriveComments(ctx, driveSvc, docID, driveCommentListOptions{ |
| 58 | includeQuoted: true, |
| 59 | all: true, |
| 60 | max: 100, |
| 61 | mode: driveCommentListModeExpanded, |
| 62 | }) |
| 63 | if err != nil { |
| 64 | return nil, "", fmt.Errorf("list document comments: %w", err) |
| 65 | } |
| 66 | |
| 67 | outgoing := docsWriteMarkdownDocument(markdown) |
| 68 | locator := DocsCommentsLocateCmd{NormalizeWhitespace: true} |
| 69 | var orphans []docsWriteOrphanComment |
| 70 | for _, comment := range comments { |
| 71 | quote := strings.TrimSpace(docsCommentQuote(comment)) |
| 72 | if quote == "" { |
| 73 | continue |
| 74 | } |
| 75 | |
| 76 | currentMatches := locator.findQuoteMatchesAcrossDocument(doc, quote) |
| 77 | if len(currentMatches) == 0 || !docsWriteCommentTouchesTarget(currentMatches, targetTabID, wholeDocument) { |
| 78 | continue |
| 79 | } |
| 80 | if len(locator.findQuoteMatchesInDoc(outgoing, quote, targetTabID)) > 0 { |
| 81 | continue |
| 82 | } |
| 83 | |
| 84 | orphan := docsWriteOrphanComment{ |
| 85 | CommentID: comment.Id, |
| 86 | Content: comment.Content, |
| 87 | Quote: docsCommentQuote(comment), |
| 88 | } |
| 89 | if comment.Author != nil { |