permalink

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package permalink 集中管理永久链接的生成、解析与运行时配置。

技术方案说明(记录在包内,便于后续维护):

  1. 站点允许配置“文章永久链接结构”以及“分类/标签页面前缀”,页面仍然保持单段 /{slug},这样可以继续复用现有页面模型与导航逻辑。
  2. 文章永久链接结构兼容一组常见 WordPress 占位符: %year% %monthnum% %day% %hour% %minute% %second% %post_id% %postname% %category% %author% 同时允许夹杂任意固定文本,比如 /%year%%post_id%.html。
  3. 包内部把结构串编译成两份产物: - 生成模板:渲染文章对象时按占位符替换; - 解析正则:收到请求路径时反向提取出 post_id、postname、日期、分类、作者等条件。
  4. 解析出来的结构化条件会交给 store 层查询数据库,真正决定“这条 URL 指向哪篇文章”的 不是 nginx/apache,而是应用自己的路由 + 规则解析 + 数据库查询。对于 Go 服务来说, 只要请求最终都进了当前进程,就完全可以自行实现 WordPress 风格固定链接。
  5. 为了兼容模板、feed、评论跳转、导出等到处都会生成文章 URL 的场景,这个包维护一份 进程级当前规则。启动时从 settings 载入;后台修改设置后立即刷新。这样整个进程内的 链接生成与请求解析始终使用同一份规则。
  6. 当前 %category% 取“文章主分类”的 slug(按分类 ID 最小值稳定选择),不展开父分类 层级路径;%author% 取后台用户的 username;%postname% 取文章 slug。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Category

func Category(slug string) string

Category 返回分类页路径。

func CurrentCategoryPrefix

func CurrentCategoryPrefix() string

func CurrentPatternUsesToken

func CurrentPatternUsesToken(token string) bool

CurrentPatternUsesToken 判断当前规则是否使用某个占位符。

func CurrentPostPattern

func CurrentPostPattern() string

CurrentPostPattern 返回当前正在使用的文章固定链接结构。

func CurrentTagPrefix

func CurrentTagPrefix() string

func NormalizePostPattern

func NormalizePostPattern(raw string) string

NormalizePostPattern 归一化固定链接结构。

func NormalizeTaxonomyPrefix

func NormalizeTaxonomyPrefix(raw, def string) string

func Page

func Page(p *model.Post) string

Page 返回页面的永久链接路径,如 /about。

func ParseCategoryPath

func ParseCategoryPath(path string) (slug string, ok bool)

func ParseTagPath

func ParseTagPath(path string) (slug string, ok bool)

func Post

func Post(p *model.Post) string

Post 返回文章或页面的永久链接路径。

func SetPostPattern

func SetPostPattern(raw string)

SetPostPattern 更新当前进程使用的文章固定链接结构。 如果传入值非法,则回退为默认结构。

func SetTaxonomyPrefixes

func SetTaxonomyPrefixes(categoryPrefix, tagPrefix string)

func Tag

func Tag(slug string) string

Tag 返回标签页路径。

func ValidatePostPattern

func ValidatePostPattern(raw string) error

ValidatePostPattern 校验固定链接结构是否合法。

func ValidateTaxonomyPrefix

func ValidateTaxonomyPrefix(raw string) error

Types

type PostPathMatch

type PostPathMatch struct {
	Year      int
	Month     int
	Day       int
	Hour      int
	Minute    int
	Second    int
	PostID    uint
	PostName  string
	Category  string
	Author    string
	HasYear   bool
	HasMonth  bool
	HasDay    bool
	HasHour   bool
	HasMinute bool
	HasSecond bool
	HasPostID bool
	HasName   bool
	HasCat    bool
	HasAuthor bool
}

PostPathMatch 是把请求路径按当前固定链接规则解析后的结果。

func ParsePostPath

func ParsePostPath(path string) (*PostPathMatch, bool)

ParsePostPath 按当前固定链接规则解析文章路径。

Jump to

Keyboard shortcuts

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