markdown

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultAction = ActionQuestion

DefaultAction is the default action type for new comments.

View Source
const OverviewSectionID = "overview"

OverviewSectionID is the virtual section ID used for file-level comments on the overview/preamble.

View Source
const OverviewTitle = "Overview"

OverviewTitle is the name the overview is listed and searched by.

Variables

ActionLabels is the ordered list of action labels for Tab cycling.

DecorationLabels is the ordered list of decoration labels for cycling.

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound marks lookups of unknown comments or sections.

Functions

func FormatActionLabel

func FormatActionLabel(action ActionType, deco Decoration) string

FormatActionLabel formats an action and decoration pair for display.

func FormatLineRef added in v0.6.0

func FormatLineRef(startLine, endLine int) string

FormatLineRef formats a line reference from start and end line numbers. Returns "L10" for single line, "L10-L15" for range, or "" if startLine is 0.

func FormatReview

func FormatReview(result *ReviewResult, d *Document, filePath string) string

FormatReview formats a ReviewResult for a single file as a Markdown string. Section-level comments are grouped under section headings. Line-level comments are listed separately by line number.

func FormatReviews added in v0.10.0

func FormatReviews(files []FileResult) string

FormatReviews formats reviews for several files as one Markdown document. Each file gets its own "## path" heading with section headings nested one level deeper. Files without comments are omitted; returns "" if none have any.

func SaveViewedState

func SaveViewedState(path string, state *ViewedState) error

SaveViewedState writes the viewed state to a JSON file.

func SkipNotice added in v0.13.0

func SkipNotice(path string, err error) string

SkipNotice is how the front ends report a file that could not be loaded and is skipped.

func StatePath

func StatePath(filePath string) string

StatePath returns the sidecar file path for persisting viewed state.

Types

type ActionType

type ActionType string

ActionType is the type of review action, based on Conventional Comments labels.

const (
	ActionSuggestion ActionType = "suggestion"
	ActionIssue      ActionType = "issue"
	ActionQuestion   ActionType = "question"
	ActionNitpick    ActionType = "nitpick"
	ActionTodo       ActionType = "todo"
	ActionThought    ActionType = "thought"
	ActionNote       ActionType = "note"
	ActionPraise     ActionType = "praise"
	ActionChore      ActionType = "chore"
)

type Decoration

type Decoration string

Decoration is the decoration modifier for a Conventional Comment.

const (
	DecorationNone        Decoration = ""
	DecorationNonBlocking Decoration = "non-blocking"
	DecorationBlocking    Decoration = "blocking"
	DecorationIfMinor     Decoration = "if-minor"
)

type Document

type Document struct {
	Title       string     // Leading H1 heading text ("" when the document has none)
	Preamble    string     // Text before the first heading
	Sections    []*Section // Top-level sections
	SourceLines []string   // Raw source lines (for line-level commenting)
}

Document is the parsed structure of an entire Markdown file.

func Parse

func Parse(source []byte) (*Document, error)

Parse parses a Markdown source into a Document structure. It uses goldmark to build an AST and walks headings to create sections.

func (*Document) AllSections

func (d *Document) AllSections() []*Section

AllSections returns a flat list of all sections in depth-first order.

func (*Document) DiffLineSections added in v0.12.0

func (d *Document) DiffLineSections(info *diff.Info) []string

DiffLineSections returns the ID of the section each diff line belongs to. Added and context lines belong to the section of their new-file line. Removed lines no longer exist in the new file, so they follow the nearest preceding line that does (or the next one at the start of the diff). The lines of a section are therefore contiguous in the diff.

func (*Document) FindSection

func (d *Document) FindSection(id string) *Section

FindSection returns the section with the given ID, or nil if not found.

func (*Document) HasOverview added in v0.12.0

func (d *Document) HasOverview() bool

HasOverview reports whether the section list starts with the overview entry: only when there is a preamble to show.

func (*Document) LineSections added in v0.12.0

func (d *Document) LineSections() []string

LineSections returns the ID of the section each source line belongs to (index i is line i+1): the last section whose heading starts at or before the line, or OverviewSectionID before the first heading.

func (*Document) Quote added in v0.12.0

