mirror of
https://github.com/yuin/goldmark
synced 2025-03-04 23:04:52 +00:00
Merge 27f4242602 into 04410ff159
This commit is contained in:
commit
fd2d07077d
1 changed files with 23 additions and 2 deletions
|
|
@ -10,6 +10,7 @@ import (
|
||||||
type HeadingConfig struct {
|
type HeadingConfig struct {
|
||||||
AutoHeadingID bool
|
AutoHeadingID bool
|
||||||
Attribute bool
|
Attribute bool
|
||||||
|
MaxLevel int
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetOption implements SetOptioner.
|
// SetOption implements SetOptioner.
|
||||||
|
|
@ -56,6 +57,22 @@ func (o *withHeadingAttribute) SetHeadingOption(p *HeadingConfig) {
|
||||||
p.Attribute = true
|
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.
|
// WithHeadingAttribute is a functional option that enables custom heading attributes.
|
||||||
func WithHeadingAttribute() HeadingOption {
|
func WithHeadingAttribute() HeadingOption {
|
||||||
return &withHeadingAttribute{WithAttribute()}
|
return &withHeadingAttribute{WithAttribute()}
|
||||||
|
|
@ -67,7 +84,11 @@ type atxHeadingParser struct {
|
||||||
|
|
||||||
// NewATXHeadingParser return a new BlockParser that can parse ATX headings.
|
// NewATXHeadingParser return a new BlockParser that can parse ATX headings.
|
||||||
func NewATXHeadingParser(opts ...HeadingOption) BlockParser {
|
func NewATXHeadingParser(opts ...HeadingOption) BlockParser {
|
||||||
p := &atxHeadingParser{}
|
p := &atxHeadingParser{
|
||||||
|
HeadingConfig: HeadingConfig{
|
||||||
|
MaxLevel: 6,
|
||||||
|
},
|
||||||
|
}
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o.SetHeadingOption(&p.HeadingConfig)
|
o.SetHeadingOption(&p.HeadingConfig)
|
||||||
}
|
}
|
||||||
|
|
@ -88,7 +109,7 @@ func (b *atxHeadingParser) Open(parent ast.Node, reader text.Reader, pc Context)
|
||||||
for ; i < len(line) && line[i] == '#'; i++ {
|
for ; i < len(line) && line[i] == '#'; i++ {
|
||||||
}
|
}
|
||||||
level := i - pos
|
level := i - pos
|
||||||
if i == pos || level > 6 {
|
if i == pos || level > b.HeadingConfig.MaxLevel {
|
||||||
return nil, NoChildren
|
return nil, NoChildren
|
||||||
}
|
}
|
||||||
if i == len(line) { // alone '#' (without a new line character)
|
if i == len(line) { // alone '#' (without a new line character)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue