Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring the max parsed heading level #477

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions parser/atx_heading.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type HeadingConfig struct {
AutoHeadingID bool
Attribute bool
MaxLevel int
}

// SetOption implements SetOptioner.
Expand Down Expand Up @@ -56,6 +57,22 @@ func (o *withHeadingAttribute) SetHeadingOption(p *HeadingConfig) {
p.Attribute = true
}

// WithMaxHeadingLevel is a functional option that enables limiting heading
// parsing a custom max level.
func WithMaxHeadingLevel(maxLevel int) HeadingOption {
return &withMaxHeadingLevel{maxLevel: maxLevel}
}

type withMaxHeadingLevel struct {
Option

maxLevel int
}

func (o *withMaxHeadingLevel) SetHeadingOption(p *HeadingConfig) {
p.MaxLevel = o.maxLevel
}

// WithHeadingAttribute is a functional option that enables custom heading attributes.
func WithHeadingAttribute() HeadingOption {
return &withHeadingAttribute{WithAttribute()}
Expand All @@ -67,7 +84,11 @@ type atxHeadingParser struct {

// NewATXHeadingParser return a new BlockParser that can parse ATX headings.
func NewATXHeadingParser(opts ...HeadingOption) BlockParser {
p := &atxHeadingParser{}
p := &atxHeadingParser{
HeadingConfig: HeadingConfig{
MaxLevel: 6,
},
}
for _, o := range opts {
o.SetHeadingOption(&p.HeadingConfig)
}
Expand All @@ -88,7 +109,7 @@ func (b *atxHeadingParser) Open(parent ast.Node, reader text.Reader, pc Context)
for ; i < len(line) && line[i] == '#'; i++ {
}
level := i - pos
if i == pos || level > 6 {
if i == pos || level > b.HeadingConfig.MaxLevel {
return nil, NoChildren
}
if i == len(line) { // alone '#' (without a new line character)
Expand Down