Skip to content

Commit

Permalink
Rename SubList to NestedList to follow CommonMark terminology
Browse files Browse the repository at this point in the history
  • Loading branch information
teekennedy committed Dec 14, 2024
1 parent b1ec3b8 commit 1d79a6d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 33 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ log.Print(buf.String()) // # My Document Title
You can control the style of various markdown elements via functional options that are passed to
the renderer.

| Functional Option | Type | Description |
| ----------------- | ---- | ----------- |
| WithIndentStyle | markdown.IndentStyle | Indent nested blocks with spaces or tabs. |
| WithHeadingStyle | markdown.HeadingStyle | Render markdown headings as ATX (`#`-based), Setext (underlined with `===` or `---`), or variants thereof. |
| WithThematicBreakStyle | markdown.ThematicBreakStyle | Render thematic breaks with `-`, `*`, or `_`. |
| WithThematicBreakLength | markdown.ThematicBreakLength | Number of characters to use in a thematic break (minimum 3). |
| WithSubListLength | markdown.SubListLength | Number of characters to use in a sub list indentation (minimum 1). |
| Functional Option | Type | Description |
| ----------------------- | ---------------------------- | ---------------------------------------------------------------------------------------------------------- |
| WithIndentStyle | markdown.IndentStyle | Indent nested blocks with spaces or tabs. |
| WithHeadingStyle | markdown.HeadingStyle | Render markdown headings as ATX (`#`-based), Setext (underlined with `===` or `---`), or variants thereof. |
| WithThematicBreakStyle | markdown.ThematicBreakStyle | Render thematic breaks with `-`, `*`, or `_`. |
| WithThematicBreakLength | markdown.ThematicBreakLength | Number of characters to use in a thematic break (minimum 3). |
| WithNestedListLength | markdown.NestedListLength | Number of characters to use in a nested list indentation (minimum 1). |

## As a markdown transformer

Expand All @@ -58,7 +58,6 @@ transform that text into a link to the resource, similar to GitHub's [custom aut
Start by adding a struct that holds a regexp pattern to scan text for and a URL replacement for the
pattern:


```go
// RegexpLinkTransformer is an AST Transformer that transforms markdown text that matches a regex
// pattern into a link.
Expand Down
40 changes: 20 additions & 20 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Config struct {
HeadingStyle
ThematicBreakStyle
ThematicBreakLength
SubListLength
NestedListLength
}

// NewConfig returns a new Config with defaults and the given options.
Expand All @@ -20,7 +20,7 @@ func NewConfig(options ...Option) *Config {
HeadingStyle: HeadingStyle(HeadingStyleATX),
ThematicBreakStyle: ThematicBreakStyle(ThematicBreakStyleDashed),
ThematicBreakLength: ThematicBreakLength(ThematicBreakLengthMinimum),
SubListLength: SubListLength(SubListLengthMinimum),
NestedListLength: NestedListLength(NestedListLengthMinimum),
}
for _, opt := range options {
opt.SetMarkdownOption(c)
Expand All @@ -39,8 +39,8 @@ func (c *Config) SetOption(name renderer.OptionName, value interface{}) {
c.ThematicBreakStyle = value.(ThematicBreakStyle)
case optThematicBreakLength:
c.ThematicBreakLength = value.(ThematicBreakLength)
case optSubListLength:
c.SubListLength = value.(SubListLength)
case optNestedListLength:
c.NestedListLength = value.(NestedListLength)
}
}

Expand Down Expand Up @@ -233,39 +233,39 @@ func WithThematicBreakLength(style ThematicBreakLength) interface {
}

// ============================================================================
// SubListLength Option
// NestedListLength Option
// ============================================================================

// optSubListLength is an option name used in WithSubListLength
const optSubListLength renderer.OptionName = "SubListLength"
// optNestedListLength is an option name used in WithNestedListLength
const optNestedListLength renderer.OptionName = "NestedListLength"

// SubListLength configures the character length of sublist indentation
type SubListLength int
// NestedListLength configures the character length of nested list indentation
type NestedListLength int

const (
// SubListLengthMinimum is the minimum length of a sublist length. This is the default.
// NestedListLengthMinimum is the minimum length of a nested list indentation. This is the default.
// Any lengths less than this minimum are converted to the minimum.
// Ex: ---
SubListLengthMinimum = 1
NestedListLengthMinimum = 1
)

type withSubListLength struct {
value SubListLength
type withNestedListLength struct {
value NestedListLength
}

func (o *withSubListLength) SetConfig(c *renderer.Config) {
c.Options[optSubListLength] = o.value
func (o *withNestedListLength) SetConfig(c *renderer.Config) {
c.Options[optNestedListLength] = o.value
}

// SetMarkdownOption implements renderer.Option
func (o *withSubListLength) SetMarkdownOption(c *Config) {
c.SubListLength = o.value
func (o *withNestedListLength) SetMarkdownOption(c *Config) {
c.NestedListLength = o.value
}

// WithSubListLength is a functional option that sets the length of sub lists indentation.
func WithSubListLength(style SubListLength) interface {
// WithNestedListLength is a functional option that sets the length of nested list indentation.
func WithNestedListLength(style NestedListLength) interface {
renderer.Option
Option
} {
return &withSubListLength{style}
return &withNestedListLength{style}
}
4 changes: 2 additions & 2 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// TestRendererOptions tests the methods for setting configuration options on the renderer
func TestRendererOptions(t *testing.T) {
var cases = []struct {
cases := []struct {
name string
options []Option
expected *Config
Expand All @@ -26,7 +26,7 @@ func TestRendererOptions(t *testing.T) {
WithHeadingStyle(HeadingStyleATX),
WithThematicBreakStyle(ThematicBreakStyleDashed),
WithThematicBreakLength(ThematicBreakLengthMinimum),
WithSubListLength(SubListLengthMinimum),
WithNestedListLength(NestedListLengthMinimum),
},
NewConfig(),
},
Expand Down
2 changes: 1 addition & 1 deletion renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func (r *Renderer) renderListItem(node ast.Node, entering bool) ast.WalkStatus {
// Prefix the current line with the item prefix
r.rc.writer.PushPrefix(itemPrefix, 0, 0)
// Prefix subsequent lines with padding the same length as the item prefix
indentLen := int(max(r.config.SubListLength, SubListLengthMinimum))
indentLen := int(max(r.config.NestedListLength, NestedListLengthMinimum))
indent := bytes.Repeat([]byte{' '}, indentLen)
r.rc.writer.PushPrefix(bytes.Repeat(indent, len(itemPrefix)), 1)
} else {
Expand Down
4 changes: 2 additions & 2 deletions renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ func TestRenderedOutput(t *testing.T) {
"1. A1\n2. B1\n - C2\n 1. D3\n 2. E3\n - F2\n - G2\n3. H1\n",
},
{
"Sub list length",
[]Option{WithSubListLength(2)},
"Nested list length",
[]Option{WithNestedListLength(2)},
"1. A1\n2. B1\n - C2\n 1. D3\n 2. E3\n - F2\n - G2\n3. H1\n",
"1. A1\n2. B1\n - C2\n 1. D3\n 2. E3\n - F2\n - G2\n3. H1\n",
},
Expand Down

0 comments on commit 1d79a6d

Please sign in to comment.