mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
feat: render Markdown in rich parameter descriptions (#6098)
This commit is contained in:
97
coderd/parameter/plaintext.go
Normal file
97
coderd/parameter/plaintext.go
Normal file
@ -0,0 +1,97 @@
|
||||
package parameter
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/glamour"
|
||||
"github.com/charmbracelet/glamour/ansi"
|
||||
"golang.org/x/xerrors"
|
||||
)
|
||||
|
||||
var plaintextStyle = ansi.StyleConfig{
|
||||
Document: ansi.StyleBlock{
|
||||
StylePrimitive: ansi.StylePrimitive{},
|
||||
},
|
||||
BlockQuote: ansi.StyleBlock{
|
||||
StylePrimitive: ansi.StylePrimitive{},
|
||||
},
|
||||
Paragraph: ansi.StyleBlock{
|
||||
StylePrimitive: ansi.StylePrimitive{},
|
||||
},
|
||||
List: ansi.StyleList{
|
||||
StyleBlock: ansi.StyleBlock{
|
||||
StylePrimitive: ansi.StylePrimitive{},
|
||||
},
|
||||
LevelIndent: 4,
|
||||
},
|
||||
Heading: ansi.StyleBlock{
|
||||
StylePrimitive: ansi.StylePrimitive{},
|
||||
},
|
||||
H1: ansi.StyleBlock{
|
||||
StylePrimitive: ansi.StylePrimitive{},
|
||||
},
|
||||
H2: ansi.StyleBlock{
|
||||
StylePrimitive: ansi.StylePrimitive{},
|
||||
},
|
||||
H3: ansi.StyleBlock{
|
||||
StylePrimitive: ansi.StylePrimitive{},
|
||||
},
|
||||
H4: ansi.StyleBlock{
|
||||
StylePrimitive: ansi.StylePrimitive{},
|
||||
},
|
||||
H5: ansi.StyleBlock{
|
||||
StylePrimitive: ansi.StylePrimitive{},
|
||||
},
|
||||
H6: ansi.StyleBlock{
|
||||
StylePrimitive: ansi.StylePrimitive{},
|
||||
},
|
||||
Strikethrough: ansi.StylePrimitive{},
|
||||
Emph: ansi.StylePrimitive{},
|
||||
Strong: ansi.StylePrimitive{},
|
||||
HorizontalRule: ansi.StylePrimitive{},
|
||||
Item: ansi.StylePrimitive{},
|
||||
Enumeration: ansi.StylePrimitive{
|
||||
BlockPrefix: ". ",
|
||||
}, Task: ansi.StyleTask{},
|
||||
Link: ansi.StylePrimitive{
|
||||
Format: "({{.text}})",
|
||||
},
|
||||
LinkText: ansi.StylePrimitive{
|
||||
Format: "{{.text}}",
|
||||
},
|
||||
ImageText: ansi.StylePrimitive{
|
||||
Format: "{{.text}}",
|
||||
},
|
||||
Image: ansi.StylePrimitive{
|
||||
Format: "({{.text}})",
|
||||
},
|
||||
Code: ansi.StyleBlock{
|
||||
StylePrimitive: ansi.StylePrimitive{},
|
||||
},
|
||||
CodeBlock: ansi.StyleCodeBlock{
|
||||
StyleBlock: ansi.StyleBlock{},
|
||||
},
|
||||
Table: ansi.StyleTable{},
|
||||
DefinitionDescription: ansi.StylePrimitive{},
|
||||
}
|
||||
|
||||
// Plaintext function converts the description with optional Markdown tags
|
||||
// to the plaintext form.
|
||||
func Plaintext(markdown string) (string, error) {
|
||||
renderer, err := glamour.NewTermRenderer(
|
||||
glamour.WithStandardStyle("ascii"),
|
||||
glamour.WithWordWrap(0), // don't need to add spaces in the end of line
|
||||
glamour.WithStyles(plaintextStyle),
|
||||
)
|
||||
if err != nil {
|
||||
return "", xerrors.Errorf("can't initialize the Markdown renderer: %w", err)
|
||||
}
|
||||
|
||||
output, err := renderer.Render(markdown)
|
||||
if err != nil {
|
||||
return "", xerrors.Errorf("can't render description to plaintext: %w", err)
|
||||
}
|
||||
defer renderer.Close()
|
||||
|
||||
return strings.TrimSpace(output), nil
|
||||
}
|
Reference in New Issue
Block a user