api cleanup
This commit is contained in:
parent
ab74339821
commit
f4102dbce5
|
|
@ -2,7 +2,6 @@ package disk
|
||||||
|
|
||||||
import (
|
import (
|
||||||
p9client "denote/internal/p9/client"
|
p9client "denote/internal/p9/client"
|
||||||
"denote/pkg/encoding/frontmatter"
|
|
||||||
"denote/pkg/metadata"
|
"denote/pkg/metadata"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
@ -62,7 +61,7 @@ func HandleUpdateEvent(f *client.Fsys, identifier, denoteDir string) error {
|
||||||
dir = denoteDir
|
dir = denoteDir
|
||||||
}
|
}
|
||||||
|
|
||||||
ext := frontmatter.GetExtension(fileType)
|
ext := metadata.GetExtension(fileType)
|
||||||
filename := metadata.BuildFilename(existing, ext)
|
filename := metadata.BuildFilename(existing, ext)
|
||||||
newPath := filepath.Join(dir, filename)
|
newPath := filepath.Join(dir, filename)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -80,7 +80,7 @@ func ExtractMetadata(path string) (*metadata.Metadata, error) {
|
||||||
|
|
||||||
// ExtractFrontMatter reads a file and parses its front matter.
|
// ExtractFrontMatter reads a file and parses its front matter.
|
||||||
// Returns the parsed FrontMatter and the detected FileType.
|
// Returns the parsed FrontMatter and the detected FileType.
|
||||||
func ExtractFrontMatter(path string) (*frontmatter.FrontMatter, frontmatter.FileType, error) {
|
func ExtractFrontMatter(path string) (*metadata.FrontMatter, metadata.FileType, error) {
|
||||||
ext := strings.ToLower(filepath.Ext(path))
|
ext := strings.ToLower(filepath.Ext(path))
|
||||||
|
|
||||||
content, err := os.ReadFile(path)
|
content, err := os.ReadFile(path)
|
||||||
|
|
@ -92,7 +92,7 @@ func ExtractFrontMatter(path string) (*frontmatter.FrontMatter, frontmatter.File
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateFrontMatter updates the front matter in a file.
|
// UpdateFrontMatter updates the front matter in a file.
|
||||||
func UpdateFrontMatter(path string, fm *frontmatter.FrontMatter, fileType frontmatter.FileType) error {
|
func UpdateFrontMatter(path string, fm *metadata.FrontMatter, fileType metadata.FileType) error {
|
||||||
// Only update frontmatter for supported file types
|
// Only update frontmatter for supported file types
|
||||||
if !SupportsFrontMatter(path) {
|
if !SupportsFrontMatter(path) {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
6
main.go
6
main.go
|
|
@ -22,7 +22,7 @@ import (
|
||||||
|
|
||||||
const (
|
const (
|
||||||
wname = "/Denote/"
|
wname = "/Denote/"
|
||||||
ftype = frontmatter.FileTypeMdYaml
|
ftype = metadata.FileTypeMdYaml
|
||||||
)
|
)
|
||||||
|
|
||||||
var denoteDir = os.Getenv("HOME") + "/doc"
|
var denoteDir = os.Getenv("HOME") + "/doc"
|
||||||
|
|
@ -114,8 +114,8 @@ func handleNewEvent(f *client.Fsys, identifier, denoteDir string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
newIdentifier := metadata.GenerateIdentifier()
|
newIdentifier := metadata.GenerateIdentifier()
|
||||||
fm := frontmatter.New(title, signature, tags, newIdentifier)
|
fm := metadata.NewFrontMatter(title, signature, tags, newIdentifier)
|
||||||
ext := frontmatter.GetExtension(ftype)
|
ext := metadata.GetExtension(ftype)
|
||||||
filename := metadata.BuildFilename(fm, ext)
|
filename := metadata.BuildFilename(fm, ext)
|
||||||
path := filepath.Join(targetDir, filename)
|
path := filepath.Join(targetDir, filename)
|
||||||
content := string(frontmatter.Marshal(fm, ftype))
|
content := string(frontmatter.Marshal(fm, ftype))
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package frontmatter
|
package frontmatter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"denote/pkg/metadata"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -8,15 +9,15 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Templates for denote frontmatter (org, md, txt)
|
// Templates for denote frontmatter (org, md, txt)
|
||||||
var templates = map[FileType]string{
|
var templates = map[metadata.FileType]string{
|
||||||
FileTypeOrg: `#+title: %s
|
metadata.FileTypeOrg: `#+title: %s
|
||||||
#+date: %s
|
#+date: %s
|
||||||
#+filetags: %s
|
#+filetags: %s
|
||||||
#+identifier: %s
|
#+identifier: %s
|
||||||
#+signature: %s
|
#+signature: %s
|
||||||
|
|
||||||
`,
|
`,
|
||||||
FileTypeMdYaml: `---
|
metadata.FileTypeMdYaml: `---
|
||||||
title: %s
|
title: %s
|
||||||
date: %s
|
date: %s
|
||||||
tags: %s
|
tags: %s
|
||||||
|
|
@ -25,7 +26,7 @@ signature: %s
|
||||||
---
|
---
|
||||||
|
|
||||||
`,
|
`,
|
||||||
FileTypeMdToml: `+++
|
metadata.FileTypeMdToml: `+++
|
||||||
title = %s
|
title = %s
|
||||||
date = %s
|
date = %s
|
||||||
tags = %s
|
tags = %s
|
||||||
|
|
@ -34,7 +35,7 @@ signature = %s
|
||||||
+++
|
+++
|
||||||
|
|
||||||
`,
|
`,
|
||||||
FileTypeTxt: `title: %s
|
metadata.FileTypeTxt: `title: %s
|
||||||
date: %s
|
date: %s
|
||||||
tags: %s
|
tags: %s
|
||||||
identifier: %s
|
identifier: %s
|
||||||
|
|
@ -44,64 +45,32 @@ signature: %s
|
||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
|
||||||
// FileType represents supported file formats for denote notes
|
// formatTags formats tags according to file type
|
||||||
type FileType string
|
func formatTags(tags []string, fileType metadata.FileType) string {
|
||||||
|
|
||||||
const (
|
|
||||||
FileTypeOrg FileType = "org"
|
|
||||||
FileTypeMdYaml FileType = "md-yaml"
|
|
||||||
FileTypeMdToml FileType = "md-toml"
|
|
||||||
FileTypeTxt FileType = "txt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// fileExtensions contains the list of file extensions
|
|
||||||
// for which Denote should add front matter.
|
|
||||||
var fileExtensions = map[FileType]string{
|
|
||||||
FileTypeOrg: ".org",
|
|
||||||
FileTypeMdYaml: ".md",
|
|
||||||
FileTypeMdToml: ".md",
|
|
||||||
FileTypeTxt: ".txt",
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetExtension returns the file extension for a given file type.
|
|
||||||
func GetExtension(fileType FileType) string {
|
|
||||||
return fileExtensions[fileType]
|
|
||||||
}
|
|
||||||
|
|
||||||
// FormatTags formats tags according to file type
|
|
||||||
func FormatTags(tags []string, fileType FileType) string {
|
|
||||||
if len(tags) == 0 {
|
if len(tags) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
switch fileType {
|
switch fileType {
|
||||||
case FileTypeOrg:
|
case metadata.FileTypeOrg:
|
||||||
return ":" + strings.Join(tags, ":") + ":"
|
return ":" + strings.Join(tags, ":") + ":"
|
||||||
case FileTypeMdYaml, FileTypeMdToml:
|
case metadata.FileTypeMdYaml, metadata.FileTypeMdToml:
|
||||||
return "[" + strings.Join(tags, ", ") + "]"
|
return "[" + strings.Join(tags, ", ") + "]"
|
||||||
default:
|
default:
|
||||||
return strings.Join(tags, " ")
|
return strings.Join(tags, " ")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FrontMatter represents parsed front matter from a note
|
|
||||||
type FrontMatter struct {
|
|
||||||
Title string
|
|
||||||
Tags []string
|
|
||||||
Identifier string
|
|
||||||
Signature string
|
|
||||||
}
|
|
||||||
|
|
||||||
// Marshal returns the formatted frontmatter content as bytes
|
// Marshal returns the formatted frontmatter content as bytes
|
||||||
func Marshal(fm *FrontMatter, fileType FileType) []byte {
|
func Marshal(fm *metadata.FrontMatter, fileType metadata.FileType) []byte {
|
||||||
template := templates[fileType]
|
template := templates[fileType]
|
||||||
dateStr := time.Now().Format("2006-01-02 Mon 15:04")
|
dateStr := time.Now().Format("2006-01-02 Mon 15:04")
|
||||||
|
|
||||||
// For org-mode, wrap date in brackets for timestamp
|
// For org-mode, wrap date in brackets for timestamp
|
||||||
if fileType == FileTypeOrg {
|
if fileType == metadata.FileTypeOrg {
|
||||||
dateStr = "[" + dateStr + "]"
|
dateStr = "[" + dateStr + "]"
|
||||||
}
|
}
|
||||||
|
|
||||||
keywordsStr := FormatTags(fm.Tags, fileType)
|
keywordsStr := formatTags(fm.Tags, fileType)
|
||||||
content := fmt.Sprintf(template, fm.Title, dateStr, keywordsStr, fm.Identifier, fm.Signature)
|
content := fmt.Sprintf(template, fm.Title, dateStr, keywordsStr, fm.Identifier, fm.Signature)
|
||||||
return []byte(content)
|
return []byte(content)
|
||||||
}
|
}
|
||||||
|
|
@ -109,16 +78,16 @@ func Marshal(fm *FrontMatter, fileType FileType) []byte {
|
||||||
// Unmarshal extracts front matter from file content.
|
// Unmarshal extracts front matter from file content.
|
||||||
// ext should be the file extension (e.g., ".md", ".org", ".txt").
|
// ext should be the file extension (e.g., ".md", ".org", ".txt").
|
||||||
// Returns the parsed frontmatter and the detected FileType.
|
// Returns the parsed frontmatter and the detected FileType.
|
||||||
func Unmarshal(content string, ext string) (*FrontMatter, FileType, error) {
|
func Unmarshal(content string, ext string) (*metadata.FrontMatter, metadata.FileType, error) {
|
||||||
ext = strings.ToLower(ext)
|
ext = strings.ToLower(ext)
|
||||||
text := content
|
text := content
|
||||||
|
|
||||||
fm := &FrontMatter{}
|
fm := &metadata.FrontMatter{}
|
||||||
var fileType FileType
|
var fileType metadata.FileType
|
||||||
|
|
||||||
switch ext {
|
switch ext {
|
||||||
case ".org":
|
case ".org":
|
||||||
fileType = FileTypeOrg
|
fileType = metadata.FileTypeOrg
|
||||||
if m := regexp.MustCompile(`(?m)^#\+title:[ \t]*(.+)$`).FindStringSubmatch(text); m != nil {
|
if m := regexp.MustCompile(`(?m)^#\+title:[ \t]*(.+)$`).FindStringSubmatch(text); m != nil {
|
||||||
fm.Title = strings.TrimSpace(m[1])
|
fm.Title = strings.TrimSpace(m[1])
|
||||||
}
|
}
|
||||||
|
|
@ -136,7 +105,7 @@ func Unmarshal(content string, ext string) (*FrontMatter, FileType, error) {
|
||||||
// Try YAML first
|
// Try YAML first
|
||||||
yamlRe := regexp.MustCompile(`(?ms)^---\n(.*?)\n---`)
|
yamlRe := regexp.MustCompile(`(?ms)^---\n(.*?)\n---`)
|
||||||
if m := yamlRe.FindStringSubmatch(text); m != nil {
|
if m := yamlRe.FindStringSubmatch(text); m != nil {
|
||||||
fileType = FileTypeMdYaml
|
fileType = metadata.FileTypeMdYaml
|
||||||
yamlContent := m[1]
|
yamlContent := m[1]
|
||||||
if m := regexp.MustCompile(`(?m)^title:[ \t]*["']?(.+?)["']?$`).FindStringSubmatch(yamlContent); m != nil {
|
if m := regexp.MustCompile(`(?m)^title:[ \t]*["']?(.+?)["']?$`).FindStringSubmatch(yamlContent); m != nil {
|
||||||
fm.Title = strings.TrimSpace(m[1])
|
fm.Title = strings.TrimSpace(m[1])
|
||||||
|
|
@ -158,7 +127,7 @@ func Unmarshal(content string, ext string) (*FrontMatter, FileType, error) {
|
||||||
// Try TOML
|
// Try TOML
|
||||||
tomlRe := regexp.MustCompile(`(?ms)^\+\+\+\n(.*?)\n\+\+\+`)
|
tomlRe := regexp.MustCompile(`(?ms)^\+\+\+\n(.*?)\n\+\+\+`)
|
||||||
if m := tomlRe.FindStringSubmatch(text); m != nil {
|
if m := tomlRe.FindStringSubmatch(text); m != nil {
|
||||||
fileType = FileTypeMdToml
|
fileType = metadata.FileTypeMdToml
|
||||||
tomlContent := m[1]
|
tomlContent := m[1]
|
||||||
if m := regexp.MustCompile(`(?m)^title[ \t]*=[ \t]*["']?(.+?)["']?$`).FindStringSubmatch(tomlContent); m != nil {
|
if m := regexp.MustCompile(`(?m)^title[ \t]*=[ \t]*["']?(.+?)["']?$`).FindStringSubmatch(tomlContent); m != nil {
|
||||||
fm.Title = strings.TrimSpace(m[1])
|
fm.Title = strings.TrimSpace(m[1])
|
||||||
|
|
@ -180,7 +149,7 @@ func Unmarshal(content string, ext string) (*FrontMatter, FileType, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
case ".txt":
|
case ".txt":
|
||||||
fileType = FileTypeTxt
|
fileType = metadata.FileTypeTxt
|
||||||
if m := regexp.MustCompile(`(?m)^title:[ \t]*(.+)$`).FindStringSubmatch(text); m != nil {
|
if m := regexp.MustCompile(`(?m)^title:[ \t]*(.+)$`).FindStringSubmatch(text); m != nil {
|
||||||
fm.Title = strings.TrimSpace(m[1])
|
fm.Title = strings.TrimSpace(m[1])
|
||||||
}
|
}
|
||||||
|
|
@ -197,13 +166,3 @@ func Unmarshal(content string, ext string) (*FrontMatter, FileType, error) {
|
||||||
|
|
||||||
return fm, fileType, nil
|
return fm, fileType, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new FrontMatter struct from given parameters
|
|
||||||
func New(title, signature string, tags []string, identifier string) *FrontMatter {
|
|
||||||
return &FrontMatter{
|
|
||||||
Title: title,
|
|
||||||
Tags: tags,
|
|
||||||
Identifier: identifier,
|
|
||||||
Signature: signature,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package frontmatter
|
package frontmatter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"denote/pkg/metadata"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -12,82 +13,82 @@ func TestFormatTags(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
tags []string
|
tags []string
|
||||||
fileType FileType
|
fileType metadata.FileType
|
||||||
want string
|
want string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "org with multiple tags",
|
name: "org with multiple tags",
|
||||||
tags: []string{"tag1", "tag2"},
|
tags: []string{"tag1", "tag2"},
|
||||||
fileType: FileTypeOrg,
|
fileType: metadata.FileTypeOrg,
|
||||||
want: ":tag1:tag2:",
|
want: ":tag1:tag2:",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "org with single tag",
|
name: "org with single tag",
|
||||||
tags: []string{"single"},
|
tags: []string{"single"},
|
||||||
fileType: FileTypeOrg,
|
fileType: metadata.FileTypeOrg,
|
||||||
want: ":single:",
|
want: ":single:",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "org with empty tags",
|
name: "org with empty tags",
|
||||||
tags: []string{},
|
tags: []string{},
|
||||||
fileType: FileTypeOrg,
|
fileType: metadata.FileTypeOrg,
|
||||||
want: "",
|
want: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "md-yaml with multiple tags",
|
name: "md-yaml with multiple tags",
|
||||||
tags: []string{"tag1", "tag2"},
|
tags: []string{"tag1", "tag2"},
|
||||||
fileType: FileTypeMdYaml,
|
fileType: metadata.FileTypeMdYaml,
|
||||||
want: "[tag1, tag2]",
|
want: "[tag1, tag2]",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "md-yaml with single tag",
|
name: "md-yaml with single tag",
|
||||||
tags: []string{"single"},
|
tags: []string{"single"},
|
||||||
fileType: FileTypeMdYaml,
|
fileType: metadata.FileTypeMdYaml,
|
||||||
want: "[single]",
|
want: "[single]",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "md-yaml with empty tags",
|
name: "md-yaml with empty tags",
|
||||||
tags: []string{},
|
tags: []string{},
|
||||||
fileType: FileTypeMdYaml,
|
fileType: metadata.FileTypeMdYaml,
|
||||||
want: "",
|
want: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "md-toml with multiple tags",
|
name: "md-toml with multiple tags",
|
||||||
tags: []string{"tag1", "tag2"},
|
tags: []string{"tag1", "tag2"},
|
||||||
fileType: FileTypeMdToml,
|
fileType: metadata.FileTypeMdToml,
|
||||||
want: "[tag1, tag2]",
|
want: "[tag1, tag2]",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "md-toml with empty tags",
|
name: "md-toml with empty tags",
|
||||||
tags: []string{},
|
tags: []string{},
|
||||||
fileType: FileTypeMdToml,
|
fileType: metadata.FileTypeMdToml,
|
||||||
want: "",
|
want: "",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "txt with multiple tags",
|
name: "txt with multiple tags",
|
||||||
tags: []string{"tag1", "tag2"},
|
tags: []string{"tag1", "tag2"},
|
||||||
fileType: FileTypeTxt,
|
fileType: metadata.FileTypeTxt,
|
||||||
want: "tag1 tag2",
|
want: "tag1 tag2",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "txt with single tag",
|
name: "txt with single tag",
|
||||||
tags: []string{"single"},
|
tags: []string{"single"},
|
||||||
fileType: FileTypeTxt,
|
fileType: metadata.FileTypeTxt,
|
||||||
want: "single",
|
want: "single",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "txt with empty tags",
|
name: "txt with empty tags",
|
||||||
tags: []string{},
|
tags: []string{},
|
||||||
fileType: FileTypeTxt,
|
fileType: metadata.FileTypeTxt,
|
||||||
want: "",
|
want: "",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
got := FormatTags(tt.tags, tt.fileType)
|
got := formatTags(tt.tags, tt.fileType)
|
||||||
if got != tt.want {
|
if got != tt.want {
|
||||||
t.Errorf("FormatTags(%v, %q) = %q, want %q", tt.tags, tt.fileType, got, tt.want)
|
t.Errorf("formatTags(%v, %q) = %q, want %q", tt.tags, tt.fileType, got, tt.want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -102,13 +103,13 @@ func TestFrontMatterBytes(t *testing.T) {
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
fileType FileType
|
fileType metadata.FileType
|
||||||
wantContains []string
|
wantContains []string
|
||||||
wantNotContains []string
|
wantNotContains []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "org format",
|
name: "org format",
|
||||||
fileType: FileTypeOrg,
|
fileType: metadata.FileTypeOrg,
|
||||||
wantContains: []string{
|
wantContains: []string{
|
||||||
"#+title: Test Note",
|
"#+title: Test Note",
|
||||||
"#+filetags: :tag1:tag2:",
|
"#+filetags: :tag1:tag2:",
|
||||||
|
|
@ -118,7 +119,7 @@ func TestFrontMatterBytes(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "md-yaml format",
|
name: "md-yaml format",
|
||||||
fileType: FileTypeMdYaml,
|
fileType: metadata.FileTypeMdYaml,
|
||||||
wantContains: []string{
|
wantContains: []string{
|
||||||
"---",
|
"---",
|
||||||
"title: Test Note",
|
"title: Test Note",
|
||||||
|
|
@ -129,7 +130,7 @@ func TestFrontMatterBytes(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "md-toml format",
|
name: "md-toml format",
|
||||||
fileType: FileTypeMdToml,
|
fileType: metadata.FileTypeMdToml,
|
||||||
wantContains: []string{
|
wantContains: []string{
|
||||||
"+++",
|
"+++",
|
||||||
"title = Test Note",
|
"title = Test Note",
|
||||||
|
|
@ -140,7 +141,7 @@ func TestFrontMatterBytes(t *testing.T) {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "txt format",
|
name: "txt format",
|
||||||
fileType: FileTypeTxt,
|
fileType: metadata.FileTypeTxt,
|
||||||
wantContains: []string{
|
wantContains: []string{
|
||||||
"title: Test Note",
|
"title: Test Note",
|
||||||
"tags: tag1 tag2",
|
"tags: tag1 tag2",
|
||||||
|
|
@ -153,7 +154,7 @@ func TestFrontMatterBytes(t *testing.T) {
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
fm := New(title, "", tags, identifier)
|
fm := metadata.NewFrontMatter(title, "", tags, identifier)
|
||||||
got := string(Marshal(fm, tt.fileType))
|
got := string(Marshal(fm, tt.fileType))
|
||||||
|
|
||||||
for _, want := range tt.wantContains {
|
for _, want := range tt.wantContains {
|
||||||
|
|
@ -179,34 +180,34 @@ func TestFrontMatterBytesEmptyTags(t *testing.T) {
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
fileType FileType
|
fileType metadata.FileType
|
||||||
wantTags string
|
wantTags string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "org with empty tags",
|
name: "org with empty tags",
|
||||||
fileType: FileTypeOrg,
|
fileType: metadata.FileTypeOrg,
|
||||||
wantTags: "#+filetags:",
|
wantTags: "#+filetags:",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "md-yaml with empty tags",
|
name: "md-yaml with empty tags",
|
||||||
fileType: FileTypeMdYaml,
|
fileType: metadata.FileTypeMdYaml,
|
||||||
wantTags: "tags:",
|
wantTags: "tags:",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "md-toml with empty tags",
|
name: "md-toml with empty tags",
|
||||||
fileType: FileTypeMdToml,
|
fileType: metadata.FileTypeMdToml,
|
||||||
wantTags: "tags =",
|
wantTags: "tags =",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "txt with empty tags",
|
name: "txt with empty tags",
|
||||||
fileType: FileTypeTxt,
|
fileType: metadata.FileTypeTxt,
|
||||||
wantTags: "tags:",
|
wantTags: "tags:",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
fm := New(title, "", tags, identifier)
|
fm := metadata.NewFrontMatter(title, "", tags, identifier)
|
||||||
got := string(Marshal(fm, tt.fileType))
|
got := string(Marshal(fm, tt.fileType))
|
||||||
|
|
||||||
if !strings.Contains(got, tt.wantTags) {
|
if !strings.Contains(got, tt.wantTags) {
|
||||||
|
|
@ -226,7 +227,7 @@ func TestUnmarshal(t *testing.T) {
|
||||||
wantTitle string
|
wantTitle string
|
||||||
wantTags []string
|
wantTags []string
|
||||||
wantIdentifier string
|
wantIdentifier string
|
||||||
wantFileType FileType
|
wantFileType metadata.FileType
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "org format",
|
name: "org format",
|
||||||
|
|
@ -240,7 +241,7 @@ func TestUnmarshal(t *testing.T) {
|
||||||
wantTitle: "Org Note",
|
wantTitle: "Org Note",
|
||||||
wantTags: []string{"work", "emacs"},
|
wantTags: []string{"work", "emacs"},
|
||||||
wantIdentifier: "20240101T120000",
|
wantIdentifier: "20240101T120000",
|
||||||
wantFileType: FileTypeOrg,
|
wantFileType: metadata.FileTypeOrg,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "org with single tag",
|
name: "org with single tag",
|
||||||
|
|
@ -251,7 +252,7 @@ func TestUnmarshal(t *testing.T) {
|
||||||
wantTitle: "Single Tag",
|
wantTitle: "Single Tag",
|
||||||
wantTags: []string{"single"},
|
wantTags: []string{"single"},
|
||||||
wantIdentifier: "20240101T120000",
|
wantIdentifier: "20240101T120000",
|
||||||
wantFileType: FileTypeOrg,
|
wantFileType: metadata.FileTypeOrg,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "org without tags",
|
name: "org without tags",
|
||||||
|
|
@ -261,7 +262,7 @@ func TestUnmarshal(t *testing.T) {
|
||||||
wantTitle: "No Tags",
|
wantTitle: "No Tags",
|
||||||
wantTags: nil,
|
wantTags: nil,
|
||||||
wantIdentifier: "20240101T120000",
|
wantIdentifier: "20240101T120000",
|
||||||
wantFileType: FileTypeOrg,
|
wantFileType: metadata.FileTypeOrg,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "markdown yaml",
|
name: "markdown yaml",
|
||||||
|
|
@ -277,7 +278,7 @@ identifier: 20240101T120000
|
||||||
wantTitle: "Markdown Note",
|
wantTitle: "Markdown Note",
|
||||||
wantTags: []string{"work", "personal"},
|
wantTags: []string{"work", "personal"},
|
||||||
wantIdentifier: "20240101T120000",
|
wantIdentifier: "20240101T120000",
|
||||||
wantFileType: FileTypeMdYaml,
|
wantFileType: metadata.FileTypeMdYaml,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "markdown yaml with quoted title",
|
name: "markdown yaml with quoted title",
|
||||||
|
|
@ -290,7 +291,7 @@ identifier: 20240101T120000
|
||||||
wantTitle: "Quoted Title",
|
wantTitle: "Quoted Title",
|
||||||
wantTags: []string{"test"},
|
wantTags: []string{"test"},
|
||||||
wantIdentifier: "20240101T120000",
|
wantIdentifier: "20240101T120000",
|
||||||
wantFileType: FileTypeMdYaml,
|
wantFileType: metadata.FileTypeMdYaml,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "markdown toml",
|
name: "markdown toml",
|
||||||
|
|
@ -306,7 +307,7 @@ Content`,
|
||||||
wantTitle: "TOML Note",
|
wantTitle: "TOML Note",
|
||||||
wantTags: []string{"rust", "go"},
|
wantTags: []string{"rust", "go"},
|
||||||
wantIdentifier: "20240101T120000",
|
wantIdentifier: "20240101T120000",
|
||||||
wantFileType: FileTypeMdToml,
|
wantFileType: metadata.FileTypeMdToml,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "txt format",
|
name: "txt format",
|
||||||
|
|
@ -321,7 +322,7 @@ Content here`,
|
||||||
wantTitle: "Plain Text",
|
wantTitle: "Plain Text",
|
||||||
wantTags: []string{"simple", "plain"},
|
wantTags: []string{"simple", "plain"},
|
||||||
wantIdentifier: "20240101T120000",
|
wantIdentifier: "20240101T120000",
|
||||||
wantFileType: FileTypeTxt,
|
wantFileType: metadata.FileTypeTxt,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "txt without tags",
|
name: "txt without tags",
|
||||||
|
|
@ -332,7 +333,7 @@ identifier: 20240101T120000
|
||||||
wantTitle: "No Tags Text",
|
wantTitle: "No Tags Text",
|
||||||
wantTags: nil,
|
wantTags: nil,
|
||||||
wantIdentifier: "20240101T120000",
|
wantIdentifier: "20240101T120000",
|
||||||
wantFileType: FileTypeTxt,
|
wantFileType: metadata.FileTypeTxt,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
package metadata
|
||||||
|
|
||||||
|
// FileType represents supported file formats for denote notes
|
||||||
|
type FileType string
|
||||||
|
|
||||||
|
const (
|
||||||
|
FileTypeOrg FileType = "org"
|
||||||
|
FileTypeMdYaml FileType = "md-yaml"
|
||||||
|
FileTypeMdToml FileType = "md-toml"
|
||||||
|
FileTypeTxt FileType = "txt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// fileExtensions contains the list of file extensions
|
||||||
|
// for which Denote should add front matter.
|
||||||
|
var fileExtensions = map[FileType]string{
|
||||||
|
FileTypeOrg: ".org",
|
||||||
|
FileTypeMdYaml: ".md",
|
||||||
|
FileTypeMdToml: ".md",
|
||||||
|
FileTypeTxt: ".txt",
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetExtension returns the file extension for a given file type.
|
||||||
|
func GetExtension(fileType FileType) string {
|
||||||
|
return fileExtensions[fileType]
|
||||||
|
}
|
||||||
|
|
||||||
|
// FrontMatter represents parsed front matter from a note
|
||||||
|
type FrontMatter struct {
|
||||||
|
Title string
|
||||||
|
Tags []string
|
||||||
|
Identifier string
|
||||||
|
Signature string
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFrontMatter creates a new FrontMatter struct from given parameters
|
||||||
|
func NewFrontMatter(title, signature string, tags []string, identifier string) *FrontMatter {
|
||||||
|
return &FrontMatter{
|
||||||
|
Title: title,
|
||||||
|
Tags: tags,
|
||||||
|
Identifier: identifier,
|
||||||
|
Signature: signature,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package metadata
|
package metadata
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"denote/pkg/encoding/frontmatter"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
@ -196,7 +195,7 @@ func formatSignature(sig string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// BuildFilename constructs a denote filename from metadata components.
|
// BuildFilename constructs a denote filename from metadata components.
|
||||||
func BuildFilename(fm *frontmatter.FrontMatter, ext string) string {
|
func BuildFilename(fm *FrontMatter, ext string) string {
|
||||||
titleSlug := slugifyTitle(fm.Title)
|
titleSlug := slugifyTitle(fm.Title)
|
||||||
signaturePart := formatSignature(fm.Signature)
|
signaturePart := formatSignature(fm.Signature)
|
||||||
keywordsPart := formatKeywords(fm.Tags)
|
keywordsPart := formatKeywords(fm.Tags)
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
package metadata
|
package metadata
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"denote/pkg/encoding/frontmatter"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"slices"
|
"slices"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -216,7 +215,7 @@ func TestBuildFilename(t *testing.T) {
|
||||||
keywords []string
|
keywords []string
|
||||||
ext string
|
ext string
|
||||||
want string
|
want string
|
||||||
ftype frontmatter.FileType
|
ftype FileType
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "complete filename with keywords",
|
name: "complete filename with keywords",
|
||||||
|
|
@ -226,7 +225,7 @@ func TestBuildFilename(t *testing.T) {
|
||||||
keywords: []string{"tag1", "tag2"},
|
keywords: []string{"tag1", "tag2"},
|
||||||
ext: ".md",
|
ext: ".md",
|
||||||
want: "20231225T120000--my-title__tag1_tag2.md",
|
want: "20231225T120000--my-title__tag1_tag2.md",
|
||||||
ftype: frontmatter.FileTypeMdYaml,
|
ftype: FileTypeMdYaml,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "filename without keywords",
|
name: "filename without keywords",
|
||||||
|
|
@ -236,7 +235,7 @@ func TestBuildFilename(t *testing.T) {
|
||||||
keywords: []string{},
|
keywords: []string{},
|
||||||
ext: ".md",
|
ext: ".md",
|
||||||
want: "20231225T120000--my-title.md",
|
want: "20231225T120000--my-title.md",
|
||||||
ftype: frontmatter.FileTypeMdYaml,
|
ftype: FileTypeMdYaml,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "filename with signature",
|
name: "filename with signature",
|
||||||
|
|
@ -246,7 +245,7 @@ func TestBuildFilename(t *testing.T) {
|
||||||
keywords: []string{"tag1"},
|
keywords: []string{"tag1"},
|
||||||
ext: ".md",
|
ext: ".md",
|
||||||
want: "20231225T120000==hello--my-title__tag1.md",
|
want: "20231225T120000==hello--my-title__tag1.md",
|
||||||
ftype: frontmatter.FileTypeMdYaml,
|
ftype: FileTypeMdYaml,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "filename with signature and no keywords",
|
name: "filename with signature and no keywords",
|
||||||
|
|
@ -256,7 +255,7 @@ func TestBuildFilename(t *testing.T) {
|
||||||
keywords: []string{},
|
keywords: []string{},
|
||||||
ext: ".md",
|
ext: ".md",
|
||||||
want: "20231225T120000==test--my-title.md",
|
want: "20231225T120000==test--my-title.md",
|
||||||
ftype: frontmatter.FileTypeMdYaml,
|
ftype: FileTypeMdYaml,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "filename with multi-part signature",
|
name: "filename with multi-part signature",
|
||||||
|
|
@ -266,7 +265,7 @@ func TestBuildFilename(t *testing.T) {
|
||||||
keywords: []string{"work"},
|
keywords: []string{"work"},
|
||||||
ext: ".md",
|
ext: ".md",
|
||||||
want: "20231225T120000==a==b--my-title__work.md",
|
want: "20231225T120000==a==b--my-title__work.md",
|
||||||
ftype: frontmatter.FileTypeMdYaml,
|
ftype: FileTypeMdYaml,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "filename with special chars in title",
|
name: "filename with special chars in title",
|
||||||
|
|
@ -276,7 +275,7 @@ func TestBuildFilename(t *testing.T) {
|
||||||
keywords: []string{"work"},
|
keywords: []string{"work"},
|
||||||
ext: ".org",
|
ext: ".org",
|
||||||
want: "20231225T120000--special-title__work.org",
|
want: "20231225T120000--special-title__work.org",
|
||||||
ftype: frontmatter.FileTypeOrg,
|
ftype: FileTypeOrg,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "org format",
|
name: "org format",
|
||||||
|
|
@ -286,7 +285,7 @@ func TestBuildFilename(t *testing.T) {
|
||||||
keywords: []string{"emacs"},
|
keywords: []string{"emacs"},
|
||||||
ext: ".org",
|
ext: ".org",
|
||||||
want: "20240101T000000--org-note__emacs.org",
|
want: "20240101T000000--org-note__emacs.org",
|
||||||
ftype: frontmatter.FileTypeOrg,
|
ftype: FileTypeOrg,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "txt format",
|
name: "txt format",
|
||||||
|
|
@ -296,13 +295,13 @@ func TestBuildFilename(t *testing.T) {
|
||||||
keywords: []string{},
|
keywords: []string{},
|
||||||
ext: ".txt",
|
ext: ".txt",
|
||||||
want: "20240101T000000--plain-text.txt",
|
want: "20240101T000000--plain-text.txt",
|
||||||
ftype: frontmatter.FileTypeTxt,
|
ftype: FileTypeTxt,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
fm := frontmatter.New(tt.title, tt.signature, tt.keywords, tt.identifier)
|
fm := NewFrontMatter(tt.title, tt.signature, tt.keywords, tt.identifier)
|
||||||
got := BuildFilename(fm, tt.ext)
|
got := BuildFilename(fm, tt.ext)
|
||||||
if got != tt.want {
|
if got != tt.want {
|
||||||
t.Errorf("BuildFilename() = %q, want %q", got, tt.want)
|
t.Errorf("BuildFilename() = %q, want %q", got, tt.want)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"denote/pkg/encoding/frontmatter"
|
"denote/pkg/encoding/frontmatter"
|
||||||
|
"denote/pkg/metadata"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
@ -9,13 +10,13 @@ import (
|
||||||
|
|
||||||
// Apply applies front matter to file content, replacing existing front matter if present.
|
// Apply applies front matter to file content, replacing existing front matter if present.
|
||||||
// originalContent is the current file content, fm is the new front matter to apply.
|
// originalContent is the current file content, fm is the new front matter to apply.
|
||||||
func Apply(originalContent string, fm *frontmatter.FrontMatter, fileType frontmatter.FileType) (string, error) {
|
func Apply(originalContent string, fm *metadata.FrontMatter, fileType metadata.FileType) (string, error) {
|
||||||
text := originalContent
|
text := originalContent
|
||||||
newFrontMatter := string(frontmatter.Marshal(fm, fileType))
|
newFrontMatter := string(frontmatter.Marshal(fm, fileType))
|
||||||
|
|
||||||
var newText string
|
var newText string
|
||||||
switch fileType {
|
switch fileType {
|
||||||
case frontmatter.FileTypeOrg:
|
case metadata.FileTypeOrg:
|
||||||
// Find end of front matter (first blank line or non-#+ line)
|
// Find end of front matter (first blank line or non-#+ line)
|
||||||
lines := strings.Split(text, "\n")
|
lines := strings.Split(text, "\n")
|
||||||
endIdx := 0
|
endIdx := 0
|
||||||
|
|
@ -35,7 +36,7 @@ func Apply(originalContent string, fm *frontmatter.FrontMatter, fileType frontma
|
||||||
newText = newFrontMatter + text
|
newText = newFrontMatter + text
|
||||||
}
|
}
|
||||||
|
|
||||||
case frontmatter.FileTypeMdYaml:
|
case metadata.FileTypeMdYaml:
|
||||||
// Replace YAML front matter (match trailing blank lines to avoid duplication)
|
// Replace YAML front matter (match trailing blank lines to avoid duplication)
|
||||||
re := regexp.MustCompile(`(?s)^---\n.*?\n---\n\n*`)
|
re := regexp.MustCompile(`(?s)^---\n.*?\n---\n\n*`)
|
||||||
if re.MatchString(text) {
|
if re.MatchString(text) {
|
||||||
|
|
@ -44,7 +45,7 @@ func Apply(originalContent string, fm *frontmatter.FrontMatter, fileType frontma
|
||||||
newText = newFrontMatter + text
|
newText = newFrontMatter + text
|
||||||
}
|
}
|
||||||
|
|
||||||
case frontmatter.FileTypeMdToml:
|
case metadata.FileTypeMdToml:
|
||||||
// Replace TOML front matter (match trailing blank lines to avoid duplication)
|
// Replace TOML front matter (match trailing blank lines to avoid duplication)
|
||||||
re := regexp.MustCompile(`(?s)^\+\+\+\n.*?\n\+\+\+\n\n*`)
|
re := regexp.MustCompile(`(?s)^\+\+\+\n.*?\n\+\+\+\n\n*`)
|
||||||
if re.MatchString(text) {
|
if re.MatchString(text) {
|
||||||
|
|
@ -53,7 +54,7 @@ func Apply(originalContent string, fm *frontmatter.FrontMatter, fileType frontma
|
||||||
newText = newFrontMatter + text
|
newText = newFrontMatter + text
|
||||||
}
|
}
|
||||||
|
|
||||||
case frontmatter.FileTypeTxt:
|
case metadata.FileTypeTxt:
|
||||||
// Replace text front matter (match trailing blank lines to avoid duplication)
|
// Replace text front matter (match trailing blank lines to avoid duplication)
|
||||||
re := regexp.MustCompile(`(?s)^title:.*?\n-+\n\n*`)
|
re := regexp.MustCompile(`(?s)^title:.*?\n-+\n\n*`)
|
||||||
if re.MatchString(text) {
|
if re.MatchString(text) {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"denote/pkg/encoding/frontmatter"
|
"denote/pkg/metadata"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
@ -10,8 +10,8 @@ func TestApply(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
original string
|
original string
|
||||||
fm *frontmatter.FrontMatter
|
fm *metadata.FrontMatter
|
||||||
fileType frontmatter.FileType
|
fileType metadata.FileType
|
||||||
wantContains []string
|
wantContains []string
|
||||||
wantPreserve string
|
wantPreserve string
|
||||||
}{
|
}{
|
||||||
|
|
@ -24,12 +24,12 @@ func TestApply(t *testing.T) {
|
||||||
|
|
||||||
* Original Content
|
* Original Content
|
||||||
This should be preserved`,
|
This should be preserved`,
|
||||||
fm: &frontmatter.FrontMatter{
|
fm: &metadata.FrontMatter{
|
||||||
Title: "New Title",
|
Title: "New Title",
|
||||||
Tags: []string{"new", "updated"},
|
Tags: []string{"new", "updated"},
|
||||||
Identifier: "20240101T120000",
|
Identifier: "20240101T120000",
|
||||||
},
|
},
|
||||||
fileType: frontmatter.FileTypeOrg,
|
fileType: metadata.FileTypeOrg,
|
||||||
wantContains: []string{
|
wantContains: []string{
|
||||||
"#+title: New Title",
|
"#+title: New Title",
|
||||||
"#+filetags: :new:updated:",
|
"#+filetags: :new:updated:",
|
||||||
|
|
@ -48,12 +48,12 @@ identifier: 20240101T120000
|
||||||
|
|
||||||
# Original Heading
|
# Original Heading
|
||||||
Content preserved`,
|
Content preserved`,
|
||||||
fm: &frontmatter.FrontMatter{
|
fm: &metadata.FrontMatter{
|
||||||
Title: "New Title",
|
Title: "New Title",
|
||||||
Tags: []string{"new"},
|
Tags: []string{"new"},
|
||||||
Identifier: "20240101T120000",
|
Identifier: "20240101T120000",
|
||||||
},
|
},
|
||||||
fileType: frontmatter.FileTypeMdYaml,
|
fileType: metadata.FileTypeMdYaml,
|
||||||
wantContains: []string{
|
wantContains: []string{
|
||||||
"---",
|
"---",
|
||||||
"title: New Title",
|
"title: New Title",
|
||||||
|
|
@ -72,12 +72,12 @@ identifier = 20240101T120000
|
||||||
+++
|
+++
|
||||||
|
|
||||||
Content here`,
|
Content here`,
|
||||||
fm: &frontmatter.FrontMatter{
|
fm: &metadata.FrontMatter{
|
||||||
Title: "New Title",
|
Title: "New Title",
|
||||||
Tags: []string{"updated"},
|
Tags: []string{"updated"},
|
||||||
Identifier: "20240101T120000",
|
Identifier: "20240101T120000",
|
||||||
},
|
},
|
||||||
fileType: frontmatter.FileTypeMdToml,
|
fileType: metadata.FileTypeMdToml,
|
||||||
wantContains: []string{
|
wantContains: []string{
|
||||||
"+++",
|
"+++",
|
||||||
"title = New Title",
|
"title = New Title",
|
||||||
|
|
@ -94,12 +94,12 @@ identifier: 20240101T120000
|
||||||
---------------------------
|
---------------------------
|
||||||
|
|
||||||
Text content`,
|
Text content`,
|
||||||
fm: &frontmatter.FrontMatter{
|
fm: &metadata.FrontMatter{
|
||||||
Title: "New Title",
|
Title: "New Title",
|
||||||
Tags: []string{"new"},
|
Tags: []string{"new"},
|
||||||
Identifier: "20240101T120000",
|
Identifier: "20240101T120000",
|
||||||
},
|
},
|
||||||
fileType: frontmatter.FileTypeTxt,
|
fileType: metadata.FileTypeTxt,
|
||||||
wantContains: []string{
|
wantContains: []string{
|
||||||
"title: New Title",
|
"title: New Title",
|
||||||
"tags: new",
|
"tags: new",
|
||||||
|
|
@ -111,12 +111,12 @@ Text content`,
|
||||||
{
|
{
|
||||||
name: "add front matter when missing (org)",
|
name: "add front matter when missing (org)",
|
||||||
original: `* Original Heading`,
|
original: `* Original Heading`,
|
||||||
fm: &frontmatter.FrontMatter{
|
fm: &metadata.FrontMatter{
|
||||||
Title: "Added Title",
|
Title: "Added Title",
|
||||||
Tags: []string{"added"},
|
Tags: []string{"added"},
|
||||||
Identifier: "20240101T120000",
|
Identifier: "20240101T120000",
|
||||||
},
|
},
|
||||||
fileType: frontmatter.FileTypeOrg,
|
fileType: metadata.FileTypeOrg,
|
||||||
wantContains: []string{
|
wantContains: []string{
|
||||||
"#+title: Added Title",
|
"#+title: Added Title",
|
||||||
"#+filetags: :added:",
|
"#+filetags: :added:",
|
||||||
|
|
@ -127,12 +127,12 @@ Text content`,
|
||||||
{
|
{
|
||||||
name: "add front matter when missing (md-yaml)",
|
name: "add front matter when missing (md-yaml)",
|
||||||
original: `# Original Heading`,
|
original: `# Original Heading`,
|
||||||
fm: &frontmatter.FrontMatter{
|
fm: &metadata.FrontMatter{
|
||||||
Title: "Added Title",
|
Title: "Added Title",
|
||||||
Tags: []string{"added"},
|
Tags: []string{"added"},
|
||||||
Identifier: "20240101T120000",
|
Identifier: "20240101T120000",
|
||||||
},
|
},
|
||||||
fileType: frontmatter.FileTypeMdYaml,
|
fileType: metadata.FileTypeMdYaml,
|
||||||
wantContains: []string{
|
wantContains: []string{
|
||||||
"---",
|
"---",
|
||||||
"title: Added Title",
|
"title: Added Title",
|
||||||
|
|
@ -174,13 +174,13 @@ identifier: 20240101T120000
|
||||||
|
|
||||||
Content`
|
Content`
|
||||||
|
|
||||||
fm := &frontmatter.FrontMatter{
|
fm := &metadata.FrontMatter{
|
||||||
Title: "Test",
|
Title: "Test",
|
||||||
Tags: []string{},
|
Tags: []string{},
|
||||||
Identifier: "20240101T120000",
|
Identifier: "20240101T120000",
|
||||||
}
|
}
|
||||||
|
|
||||||
got, err := Apply(original, fm, frontmatter.FileTypeMdYaml)
|
got, err := Apply(original, fm, metadata.FileTypeMdYaml)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Apply() error = %v", err)
|
t.Fatalf("Apply() error = %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -198,7 +198,7 @@ Content`
|
||||||
|
|
||||||
// TestApplyUnsupportedType validates error handling
|
// TestApplyUnsupportedType validates error handling
|
||||||
func TestApplyUnsupportedType(t *testing.T) {
|
func TestApplyUnsupportedType(t *testing.T) {
|
||||||
fm := &frontmatter.FrontMatter{
|
fm := &metadata.FrontMatter{
|
||||||
Title: "Test",
|
Title: "Test",
|
||||||
Tags: []string{"test"},
|
Tags: []string{"test"},
|
||||||
Identifier: "20240101T120000",
|
Identifier: "20240101T120000",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue