Skip to content

Commit

Permalink
feat: add code block generation to markdown doc
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Rose <[email protected]>
  • Loading branch information
matty-rose committed Nov 27, 2021
1 parent dd48887 commit 1a85a15
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/document/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,19 @@ func (m MarkdownDocument) FormatCode(text string) string {

return fmt.Sprintf("`%s`", text)
}

const CodeBlockMarker string = "```"

func (m *MarkdownDocument) WriteCodeBlockMarker() *MarkdownDocument {
m.WriteText(CodeBlockMarker)
m.WriteNewLine()

return m
}

func (m *MarkdownDocument) WriteCodeBlockMarkerWithFormat(format string) *MarkdownDocument {
m.WriteText(fmt.Sprintf("%s%s", CodeBlockMarker, format))
m.WriteNewLine()

return m
}
36 changes: 36 additions & 0 deletions pkg/document/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,39 @@ func TestMarkdownFormatCode(t *testing.T) {
assert.Equal(t, tc.expectedCode, document.NewMarkdownDocument().FormatCode(tc.text))
}
}

func TestMarkdownWriteCodeBlockMarker(t *testing.T) {
t.Parallel()

doc := document.NewMarkdownDocument()
doc.WriteCodeBlockMarker()
assert.Equal(t, "```\n", doc.Render())
}

func TestMarkdownWriteCodeBlockMarkerFormat(t *testing.T) {
t.Parallel()

testCases := []struct {
format string
expected string
}{
{
"yaml",
"```yaml\n",
},
{
"python",
"```python\n",
},
{
"sh",
"```sh\n",
},
}

for _, tc := range testCases {
doc := document.NewMarkdownDocument()
doc.WriteCodeBlockMarkerWithFormat(tc.format)
assert.Equal(t, tc.expected, doc.Render())
}
}

0 comments on commit 1a85a15

Please sign in to comment.