Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ async-trait = "0.1.89"
serde_yaml = "0.9.34"
percent-encoding = "2.3.2"
zeroize = { version = "1.8.2", features = ["derive"] }
pulldown-cmark = "0.12"


# The profile that 'cargo dist' will build with
Expand Down
67 changes: 57 additions & 10 deletions src/helpers/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use super::docs_markdown;
use super::Helper;
use crate::auth;
use crate::error::GwsError;
Expand Down Expand Up @@ -42,18 +43,27 @@ impl Helper for DocsHelper {
.arg(
Arg::new("text")
.long("text")
.help("Text to append (plain text)")
.help("Text to append")
.required(true)
.value_name("TEXT"),
)
.arg(
Arg::new("content-format")
.long("content-format")
.help("Content format: 'plaintext' or 'markdown'")
.value_name("FORMAT")
.value_parser(["plaintext", "markdown"])
.default_value("plaintext"),
)
.after_help(
"\
EXAMPLES:
gws docs +write --document DOC_ID --text 'Hello, world!'
gws docs +write --document DOC_ID --content-format markdown --text '# Title\n\nSome **bold** text.'

TIPS:
Text is inserted at the end of the document body.
For rich formatting, use the raw batchUpdate API instead.",
Use --content-format markdown to convert markdown to rich formatting.",
),
);
cmd
Expand Down Expand Up @@ -121,6 +131,10 @@ fn build_write_request(
) -> Result<(String, String, Vec<String>), GwsError> {
let document_id = matches.get_one::<String>("document").unwrap();
let text = matches.get_one::<String>("text").unwrap();
let content_format = matches
.get_one::<String>("content-format")
.map(|s| s.as_str())
.unwrap_or("plaintext");

let documents_res = doc
.resources
Expand All @@ -134,18 +148,22 @@ fn build_write_request(
"documentId": document_id
});

let body = json!({
"requests": [
{
let requests = match content_format {
"markdown" => docs_markdown::markdown_to_batch_requests(text),
_ => {
// Default: plain text insertion
vec![json!({
"insertText": {
"text": text,
"endOfSegmentLocation": {
"segmentId": "" // Empty means body
"segmentId": ""
}
}
}
]
});
})]
}
};

let body = json!({ "requests": requests });

let scopes: Vec<String> = batch_update_method
.scopes
Expand Down Expand Up @@ -187,7 +205,12 @@ mod tests {
fn make_matches_write(args: &[&str]) -> ArgMatches {
let cmd = Command::new("test")
.arg(Arg::new("document").long("document"))
.arg(Arg::new("text").long("text"));
.arg(Arg::new("text").long("text"))
.arg(
Arg::new("content-format")
.long("content-format")
.default_value("plaintext"),
);
cmd.try_get_matches_from(args).unwrap()
}

Expand All @@ -202,4 +225,28 @@ mod tests {
assert!(body.contains("endOfSegmentLocation"));
assert_eq!(scopes[0], "https://scope");
}

#[test]
fn test_build_write_request_markdown() {
let doc = make_mock_doc();
let matches = make_matches_write(&[
"test",
"--document",
"456",
"--text",
"# Hello\n\nSome **bold** text.",
"--content-format",
"markdown",
]);
let (params, body, scopes) = build_write_request(&matches, &doc).unwrap();

assert!(params.contains("456"));
// Should contain insertText and updateParagraphStyle (for heading) and updateTextStyle (for bold)
assert!(body.contains("insertText"));
assert!(body.contains("updateParagraphStyle"));
assert!(body.contains("HEADING_1"));
assert!(body.contains("updateTextStyle"));
assert!(body.contains("\"bold\":true") || body.contains("\"bold\": true"));
assert_eq!(scopes[0], "https://scope");
}
}
Loading
Loading