Documentation
¶
Index ¶
- Variables
- func EstimateReadingTime(content string) string
- func GenerateETag(content string) string
- func IsValidPostPath(path string) bool
- func PostID(postType, slug string) string
- type Author
- type AuthorLink
- type Database
- type DownCache
- func (dg *DownCache) Close() error
- func (dg *DownCache) CreatePost(postType, path, content string, meta *PostMeta) (string, error)
- func (dg *DownCache) DeIndexPost(pathID string) error
- func (dg *DownCache) DeletePost(postType, path string) error
- func (dg *DownCache) GetPost(pathID string) (*Post, error)
- func (dg *DownCache) GetPosts(filter FilterOptions) (Paginator, error)
- func (dg *DownCache) GetTaxonomyTerms(taxonomy string) ([]string, error)
- func (dg *DownCache) IndexPost(doc *Post) error
- func (dg *DownCache) IndexPostType(postType PostType) (int, error)
- func (dg *DownCache) Paginator(docs []*Post, total, currentPage, pageSize int, includeFeatured bool) Paginator
- func (dg *DownCache) Reindex() (map[string]int, error)
- func (dg *DownCache) SetFrontmatterFormat(format FrontmatterFormat)
- func (dg *DownCache) SetPostType(typeKey PostTypeKey, postType PostType) error
- func (dg *DownCache) UpdatePost(postType, path, content string, meta *PostMeta) (string, error)
- type FilterOptions
- type FilterType
- type FrontmatterFormat
- type MarkdownParserFunc
- type Options
- type Paginator
- type Post
- func (d *Post) FileTimeInSlug() string
- func (d *Post) HasAuthor() bool
- func (d *Post) HasFileTimeInSlug() bool
- func (d *Post) HasName() bool
- func (d *Post) HasPhoto() bool
- func (d *Post) HasProperties() bool
- func (d *Post) HasPublished() bool
- func (d *Post) HasSubtitle() bool
- func (d *Post) HasSummary() bool
- func (d *Post) HasTaxonomies() bool
- func (d *Post) HasTaxonomy(taxonomy string) bool
- func (d *Post) HasUpdated() bool
- func (d *Post) PublishedDate() string
- func (d *Post) PublishedYear() int
- func (d *Post) Serialize() ([]byte, error)
- func (d *Post) SlugWithYear() string
- func (d *Post) SlugWithYearMonth() string
- func (d *Post) SlugWithYearMonthDay() string
- func (d *Post) SlugWithoutDate() string
- func (d *Post) Taxonomy(taxonomy string) []string
- type PostMeta
- type PostStore
- type PostType
- type PostTypeKey
- type PostTypesMap
- type SlugPath
Constants ¶
This section is empty.
Variables ¶
var ( ErrPostExists = errors.New("post already exists") ErrPostNotFound = errors.New("post not found") ErrInvalidPostType = errors.New("invalid post type") ErrInvalidPostSlug = errors.New("invalid post slug") ErrInvalidPostMeta = errors.New("invalid post metadata") ErrMissingPostContent = errors.New("missing post content") )
Functions ¶
func EstimateReadingTime ¶
EstimateReadingTime estimates the reading time of the content.
func GenerateETag ¶
GenerateETag generates an ETag for the content.
func IsValidPostPath ¶
Types ¶
type Author ¶
type Author struct {
Username string `json:"username" yaml:"username" toml:"username"`
Name string `json:"name" yaml:"name" toml:"name"`
Country string `json:"country" yaml:"country" toml:"country"`
Active bool `json:"active" yaml:"active" toml:"active"`
Bio string `json:"bio" yaml:"bio" toml:"bio"`
AvatarURL string `json:"avatarURL" yaml:"avatarURL" toml:"avatarURL"`
Links []AuthorLink `json:"links" yaml:"links" toml:"links"`
}
type AuthorLink ¶
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
func NewDatabase ¶
type DownCache ¶
type DownCache struct {
// contains filtered or unexported fields
}
DownCache is the main struct for interacting with the DownCache library. It provides methods for indexing, querying, and managing markdown posts.
func NewDownCache ¶
NewDownCache creates a new DownCache instance with the provided options.
func (*DownCache) Close ¶
Close closes the DownCache instance by closing the bolt and bleve indexes.
func (*DownCache) CreatePost ¶
CreatePost creates a new post on the filesystem and indexes it. If the post already exists, an error will be returned.
func (*DownCache) DeIndexPost ¶
DeIndexPost removes a post from the indexes.
func (*DownCache) DeletePost ¶
DeletePost deletes a post from the filesystem and deindexes it. If the post does not exist, an error will be returned.
func (*DownCache) GetPost ¶
GetPost retrieves a post by its Path PostID. If the post does not exist, an error will be returned. The Path PostID should be the post's type (e.g. page, post) plus the post's slug, without the file extension. For example, a post with the path "post/my-first-post.md" would have a Path PostID of "post/my-first-post".
func (*DownCache) GetPosts ¶
func (dg *DownCache) GetPosts(filter FilterOptions) (Paginator, error)
GetPosts retrieves a list of posts based on the provided filter options.
func (*DownCache) GetTaxonomyTerms ¶
GetTaxonomyTerms returns a list of terms for a given taxonomy.
func (*DownCache) IndexPostType ¶
IndexPostType indexes all posts of a given type.
func (*DownCache) Paginator ¶
func (dg *DownCache) Paginator(docs []*Post, total, currentPage, pageSize int, includeFeatured bool) Paginator
Paginator returns a Paginator struct with the given parameters.
func (*DownCache) SetFrontmatterFormat ¶
func (dg *DownCache) SetFrontmatterFormat(format FrontmatterFormat)
SetFrontmatterFormat sets the format used for frontmatter in markdown files.
func (*DownCache) SetPostType ¶
func (dg *DownCache) SetPostType(typeKey PostTypeKey, postType PostType) error
SetPostType sets a PostType for a given PostTypeKey.
type FilterOptions ¶
type FilterOptions struct {
PageNum int // The page number to retrieve
PageSize int // The number of items per page
SortBy []string // The frontmatter fields to sort by. Default is ["-featured", "-published", "name]
FilterType FilterType // The type of filter to apply (author, article, taxonomy)
FilterKey string // The key to filter by (e.g. "categories", "tags"). Only used for taxonomy filters currently.
FilterTerm string // The term to filter by (e.g. "cat3", "tag3", "author1"). Used with FilterTypeAuthor and FilterTypeTaxonomy.
FilterSearch string // A search string to filter by. Searches the post content, title, etc.
FilterPostType PostTypeKey // The type of post to filter by (e.g. PostTypeKeyArticle, PostTypeKeyPage). Default is PostTypeKeyAny.
FilterStatus string // The status of the post to filter by (e.g. "published", "draft"). Default is "published".
FilterVisibility string // The visibility of the post to filter by (e.g. "public", "private"). Default is "public".
SplitFeatured bool // Whether to split featured items from the main list
IncludeUnpublished bool
}
FilterOptions contains the options to filter posts.
type FilterType ¶
type FilterType string
const ( FilterTypeAny FilterType = "any" FilterTypeAuthor FilterType = "author" FilterTypeTaxonomy FilterType = "taxonomy" )
func (FilterType) String ¶
func (ft FilterType) String() string
type FrontmatterFormat ¶
type FrontmatterFormat string
const ( FrontmatterTOML FrontmatterFormat = "toml" FrontmatterYAML FrontmatterFormat = "yaml" )
type MarkdownParserFunc ¶
func DefaultMarkdownParser ¶
func DefaultMarkdownParser() MarkdownParserFunc
DefaultMarkdownParser returns a MarkdownParserFunc that uses the default goldmark parser with the following extensions: - GFM - Typographer - Footnote - Frontmatter It also enables the following parser options: - AutoHeadingID - Attribute
type Options ¶
type Options struct {
Authors map[string]Author // Authors is a map of author IDs to Author structs.
ClearIndexes bool // ClearIndexes will remove existing indexes before reindexing.
DataDir string // DataDir is the directory where DownCache will store its indexes.
FrontMatterFormat FrontmatterFormat // FrontMatterFormat is the format used for frontmatter in markdown files. Default is TOML.
Logger *slog.Logger // Logger is the logger used by DownCache. Default is a debug logger to stderr.
MarkdownDir string // MarkdownDir is the directory where markdown files are stored.
MarkdownParser MarkdownParserFunc // MarkdownParser is the function used to parse markdown files. A default parser is used if not provided.
Reindex bool // Reindex will reindex all markdown files when DownCache is initialized.
Taxonomies map[string]string // Taxonomies is a map of taxonomy plural names to their singular names.
}
Options is a struct for configuring a new DownCache instance.
type Paginator ¶
type Paginator struct {
TotalPages int
CurrentPage int
NextPage int
PrevPage int
PageSize int
HasNext bool
HasPrev bool
HasPosts bool
TotalPosts int
AllPosts []*Post
FeaturedPosts []*Post
NonFeaturedPosts []*Post
Visible bool // True by default, but can be set to false in the view. E.g. on the home page.
}
Paginator is a struct that holds information about pagination, such as the total number of pages, the current page, the next and previous pages, the page size, whether there are more pages, whether there are posts, the total number of posts, all posts, featured posts, non-featured posts, and whether the paginator is visible.
type Post ¶
type Post struct {
ID string `json:"id"` // ID is the unique identifier for the post
Slug string `json:"slug"` // Slug is the URL-friendly version of the name
PostType string `json:"postType"` // PostType is the type of post (e.g. post, page)
Author string `json:"author"` // Author is a list of author
Content string `json:"content"` // Content is the HTML content of the post
ETag string `json:"etag"` // ETag is the entity tag
EstimatedReadTime string `json:"estimatedReadTime"` // EstimatedReadTime is the estimated reading time
Pinned bool `json:"pinned"` // Pinned is true if the post is pinned
Photo string `json:"photo"` // Photo is the URL of the featured image
FileTimePath string `json:"fileTimePath"` // FileTimePath is the file time path in the format YYYY-MM-DD for the original file path
Updated time.Time `json:"updated"` // Updated is the last modified date
Name string `json:"name"` // Name is the name/title of the post
Properties map[string]any `json:"properties"` // Properties is a map of additional, arbitrary key-value pairs. This can be used to store additional metadata such as extra microformat properties.
Published time.Time `json:"published"` // Published is the published date
Status string `json:"status"` // Status is the status of the post (should be one of draft, published, or archived)
Subtitle string `json:"subtitle"` // Subtitle is the subtitle
Summary string `json:"summary"` // Summary is the summary
Taxonomies map[string][]string `json:"taxonomies"` // Taxonomies is a map of taxonomies (e.g. tags, categories)
Visibility string `json:"visibility"` // Visibility is the visibility of the post (should be one of public, private, or unlisted)
// contains filtered or unexported fields
}
Post represents a Markdown post
func Deserialize ¶
Deserialize deserializes the byte slice to a post
func MarkdownToPost ¶
MarkdownToPost converts markdown content to a Post.
func ReadFile ¶
func ReadFile(markdownParser MarkdownParserFunc, rootPath, path string, postType PostType) (*Post, error)
ReadFile reads a markdown file from the filesystem and converts it to a Post.
func (*Post) FileTimeInSlug ¶
FileTimeInSlug returns the file date
func (*Post) HasFileTimeInSlug ¶
HasFileTimeInSlug returns true if the post has a file time path. This is the date part of the original file path.
func (*Post) HasProperties ¶
HasProperties returns true if the post has additional/arbitrary metadata properties
func (*Post) HasPublished ¶
HasPublished returns true if the post has a published date
func (*Post) HasSubtitle ¶
HasSubtitle returns true if the post has a subtitle
func (*Post) HasSummary ¶
HasSummary returns true if the post has a summary
func (*Post) HasTaxonomies ¶
HasTaxonomies returns true if the post has taxonomies
func (*Post) HasTaxonomy ¶
HasTaxonomy returns true if the post has the specified taxonomy
func (*Post) HasUpdated ¶
HasUpdated returns true if the post has a last modified date
func (*Post) PublishedDate ¶
PublishedDate returns the published date in the format Jan 2, 2006
func (*Post) PublishedYear ¶
PublishedYear returns the year of the published date
func (*Post) SlugWithYear ¶
SlugWithYear returns the slug with the published year prepended as a directory (if it exists)
func (*Post) SlugWithYearMonth ¶
SlugWithYearMonth returns the slug with the published year and month prepended as a directory (if it exists)
func (*Post) SlugWithYearMonthDay ¶
SlugWithYearMonthDay returns the slug with the published year, month, and day prepended as a directory (if it exists)
func (*Post) SlugWithoutDate ¶
SlugWithoutDate returns the slug without a file time path (if it exists)
type PostMeta ¶
type PostMeta struct {
Authors string `yaml:"author,omitempty" toml:"author,omitempty"`
Pinned bool `yaml:"pinned,omitempty" toml:"pinned,omitempty"`
Name string `yaml:"name,omitempty" toml:"name,omitempty"`
Photo string `yaml:"photo,omitempty" toml:"photo,omitempty"`
Properties map[string]any `yaml:"properties,omitempty" toml:"properties,omitempty"`
Published time.Time `yaml:"published,omitempty" toml:"published,omitempty"`
Status string `yaml:"status,omitempty" toml:"status,omitempty"`
Subtitle string `yaml:"subtitle,omitempty" toml:"subtitle,omitempty"`
Summary string `yaml:"summary,omitempty" toml:"summary,omitempty"`
Taxonomies map[string][]string `yaml:"taxonomies,omitempty" toml:"taxonomies,omitempty"`
Updated time.Time `yaml:"updated,omitempty" toml:"updated,omitempty"`
Visibility string `yaml:"visibility,omitempty" toml:"visibility,omitempty"`
}
PostMeta represents the frontmatter of a post
type PostStore ¶
type PostStore interface {
// Init initializes the post store, such as creating the necessary tables or indexes.
Init() error
// Create creates a new post.
Create(post *Post) (*Post, error)
// Update updates an existing post.
Update(post *Post) error
// Delete deletes a post.
Delete(post *Post) error
// GetBySlug retrieves a post by its slug.
GetBySlug(slug string) (*Post, error)
// Search searches for posts based on the provided filter options.
Search(opts FilterOptions) ([]*Post, error)
// Close closes the post store.
Close() error
}
type PostType ¶
type PostType struct {
TypeKey PostTypeKey `json:"type"`
DirPattern string `json:"dirPattern"`
FrontmatterCheck func(map[string]any) bool `json:"-"`
}
PostType defines a rule for determining the type of post based on its path and frontmatter.
type PostTypeKey ¶
type PostTypeKey string
PostTypeKey is a string key that represents a post type.
const ( PostTypeKeyPage PostTypeKey = "pages" PostTypeKeyArticle PostTypeKey = "articles" PostTypeKeyNote PostTypeKey = "notes" PostTypeKeyLink PostTypeKey = "links" PostTypeKeyBookmark PostTypeKey = "bookmarks" PostTypeKeyAny PostTypeKey = "any" )
func (PostTypeKey) String ¶
func (ptk PostTypeKey) String() string
String returns the string representation of the PostTypeKey.
type PostTypesMap ¶
type PostTypesMap map[PostTypeKey]PostType
PostTypesMap is a map of PostTypeKey to PostType.
func DefaultPostTypes ¶
func DefaultPostTypes() PostTypesMap
DefaultPostTypes returns the default post types for a DownCache instance. The default rules are: - Posts are in the "articles/" directory and have a "date" frontmatter key. - Pages are in the "pages/" directory and have no specific frontmatter requirements.
func (PostTypesMap) IsValidTypeKey ¶
func (ptm PostTypesMap) IsValidTypeKey(key string) bool
IsValidTypeKey returns true if the given key is a valid PostTypeKey in the PostTypesMap.
func (PostTypesMap) PostTypeFromPath ¶
func (ptm PostTypesMap) PostTypeFromPath(filePath string) PostType
PostTypeFromPath returns the PostType for the given file path. If no rule matches, an empty PostType is returned.
type SlugPath ¶
func SlugifyPath ¶
SlugifyPath transforms a full OS path into a slugified path. - It trims the `rootPath` from the beginning of the `fullPath` to get the relative path. - The slug is then a combination of the `postType` and the relative path. - It removes leading and trailing slashes, and ensures no leading slash remains. - It finds the extension based on the last period in the path and trims it from the path. - If the file part of a path starts with an RFC3339 date (2006-01-02), it extracts it and removes it from the path. Note, it does not include the time part. - It trims the "/index" suffix if it exists. - It replaces all path separators with browser-compatible forward slashes. - Finally, it slugifies each path part using the slug package.
The function returns a SlugPath struct with the slugified path, the file time path, the file time, and the post type.