seo.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 global SEO name
  38. ~~~go
  39. collection.SetGlobalName("My Global SEO")
  40. ~~~
  41. ### Change the default context db key
  42. ~~~go
  43. collection.SetDBContextKey("My DB")
  44. ~~~
  45. ### Change the default SEO name
  46. ~~~go
  47. collection.RegisterSEO(&Product{}).SetName("My Product")
  48. ~~~
  49. ### Register customized variables
  50. ~~~go
  51. collection.RegisterSEO(&Product{}).
  52. RegisterContextVariables("og:image", func(obj interface{}, _ *Setting, _ *http.Request) string {
  53. // this will render "og:image" with the value of the object in the current request
  54. return obj.image.url
  55. }).
  56. RegisterContextVariables("Name", func(obj interface{}, _ *Setting, _ *http.Request) string {
  57. return obj.Name
  58. })
  59. ~~~
  60. ### Register setting variable
  61. This variable will be saved in the database and available as a global variable while editing SEO settings.
  62. ~~~go
  63. collection.RegisterSEO(&Product{}).RegisterSettingVaribles(struct{ProductTag string}{})
  64. ~~~
  65. ### Render SEO html data
  66. ~~~go
  67. // Render Global SEO
  68. collection.RenderGlobal(request)
  69. // Render SEO by name
  70. collection.Render("product", request)
  71. // Render SEO by model
  72. collection.Render(Product{}, request)
  73. ~~~
  74. ## Customization
  75. `),
  76. Markdown(`
  77. You can customize your SEO settings by implementing the interface and adding functions such as l10n and publish.`),
  78. ch.Code(generated.QorSEOSettingInterface).Language("go"),
  79. Markdown(`
  80. Suppose ~~MySEOSetting~~ implemented the above interface
  81. ~~~go
  82. type MySEOSetting struct{
  83. QorSEOSetting
  84. // publish
  85. // l10n
  86. }
  87. ~~~
  88. Use ~~SetSettingModel~~ function to set it
  89. ~~~go
  90. collection.SetSettingModel(&MySEOSetting{})
  91. ~~~`),
  92. htmlgo.H2("Example"),
  93. ch.Code(generated.SeoExample).Language("go"),
  94. Markdown(`
  95. ## Definition
  96. ~~Collection~~ manages all the registered models and hold global seo settings.`),
  97. ch.Code(generated.SeoCollectionDefinition).Language("go"),
  98. Markdown(`
  99. ~~SEO~~ provides system-level default page matadata.`),
  100. ch.Code(generated.SeoDefinition).Language("go"),
  101. Markdown(`
  102. You can use seo setting at the model level, but you need to register the model to the system SEO`),
  103. ch.Code(generated.SeoModelExample).Language("go"),
  104. ch.Code(`collection.RegisterSEO(&Product{})`).Language("go"),
  105. ).Title("SEO")