seo.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package basics
  2. import (
  3. "github.com/qor5/docs/docsrc/generated"
  4. . "github.com/theplant/docgo"
  5. "github.com/theplant/docgo/ch"
  6. "github.com/theplant/htmlgo"
  7. )
  8. var SEO = Doc(
  9. Markdown(`
  10. The SEO library facilitates the optimization of Search Engine results by managing and injecting dynamic data into HTML tags.
  11. ## Usage
  12. Initialize a ~~Collection~~ instance. The ~~Collection~~ manages all the registered models and hold global seo settings
  13. ~~~go
  14. collection := seo.NewCollection()
  15. // Turn off the default inherit the upper level SEO data when the current SEO data is missing
  16. collection.SetInherited(false)
  17. ~~~
  18. ### Register models to SEO
  19. ~~~go
  20. // Register mutiple SEO by name
  21. collection.RegisterSEOByNames("Product", "Announcement")
  22. // Register a SEO by model
  23. type Product struct{
  24. Name string
  25. Setting Setting
  26. }
  27. collection.RegisterSEO(&Product{})
  28. ~~~
  29. ### Remove models from SEO
  30. ~~~
  31. // Remove by struct
  32. collection.RemoveSEO(&Product{})
  33. // Remove by name
  34. collection.RemoveSEO("Not Found")
  35. ~~~
  36. ## Configuration
  37. ### Change the default SEO name
  38. ~~~go
  39. collection.RegisterSEO(&Product{}).SetName("My Product")
  40. ~~~
  41. ### Register customized variables
  42. ~~~go
  43. collection.RegisterSEO(&Product{}).
  44. RegisterContextVariables("og:image", func(obj interface{}, _ *Setting, _ *http.Request) string {
  45. // this will render "og:image" with the value of the object in the current request
  46. return obj.image.url
  47. }).
  48. RegisterContextVariables("Name", func(obj interface{}, _ *Setting, _ *http.Request) string {
  49. return obj.Name
  50. })
  51. ~~~
  52. ### Register setting variable
  53. This variable will be saved in the database and available as a global variable while editing SEO settings.
  54. ~~~go
  55. collection.RegisterSEO(&Product{}).RegisterSettingVaribles(struct{ProductTag string}{})
  56. ~~~
  57. ### Render SEO html data
  58. ~~~go
  59. // Render Global SEO
  60. collection.RenderGlobal(request)
  61. // Render SEO by name
  62. collection.Render("product", request)
  63. // Render SEO by model
  64. collection.Render(Product{}, request)
  65. ~~~
  66. ## Customization
  67. `),
  68. Markdown(`
  69. You can customize your SEO settings by implementing the interface and adding functions such as l10n and publish.`),
  70. ch.Code(generated.QorSEOSettingInterface).Language("go"),
  71. htmlgo.H2("Example"),
  72. ch.Code(generated.SeoExample).Language("go"),
  73. Markdown(`
  74. ## Definition
  75. ~~Collection~~ manages all the registered models and hold global seo settings.`),
  76. ch.Code(generated.SeoCollectionDefinition).Language("go"),
  77. Markdown(`
  78. ~~SEO~~ provides system-level default page matadata.`),
  79. ch.Code(generated.SeoDefinition).Language("go"),
  80. Markdown(`
  81. You can use seo setting at the model level, but you need to register the model to the system SEO`),
  82. ch.Code(generated.SeoModelExample).Language("go"),
  83. ch.Code(`collection.RegisterSEO(&Product{})`).Language("go"),
  84. ).Title("SEO")