This is my new blog CMS https://jlelse.blog
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.

143 lines
3.6 KiB

8 months ago
8 months ago
7 months ago
8 months ago
8 months ago
8 months ago
8 months ago
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. "github.com/araddon/dateparse"
  7. "github.com/snabb/sitemap"
  8. )
  9. const sitemapPath = "/sitemap.xml"
  10. func serveSitemap(w http.ResponseWriter, r *http.Request) {
  11. sm := sitemap.New()
  12. sm.Minify = true
  13. // Blogs
  14. for b, bc := range appConfig.Blogs {
  15. // Blog
  16. blogPath := bc.Path
  17. if blogPath == "/" {
  18. blogPath = ""
  19. }
  20. sm.Add(&sitemap.URL{
  21. Loc: appConfig.Server.PublicAddress + blogPath,
  22. })
  23. // Sections
  24. for _, section := range bc.Sections {
  25. if section.Name != "" {
  26. sm.Add(&sitemap.URL{
  27. Loc: appConfig.Server.PublicAddress + bc.getRelativePath("/"+section.Name),
  28. })
  29. }
  30. }
  31. // Taxonomies
  32. for _, taxonomy := range bc.Taxonomies {
  33. if taxonomy.Name != "" {
  34. // Taxonomy
  35. taxPath := bc.getRelativePath("/" + taxonomy.Name)
  36. sm.Add(&sitemap.URL{
  37. Loc: appConfig.Server.PublicAddress + taxPath,
  38. })
  39. // Values
  40. if taxValues, err := allTaxonomyValues(b, taxonomy.Name); err == nil {
  41. for _, tv := range taxValues {
  42. sm.Add(&sitemap.URL{
  43. Loc: appConfig.Server.PublicAddress + taxPath + "/" + urlize(tv),
  44. })
  45. }
  46. }
  47. }
  48. }
  49. // Year / month archives
  50. if dates, err := allPublishedDates(b); err == nil {
  51. already := map[string]bool{}
  52. for _, d := range dates {
  53. // Year
  54. yearPath := bc.getRelativePath("/" + fmt.Sprintf("%0004d", d.year))
  55. if !already[yearPath] {
  56. sm.Add(&sitemap.URL{
  57. Loc: appConfig.Server.PublicAddress + yearPath,
  58. })
  59. already[yearPath] = true
  60. }
  61. // Specific month
  62. monthPath := yearPath + "/" + fmt.Sprintf("%02d", d.month)
  63. if !already[monthPath] {
  64. sm.Add(&sitemap.URL{
  65. Loc: appConfig.Server.PublicAddress + monthPath,
  66. })
  67. already[monthPath] = true
  68. }
  69. // Specific day
  70. dayPath := monthPath + "/" + fmt.Sprintf("%02d", d.day)
  71. if !already[dayPath] {
  72. sm.Add(&sitemap.URL{
  73. Loc: appConfig.Server.PublicAddress + dayPath,
  74. })
  75. already[dayPath] = true
  76. }
  77. // Generic month
  78. genericMonthPath := blogPath + "/x/" + fmt.Sprintf("%02d", d.month)
  79. if !already[genericMonthPath] {
  80. sm.Add(&sitemap.URL{
  81. Loc: appConfig.Server.PublicAddress + genericMonthPath,
  82. })
  83. already[genericMonthPath] = true
  84. }
  85. // Specific day
  86. genericMonthDayPath := genericMonthPath + "/" + fmt.Sprintf("%02d", d.day)
  87. if !already[genericMonthDayPath] {
  88. sm.Add(&sitemap.URL{
  89. Loc: appConfig.Server.PublicAddress + genericMonthDayPath,
  90. })
  91. already[genericMonthDayPath] = true
  92. }
  93. }
  94. }
  95. // Photos
  96. if bc.Photos != nil && bc.Photos.Enabled {
  97. sm.Add(&sitemap.URL{
  98. Loc: appConfig.Server.PublicAddress + bc.getRelativePath(bc.Photos.Path),
  99. })
  100. }
  101. // Search
  102. if bc.Search != nil && bc.Search.Enabled {
  103. sm.Add(&sitemap.URL{
  104. Loc: appConfig.Server.PublicAddress + bc.getRelativePath(bc.Search.Path),
  105. })
  106. }
  107. // Stats
  108. if bc.BlogStats != nil && bc.BlogStats.Enabled {
  109. sm.Add(&sitemap.URL{
  110. Loc: appConfig.Server.PublicAddress + bc.getRelativePath(bc.BlogStats.Path),
  111. })
  112. }
  113. // Custom pages
  114. for _, cp := range bc.CustomPages {
  115. sm.Add(&sitemap.URL{
  116. Loc: appConfig.Server.PublicAddress + cp.Path,
  117. })
  118. }
  119. }
  120. // Posts
  121. if posts, err := getPosts(&postsRequestConfig{status: statusPublished}); err == nil {
  122. for _, p := range posts {
  123. item := &sitemap.URL{Loc: p.fullURL()}
  124. var lastMod time.Time
  125. if p.Updated != "" {
  126. lastMod, _ = dateparse.ParseLocal(p.Updated)
  127. }
  128. if p.Published != "" && lastMod.IsZero() {
  129. lastMod, _ = dateparse.ParseLocal(p.Published)
  130. }
  131. if !lastMod.IsZero() {
  132. item.LastMod = &lastMod
  133. }
  134. sm.Add(item)
  135. }
  136. }
  137. // Write...
  138. _, _ = sm.WriteTo(w) // Already minified
  139. }