Skip to content

Commit

Permalink
Rename HeadingID option to TitleID, release v0.9.0 (#52)
Browse files Browse the repository at this point in the history
The name HeadingID was a poor choice.
TitleID is more accurate.
  • Loading branch information
abhinav authored Nov 24, 2023
1 parent 34b3d3d commit b4c44d5
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .changes/v0.9.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## v0.9.0 - 2023-11-24
### Changed
- Rename HeadingID option to TitleID. HeadingID is too generic.
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.9.0 - 2023-11-24
### Changed
- Rename HeadingID option to TitleID. HeadingID is too generic.

## v0.8.0 - 2023-11-24
### Added
- Add a HeadingID option to specify a custom ID for the Table of Contents heading.
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ set the `Title` field of `Extender`.
}
```

You can specify an ID for the title heading with the `TitleID` option.

```go
&toc.Extender{
Title: "Contents",
TitleID: "toc-header",
}
```

#### Adding an ID

If you want the rendered HTML list to include an id,
Expand Down
18 changes: 9 additions & 9 deletions extend.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ type Extender struct {
// See the documentation for Transformer.ListID for more information.
ListID string

// HeadingID is the id for the Title heading rendered in the HTML.
// TitleID is the id for the Title heading rendered in the HTML.
//
// See the documentation for Transformer.HeadingID for more information.
HeadingID string
// See the documentation for Transformer.TitleID for more information.
TitleID string

// Compact controls whether empty items should be removed
// from the table of contents.
Expand All @@ -69,12 +69,12 @@ func (e *Extender) Extend(md goldmark.Markdown) {
md.Parser().AddOptions(
parser.WithASTTransformers(
util.Prioritized(&Transformer{
Title: e.Title,
MinDepth: e.MinDepth,
MaxDepth: e.MaxDepth,
ListID: e.ListID,
HeadingID: e.HeadingID,
Compact: e.Compact,
Title: e.Title,
MinDepth: e.MinDepth,
MaxDepth: e.MaxDepth,
ListID: e.ListID,
TitleID: e.TitleID,
Compact: e.Compact,
}, 100),
),
)
Expand Down
24 changes: 12 additions & 12 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ func TestIntegration(t *testing.T) {
require.NoError(t, err)

var tests []struct {
Desc string `yaml:"desc"`
Give string `yaml:"give"`
Want string `yaml:"want"`
Title string `yaml:"title"`
ListID string `yaml:"listID"`
HeadingID string `yaml:"headingID"`
Desc string `yaml:"desc"`
Give string `yaml:"give"`
Want string `yaml:"want"`
Title string `yaml:"title"`
ListID string `yaml:"listID"`
TitleID string `yaml:"titleID"`

MinDepth int `yaml:"minDepth"`
MaxDepth int `yaml:"maxDepth"`
Expand All @@ -39,12 +39,12 @@ func TestIntegration(t *testing.T) {

md := goldmark.New(
goldmark.WithExtensions(&toc.Extender{
Title: tt.Title,
MinDepth: tt.MinDepth,
MaxDepth: tt.MaxDepth,
Compact: tt.Compact,
ListID: tt.ListID,
HeadingID: tt.HeadingID,
Title: tt.Title,
MinDepth: tt.MinDepth,
MaxDepth: tt.MaxDepth,
Compact: tt.Compact,
ListID: tt.ListID,
TitleID: tt.TitleID,
}),
goldmark.WithParserOptions(parser.WithAutoHeadingID()),
)
Expand Down
4 changes: 2 additions & 2 deletions testdata/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@
<h1 id="h1">h1</h1>
<h3 id="h3">h3</h3>
- desc: custom heading ID
headingID: toc-title
- desc: custom title ID
titleID: toc-title
give: |
# Foo
Expand Down
10 changes: 5 additions & 5 deletions transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ type Transformer struct {
// The HTML element does not have an ID if ListID is empty.
ListID string

// HeadingID is the id for the Title heading rendered in the HTML.
// TitleID is the id for the Title heading rendered in the HTML.
//
// For example, if HeadingID is "toc-title",
// For example, if TitleID is "toc-title",
// the title will be rendered as:
//
// <h1 id="toc-title">Table of Contents</h1>
//
// If HeadingID is empty, a value will be requested
// If TitleID is empty, a value will be requested
// from the Goldmark Parser.
HeadingID string
TitleID string

// Compact controls whether empty items should be removed
// from the table of contents.
Expand Down Expand Up @@ -101,7 +101,7 @@ func (t *Transformer) Transform(doc *ast.Document, reader text.Reader, ctx parse
titleBytes := []byte(title)
heading := ast.NewHeading(1)
heading.AppendChild(heading, ast.NewString(titleBytes))
if id := t.HeadingID; len(id) > 0 {
if id := t.TitleID; len(id) > 0 {
heading.SetAttributeString("id", []byte(id))
} else if ids := ctx.IDs(); ids != nil {
id := ids.Generate(titleBytes, heading.Kind())
Expand Down

0 comments on commit b4c44d5

Please sign in to comment.