fix: linter errors (#263)

* refactor: extract magic numbers to const

* fix: linter errors
This commit is contained in:
Maas Lalani
2023-10-02 19:28:35 -07:00
committed by GitHub
parent ea723982eb
commit 0aaa280081
7 changed files with 40 additions and 130 deletions
+1 -1
View File
@@ -57,7 +57,7 @@ func (s BlockStack) Margin() uint {
return i return i
} }
// Width returns the available rendering width // Width returns the available rendering width.
func (s BlockStack) Width(ctx RenderContext) uint { func (s BlockStack) Width(ctx RenderContext) uint {
if s.Indent()+s.Margin()*2 > uint(ctx.options.WordWrap) { if s.Indent()+s.Margin()*2 > uint(ctx.options.WordWrap) {
return 0 return 0
+15 -6
View File
@@ -14,22 +14,31 @@ type HeadingElement struct {
First bool First bool
} }
const (
h1 = iota + 1
h2
h3
h4
h5
h6
)
func (e *HeadingElement) Render(w io.Writer, ctx RenderContext) error { func (e *HeadingElement) Render(w io.Writer, ctx RenderContext) error {
bs := ctx.blockStack bs := ctx.blockStack
rules := ctx.options.Styles.Heading rules := ctx.options.Styles.Heading
switch e.Level { switch e.Level {
case 1: case h1:
rules = cascadeStyles(true, rules, ctx.options.Styles.H1) rules = cascadeStyles(true, rules, ctx.options.Styles.H1)
case 2: case h2:
rules = cascadeStyles(true, rules, ctx.options.Styles.H2) rules = cascadeStyles(true, rules, ctx.options.Styles.H2)
case 3: case h3:
rules = cascadeStyles(true, rules, ctx.options.Styles.H3) rules = cascadeStyles(true, rules, ctx.options.Styles.H3)
case 4: case h4:
rules = cascadeStyles(true, rules, ctx.options.Styles.H4) rules = cascadeStyles(true, rules, ctx.options.Styles.H4)
case 5: case h5:
rules = cascadeStyles(true, rules, ctx.options.Styles.H5) rules = cascadeStyles(true, rules, ctx.options.Styles.H5)
case 6: case h6:
rules = cascadeStyles(true, rules, ctx.options.Styles.H6) rules = cascadeStyles(true, rules, ctx.options.Styles.H6)
} }
+1 -1
View File
@@ -9,7 +9,7 @@ type ImageElement struct {
Text string Text string
BaseURL string BaseURL string
URL string URL string
Child ElementRenderer // FIXME Child ElementRenderer
} }
func (e *ImageElement) Render(w io.Writer, ctx RenderContext) error { func (e *ImageElement) Render(w io.Writer, ctx RenderContext) error {
+1 -1
View File
@@ -10,7 +10,7 @@ type LinkElement struct {
Text string Text string
BaseURL string BaseURL string
URL string URL string
Child ElementRenderer // FIXME Child ElementRenderer
} }
func (e *LinkElement) Render(w io.Writer, ctx RenderContext) error { func (e *LinkElement) Render(w io.Writer, ctx RenderContext) error {
+1 -1
View File
@@ -6,7 +6,7 @@ import (
"text/template" "text/template"
) )
// TemplateFuncMap contains a few useful template helpers // TemplateFuncMap contains a few useful template helpers.
var ( var (
TemplateFuncMap = template.FuncMap{ TemplateFuncMap = template.FuncMap{
"Left": func(values ...interface{}) string { "Left": func(values ...interface{}) string {
+7 -5
View File
@@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"github.com/muesli/termenv" "github.com/muesli/termenv"
@@ -29,6 +28,9 @@ const (
PinkStyle = "pink" PinkStyle = "pink"
) )
const defaultWidth = 80
const highPriority = 1000
// A TermRendererOption sets an option on a TermRenderer. // A TermRendererOption sets an option on a TermRenderer.
type TermRendererOption func(*TermRenderer) error type TermRendererOption func(*TermRenderer) error
@@ -80,7 +82,7 @@ func NewTermRenderer(options ...TermRendererOption) (*TermRenderer, error) {
), ),
), ),
ansiOptions: ansi.Options{ ansiOptions: ansi.Options{
WordWrap: 80, WordWrap: defaultWidth,
ColorProfile: termenv.TrueColor, ColorProfile: termenv.TrueColor,
}, },
} }
@@ -93,7 +95,7 @@ func NewTermRenderer(options ...TermRendererOption) (*TermRenderer, error) {
tr.md.SetRenderer( tr.md.SetRenderer(
renderer.NewRenderer( renderer.NewRenderer(
renderer.WithNodeRenderers( renderer.WithNodeRenderers(
util.Prioritized(ar, 1000), util.Prioritized(ar, highPriority),
), ),
), ),
) )
@@ -149,7 +151,7 @@ func WithStylePath(stylePath string) TermRendererOption {
return func(tr *TermRenderer) error { return func(tr *TermRenderer) error {
styles, err := getDefaultStyle(stylePath) styles, err := getDefaultStyle(stylePath)
if err != nil { if err != nil {
jsonBytes, err := ioutil.ReadFile(stylePath) jsonBytes, err := os.ReadFile(stylePath)
if err != nil { if err != nil {
return err return err
} }
@@ -180,7 +182,7 @@ func WithStylesFromJSONBytes(jsonBytes []byte) TermRendererOption {
// WithStylesFromJSONFile sets a TermRenderer's styles from a JSON file. // WithStylesFromJSONFile sets a TermRenderer's styles from a JSON file.
func WithStylesFromJSONFile(filename string) TermRendererOption { func WithStylesFromJSONFile(filename string) TermRendererOption {
return func(tr *TermRenderer) error { return func(tr *TermRenderer) error {
jsonBytes, err := ioutil.ReadFile(filename) jsonBytes, err := os.ReadFile(filename)
if err != nil { if err != nil {
return err return err
} }
+14 -115
View File
@@ -6,6 +6,9 @@ import (
"github.com/charmbracelet/glamour/ansi" "github.com/charmbracelet/glamour/ansi"
) )
const defaultListIndent = 2
const defaultMargin = 2
var ( var (
// ASCIIStyleConfig uses only ASCII characters. // ASCIIStyleConfig uses only ASCII characters.
ASCIIStyleConfig = ansi.StyleConfig{ ASCIIStyleConfig = ansi.StyleConfig{
@@ -14,7 +17,7 @@ var (
BlockPrefix: "\n", BlockPrefix: "\n",
BlockSuffix: "\n", BlockSuffix: "\n",
}, },
Margin: uintPtr(2), Margin: uintPtr(defaultMargin),
}, },
BlockQuote: ansi.StyleBlock{ BlockQuote: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{}, StylePrimitive: ansi.StylePrimitive{},
@@ -101,7 +104,7 @@ var (
}, },
CodeBlock: ansi.StyleCodeBlock{ CodeBlock: ansi.StyleCodeBlock{
StyleBlock: ansi.StyleBlock{ StyleBlock: ansi.StyleBlock{
Margin: uintPtr(2), Margin: uintPtr(defaultMargin),
}, },
}, },
Table: ansi.StyleTable{ Table: ansi.StyleTable{
@@ -122,7 +125,7 @@ var (
BlockSuffix: "\n", BlockSuffix: "\n",
Color: stringPtr("252"), Color: stringPtr("252"),
}, },
Margin: uintPtr(2), Margin: uintPtr(defaultMargin),
}, },
BlockQuote: ansi.StyleBlock{ BlockQuote: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{}, StylePrimitive: ansi.StylePrimitive{},
@@ -130,7 +133,7 @@ var (
IndentToken: stringPtr("│ "), IndentToken: stringPtr("│ "),
}, },
List: ansi.StyleList{ List: ansi.StyleList{
LevelIndent: 2, LevelIndent: defaultListIndent,
}, },
Heading: ansi.StyleBlock{ Heading: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{ StylePrimitive: ansi.StylePrimitive{
@@ -228,7 +231,7 @@ var (
StylePrimitive: ansi.StylePrimitive{ StylePrimitive: ansi.StylePrimitive{
Color: stringPtr("244"), Color: stringPtr("244"),
}, },
Margin: uintPtr(2), Margin: uintPtr(defaultMargin),
}, },
Chroma: &ansi.Chroma{ Chroma: &ansi.Chroma{
Text: ansi.StylePrimitive{ Text: ansi.StylePrimitive{
@@ -335,7 +338,7 @@ var (
BlockSuffix: "\n", BlockSuffix: "\n",
Color: stringPtr("234"), Color: stringPtr("234"),
}, },
Margin: uintPtr(2), Margin: uintPtr(defaultMargin),
}, },
BlockQuote: ansi.StyleBlock{ BlockQuote: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{}, StylePrimitive: ansi.StylePrimitive{},
@@ -343,7 +346,7 @@ var (
IndentToken: stringPtr("│ "), IndentToken: stringPtr("│ "),
}, },
List: ansi.StyleList{ List: ansi.StyleList{
LevelIndent: 2, LevelIndent: defaultListIndent,
}, },
Heading: ansi.StyleBlock{ Heading: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{ StylePrimitive: ansi.StylePrimitive{
@@ -440,7 +443,7 @@ var (
StylePrimitive: ansi.StylePrimitive{ StylePrimitive: ansi.StylePrimitive{
Color: stringPtr("242"), Color: stringPtr("242"),
}, },
Margin: uintPtr(2), Margin: uintPtr(defaultMargin),
}, },
Chroma: &ansi.Chroma{ Chroma: &ansi.Chroma{
Text: ansi.StylePrimitive{ Text: ansi.StylePrimitive{
@@ -542,14 +545,14 @@ var (
// PinkStyleConfig is the default pink style. // PinkStyleConfig is the default pink style.
PinkStyleConfig = ansi.StyleConfig{ PinkStyleConfig = ansi.StyleConfig{
Document: ansi.StyleBlock{ Document: ansi.StyleBlock{
Margin: uintPtr(2), Margin: uintPtr(defaultMargin),
}, },
BlockQuote: ansi.StyleBlock{ BlockQuote: ansi.StyleBlock{
Indent: uintPtr(1), Indent: uintPtr(1),
IndentToken: stringPtr("│ "), IndentToken: stringPtr("│ "),
}, },
List: ansi.StyleList{ List: ansi.StyleList{
LevelIndent: 2, LevelIndent: defaultListIndent,
}, },
Heading: ansi.StyleBlock{ Heading: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{ StylePrimitive: ansi.StylePrimitive{
@@ -651,111 +654,7 @@ var (
} }
// NoTTYStyleConfig is the default notty style. // NoTTYStyleConfig is the default notty style.
NoTTYStyleConfig = ansi.StyleConfig{ NoTTYStyleConfig = ASCIIStyleConfig
Document: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
BlockPrefix: "\n",
BlockSuffix: "\n",
},
Margin: uintPtr(2),
},
BlockQuote: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{},
Indent: uintPtr(1),
IndentToken: stringPtr("│ "),
},
Paragraph: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{},
},
List: ansi.StyleList{
StyleBlock: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{},
},
LevelIndent: 4,
},
Heading: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
BlockSuffix: "\n",
},
},
H1: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Prefix: "# ",
},
},
H2: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Prefix: "## ",
},
},
H3: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Prefix: "### ",
},
},
H4: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Prefix: "#### ",
},
},
H5: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Prefix: "##### ",
},
},
H6: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
Prefix: "###### ",
},
},
Strikethrough: ansi.StylePrimitive{
BlockPrefix: "~~",
BlockSuffix: "~~",
},
Emph: ansi.StylePrimitive{
BlockPrefix: "*",
BlockSuffix: "*",
},
Strong: ansi.StylePrimitive{
BlockPrefix: "**",
BlockSuffix: "**",
},
HorizontalRule: ansi.StylePrimitive{
Format: "\n--------\n",
},
Item: ansi.StylePrimitive{
BlockPrefix: "• ",
},
Enumeration: ansi.StylePrimitive{
BlockPrefix: ". ",
},
Task: ansi.StyleTask{
Ticked: "[✓] ",
Unticked: "[ ] ",
},
ImageText: ansi.StylePrimitive{
Format: "Image: {{.text}} →",
},
Code: ansi.StyleBlock{
StylePrimitive: ansi.StylePrimitive{
BlockPrefix: "`",
BlockSuffix: "`",
},
},
CodeBlock: ansi.StyleCodeBlock{
StyleBlock: ansi.StyleBlock{
Margin: uintPtr(2),
},
},
Table: ansi.StyleTable{
CenterSeparator: stringPtr("┼"),
ColumnSeparator: stringPtr("│"),
RowSeparator: stringPtr("─"),
},
DefinitionDescription: ansi.StylePrimitive{
BlockPrefix: "\n🠶 ",
},
}
// DefaultStyles are the default styles. // DefaultStyles are the default styles.
DefaultStyles = map[string]*ansi.StyleConfig{ DefaultStyles = map[string]*ansi.StyleConfig{