1
1
use std:: path:: { Path , PathBuf } ;
2
2
3
- use colored:: Colorize ;
4
3
use chrono:: { Local , SecondsFormat } ;
4
+ use colored:: Colorize ;
5
5
use eyre:: { bail, eyre, Context , Result } ;
6
6
use indoc:: formatdoc;
7
+ use inquire:: Text ;
8
+ use regex:: Regex ;
7
9
use tracing:: { debug, info, instrument, warn} ;
8
10
use whoami:: username;
9
11
@@ -96,18 +98,44 @@ fn generate_content_title(base_path: &Path, full_path: &Path) -> String {
96
98
#[ instrument( level = "debug" , skip( path, title) ) ]
97
99
async fn create_norg_document ( path : & Path , title : & str ) -> Result < ( ) > {
98
100
debug ! ( "Creating new norg document: {}" , path. display( ) ) ;
101
+ let re = Regex :: new ( ", ?" ) ?;
99
102
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) ) ?;
101
127
102
128
let content = formatdoc ! (
103
129
r#"
104
130
@document.meta
105
131
title: {title}
106
- description:
132
+ description: {description}
107
133
authors: [
108
- {username}
134
+ {}
135
+ ]
136
+ categories: [
137
+ {}
109
138
]
110
- categories: []
111
139
created: {creation_date}
112
140
updated: {creation_date}
113
141
draft: true
@@ -117,12 +145,12 @@ async fn create_norg_document(path: &Path, title: &str) -> Result<()> {
117
145
* {title}
118
146
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
119
147
labore et dolore magna aliqua. Lobortis scelerisque fermentum dui faucibus in ornare."# ,
148
+ re. replace( & authors, "\n " ) ,
149
+ re. replace( & categories, "\n " ) ,
120
150
) ;
121
151
tokio:: fs:: write ( path, content)
122
152
. 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) ) ?;
126
154
127
155
info ! ( "Created norg document: {}" , path. display( ) ) ;
128
156
Ok ( ( ) )
@@ -170,9 +198,12 @@ pub async fn new(kind: &str, name: &str, open: bool) -> Result<()> {
170
198
}
171
199
172
200
// 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
+ } ) ?;
176
207
// Remove norgolith.toml from the site_root
177
208
site_root. pop ( ) ;
178
209
0 commit comments