Skip to content

Commit bed95de

Browse files
committed
ref(new): prompt for metadata values if creating a norg document
1 parent 2b163c4 commit bed95de

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

src/cmd/new.rs

+42-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use std::path::{Path, PathBuf};
22

3-
use colored::Colorize;
43
use chrono::{Local, SecondsFormat};
4+
use colored::Colorize;
55
use eyre::{bail, eyre, Context, Result};
66
use indoc::formatdoc;
7+
use inquire::Text;
8+
use regex::Regex;
79
use tracing::{debug, info, instrument, warn};
810
use whoami::username;
911

@@ -96,18 +98,44 @@ fn generate_content_title(base_path: &Path, full_path: &Path) -> String {
9698
#[instrument(level = "debug", skip(path, title))]
9799
async fn create_norg_document(path: &Path, title: &str) -> Result<()> {
98100
debug!("Creating new norg document: {}", path.display());
101+
let re = Regex::new(", ?")?;
99102
let creation_date = Local::now().to_rfc3339_opts(SecondsFormat::Secs, false);
100-
let username = username();
103+
104+
// Prompt norg file metadata
105+
let title = Text::new("Title:")
106+
.with_default(title)
107+
.with_help_message("Document title")
108+
.prompt()
109+
.map_err(|e| eyre!("Failed to get document title: {}", e))?;
110+
let description = Text::new("Description:")
111+
.with_default("")
112+
.with_help_message("Document description")
113+
.prompt()
114+
.map_err(|e| eyre!("Failed to get document description: {}", e))?;
115+
let authors = Text::new("Author(s):")
116+
.with_default(username().as_str())
117+
.with_help_message("Document authors separated by comma")
118+
.with_placeholder("e.g. NTBBloodbath, Vhyrro")
119+
.prompt()
120+
.map_err(|e| eyre!("Failed to get document author: {}", e))?;
121+
let categories = Text::new("Categories:")
122+
.with_default("")
123+
.with_help_message("Document categories separated by comma")
124+
.with_placeholder("e.g. Neovim, Neorg")
125+
.prompt()
126+
.map_err(|e| eyre!("Failed to get document categories: {}", e))?;
101127

102128
let content = formatdoc!(
103129
r#"
104130
@document.meta
105131
title: {title}
106-
description:
132+
description: {description}
107133
authors: [
108-
{username}
134+
{}
135+
]
136+
categories: [
137+
{}
109138
]
110-
categories: []
111139
created: {creation_date}
112140
updated: {creation_date}
113141
draft: true
@@ -117,12 +145,12 @@ async fn create_norg_document(path: &Path, title: &str) -> Result<()> {
117145
* {title}
118146
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
119147
labore et dolore magna aliqua. Lobortis scelerisque fermentum dui faucibus in ornare."#,
148+
re.replace(&authors, "\n "),
149+
re.replace(&categories, "\n "),
120150
);
121151
tokio::fs::write(path, content)
122152
.await
123-
.map_err(|e| {
124-
eyre!("Failed to write norg document: {}", e)
125-
})?;
153+
.map_err(|e| eyre!("Failed to write norg document: {}", e))?;
126154

127155
info!("Created norg document: {}", path.display());
128156
Ok(())
@@ -170,9 +198,12 @@ pub async fn new(kind: &str, name: &str, open: bool) -> Result<()> {
170198
}
171199

172200
// Find site root
173-
let mut site_root = fs::find_config_file()
174-
.await?
175-
.ok_or_else(|| eyre!("{}: not in a Norgolith site directory", "Unable to create site asset".bold()))?;
201+
let mut site_root = fs::find_config_file().await?.ok_or_else(|| {
202+
eyre!(
203+
"{}: not in a Norgolith site directory",
204+
"Unable to create site asset".bold()
205+
)
206+
})?;
176207
// Remove norgolith.toml from the site_root
177208
site_root.pop();
178209

0 commit comments

Comments
 (0)