diff --git a/ansi/blockstack.go b/ansi/blockstack.go index 96fdb91..5440dcb 100644 --- a/ansi/blockstack.go +++ b/ansi/blockstack.go @@ -57,7 +57,7 @@ func (s BlockStack) Margin() uint { return i } -// Width returns the available rendering width +// Width returns the available rendering width. func (s BlockStack) Width(ctx RenderContext) uint { if s.Indent()+s.Margin()*2 > uint(ctx.options.WordWrap) { return 0 diff --git a/ansi/heading.go b/ansi/heading.go index e7ff939..46ff8ad 100644 --- a/ansi/heading.go +++ b/ansi/heading.go @@ -14,22 +14,31 @@ type HeadingElement struct { First bool } +const ( + h1 = iota + 1 + h2 + h3 + h4 + h5 + h6 +) + func (e *HeadingElement) Render(w io.Writer, ctx RenderContext) error { bs := ctx.blockStack rules := ctx.options.Styles.Heading switch e.Level { - case 1: + case h1: rules = cascadeStyles(true, rules, ctx.options.Styles.H1) - case 2: + case h2: rules = cascadeStyles(true, rules, ctx.options.Styles.H2) - case 3: + case h3: rules = cascadeStyles(true, rules, ctx.options.Styles.H3) - case 4: + case h4: rules = cascadeStyles(true, rules, ctx.options.Styles.H4) - case 5: + case h5: rules = cascadeStyles(true, rules, ctx.options.Styles.H5) - case 6: + case h6: rules = cascadeStyles(true, rules, ctx.options.Styles.H6) } diff --git a/ansi/image.go b/ansi/image.go index f5edfaf..ad9fe2c 100644 --- a/ansi/image.go +++ b/ansi/image.go @@ -9,7 +9,7 @@ type ImageElement struct { Text string BaseURL string URL string - Child ElementRenderer // FIXME + Child ElementRenderer } func (e *ImageElement) Render(w io.Writer, ctx RenderContext) error { diff --git a/ansi/link.go b/ansi/link.go index 4cb5931..9028395 100644 --- a/ansi/link.go +++ b/ansi/link.go @@ -10,7 +10,7 @@ type LinkElement struct { Text string BaseURL string URL string - Child ElementRenderer // FIXME + Child ElementRenderer } func (e *LinkElement) Render(w io.Writer, ctx RenderContext) error { diff --git a/ansi/templatehelper.go b/ansi/templatehelper.go index 2b41820..a82d75c 100644 --- a/ansi/templatehelper.go +++ b/ansi/templatehelper.go @@ -6,7 +6,7 @@ import ( "text/template" ) -// TemplateFuncMap contains a few useful template helpers +// TemplateFuncMap contains a few useful template helpers. var ( TemplateFuncMap = template.FuncMap{ "Left": func(values ...interface{}) string { diff --git a/glamour.go b/glamour.go index 8cb7b7c..ad52cea 100644 --- a/glamour.go +++ b/glamour.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" "os" "github.com/muesli/termenv" @@ -29,6 +28,9 @@ const ( PinkStyle = "pink" ) +const defaultWidth = 80 +const highPriority = 1000 + // A TermRendererOption sets an option on a TermRenderer. type TermRendererOption func(*TermRenderer) error @@ -80,7 +82,7 @@ func NewTermRenderer(options ...TermRendererOption) (*TermRenderer, error) { ), ), ansiOptions: ansi.Options{ - WordWrap: 80, + WordWrap: defaultWidth, ColorProfile: termenv.TrueColor, }, } @@ -93,7 +95,7 @@ func NewTermRenderer(options ...TermRendererOption) (*TermRenderer, error) { tr.md.SetRenderer( renderer.NewRenderer( renderer.WithNodeRenderers( - util.Prioritized(ar, 1000), + util.Prioritized(ar, highPriority), ), ), ) @@ -149,7 +151,7 @@ func WithStylePath(stylePath string) TermRendererOption { return func(tr *TermRenderer) error { styles, err := getDefaultStyle(stylePath) if err != nil { - jsonBytes, err := ioutil.ReadFile(stylePath) + jsonBytes, err := os.ReadFile(stylePath) if err != nil { return err } @@ -180,7 +182,7 @@ func WithStylesFromJSONBytes(jsonBytes []byte) TermRendererOption { // WithStylesFromJSONFile sets a TermRenderer's styles from a JSON file. func WithStylesFromJSONFile(filename string) TermRendererOption { return func(tr *TermRenderer) error { - jsonBytes, err := ioutil.ReadFile(filename) + jsonBytes, err := os.ReadFile(filename) if err != nil { return err } diff --git a/styles.go b/styles.go index b83aa32..7089145 100644 --- a/styles.go +++ b/styles.go @@ -6,6 +6,9 @@ import ( "github.com/charmbracelet/glamour/ansi" ) +const defaultListIndent = 2 +const defaultMargin = 2 + var ( // ASCIIStyleConfig uses only ASCII characters. ASCIIStyleConfig = ansi.StyleConfig{ @@ -14,7 +17,7 @@ var ( BlockPrefix: "\n", BlockSuffix: "\n", }, - Margin: uintPtr(2), + Margin: uintPtr(defaultMargin), }, BlockQuote: ansi.StyleBlock{ StylePrimitive: ansi.StylePrimitive{}, @@ -101,7 +104,7 @@ var ( }, CodeBlock: ansi.StyleCodeBlock{ StyleBlock: ansi.StyleBlock{ - Margin: uintPtr(2), + Margin: uintPtr(defaultMargin), }, }, Table: ansi.StyleTable{ @@ -122,7 +125,7 @@ var ( BlockSuffix: "\n", Color: stringPtr("252"), }, - Margin: uintPtr(2), + Margin: uintPtr(defaultMargin), }, BlockQuote: ansi.StyleBlock{ StylePrimitive: ansi.StylePrimitive{}, @@ -130,7 +133,7 @@ var ( IndentToken: stringPtr("│ "), }, List: ansi.StyleList{ - LevelIndent: 2, + LevelIndent: defaultListIndent, }, Heading: ansi.StyleBlock{ StylePrimitive: ansi.StylePrimitive{ @@ -228,7 +231,7 @@ var ( StylePrimitive: ansi.StylePrimitive{ Color: stringPtr("244"), }, - Margin: uintPtr(2), + Margin: uintPtr(defaultMargin), }, Chroma: &ansi.Chroma{ Text: ansi.StylePrimitive{ @@ -335,7 +338,7 @@ var ( BlockSuffix: "\n", Color: stringPtr("234"), }, - Margin: uintPtr(2), + Margin: uintPtr(defaultMargin), }, BlockQuote: ansi.StyleBlock{ StylePrimitive: ansi.StylePrimitive{}, @@ -343,7 +346,7 @@ var ( IndentToken: stringPtr("│ "), }, List: ansi.StyleList{ - LevelIndent: 2, + LevelIndent: defaultListIndent, }, Heading: ansi.StyleBlock{ StylePrimitive: ansi.StylePrimitive{ @@ -440,7 +443,7 @@ var ( StylePrimitive: ansi.StylePrimitive{ Color: stringPtr("242"), }, - Margin: uintPtr(2), + Margin: uintPtr(defaultMargin), }, Chroma: &ansi.Chroma{ Text: ansi.StylePrimitive{ @@ -542,14 +545,14 @@ var ( // PinkStyleConfig is the default pink style. PinkStyleConfig = ansi.StyleConfig{ Document: ansi.StyleBlock{ - Margin: uintPtr(2), + Margin: uintPtr(defaultMargin), }, BlockQuote: ansi.StyleBlock{ Indent: uintPtr(1), IndentToken: stringPtr("│ "), }, List: ansi.StyleList{ - LevelIndent: 2, + LevelIndent: defaultListIndent, }, Heading: ansi.StyleBlock{ StylePrimitive: ansi.StylePrimitive{ @@ -651,111 +654,7 @@ var ( } // NoTTYStyleConfig is the default notty style. - NoTTYStyleConfig = ansi.StyleConfig{ - 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🠶 ", - }, - } + NoTTYStyleConfig = ASCIIStyleConfig // DefaultStyles are the default styles. DefaultStyles = map[string]*ansi.StyleConfig{