Skip to content

Commit

Permalink
fix: Summary includes were not adjusting heading levels (#119)
Browse files Browse the repository at this point in the history
All e2e summary include tests had the includes at level 1
so we didn't exercise the case where the include is at a level deeper
than that.

Add tests for this and fix the behavior discrepancies.
  • Loading branch information
abhinav authored Nov 8, 2023
1 parent 0c8b574 commit ba90473
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changes/v0.8.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## v0.8.1 - 2023-11-08
### Fixed
- Fix included summaries not adjusting item heading levels based on the include's position in the list.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html),
and is generated by [Changie](https://github.com/miniscruff/changie).

## v0.8.1 - 2023-11-08
### Fixed
- Fix included summaries not adjusting item heading levels based on the include's position in the list.

## v0.8.0 - 2023-11-07

This release adds new syntax:
Expand Down
13 changes: 11 additions & 2 deletions collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,19 @@ func (c *collector) collectEmbedItem(item *stitch.EmbedItem, cursor tree.Cursor[
var heading *markdownHeading

if h := section.Title; h != nil {
heading = c.newHeading(summaryFile, c.idGen, h)
// Ignore the heading level in the summary file.
// It'll get whatever the depth of the embed is.
heading.Lvl = 0
h.Level = 1
id, _ := c.idGen.GenerateID(string(h.Text(summaryFile.Source)))
heading = &markdownHeading{
AST: h,
ID: id,
Lvl: h.Level,
}

// Unset the section title so it doesn't transform
// heading levels.
section.Title = nil
} else {
// The included file does not have a title.
// Generate one from the TOC link.
Expand Down
102 changes: 102 additions & 0 deletions testdata/e2e/embed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,105 @@
## Usage
Start the program.
- name: heading levels
give: |
- Level 1
- ![Foo](foo.md)
- Level 2
- ![Foo](foo.md)
- Level 3
- ![Foo](foo.md)
files:
foo.md: |
- [Bar](bar.md)
bar.md: |
# Bar
## Baz
want: |
- [Level 1](#level-1)
- [Foo](#foo)
- [Bar](#bar)
- [Level 2](#level-2)
- [Foo](#foo-1)
- [Bar](#bar-1)
- [Level 3](#level-3)
- [Foo](#foo-2)
- [Bar](#bar-2)
# Level 1
## Foo
### Bar
#### Baz
## Level 2
### Foo
#### Bar
##### Baz
### Level 3
#### Foo
##### Bar
###### Baz
- name: heading levels with section header
give: |
- Level 1
- ![Foo](foo.md)
- Level 2
- ![Foo](foo.md)
- Level 3
- ![Foo](foo.md)
files:
foo.md: |
## Foo
- [Bar](bar.md)
bar.md: |
# Bar
## Baz
want: |
- [Level 1](#level-1)
- [Foo](#foo)
- [Bar](#bar)
- [Level 2](#level-2)
- [Foo](#foo-1)
- [Bar](#bar-1)
- [Level 3](#level-3)
- [Foo](#foo-2)
- [Bar](#bar-2)
# Level 1
## Foo
### Bar
#### Baz
## Level 2
### Foo
#### Bar
##### Baz
### Level 3
#### Foo
##### Bar
###### Baz
2 changes: 1 addition & 1 deletion transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (t *transformer) transformEmbed(embed *markdownEmbedItem) {
(&transformer{
Log: t.Log,
InputRelPath: t.InputRelPath,
Offset: t.Offset + embed.Heading.Level(),
Offset: t.sectionOffset + embed.Item.ItemDepth() + 1,
SummaryFile: embed.SummaryFile,
}).Transform(&markdownCollection{
Sections: []*markdownSection{embed.Section},
Expand Down

0 comments on commit ba90473

Please sign in to comment.