Skip to content

Commit 31cf16d

Browse files
committed
Added document update command
1 parent 2663972 commit 31cf16d

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,76 @@ funcli documents create z2zK66AaEF my-document.md
269269
# Creates "API Documentation" as child of def456
270270
```
271271

272+
#### Update a document
273+
274+
```bash
275+
funcli documents update <document-npi> [file]
276+
```
277+
278+
Updates an existing document with new markdown content from a file or stdin. This creates a new version of the document while preserving its history.
279+
280+
**Arguments:**
281+
- `<document-npi>` - NPI of the document to update
282+
- `[file]` - Path to markdown file (optional, reads from stdin if omitted)
283+
284+
**Frontmatter Support:**
285+
286+
The update command supports YAML frontmatter for tags, just like the create command:
287+
288+
```markdown
289+
---
290+
tags:
291+
- updated/tag1
292+
- updated/tag2
293+
---
294+
295+
# Updated content starts here
296+
```
297+
298+
**Note:** Tags from frontmatter will replace any existing tags on the document.
299+
300+
**Examples:**
301+
302+
```bash
303+
# Update from file
304+
funcli documents update abc123 updated-content.md
305+
306+
# Update from stdin
307+
echo "# Updated Content\n\nNew text here" | funcli documents update abc123
308+
309+
# Update from stdin (multiline)
310+
cat updated-document.md | funcli documents update abc123
311+
312+
# Update with tags from frontmatter
313+
funcli documents update abc123 document-with-tags.md
314+
```
315+
316+
**Example with frontmatter:**
317+
318+
```bash
319+
# updated-content.md
320+
---
321+
tags:
322+
- version/2.0
323+
- status/reviewed
324+
---
325+
326+
# Updated API Documentation
327+
328+
This document has been updated with new information...
329+
```
330+
331+
```bash
332+
funcli documents update abc123 updated-content.md
333+
# Updates document abc123 and replaces tags with "version/2.0" and "status/reviewed"
334+
```
335+
336+
**Notes:**
337+
- Creates a new version of the document (preserves history)
338+
- Updates the document's sync state for real-time collaboration
339+
- Tags from frontmatter replace existing tags
340+
- Does not change document title or hierarchy position
341+
272342
#### Import documents from directory
273343

274344
```bash
@@ -358,6 +428,12 @@ echo "# My Notes\n\nSome content" | funcli documents create z2zK66AaEF --title "
358428
# Import an existing markdown file
359429
funcli documents create z2zK66AaEF README.md
360430

431+
# Update an existing document
432+
funcli documents update abc123 updated-content.md
433+
434+
# Update from stdin
435+
cat changes.md | funcli documents update abc123
436+
361437
# Create a nested document hierarchy
362438
funcli documents create z2zK66AaEF parent.md
363439
# Note the NPI of the created document, then:

src/cli.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,29 @@ documentsCommand
225225
}
226226
}));
227227

228+
documentsCommand
229+
.command("update <npi>")
230+
.description("Update a document from markdown file or stdin")
231+
.argument("[file]", "Markdown file (omit to read from stdin)")
232+
.action(withClient(async (client, npi, file) => {
233+
// Read content from file or stdin
234+
let content;
235+
if (file) {
236+
content = fs.readFileSync(file, "utf8");
237+
} else {
238+
// Read from stdin
239+
content = await readStdin();
240+
}
241+
242+
// Update document
243+
const document = await client.updateDocument(npi, {
244+
markdown: content
245+
});
246+
247+
console.log(chalk.green("✓") + " Document updated successfully!");
248+
console.log(chalk.bold(document.title) + chalk.gray(` (${document.npi})`));
249+
}));
250+
228251
function printDocumentTree(documents, level) {
229252
const indent = " ".repeat(level);
230253

src/client.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,13 @@ export class FundamentoClient {
6565
});
6666
return response.data;
6767
}
68+
69+
async updateDocument(npi, { markdown }) {
70+
const response = await this.axios.patch(`/api/v1/documents/${npi}`, {
71+
document: {
72+
markdown
73+
}
74+
});
75+
return response.data;
76+
}
6877
}

test/client.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,34 @@ test("FundamentoClient createSpace should format request correctly", () => {
9898
// Restore original post
9999
client.axios.post = originalPost;
100100
});
101+
102+
test("FundamentoClient updateDocument should format request correctly", () => {
103+
const config = new Config({ apiKey: "test-key" });
104+
const client = new FundamentoClient(config);
105+
106+
// Mock axios patch
107+
const originalPatch = client.axios.patch;
108+
let capturedUrl, capturedData;
109+
110+
client.axios.patch = async (url, data) => {
111+
capturedUrl = url;
112+
capturedData = data;
113+
return { data: { npi: "doc123", title: "Updated Document" } };
114+
};
115+
116+
// Call updateDocument
117+
client.updateDocument("doc123", {
118+
markdown: "# Updated Content"
119+
});
120+
121+
// Verify the request format
122+
assert.strictEqual(capturedUrl, "/api/v1/documents/doc123");
123+
assert.deepStrictEqual(capturedData, {
124+
document: {
125+
markdown: "# Updated Content"
126+
}
127+
});
128+
129+
// Restore original patch
130+
client.axios.patch = originalPatch;
131+
});

0 commit comments

Comments
 (0)