Documentation
¶
Overview ¶
Package markdown reads markdown documents, extracts fenced code blocks, parses frontmatter and updates delimited sections in place.
It has zero dependencies and does no I/O of its own: the caller injects the read and write functions, so the same Doc works over the filesystem, an embed.FS, or an in-memory buffer.
Index ¶
- Constants
- Variables
- func ParseFrontmatter(content string) (map[string]string, error)
- type Doc
- func (d *Doc) Extract(outputFile string) error
- func (d *Doc) Frontmatter() (map[string]string, error)
- func (d *Doc) InputByte(content []byte) *Doc
- func (d *Doc) InputEmbed(path string, readFile ReadFunc) *Doc
- func (d *Doc) InputPath(pathFile string, readFile ReadFunc) *Doc
- func (d *Doc) SetLog(fn func(...any))
- func (d *Doc) UpdateSection(sectionID, content string, afterLine ...string) error
- type ReadFunc
- type WriteFunc
Constants ¶
const Fence = "---"
Fence opens and closes a frontmatter block.
Variables ¶
var ( // ErrFrontmatterMissing: the document does not open with a Fence line. ErrFrontmatterMissing = errors.New("frontmatter: file must start with a '---' line") // ErrFrontmatterUnclosed: the opening Fence has no matching closing Fence. ErrFrontmatterUnclosed = errors.New("frontmatter: opening '---' has no matching closing '---'") )
var ErrNoDestination = fmt.Errorf("markdown: destination not set; provide it to New(rootDir, destination, writeFile)")
ErrNoDestination is returned by Extract when the Doc has no output directory.
var ErrNoReader = errors.New("markdown: no input configured; call InputPath, InputByte or InputEmbed")
ErrNoReader is returned when a Doc is used before an input source is set.
var ErrSectionArgs = errors.New("markdown: sectionID and content are required")
ErrSectionArgs is returned by UpdateSection when sectionID or content is empty.
Functions ¶
func ParseFrontmatter ¶ added in v0.0.2
ParseFrontmatter parses the leading frontmatter block of content and returns its key/value pairs.
Rules: the block must start at byte 0 with a "---" line and close at the next "---" line. Between them, each non-empty line is a "key: value" pair split on the first ':'; lines without a ':' are skipped; surrounding single or double quotes are stripped from values. CRLF endings are tolerated.
Which keys are required is NOT this package's business — the caller decides. An empty block parses to an empty (non-nil) map with no error.
Types ¶
type Doc ¶ added in v0.0.2
type Doc struct {
// contains filtered or unexported fields
}
Doc is a markdown document with a configured input source and output target.
func New ¶
New creates a Doc rooted at rootDir that writes its output under destination. The input source must be set with InputPath, InputByte or InputEmbed.
func (*Doc) Extract ¶ added in v0.0.2
Extract concatenates every fenced code block of the language implied by outputFile's extension and writes the result to destination/outputFile. The file is left untouched when the extracted code is already identical.
func (*Doc) Frontmatter ¶ added in v0.0.2
Frontmatter reads the configured input and parses its frontmatter block.
func (*Doc) InputEmbed ¶ added in v0.0.2
InputEmbed sets the input to a path inside any reader (e.g. an embed.FS).
func (*Doc) InputPath ¶ added in v0.0.2
InputPath sets the input to a file path read through readFile.
func (*Doc) UpdateSection ¶ added in v0.0.2
UpdateSection creates or replaces the block delimited by <!-- START_SECTION:id --> ... <!-- END_SECTION:id --> in the input document, writing it back through the Doc's write function.
Duplicate sections with the same id are collapsed into one. afterLine is an optional 1-based line number telling where to place the section when it does not exist yet; without it, a new section is appended at the end. The document is left untouched when the section is already up to date.