Skip to content

Commit

Permalink
document that Parser is not reusable; if Parse() is called second tim…
Browse files Browse the repository at this point in the history
…e, throw a panic() with error message saying so (fixes #280)
  • Loading branch information
kjk committed Nov 5, 2024
1 parent 6bc1ffd commit d03b890
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ type Parser struct {
// collect headings where we auto-generated id so that we can
// ensure they are unique at the end
allHeadingsWithAutoID []*ast.Heading

didParse bool
}

// New creates a markdown parser with CommonExtensions.
Expand Down Expand Up @@ -292,7 +294,14 @@ type Reference struct {
//
// You can then convert AST to html using html.Renderer, to some other format
// using a custom renderer or transform the tree.
//
// Parser is not reusable. Create a new Parser for each Parse() call.
func (p *Parser) Parse(input []byte) ast.Node {
if p.didParse {
panic("Parser is not reusable. Must create new Parser for each Parse() call.")
}
p.didParse = true

// the code only works with Unix CR newlines so to make life easy for
// callers normalize newlines
input = NormalizeNewlines(input)
Expand Down

0 comments on commit d03b890

Please sign in to comment.