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