func (d *Document) Quote(info *diff.Info, start, end int, side string) []string

Quote returns the source text a line comment on start..end (1-based, inclusive; end 0 means start only) quotes: the document's lines, or with info the diff lines on side (see diff.Line.Position).

func (*Document) SearchSections added in v0.12.0

func (d *Document) SearchSections(query string) map[string]bool

SearchSections returns the IDs of the sections a search for query shows: those whose ID, title or body contain it (ignoring case), with their ancestors and descendants. The overview, when listed, matches by name only. An empty query matches nothing.

type File added in v0.12.0

type File struct {
	Path   string
	Doc    *Document
	Diff   *diff.Info   // nil: review the full source instead of a diff
	Viewed *ViewedState // nil: viewed marks live only for the session
}

File is a file to review, as the TUI and the browser receive it.

type FileResult added in v0.13.0

type FileResult struct {
	File
	Status Status
	Review *ReviewResult // nil when the file was quit (skipped)
}

FileResult is the outcome of reviewing one file.

type Loader added in v0.13.0

type Loader func(path string) (File, error)

Loader reads a file for review. The TUI and the browser call it when a file's turn comes and again when the reviewer reloads it.

type ReloadResult added in v0.13.0

type ReloadResult struct {
	Err     error // the file could not be read; the review is unchanged
	Changed bool  // the review now shows the new content
	Moved   int   // comments that could not be placed and were moved (see reload)
	// Sections maps the section IDs before the reload to those of the same
	// sections after it, so a front end can stay on the selected one.
	Sections map[string]string
}

ReloadResult is the outcome of ReviewState.Reread.

func (ReloadResult) Message added in v0.13.0

func (res ReloadResult) Message() string

Message is how the front ends report the reload.

type ReviewComment

type ReviewComment struct {
	ID         string     `json:"id"`                  // assigned by ReviewState
	SectionID  string     `json:"sectionId"`           // Target section ID
	Action     ActionType `json:"action"`              // Comment action type
	Decoration Decoration `json:"decoration"`          // Comment decoration (e.g. non-blocking, blocking)
	Body       string     `json:"body"`                // Comment body text
	StartLine  int        `json:"startLine,omitempty"` // 1-based start line (0 = section-level comment)
	EndLine    int        `json:"endLine,omitempty"`   // 1-based end line (0 = single line if StartLine > 0)
	Side       string     `json:"side,omitempty"`      // "RIGHT" or "LEFT" (for diff comments)
	Quote      []string   `json:"quote,omitempty"`     // source text of the commented lines (line-level only)
}

ReviewComment is a review comment on a single section. The JSON form is what the browser review exchanges with the page.

func (*ReviewComment) DisplayLineRef added in v0.13.0

func (c *ReviewComment) DisplayLineRef() string

DisplayLineRef returns FormatLineRef marked " (removed)" for removed diff lines, which are numbered by the old file and must not be read as current line numbers.

func (*ReviewComment) FormatLabel

func (c *ReviewComment) FormatLabel() string

FormatLabel returns the formatted label string for display. With decoration: "action (decoration)", without: "action".

func (*ReviewComment) FormatLineRef added in v0.6.0

func (c *ReviewComment) FormatLineRef() string

FormatLineRef returns a line reference string for display. Returns "L10" for single line, "L10-L15" for range, or "" for section-level.

func (*ReviewComment) IsRemoved added in v0.10.0

func (c *ReviewComment) IsRemoved() bool

IsRemoved reports whether the comment targets removed (old-side) diff lines.

type ReviewResult

type ReviewResult struct {
	Comments []ReviewComment
}

ReviewResult holds the entire review output.

func (*ReviewResult) Status added in v0.12.0

func (r *ReviewResult) Status() Status

Status returns the status of a submitted review: approved when it has no comments.

type ReviewState added in v0.13.0

type ReviewState struct {
	File
	// contains filtered or unexported fields
}

ReviewState is the review of one file in progress: its comments and viewed marks. The TUI and the browser both keep their review here, so the rules for saving comments and marking sections are the same in both.

func NewReviewState added in v0.13.0

func NewReviewState(f File) *ReviewState

