seo.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. htmlgo.Text("The SEO library allows for the management and injection of dynamic data into HTML tags for the purpose of Search Engine Optimisation."),
  10. htmlgo.H2("Definition"),
  11. htmlgo.H5(`Collection is used to manage all SEO and render seo setting to html data.`),
  12. ch.Code(generated.SeoCollectionDefinition).Language("go"),
  13. htmlgo.H5(`SEO is used to provide system-level default page matadata.`),
  14. ch.Code(generated.SeoDefinition).Language("go"),
  15. htmlgo.H5(`You can use seo setting at the model level, but you need to register the model to the system SEO`),
  16. ch.Code(generated.SeoModelExample).Language("go"),
  17. ch.Code(`collection.RegisterSEO(&Product{})`).Language("go"),
  18. htmlgo.H5(`Support customizing your own seo setting when you need more functions such as l10n, publish. Only need to implement this interface.`),
  19. ch.Code(generated.QorSEOSettingInterface).Language("go"),
  20. Markdown(`
  21. ## Usage
  22. - Create a SEO collection
  23. ~~~go
  24. // Create a collection and register global seo by default
  25. collection := seo.NewCollection()
  26. // Change the default global name
  27. collection.SetGlobalName("My Global SEO")
  28. // Change the default context db key
  29. collection.SetDBContextKey("My DB")
  30. // Change the default seo model setting
  31. type MySEOSetting struct{
  32. QorSEOSetting
  33. publish
  34. l10n
  35. }
  36. collection.SetSettingModel(&MySEOSetting{})
  37. // Turn off the default inherit the upper level SEO data when the current SEO data is missing
  38. collection.SetInherited(false)
  39. ~~~
  40. - Register and remove SEO
  41. ~~~go
  42. // Register mutiple SEO by name
  43. collection.RegisterSEOByNames("Not Found", "Internal Server Error")
  44. // Register a SEO by model
  45. type Product struct{
  46. Name string
  47. Setting Setting
  48. }
  49. collection.RegisterSEO(&Product{})
  50. // Remove a SEO
  51. collection.RemoveSEO(&Product{}).RemoveSEO("Not Found")
  52. ~~~
  53. - Configure SEO
  54. ~~~go
  55. // Change the default SEO name when register a SEO by model
  56. collection.RegisterSEO(&Product{}).SetName("My Product")
  57. // Register a context Variable
  58. collection.RegisterSEO(&Product{}).
  59. RegisterContextVariables("og:image", func(obj interface{}, _ *Setting, _ *http.Request) string {
  60. return obj.image.url
  61. }).
  62. RegisterContextVariables("Name", func(obj interface{}, _ *Setting, _ *http.Request) string {
  63. return obj.Name
  64. })
  65. // Register setting variable
  66. collection.RegisterSEO(&Product{}).
  67. RegisterSettingVaribles(struct{ProductTag string}{})
  68. ~~~
  69. - Render SEO html data
  70. ~~~go
  71. // Render Global SEO
  72. collection.RenderGlobal(request)
  73. // Render SEO by a name
  74. collection.Render("product", request)
  75. // Render SEO by a model
  76. collection.Render(Product{}, request)
  77. ~~~
  78. `),
  79. htmlgo.H2("Example"),
  80. ch.Code(generated.SeoExample).Language("go"),
  81. ).Title("SEO")