Skip to content

Instantly share code, notes, and snippets.

@AvidDabbler
Last active September 12, 2024 21:07
Show Gist options
  • Save AvidDabbler/df3705ff1c77c7646c49458a69b98f2b to your computer and use it in GitHub Desktop.
Save AvidDabbler/df3705ff1c77c7646c49458a69b98f2b to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/joho/godotenv"
)
type snippetType struct {
Key string `json:"key"`
Name string `json:"name"`
Text string `json:"text"`
}
var (
Output []snippetType
SNIPPET_FOLDER string
)
func getFilesAndFolders(path string) {
entries, err := os.ReadDir(path)
if err != nil {
log.Fatal(err)
}
for _, entry := range entries {
entryPath := fmt.Sprint(path, "/", entry.Name())
if entry.IsDir() {
getFilesAndFolders(entryPath)
} else if strings.HasSuffix(entryPath, ".md") {
output, err := os.ReadFile(entryPath)
if err != nil {
log.Fatalf("issue with files: %s", entryPath)
}
text := string(output)
keys := strings.Split(entryPath, SNIPPET_FOLDER)
keys = strings.Split(keys[1], ".md")
name := keys[0]
key := strings.ReplaceAll(keys[0], " ", "-")
key = strings.ReplaceAll(key, "/", "-")
key = strings.Replace(key, "-", ":", 1)
snippet := snippetType{
Text: text,
Name: name,
Key: key,
}
Output = append(Output, snippet)
}
}
}
func main() {
godotenv.Load()
SNIPPET_FOLDER = os.Getenv("SNIPPET_FOLDER")
getFilesAndFolders(SNIPPET_FOLDER)
outputJson, err := json.Marshal(Output)
if err != nil {
log.Fatal("issue with Marshal")
}
os.WriteFile(fmt.Sprintf("%s/%s%s", SNIPPET_FOLDER, "my-snippets", ".json"), outputJson, 0666)
os.WriteFile(fmt.Sprintf("./%s%s", "my-snippets", ".json"), outputJson, 0666)
}
@AvidDabbler
Copy link
Author

just run this with a .env file with the SNIPPET_FOLDER pointing to your snippet folder in obsidian.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment