Documentation
¶
Overview ¶
Package seo 提供搜索引擎与订阅源的输出。
分两部分:
- 根路径上的 robots.txt、sitemap.xml、feed.xml、atom.xml —— 非 JSON 且必须位于根路径, 经 app.Router.Raw 注册;
- Public 平面上的 SEO 元信息端点,供主题渲染 head 时取 canonical、OpenGraph 与 JSON-LD。
无数据库表,故不实现 Migrator。依赖 content 模块的 posts 表与 settings 模块的站点设置。
Index ¶
- Constants
- Variables
- func ArticleJSONLD(a *Article) map[string]any
- func WebSiteJSONLD(title, siteURL, description string) map[string]any
- type Article
- type Entry
- type Meta
- type Module
- type Service
- func (s *Service) BuildMeta(ctx context.Context, kind, slug string, overrides map[string]any) (*Meta, error)
- func (s *Service) Feed(ctx context.Context, kind string) ([]byte, error)
- func (s *Service) Invalidate()
- func (s *Service) Robots(ctx context.Context) []byte
- func (s *Service) SEO(ctx context.Context) Settings
- func (s *Service) Site(ctx context.Context) SiteInfo
- func (s *Service) Sitemap(ctx context.Context) ([]byte, error)
- type Settings
- type SiteInfo
- type Store
- func (s *Store) GetPost(ctx context.Context, kind, slug string) (*Entry, error)
- func (s *Store) ListAll(ctx context.Context, limit int) ([]Entry, error)
- func (s *Store) ListCategories(ctx context.Context) ([]Entry, error)
- func (s *Store) ListPosts(ctx context.Context, limit int) ([]Entry, error)
- func (s *Store) ListTags(ctx context.Context) ([]Entry, error)
Constants ¶
const ( PathRobots = "/robots.txt" PathSitemap = "/sitemap.xml" PathFeed = "/feed.xml" PathAtom = "/atom.xml" )
根路径上的文档地址。
const GroupSEO = "seo"
GroupSEO 是 SEO 设置分组。
const Name = "seo"
Name 是模块名。
Variables ¶
var ErrNoSiteURL = errors.New("站点未配置对外地址(site.url),无法生成绝对链接")
ErrNoSiteURL 表示站点未配置对外地址。
sitemap 与订阅源要求绝对地址,站点地址缺失时只能报错: 编一个相对地址出来的文档,爬虫会直接丢掉。
var ErrNotFound = errors.New("内容不存在")
ErrNotFound 表示内容不存在。
Functions ¶
func ArticleJSONLD ¶ added in v0.1.1
ArticleJSONLD 组装 schema.org 的 Article 结构化数据。
func WebSiteJSONLD ¶ added in v0.1.1
WebSiteJSONLD 组装站点级的 WebSite 结构化数据,供首页使用。
带 SearchAction:站内搜索是固定路由,声明出来搜索引擎才可能给出搜索框。
Types ¶
type Article ¶ added in v0.1.1
type Article struct {
Title string
Description string
Canonical string
Image string
AuthorName string
SiteTitle string
// PublishedAt 与 UpdatedAt 是 RFC 3339 字符串,空则不输出对应字段。
PublishedAt string
UpdatedAt string
}
Article 是构造 Article 结构化数据所需的信息。
独立于 Entry:主题渲染时手里已经有内容视图,不该为了一段 JSON-LD 再查一次库。
type Entry ¶
type Entry struct {
ID int64 `bun:"id"`
Type string `bun:"type"`
Title string `bun:"title"`
Slug string `bun:"slug"`
Excerpt string `bun:"excerpt"`
Content string `bun:"content"`
PublishedAt time.Time `bun:"published_at"`
UpdatedAt time.Time `bun:"updated_at"`
AuthorName string `bun:"author_name"`
// URL 由服务层按站点地址与路径前缀补全。
URL string `bun:"-"`
// Summary 是摘要;订阅源在非全文模式下用它。
Summary string `bun:"-"`
// ContentHTML 是全文;订阅源在全文模式下用它。
ContentHTML string `bun:"-"`
}
Entry 是一条可供索引或订阅的内容记录。
直接用 bun 读 posts 表:SEO 只关心标题、摘要、发布时间与更新时间, 为此引入对 content 模块的编译期依赖并不划算。
type Meta ¶
type Meta struct {
Title string `json:"title"`
Description string `json:"description"`
Canonical string `json:"canonical"`
Type string `json:"type" doc:"OpenGraph 的 og:type"`
Image string `json:"image,omitempty"`
PublishedAt string `json:"publishedAt,omitempty"`
UpdatedAt string `json:"updatedAt,omitempty"`
Author string `json:"author,omitempty"`
SiteName string `json:"siteName"`
Locale string `json:"locale"`
TwitterSite string `json:"twitterSite,omitempty"`
// JSONLD 是可直接内联进 <script type="application/ld+json"> 的结构化数据。
JSONLD map[string]any `json:"jsonld,omitempty"`
}
Meta 描述一条内容的搜索引擎元信息,供主题渲染 head。
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module 是 SEO 模块。
func (*Module) Settings ¶
func (m *Module) Settings() []app.SettingGroup
Settings 实现 app.SettingsProvider。
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service 生成 sitemap、robots 与订阅源。
func NewService ¶
NewService 构造 Service。settings 为 nil 时全部退回缺省值。
func (*Service) BuildMeta ¶
func (s *Service) BuildMeta(ctx context.Context, kind, slug string, overrides map[string]any) (*Meta, error)
BuildMeta 组装一条内容的 SEO 元信息。
内容里写过的 meta 覆写优先于设置里的默认值:站长对单篇内容的判断比全局默认更准。
type Settings ¶
type Settings struct {
TitleSuffix string `json:"titleSuffix"`
DefaultDesc string `json:"defaultDescription"`
DefaultImage string `json:"defaultImage"`
TwitterSite string `json:"twitterSite"`
RobotsExtra string `json:"robotsExtra"`
SitemapEnabled bool `json:"sitemapEnabled"`
FeedEnabled bool `json:"feedEnabled"`
FeedSize int `json:"feedSize"`
FeedFullText bool `json:"feedFullText"`
IncludePages bool `json:"includePages"`
}
Settings 是 seo 分组的有效值。
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store 提供内容元数据的只读查询。
func (*Store) ListCategories ¶
ListCategories 返回全部分类,供 sitemap 使用。