You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

84 lines
2.3 KiB

  1. package main
  2. import (
  3. "bytes"
  4. "errors"
  5. "gopkg.in/yaml.v3"
  6. )
  7. type HugoFrontMatter struct {
  8. Title string `yaml:"title,omitempty"`
  9. Published string `yaml:"date,omitempty"`
  10. Updated string `yaml:"lastmod,omitempty"`
  11. Tags []string `yaml:"tags,omitempty"`
  12. Series []string `yaml:"series,omitempty"`
  13. ExternalURL string `yaml:"externalURL,omitempty"`
  14. IndieWeb HugoFrontMatterIndieWeb `yaml:"indieweb,omitempty"`
  15. Syndicate []string `yaml:"syndicate,omitempty"`
  16. TranslationKey string `yaml:"translationKey,omitempty"`
  17. Images []string `yaml:"images,omitempty"`
  18. Audio string `yaml:"audio,omitempty"`
  19. }
  20. type HugoFrontMatterIndieWeb struct {
  21. Reply HugoFrontMatterReply `yaml:"reply,omitempty"`
  22. Like HugoFrontMatterLike `yaml:"like,omitempty"`
  23. }
  24. type HugoFrontMatterReply struct {
  25. Link string `yaml:"link,omitempty"`
  26. Title string `yaml:"title,omitempty"`
  27. }
  28. type HugoFrontMatterLike struct {
  29. Link string `yaml:"link,omitempty"`
  30. Title string `yaml:"title,omitempty"`
  31. }
  32. func writeFrontMatter(entry *Entry) (string, error) {
  33. frontMatter := &HugoFrontMatter{
  34. Title: entry.title,
  35. Published: entry.date,
  36. Updated: entry.lastmod,
  37. Tags: entry.tags,
  38. Series: entry.series,
  39. ExternalURL: entry.link,
  40. IndieWeb: HugoFrontMatterIndieWeb{
  41. Reply: HugoFrontMatterReply{
  42. Link: entry.replyLink,
  43. Title: entry.replyTitle,
  44. },
  45. Like: HugoFrontMatterLike{
  46. Link: entry.likeLink,
  47. Title: entry.likeTitle,
  48. },
  49. },
  50. Syndicate: entry.syndicate,
  51. TranslationKey: entry.translationKey,
  52. Audio: entry.audio,
  53. }
  54. for _, image := range entry.images {
  55. frontMatter.Images = append(frontMatter.Images, image.url)
  56. }
  57. writer := new(bytes.Buffer)
  58. writer.WriteString("---\n")
  59. err := yaml.NewEncoder(writer).Encode(&frontMatter)
  60. if err != nil {
  61. return "", errors.New("failed encoding frontmatter")
  62. }
  63. writer.WriteString("---\n")
  64. return writer.String(), nil
  65. }
  66. func WriteHugoPost(entry *Entry) (string, error) {
  67. buff := new(bytes.Buffer)
  68. f, err := writeFrontMatter(entry)
  69. if err != nil {
  70. return "", err
  71. }
  72. buff.WriteString(f)
  73. buff.WriteString(entry.content)
  74. return buff.String(), nil
  75. }