-
Notifications
You must be signed in to change notification settings - Fork 894
feat: render Markdown in rich parameter descriptions #6098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ea09f5e
WIP
mtojek a3d6df1
Render description_markdown with glow
mtojek d6477c1
API tests
mtojek dfffe4d
Description plaintext
mtojek ccffe0f
Site markdown
mtojek b804fe6
Description can be optional
mtojek 63b40c1
Merge branch 'main' into 5931-description-markdown-2
mtojek b93ea13
Fix
mtojek 4dda465
Fix: mock
mtojek b0ade51
Leave newlines and links
mtojek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Render description_markdown with glow
- Loading branch information
commit a3d6df196e05251c6ab53e2645aed19497dba8ea
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,99 @@ | ||
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: " ", | ||
}, | ||
LinkText: ansi.StylePrimitive{ | ||
Format: "{{.text}}", | ||
}, | ||
ImageText: ansi.StylePrimitive{ | ||
Format: "{{.text}}", | ||
}, | ||
Image: ansi.StylePrimitive{ | ||
Format: " ", | ||
}, | ||
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 { | ||
return "" | ||
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() | ||
|
||
output = strings.ReplaceAll(output, "\n", " ") | ||
output = strings.Join(strings.Fields(output), " ") | ||
return output, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we actually want the CLI output to be this lossy? For instance:
See the registry (https://container.registry.blah/namespace) for options
.If newlines are not wanted here, perhaps we could call the field
DescriptionCondensed
instead of plaintext?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call, mate! We can safely leave them.