Skip to content

Commit f0a0ace

Browse files
committed
fix(docs): preserve revision for markdown inserts
1 parent c0a5d94 commit f0a0ace

4 files changed

Lines changed: 60 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Added
66

7-
- Docs: add `insert --markdown` to convert markdown to Google Docs formatting and place the converted block at a position resolved by `--index`/`--at`/`--occurrence`, giving `insert` parity with the existing `update --markdown` and `write --markdown` paths. (#851)
7+
- Docs: add `insert --markdown` to convert markdown to Google Docs formatting and place the converted block at a position resolved by `--index`/`--at`/`--occurrence`, giving `insert` parity with the existing `update --markdown` and `write --markdown` paths. (#851, #854) — thanks @sebsnyk.
88

99
## 0.29.0 - 2026-06-19
1010

internal/cmd/docs_edit.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ func (c *DocsInsertCmd) Run(ctx context.Context, kctx *kong.Context, flags *Root
969969
c.Tab = resolvedPlacement.TabID
970970

971971
if c.Markdown {
972-
return c.runMarkdown(ctx, svc, docID, insertIndex, content)
972+
return c.runMarkdown(ctx, svc, docID, insertIndex, resolvedPlacement.RequiredRevisionID, content)
973973
}
974974

975975
batchReq := &docs.BatchUpdateDocumentRequest{
@@ -1006,9 +1006,25 @@ func (c *DocsInsertCmd) Run(ctx context.Context, kctx *kong.Context, flags *Root
10061006
// insertion helper that backs `docs write --markdown` and the non-replacing
10071007
// branch of `docs update --markdown`, so headings, fenced code blocks, lists,
10081008
// tables and images render identically regardless of which command placed them.
1009-
func (c *DocsInsertCmd) runMarkdown(ctx context.Context, svc *docs.Service, docID string, insertIndex int64, content string) error {
1009+
func (c *DocsInsertCmd) runMarkdown(
1010+
ctx context.Context,
1011+
svc *docs.Service,
1012+
docID string,
1013+
insertIndex int64,
1014+
requiredRevisionID string,
1015+
content string,
1016+
) error {
10101017
markdown := prepareMarkdown(content)
1011-
insertResult, err := insertPreparedDocsMarkdownAt(ctx, svc, docID, insertIndex, markdown, c.Tab, true)
1018+
insertResult, err := insertPreparedDocsMarkdownAtWithWriteControl(
1019+
ctx,
1020+
svc,
1021+
docID,
1022+
insertIndex,
1023+
markdown,
1024+
c.Tab,
1025+
true,
1026+
docsRequiredRevisionWriteControl(requiredRevisionID),
1027+
)
10121028
if err != nil {
10131029
if isDocsNotFound(err) {
10141030
return fmt.Errorf("doc not found or not a Google Doc (id=%s)", docID)

internal/cmd/docs_insert_markdown_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ func TestDocsInsertCmd_MarkdownAtIndex(t *testing.T) {
4343
flags := &RootFlags{Account: "[email protected]"}
4444
ctx := withDocsTestService(newCmdRuntimeOutputContext(t, io.Discard, io.Discard), docSvc)
4545

46-
if err := runKong(t, &DocsInsertCmd{}, []string{"doc1", "## Heading", "--markdown", "--index", "7"}, ctx, flags); err != nil {
46+
markdown := "## Heading\n\n```\ncode\n```"
47+
if err := runKong(t, &DocsInsertCmd{}, []string{"doc1", markdown, "--markdown", "--index", "7"}, ctx, flags); err != nil {
4748
t.Fatalf("insert --markdown: %v", err)
4849
}
4950

@@ -58,6 +59,7 @@ func TestDocsInsertCmd_MarkdownAtIndex(t *testing.T) {
5859

5960
var insertAtIndex *docs.InsertTextRequest
6061
var sawHeadingStyle bool
62+
var fencedCodeStyle *docs.UpdateTextStyleRequest
6163
for _, req := range batchRequests[0] {
6264
if req.InsertText != nil && req.InsertText.Location != nil && req.InsertText.Location.Index == 7 {
6365
insertAtIndex = req.InsertText
@@ -67,6 +69,9 @@ func TestDocsInsertCmd_MarkdownAtIndex(t *testing.T) {
6769
strings.HasPrefix(req.UpdateParagraphStyle.ParagraphStyle.NamedStyleType, "HEADING") {
6870
sawHeadingStyle = true
6971
}
72+
if req.UpdateTextStyle != nil && strings.Contains(req.UpdateTextStyle.Fields, "weightedFontFamily") {
73+
fencedCodeStyle = req.UpdateTextStyle
74+
}
7075
}
7176
if insertAtIndex == nil {
7277
t.Fatalf("expected an InsertText at index 7, requests: %#v", batchRequests[0])
@@ -77,6 +82,26 @@ func TestDocsInsertCmd_MarkdownAtIndex(t *testing.T) {
7782
if !sawHeadingStyle {
7883
t.Fatalf("expected a HEADING paragraph-style request from the markdown converter, requests: %#v", batchRequests[0])
7984
}
85+
assertFencedCodeTextStyle(t, fencedCodeStyle)
86+
}
87+
88+
func TestDocsInsertCmd_MarkdownAtAnchorUsesRevisionControl(t *testing.T) {
89+
t.Parallel()
90+
91+
rec := &docsAtAnchorRecorder{}
92+
doc := docsFindRangeDoc(docsFindRangeParagraph(1, "Before anchor after\n"))
93+
doc.RevisionId = "rev-markdown-anchor"
94+
svc := setupDocsAtAnchorTestService(t, doc, rec)
95+
96+
flags := &RootFlags{Account: "[email protected]"}
97+
ctx := withDocsTestService(newCmdRuntimeOutputContext(t, io.Discard, io.Discard), svc)
98+
if err := runKong(t, &DocsInsertCmd{}, []string{"doc1", "## Heading", "--markdown", "--at", "anchor"}, ctx, flags); err != nil {
99+
t.Fatalf("insert --markdown --at: %v", err)
100+
}
101+
if len(rec.batchRequests) != 1 {
102+
t.Fatalf("batch calls = %d, want 1", len(rec.batchRequests))
103+
}
104+
assertDocsAtAnchorWriteControl(t, rec, 0, "rev-markdown-anchor")
80105
}
81106

82107
// TestDocsInsertCmd_MarkdownRejectsBatch verifies the --markdown/--batch combo

internal/cmd/docs_mutation.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,19 @@ func insertPreparedDocsMarkdownAt(
284284
markdown preparedMarkdown,
285285
tabID string,
286286
stripHeadingAnchors bool,
287+
) (docsMarkdownInsertResult, error) {
288+
return insertPreparedDocsMarkdownAtWithWriteControl(ctx, svc, docID, insertIdx, markdown, tabID, stripHeadingAnchors, nil)
289+
}
290+
291+
func insertPreparedDocsMarkdownAtWithWriteControl(
292+
ctx context.Context,
293+
svc *docs.Service,
294+
docID string,
295+
insertIdx int64,
296+
markdown preparedMarkdown,
297+
tabID string,
298+
stripHeadingAnchors bool,
299+
writeControl *docs.WriteControl,
287300
) (docsMarkdownInsertResult, error) {
288301
elements := docsmarkdown.ParseMarkdown(markdown.cleaned)
289302
if stripHeadingAnchors {
@@ -321,7 +334,7 @@ func insertPreparedDocsMarkdownAt(
321334
})
322335
requests = append(requests, formattingRequests...)
323336

324-
requestCount, err := submitBatchedDocsRequests(ctx, svc, docID, requests, nil)
337+
requestCount, err := submitBatchedDocsRequests(ctx, svc, docID, requests, writeControl)
325338
if err != nil {
326339
return docsMarkdownInsertResult{}, fmt.Errorf("append (markdown): %w", err)
327340
}

0 commit comments

Comments
 (0)