output

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package output handles writing rendered HTML and assets to the publish directory.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Canonify

func Canonify(html string, opts CanonifyOptions) string

Canonify rewrites root-relative URLs in href/src attributes to absolute URLs. Mirrors Hugo's canonifyURLs = true behavior.

Handles both quoted and unquoted (post-minify) attribute values. Skips protocol-relative URLs (//example.com) and absolute http(s) URLs.

Skips <code> and <pre> regions: URLs inside code blocks are raw text content (e.g., a code sample showing `<link href=/api/foo />`), not actual HTML attributes. Hugo's canonifyURLs does not rewrite inside code/pre either.

func CleanPublishDir

func CleanPublishDir(publishDir string) error

CleanPublishDir removes the publish directory. Use cautiously - this is destructive.

func GenerateContentAPI

func GenerateContentAPI(outputDir string, site *content.Site, cfg *config.Config, includeDrafts, includeFuture, includeExpired bool, now time.Time) error

GenerateContentAPI writes /api/{section}.json for each top-level section. Only pages that pass the draft/future/expired filter are included.

func GenerateLlmsTxt

func GenerateLlmsTxt(outputDir, sourceDir string, cfg *config.Config) error

GenerateLlmsTxt writes /llms.txt to the output directory. If layouts/llms.txt exists, it's used as-is; otherwise a default is generated from the site config.

func PathToFilePath

func PathToFilePath(path, publishDir string) string

PathToFilePath maps a path (without trailing slash) directly. e.g., ("sitemap.xml") → publishDir/sitemap.xml e.g., ("posts/index.xml") → publishDir/posts/index.xml

func URLToFilePath

func URLToFilePath(url, publishDir string) string

URLToFilePath converts a URL path to an output file path under publishDir. Hugo convention: /foo/ → publishDir/foo/index.html

Types

type CanonifyOptions

type CanonifyOptions struct {
	BaseURL string // e.g., "https://zhurongshuo.com/"
	IsHome  bool   // if true, inject Hugo generator meta
}

CanonifyOptions controls how canonifyURLs rewrites paths.

type ContentItem

type ContentItem struct {
	Title       string   `json:"title"`
	URL         string   `json:"url"`
	Date        string   `json:"date"`
	Description string   `json:"description,omitempty"`
	Summary     string   `json:"summary,omitempty"`
	Plain       string   `json:"plain,omitempty"`
	Tags        []string `json:"tags,omitempty"`
}

ContentItem is a single content entry in the API output.

type Minifier

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

Minifier wraps tdewolff/minify with the same configuration Hugo uses.

func NewMinifier

func NewMinifier() *Minifier

NewMinifier creates a minifier matching Hugo's tdewolff settings:

HTML: keepWhitespace = false
CSS:  precision = 0
JS:   precision = 0

func (*Minifier) Minify

func (mi *Minifier) Minify(relPath, content string) string

Minify applies the right minifier based on the file extension. Returns the original content if minification fails or is disabled for the type.

func (*Minifier) MinifyBytes

func (mi *Minifier) MinifyBytes(relPath string, data []byte) []byte

MinifyBytes is the byte-slice variant of Minify.

type Writer

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

Writer writes files to the publish directory.

func NewWriter

func NewWriter(publishDir string) *Writer

NewWriter creates a new Writer targeting publishDir without minification.

func NewWriterWithMinify

func NewWriterWithMinify(publishDir string) *Writer

NewWriterWithMinify creates a Writer that minifies output before writing.

func (*Writer) CopyStatic

func (w *Writer) CopyStatic(srcDir string) error

CopyStatic copies all files from srcDir into publishDir, preserving relative paths.

func (*Writer) SetCanonify

func (w *Writer) SetCanonify(opts CanonifyOptions)

SetCanonify enables root-relative URL canonification with the given options.

func (*Writer) Stats

func (w *Writer) Stats() (files int, bytes int64)

Stats returns the number of files written and total bytes.

func (*Writer) Write

func (w *Writer) Write(relPath, content string) error

Write writes content to a file path under publishDir. Creates parent directories as needed. If a minifier is set, content is minified according to the file's media type before writing. If canonify is set, root-relative URLs in HTML are rewritten to absolute URLs.

func (*Writer) WriteBytes

func (w *Writer) WriteBytes(relPath string, data []byte) error

WriteBytes writes raw bytes to a file path under publishDir. Minification is applied if a minifier is set.

func (*Writer) WriteBytesPath

func (w *Writer) WriteBytesPath(relPath string, data []byte) error

WriteBytesPath writes raw bytes WITHOUT applying minify/canonify. Use for pre-formatted content that should be emitted verbatim.

Jump to

Keyboard shortcuts

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