seo

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2026 License: AGPL-3.0 Imports: 16 Imported by: 0

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

View Source
const (
	PathRobots  = "/robots.txt"
	PathSitemap = "/sitemap.xml"
	PathFeed    = "/feed.xml"
	PathAtom    = "/atom.xml"
)

根路径上的文档地址。

View Source
const GroupSEO = "seo"

GroupSEO 是 SEO 设置分组。

View Source
const Name = "seo"

Name 是模块名。

Variables

View Source
var ErrNoSiteURL = errors.New("站点未配置对外地址(site.url),无法生成绝对链接")

ErrNoSiteURL 表示站点未配置对外地址。

sitemap 与订阅源要求绝对地址,站点地址缺失时只能报错: 编一个相对地址出来的文档,爬虫会直接丢掉。

View Source
var ErrNotFound = errors.New("内容不存在")

ErrNotFound 表示内容不存在。

Functions

func ArticleJSONLD added in v0.1.1

func ArticleJSONLD(a *Article) map[string]any

ArticleJSONLD 组装 schema.org 的 Article 结构化数据。

func WebSiteJSONLD added in v0.1.1

func WebSiteJSONLD(title, siteURL, description string) map[string]any

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 New

func New() *Module

New 构造模块。

func (*Module) Name

func (m *Module) Name() string

Name 实现 app.Module。

func (*Module) Register

func (m *Module) Register(a *app.App) error

Register 实现 app.Module:只做装配,不访问数据库。

func (*Module) Routes

func (m *Module) Routes(r app.Router)

Routes 实现 app.RouteProvider。

func (*Module) Service

func (m *Module) Service() *Service

Service 返回 SEO 服务,供其他模块(主题、站点地图扩展)取用。

func (*Module) Settings

func (m *Module) Settings() []app.SettingGroup

Settings 实现 app.SettingsProvider。

func (*Module) Start

func (m *Module) Start(context.Context) error

Start 实现 app.Starter:本模块没有需要启动的后台任务。

文档缓存按 TTL 自然过期(见 cacheTTL),不额外挂钩设置变更事件: 一分钟的延迟对爬虫与阅读器完全无感,为它引入一条跨模块的事件链不划算。

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service 生成 sitemap、robots 与订阅源。

func From

func From(a *app.App) *Service

From 取回 SEO 服务;模块未装配时返回 nil。

func NewService

func NewService(store *Store, svc *settings.Service, log logger) *Service

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 覆写优先于设置里的默认值:站长对单篇内容的判断比全局默认更准。

func (*Service) Feed

func (s *Service) Feed(ctx context.Context, kind string) ([]byte, error)

Feed 生成订阅源;kind 取 rss 或 atom。

func (*Service) Invalidate

func (s *Service) Invalidate()

Invalidate 清空缓存,供设置变更后立即生效。

func (*Service) Robots

func (s *Service) Robots(ctx context.Context) []byte

Robots 生成 robots.txt。

func (*Service) SEO

func (s *Service) SEO(ctx context.Context) Settings

SEO 读取 SEO 设置;未装配设置模块时用缺省值。

func (*Service) Site

func (s *Service) Site(ctx context.Context) SiteInfo

Site 读取站点信息。

func (*Service) Sitemap

func (s *Service) Sitemap(ctx context.Context) ([]byte, error)

Sitemap 生成 sitemap.xml。

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 SiteInfo

type SiteInfo struct {
	Title       string
	Description string
	URL         string
	Language    string
}

SiteInfo 是生成文档所需的站点信息。

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store 提供内容元数据的只读查询。

func NewStore

func NewStore(db *bun.DB) *Store

NewStore 构造 Store。

func (*Store) GetPost

func (s *Store) GetPost(ctx context.Context, kind, slug string) (*Entry, error)

GetPost 按 slug 取一条已发布且公开的内容,供生成单页 SEO 元信息。

func (*Store) ListAll

func (s *Store) ListAll(ctx context.Context, limit int) ([]Entry, error)

ListAll 返回已发布且公开的文章与页面,按发布时间倒序,供 sitemap 使用。

func (*Store) ListCategories

func (s *Store) ListCategories(ctx context.Context) ([]Entry, error)

ListCategories 返回全部分类,供 sitemap 使用。

func (*Store) ListPosts

func (s *Store) ListPosts(ctx context.Context, limit int) ([]Entry, error)

ListPosts 返回已发布且公开的文章,按发布时间倒序。

func (*Store) ListTags

func (s *Store) ListTags(ctx context.Context) ([]Entry, error)

ListTags 返回全部标签,供 sitemap 使用。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL