|
| 1 | +package blog |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io/fs" |
| 7 | + "net/url" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + chroma "github.com/alecthomas/chroma/v2/formatters/html" |
| 12 | + "github.com/charmbracelet/log" |
| 13 | + "github.com/yuin/goldmark" |
| 14 | + highlighting "github.com/yuin/goldmark-highlighting/v2" |
| 15 | + meta "github.com/yuin/goldmark-meta" |
| 16 | + mdParser "github.com/yuin/goldmark/parser" |
| 17 | +) |
| 18 | + |
| 19 | +type article struct { |
| 20 | + Title string |
| 21 | + Date time.Time |
| 22 | + Content []byte |
| 23 | + Uri string |
| 24 | +} |
| 25 | + |
| 26 | +// TODO: Add metadata validation |
| 27 | + |
| 28 | +func NewArticle(file fs.File) (*article, error) { |
| 29 | + |
| 30 | + var buf bytes.Buffer |
| 31 | + context, err := fileToMarkdown(file, &buf) |
| 32 | + if err != nil { |
| 33 | + return nil, fmt.Errorf("Could not convert to markdown: %s", err) |
| 34 | + } |
| 35 | + |
| 36 | + metadata := meta.Get(context) |
| 37 | + |
| 38 | + date, err := time.Parse("2006-01-02", metadata["date"].(string)) |
| 39 | + if err != nil { |
| 40 | + return nil, fmt.Errorf("Could not parse as date : %s", err) |
| 41 | + } |
| 42 | + |
| 43 | + uri, err := makeArticleUri(date, metadata) |
| 44 | + if err != nil { |
| 45 | + return nil, fmt.Errorf("Could make article uri: %s", err) |
| 46 | + } |
| 47 | + |
| 48 | + return &article{ |
| 49 | + Title: metadata["title"].(string), |
| 50 | + Date: date, |
| 51 | + Content: buf.Bytes(), |
| 52 | + Uri: uri, |
| 53 | + }, nil |
| 54 | +} |
| 55 | + |
| 56 | +func fileToMarkdown(file fs.File, buf *bytes.Buffer) (mdParser.Context, error) { |
| 57 | + info, err := file.Stat() |
| 58 | + if err != nil { |
| 59 | + return nil, fmt.Errorf("Error getting file info: %s", err) |
| 60 | + } |
| 61 | + log.Info("Opening new article", "name", info.Name) |
| 62 | + |
| 63 | + content := make([]byte, info.Size()) |
| 64 | + if _, err = file.Read(content); err != nil { |
| 65 | + return nil, fmt.Errorf("Could not read file: %s", err) |
| 66 | + } |
| 67 | + |
| 68 | + markdown := goldmark.New( |
| 69 | + goldmark.WithExtensions( |
| 70 | + highlighting.NewHighlighting( |
| 71 | + highlighting.WithStyle("catppuccin-mocha"), |
| 72 | + highlighting.WithFormatOptions(chroma.WithLineNumbers(true)), |
| 73 | + ), |
| 74 | + meta.Meta, |
| 75 | + ), |
| 76 | + ) |
| 77 | + context := mdParser.NewContext() |
| 78 | + if err = markdown.Convert(content, buf, mdParser.WithContext(context)); err != nil { |
| 79 | + return nil, fmt.Errorf("Could not converting file to markdown: %s", err) |
| 80 | + } |
| 81 | + return context, nil |
| 82 | +} |
| 83 | + |
| 84 | +func makeArticleUri(date time.Time, metadata map[string]any) (string, error) { |
| 85 | + |
| 86 | + metaUri := metadata["uri"].(string) |
| 87 | + |
| 88 | + if metaUri == "" { |
| 89 | + metaTitle := metadata["title"].(string) |
| 90 | + if metaTitle == "" { |
| 91 | + return "", fmt.Errorf("Either one of 'uri' or 'title' needs to be set") |
| 92 | + } |
| 93 | + metaUri = url.QueryEscape(strings.ReplaceAll(strings.ToLower(metaTitle), " ", "-")) |
| 94 | + } |
| 95 | + |
| 96 | + return fmt.Sprintf("%s/%s", date.Format("2006/01"), metaUri), nil |
| 97 | +} |
0 commit comments