content

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractSummary

func ExtractSummary(markdown, html string, maxWords int) string

ExtractSummary extracts a plain-text summary from markdown and rendered HTML. Priority: 1) Content before <!--more-->, 2) First paragraph, 3) Truncated content. The result is always plain text — HTML tags and footnote references are stripped so the summary is safe for meta descriptions, RSS feeds, and templ text nodes.

func GenerateTOC

func GenerateTOC(html string, minLevel, maxLevel int, tocID ...string) string

GenerateTOC extracts headings from HTML and generates a table of contents. minLevel and maxLevel control which heading levels to include (default: 2-4). The optional tocID parameter scopes the TOC for go-components JS scroll-spy; when provided, each link gets data-tui-toc-id for IntersectionObserver binding.

func ReadingTime

func ReadingTime(wordCount int) int

ReadingTime estimates reading time in minutes (assumes 200 words per minute)

func StripHTML

func StripHTML(s string) string

StripHTML removes HTML tags from a string and decodes HTML entities to their plain-text equivalents (e.g. &ldquo; → ", &amp; → &).

func TOCFromEntries

func TOCFromEntries(entries []*TOCEntry, tocID ...string) string

TOCFromEntries builds TOC HTML from a slice of entries (alternative API). The optional tocID parameter enables go-components JS scroll-spy binding.

func WordCount

func WordCount(text string) int

WordCount returns the number of words in plain text

func WrapHeadingSections

func WrapHeadingSections(html string) string

WrapHeadingSections wraps each h2-delimited block of HTML in <section> tags. Content before the first h2 is wrapped in its own section. This enables CSS structural selectors like section:nth-of-type(even) for alternating backgrounds via the token system.

Input: <h2 id="a">A</h2><p>...</p><h2 id="b">B</h2><p>...</p> Output: <section><h2 id="a">A</h2><p>...</p></section><section><h2 id="b">B</h2><p>...</p></section>

Types

type HighlightOptions

type HighlightOptions struct {
	Style       string // Chroma style name (e.g. "monokai", "github")
	LineNumbers bool   // Show line numbers in code blocks
}

HighlightOptions configures syntax highlighting in the parser. Pass to NewParser to enable Chroma-based code highlighting.

type Page

type Page struct {
	// Path is the original file path
	Path string

	// Content is the rendered HTML content
	Content string

	// RawContent is the original markdown (for custom processing)
	RawContent string

	// Frontmatter contains the parsed YAML frontmatter
	Frontmatter map[string]any

	// Layout specifies which layout template to use
	Layout string

	// URL is the generated URL path for this page
	URL string

	// Date is the publication date
	Date time.Time

	// Draft indicates if this is a draft post
	Draft bool

	// Title is extracted from frontmatter
	Title string

	// Description is extracted from frontmatter
	Description string

	// Tags are extracted from frontmatter
	Tags []string

	// Author is extracted from frontmatter
	Author string

	// Section is the content section (e.g., "blog" from content/blog/post.md)
	Section string

	// Weight is used for manual ordering (from frontmatter)
	Weight int

	// Summary is the page summary (first paragraph or content before <!--more-->)
	Summary string

	// WordCount is the number of words in the content
	WordCount int

	// ReadingTime is the estimated reading time in minutes
	ReadingTime int

	// TOC is the rendered table of contents HTML
	TOC string

	// Prev is the previous page in the section (by date)
	Prev *Page

	// Next is the next page in the section (by date)
	Next *Page

	// Aliases are alternative URLs that redirect to this page
	Aliases []string
}

Page represents a parsed content page with metadata

func (*Page) HasTag

func (p *Page) HasTag(tag string) bool

HasTag returns true if the page has the specified tag

func (*Page) IsPublished

func (p *Page) IsPublished() bool

IsPublished returns true if the page is not a draft and has a past or current date

type Parser

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

Parser handles parsing of Markdown content files

func NewParser

func NewParser(contentDir string, opts ...HighlightOptions) *Parser

NewParser creates a new Parser instance. Pass a HighlightOptions to enable syntax highlighting; omit it to disable highlighting.

func (*Parser) ParseAll

func (p *Parser) ParseAll(ctx context.Context) ([]*Page, error)

ParseAll walks the content directory and parses all Markdown files

func (*Parser) ParseFile

func (p *Parser) ParseFile(ctx context.Context, path string) (*Page, error)

ParseFile parses a single Markdown file

type TOCEntry

type TOCEntry struct {
	Level    int
	ID       string
	Text     string
	Children []*TOCEntry
}

TOCEntry represents a heading in the table of contents

Jump to

Keyboard shortcuts

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