Skip to content

Commit

Permalink
Add --id flag to zk new (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
skbolton authored Mar 27, 2022
1 parent a0ced3c commit 9e33d67
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion internal/core/notebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand All @@ -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()
Expand Down
5 changes: 5 additions & 0 deletions tests/cmd-new.tesh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/new/.zk/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 9e33d67

Please sign in to comment.