Skip to content

Instantly share code, notes, and snippets.

@thrawn01
Created July 23, 2023 15:42
Show Gist options
  • Save thrawn01/1dd33f4b86242301bb2a75e0829e4ec2 to your computer and use it in GitHub Desktop.
Save thrawn01/1dd33f4b86242301bb2a75e0829e4ec2 to your computer and use it in GitHub Desktop.
Tag all daily notes with #daily-notes
// This code is used to re-tag my daily notes from front matter style to `#tag` style and add
// a #daily-notes tag to each note. I'm saving it here as it might be useful as a template for
// others or myself in the future.
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
func processFile(filename string) error {
// Read the content of the file
content, err := os.ReadFile(filename)
if err != nil {
return err
}
// Check if the file already contains the tag line
if strings.HasPrefix(string(content), "Tags:") {
fmt.Printf("Skipping %s: Tag line already exists.\n", filename)
return nil
}
content = convertTags(content)
content = addDailyNotesTag(content)
err = os.WriteFile(filename, content, 644)
if err != nil {
return err
}
fmt.Printf("Filename: %s\n", filename)
return nil
}
func traverseDirectory(dirPath string) {
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// Skip directories or files that are not regular files
if !info.Mode().IsRegular() {
return nil
}
// Process only files with specific extensions (e.g., .txt, .md, etc.)
extensions := []string{".txt", ".md"}
for _, ext := range extensions {
if strings.HasSuffix(path, ext) {
if err := processFile(path); err != nil {
fmt.Printf("Error processing %s: %s\n", path, err)
}
break
}
}
return nil
})
if err != nil {
fmt.Println("Error traversing directory:", err)
}
}
func addDailyNotesTag(data []byte) []byte {
// Define the regular expression pattern to match the "Tags:" line
pattern := `(?m)^Tags:(.*)$`
// Compile the regular expression pattern
re := regexp.MustCompile(pattern)
// Find the "Tags:" line in the data
matches := re.FindSubmatch(data)
if len(matches) == 2 {
// Append "#daily-notes" to the existing "Tags:" line
dailyNotesTag := []byte("#daily-notes")
newLine := append(matches[1], ' ')
newLine = append(newLine, dailyNotesTag...)
data = re.ReplaceAll(data, []byte("Tags:"+string(newLine)))
} else {
// Prepend "Tags: #daily-notes" to the beginning of the data
data = append([]byte("Tags: #daily-notes\n\n"), data...)
}
return data
}
func convertTags(input []byte) []byte {
// Define the regular expression pattern to match the tags
pattern := `---\s*tags:\s*(.*?)\s*---`
// Compile the regular expression pattern
re := regexp.MustCompile(pattern)
// Find the tags in the input byte slice
matches := re.FindSubmatch(input)
if len(matches) == 2 {
tags := strings.Fields(string(matches[1])) // Split the matched tags into individual words
for i, tag := range tags {
tags[i] = "#" + tag // Add "#" prefix to each tag
}
// Create the new tags line
newTagsLine := "Tags: " + strings.Join(tags, " ") + "\n"
// Replace the old tags line with the new tags line in the input byte slice
output := re.ReplaceAll(input, []byte(newTagsLine))
return output
}
return input // Return the input byte slice unchanged if no tags were found
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: go run main.go <directory_path>")
return
}
dirPath := os.Args[1]
traverseDirectory(dirPath)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment