From 9fda155a9952d4ef62c012a7f61ff386e5b0b58f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mickae=CC=88l=20Menu?= Date: Thu, 11 Mar 2021 20:59:53 +0100 Subject: [PATCH] Add a [format.markdown] config section to customize tags syntax --- CHANGELOG.md | 2 +- cmd/container.go | 10 +++++----- core/zk/config.go | 18 ++++++++++++++++++ core/zk/config_test.go | 26 ++++++++++++++++++++++++++ core/zk/zk.go | 11 +++++++++++ docs/config.md | 9 +++++++++ docs/future-proof.md | 2 +- docs/note-format.md | 13 +++++++++++++ 8 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 docs/note-format.md diff --git a/CHANGELOG.md b/CHANGELOG.md index f538e46..cd64a7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ All notable changes to this project will be documented in this file. * To match notes associated with either tags, use a pipe `|` or `OR` (all caps), e.g. `--tag "inbox OR todo"`. * If you want to exclude notes having a particular tag, prefix it with `-` or `NOT` (all caps), e.g. `--tag "NOT done"`. * Use glob patterns to match multiple tags, e.g. `--tag "book-*"`. - * Many tag flavors are supported: `#hashtags`, `:colon:separated:tags:` and even Bear's [`#multi-word tags#`](https://blog.bear.app/2017/11/bear-tips-how-to-create-multi-word-tags/). If you prefer to use a YAML frontmatter, list your tags with the key `tags` or `keywords`. + * Many tag flavors are supported: `#hashtags`, `:colon:separated:tags:` ([opt-in](docs/note-format.md)) and even Bear's [`#multi-word tags#`](https://blog.bear.app/2017/11/bear-tips-how-to-create-multi-word-tags/) ([opt-in](docs/note-format.md)). If you prefer to use a YAML frontmatter, list your tags with the key `tags` or `keywords`. * Print metadata from the YAML frontmatter in `list` output using `{{metadata.}}`, e.g. `{{metadata.description}}`. Keys are normalized to lower case. ### Changed diff --git a/cmd/container.go b/cmd/container.go index 3168007..5d71e75 100644 --- a/cmd/container.go +++ b/cmd/container.go @@ -62,11 +62,11 @@ func (c *Container) TemplateLoader(lang string) *handlebars.Loader { return c.templateLoader } -func (c *Container) Parser() *markdown.Parser { +func (c *Container) Parser(zk *zk.Zk) *markdown.Parser { return markdown.NewParser(markdown.ParserOpts{ - HashtagEnabled: true, - MultiWordTagEnabled: false, - ColontagEnabled: true, + HashtagEnabled: zk.Config.Format.Markdown.Hashtags, + MultiWordTagEnabled: zk.Config.Format.Markdown.MultiwordTags, + ColontagEnabled: zk.Config.Format.Markdown.ColonTags, }) } @@ -116,7 +116,7 @@ func (c *Container) index(zk *zk.Zk, db *sqlite.DB, force bool) (note.IndexingSt stats, err = note.Index( zk, force, - c.Parser(), + c.Parser(zk), c.NoteIndexer(tx), c.Logger, func(change paths.DiffChange) { diff --git a/core/zk/config.go b/core/zk/config.go index 257e54f..93a1bc1 100644 --- a/core/zk/config.go +++ b/core/zk/config.go @@ -12,6 +12,7 @@ import ( type Config struct { Note NoteConfig Groups map[string]GroupConfig + Format FormatConfig Tool ToolConfig Aliases map[string]string Extra map[string]string @@ -26,6 +27,21 @@ func (c Config) RootGroupConfig() GroupConfig { } } +// FormatConfig holds the configuration for document formats, such as Markdown. +type FormatConfig struct { + Markdown MarkdownConfig +} + +// MarkdownConfig holds the configuration for Markdown documents. +type MarkdownConfig struct { + // Hashtags indicates whether #hashtags are supported. + Hashtags bool `toml:"hashtags" default:"true"` + // ColonTags indicates whether :colon:tags: are supported. + ColonTags bool `toml:"colon-tags" default:"false"` + // MultiwordTags indicates whether #multi-word tags# are supported. + MultiwordTags bool `toml:"multiword-tags" default:"false"` +} + // ToolConfig holds the external tooling configuration. type ToolConfig struct { Editor opt.String @@ -163,6 +179,7 @@ func ParseConfig(content []byte, templatesDir string) (*Config, error) { return &Config{ Note: root.Note, Groups: groups, + Format: tomlConf.Format, Tool: ToolConfig{ Editor: opt.NewNotEmptyString(tomlConf.Tool.Editor), Pager: opt.NewStringWithPtr(tomlConf.Tool.Pager), @@ -224,6 +241,7 @@ func (c GroupConfig) merge(tomlConf tomlGroupConfig, name string, templatesDir s type tomlConfig struct { Note tomlNoteConfig Groups map[string]tomlGroupConfig `toml:"group"` + Format FormatConfig Tool tomlToolConfig Extra map[string]string Aliases map[string]string `toml:"alias"` diff --git a/core/zk/config_test.go b/core/zk/config_test.go index d54160c..73f38ba 100644 --- a/core/zk/config_test.go +++ b/core/zk/config_test.go @@ -27,6 +27,13 @@ func TestParseDefaultConfig(t *testing.T) { Lang: "en", }, Groups: make(map[string]GroupConfig), + Format: FormatConfig{ + Markdown: MarkdownConfig{ + Hashtags: true, + ColonTags: false, + MultiwordTags: false, + }, + }, Tool: ToolConfig{ Editor: opt.NullString, Pager: opt.NullString, @@ -58,6 +65,11 @@ func TestParseComplete(t *testing.T) { id-length = 4 id-case = "lower" + [format.markdown] + hashtags = false + colon-tags = true + multiword-tags = true + [tool] editor = "vim" pager = "less" @@ -168,6 +180,13 @@ func TestParseComplete(t *testing.T) { }, }, }, + Format: FormatConfig{ + Markdown: MarkdownConfig{ + Hashtags: false, + ColonTags: true, + MultiwordTags: true, + }, + }, Tool: ToolConfig{ Editor: opt.NewString("vim"), Pager: opt.NewString("less"), @@ -269,6 +288,13 @@ func TestParseMergesGroupConfig(t *testing.T) { }, }, }, + Format: FormatConfig{ + Markdown: MarkdownConfig{ + Hashtags: true, + ColonTags: false, + MultiwordTags: false, + }, + }, Aliases: make(map[string]string), Extra: map[string]string{ "hello": "world", diff --git a/core/zk/zk.go b/core/zk/zk.go index 67d05cf..3559f93 100644 --- a/core/zk/zk.go +++ b/core/zk/zk.go @@ -82,6 +82,17 @@ template = "default.md" #key = "value" +# MARKDOWN SETTINGS +[format.markdown] +# Enable support for #hashtags +hashtags = true +# Enable support for :colon:separated:tags: +#colon-tags = true +# Enable support for Bear's #multi-word tags# +# Hashtags must be enabled for multi-word tags to work. +#multiword-tags = true + + # EXTERNAL TOOLS [tool] diff --git a/docs/config.md b/docs/config.md index 50be448..c3b892f 100644 --- a/docs/config.md +++ b/docs/config.md @@ -5,6 +5,7 @@ Each [notebook](notebook.md) contains a configuration file used to customize you * `[note]` sets the [note creation rules](config-note.md) * `[extra]` contains free [user variables](config-extra.md) which can be expanded in templates * `[group]` defines [note groups](config-group.md) with custom rules +* `[format]` configures the [note format settings](note-format.md), such as Markdown options. * `[tool]` customizes interaction with external programs such as: * [your default editor](tool-editor.md) * [your default pager](tool-pager.md) @@ -61,6 +62,14 @@ paths = ["journal/weekly", "journal/daily"] filename = "{{date now}}" +# MARKDOWN SETTINGS +[format.markdown] +# Enable support for #hashtags +hashtags = true +# Enable support for :colon:separated:tags: +colon-tags = true + + # EXTERNAL TOOLS [tool] diff --git a/docs/future-proof.md b/docs/future-proof.md index 4384200..6035c76 100644 --- a/docs/future-proof.md +++ b/docs/future-proof.md @@ -1,5 +1,5 @@ # A future-proof notebook -`zk` is designed to be future-proof and rely on simple plain text formats such as Markdown. +`zk` is designed to be future-proof and rely on simple [plain text formats](note-format.md) such as Markdown. The shape of your [notebook](notebook.md) is entirely up to you, making `zk` flexible enough to be used in a variety of contexts. However, `zk` shines in a Zettelkasten-style notebook with many small interlinked notes. diff --git a/docs/note-format.md b/docs/note-format.md new file mode 100644 index 0000000..6e84c08 --- /dev/null +++ b/docs/note-format.md @@ -0,0 +1,13 @@ +# Note formats + +To keep your notebooks [future-proof](future-proof.md), `zk` uses a simple plain text format for your notes. Only Markdown is supported at the moment, but more formats may be added in the future. + +You can set up some features of `zk`'s Markdown parser from your [configuration file](config.md), under the `[format.markdown]` section. + +| Setting | Default | Description | +|------------------|---------|------------------------------------------------------------------------| +| `hashtags ` | `true` | Enable `#hashtags` support | +| `colon-tags` | `false` | Enable `:colon:separated:tags:` support | +| `multiword-tags` | `false` | Enable Bear's [`#multi-word tags#`][1]. Hashtags must also be enabled. | + +[1]: https://blog.bear.app/2017/11/bear-tips-how-to-create-multi-word-tags/