Skip to content

Commit

Permalink
fix(parser): detect DocumentHeader when doc has FrontMatter (#1042)
Browse files Browse the repository at this point in the history
Only disable the `DocumentHeader` rule if the element is not a `FrontMatter` or a `BlankLine` (in case it's between the `FrontMatter` and the `DocumentHeader`)

Fixes #1041

Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
  • Loading branch information
xcoulon authored Jun 12, 2022
1 parent b8e1fb4 commit 8adac29
Show file tree
Hide file tree
Showing 12 changed files with 15,399 additions and 15,204 deletions.
15 changes: 9 additions & 6 deletions pkg/parser/document_processing_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,14 @@ func insertPreamble(doc *types.Document) {
// now, insert the preamble instead of the 'n' blocks that belong to the preamble
// and copy the other items
elements := make([]interface{}, len(doc.Elements)-len(preamble.Elements)+1)
if header := doc.Header(); header != nil {
if frontmatter := doc.FrontMatter(); frontmatter != nil {
elements[0] = frontmatter
}
if header, offset := doc.Header(); header != nil {
log.Debug("inserting preamble after header")
elements[0] = header
elements[1] = preamble
copy(elements[2:], doc.Elements[1+len(preamble.Elements):])
elements[0+offset] = header
elements[1+offset] = preamble
copy(elements[2+offset:], doc.Elements[1+len(preamble.Elements)+offset:])
} else {
log.Debug("inserting preamble at beginning of document")
elements[0] = preamble
Expand All @@ -176,7 +179,7 @@ func insertPreamble(doc *types.Document) {
}

func newPreamble(doc *types.Document) *types.Preamble {
if doc.Header() == nil {
if header, _ := doc.Header(); header == nil {
log.Debug("skipping preamble: no header in doc")
return nil
}
Expand All @@ -185,7 +188,7 @@ func newPreamble(doc *types.Document) *types.Preamble {
}
for _, e := range doc.Elements {
switch e.(type) {
case *types.DocumentHeader:
case *types.DocumentHeader, *types.FrontMatter:
continue
case *types.Section:
return preamble
Expand Down
37 changes: 37 additions & 0 deletions pkg/parser/document_processing_insert_preamble_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import (

var _ = Describe("insert preambles", func() {

frontmatter := &types.FrontMatter{
Attributes: map[string]interface{}{
"draft": true,
},
}
header := &types.DocumentHeader{
Title: []interface{}{
&types.StringElement{
Expand Down Expand Up @@ -169,4 +174,36 @@ var _ = Describe("insert preambles", func() {
Expect(doc).To(Equal(expected))
})

It("should insert preamble with 1 paragraph and blankline when doc has frontmatter", func() {
// given
doc := &types.Document{
Elements: []interface{}{
frontmatter,
header,
paragraph,
blankline,
sectionA,
sectionB,
},
}
expected := &types.Document{
Elements: []interface{}{
frontmatter,
header,
&types.Preamble{
Elements: []interface{}{
paragraph,
blankline,
},
},
sectionA,
sectionB,
},
}
// when
insertPreamble(doc)
// then
Expect(doc).To(Equal(expected))
})

})
10 changes: 8 additions & 2 deletions pkg/parser/document_processing_parse_fragments.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,14 @@ func (c *current) isDocumentHeaderAllowed() bool {
return found && allowed && !c.isWithinDelimitedBlock()
}

func (c *current) disableDocumentHeaderRule() {
c.globalStore[documentHeaderKey] = false
// disables the `DocumentHeader` grammar rule if the element is anything but a BlankLine or a FrontMatter
func (c *current) disableDocumentHeaderRule(element interface{}) {
switch element.(type) {
case *types.BlankLine, *types.FrontMatter:
return
default:
c.globalStore[documentHeaderKey] = false
}
}

const blockAttributesKey = "block_attributes"
Expand Down
Loading

0 comments on commit 8adac29

Please sign in to comment.