Add `--id` flag to `zk new` (#183)

pull/186/head^2
Stephen Bolton 2 years ago committed by GitHub
parent a0ced3c330
commit 9e33d679e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
### Added
* New `--date` flag for `zk new` to set the current date manually.
* New `--id` flag for `zk new` to skip ID generation and use a provided value (contributed by [@skbolton](https://github.com/mickael-menu/zk/pull/183)).
* [#144](https://github.com/mickael-menu/zk/issues/144) LSP auto-completion of YAML frontmatter tags.
* [zk-nvim#26](https://github.com/mickael-menu/zk-nvim/issues/26) The LSP server doesn't use `additionalTextEdits` anymore to remove the trigger characters when completing links.
* You can customize the default behavior with the [`use-additional-text-edits` configuration key](docs/config-lsp.md).

@ -24,6 +24,7 @@ type New struct {
Template string ` placeholder:PATH help:"Custom template used to render the note."`
PrintPath bool `short:p help:"Print the path of the created note instead of editing it."`
DryRun bool `short:n help:"Don't actually create the note. Instead, prints its content on stdout and the generated path on stderr."`
ID string ` placeholder:ID help:"Skip id generation and use provided value."`
}
func (cmd *New) Run(container *cli.Container) error {
@ -54,6 +55,7 @@ func (cmd *New) Run(container *cli.Container) error {
Extra: cmd.Extra,
Date: date,
DryRun: cmd.DryRun,
ID: cmd.ID,
})
if cmd.DryRun {

@ -109,6 +109,8 @@ type NewNoteOpts struct {
Date time.Time
// Don't save the generated note on the file system.
DryRun bool
// Use a provided id over generating one
ID string
}
// ErrNoteExists is an error returned when a note already exists with the
@ -148,6 +150,15 @@ func (n *Notebook) NewNote(opts NewNoteOpts) (*Note, error) {
return nil, wrap(err)
}
var idGenerator IDGenerator
if opts.ID != "" {
idGenerator = func() string {
return opts.ID
}
} else {
idGenerator = n.idGeneratorFactory(config.Note.IDOptions)
}
task := newNoteTask{
dir: dir,
title: opts.Title.OrString(config.Note.DefaultTitle).Unwrap(),
@ -159,7 +170,7 @@ func (n *Notebook) NewNote(opts NewNoteOpts) (*Note, error) {
filenameTemplate: config.Note.FilenameTemplate + "." + config.Note.Extension,
bodyTemplatePath: opts.Template.Or(config.Note.BodyTemplatePath),
templates: templates,
genID: n.idGeneratorFactory(config.Note.IDOptions),
genID: idGenerator,
dryRun: opts.DryRun,
}
path, content, err := task.execute()

@ -29,6 +29,7 @@ $ zk new --help
> -n, --dry-run Don't actually create the note. Instead, prints
> its content on stdout and the generated path on
> stderr.
> --id=ID Skip id generation and use provided value.
# Default note title.
$ zk new --print-path
@ -48,6 +49,10 @@ $ cat custom-title.md
>
>
# Provide a custom note id
$ zk new --group id --id 123abc --dry-run
2>{{working-dir}}/123abc.md
# Provide a custom title (short flag).
$ zk new -t "Another custom title" -p
>{{working-dir}}/another-custom-title.md

@ -10,6 +10,10 @@ filename = "{{date now '%d-%m'}}"
template = "empty.md"
filename = "{{now}}"
[group.id.note]
template = "empty.md"
filename = "{{id}}"
[group.handlebars.note]
template = "handlebars.md"

Loading…
Cancel
Save