NewReviewState starts the review of f, restoring the viewed marks from f.Viewed. Marking sections updates f.Viewed in place, for the caller to persist.

func (*ReviewState) Comments added in v0.13.0

func (r *ReviewState) Comments() []*ReviewComment

Comments returns every comment in creation order.

func (*ReviewState) ConfirmQuitMessage added in v0.13.0

func (r *ReviewState) ConfirmQuitMessage(multiFile bool) string

ConfirmQuitMessage is the question the quit dialog asks.

func (*ReviewState) ConfirmSubmitMessage added in v0.13.0

func (r *ReviewState) ConfirmSubmitMessage(multiFile bool) string

ConfirmSubmitMessage is the question the submit dialog asks.

func (*ReviewState) DeleteComment added in v0.13.0

func (r *ReviewState) DeleteComment(id string) error

DeleteComment removes the comment with the given ID.

func (*ReviewState) IsViewed added in v0.13.0

func (r *ReviewState) IsViewed(sectionID string) bool

IsViewed reports whether a section is marked as viewed.

func (*ReviewState) Reread added in v0.13.0

func (r *ReviewState) Reread(load Loader) ReloadResult

Reread reads the file under review again with load and, when its content changed, switches the review to it (see reload). A file that cannot be read leaves the review as it was.

func (*ReviewState) Result added in v0.13.0

func (r *ReviewState) Result() *ReviewResult

Result returns the comments in document order, which the output follows: the overview first, then the sections depth-first, keeping creation order within a section.

func (*ReviewState) SaveComment added in v0.13.0

func (r *ReviewState) SaveComment(c ReviewComment) error

SaveComment stores c: a new comment when c.ID is empty, otherwise a change to the label, decoration and body of the comment with that ID (its target never changes). An empty body drops a new comment and deletes an existing one. A new comment is validated and gets its ID, and a line comment also its section and quoted source text.

func (*ReviewState) SectionComments added in v0.13.0

func (r *ReviewState) SectionComments(sectionID string) []*ReviewComment

SectionComments returns the comments on a section in creation order.

func (*ReviewState) SetViewed added in v0.13.0

func (r *ReviewState) SetViewed(sectionID string, viewed bool) error

SetViewed marks a section as viewed or not, updating File.Viewed when it is set. Only real sections can be marked, not the overview.

func (*ReviewState) ViewedCount added in v0.13.0

func (r *ReviewState) ViewedCount() int

ViewedCount returns the number of sections marked as viewed.

type Section

type Section struct {
	ID        string     // Auto-numbered: "S1", "S1.1", "S2", etc.
	Title     string     // Heading text (without the "## " prefix)
	Level     int        // Heading level (2=##, 3=###, ...)
	Body      string     // Markdown text from heading to next heading
	Children  []*Section // Sub-sections (lower-level headings)
	Parent    *Section   // Parent section (nil for top-level)
	StartLine int        // 1-based line number of heading (0 = not set)
	EndLine   int        // 1-based line number of last body line (0 = not set)
}

Section is a single section in a document, corresponding to one heading.

type Status

type Status string

Status is the exit status of a review session (TUI or browser).

const (
	StatusSubmitted Status = "submitted"
	StatusApproved  Status = "approved"
	StatusCancelled Status = "cancelled"
)

type ViewedState

type ViewedState struct {
	Sections map[string]string `json:"sections"` // title -> content hash
}

ViewedState tracks which sections have been viewed and their content hashes.

func LoadViewedState

func LoadViewedState(path string) *ViewedState

LoadViewedState reads a viewed state file. Returns an empty state on any error.

func NewViewedState

func NewViewedState() *ViewedState

NewViewedState creates an empty ViewedState.

func (*ViewedState) IsSectionViewed

func (vs *ViewedState) IsSectionViewed(s *Section) bool

IsSectionViewed returns true if the section's title is tracked and its content hash matches.

func (*ViewedState) MarkViewed

func (vs *ViewedState) MarkViewed(s *Section)

MarkViewed records a section as viewed with its current content hash.

func (*ViewedState) UnmarkViewed

func (vs *ViewedState) UnmarkViewed(s *Section)

UnmarkViewed removes a section's viewed status.

Jump to

Keyboard shortcuts

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