From 4f2167db2b5ec11911cb370abd651d5457da13c1 Mon Sep 17 00:00:00 2001 From: Matthew Rose Date: Sun, 24 Oct 2021 21:37:16 +1000 Subject: [PATCH] feat: add code blocks and explicit empty sections --- pkg/document/markdown.go | 4 ++ pkg/generator/markdown.go | 105 +++++++++++++++++++++------------ pkg/generator/markdown_test.go | 46 ++++++++++++--- test.md | 26 ++++++++ 4 files changed, 135 insertions(+), 46 deletions(-) create mode 100644 test.md diff --git a/pkg/document/markdown.go b/pkg/document/markdown.go index a0f7cb5..7bfe1ef 100644 --- a/pkg/document/markdown.go +++ b/pkg/document/markdown.go @@ -126,3 +126,7 @@ func (m *MarkdownDocument) WriteTable(columns []string, rows [][]string) (*Markd func CreateMarkdownLink(title, url string) string { return fmt.Sprintf("[%s](%s)", title, url) } + +func FormatCode(text string) string { + return fmt.Sprintf("`%s`", text) +} diff --git a/pkg/generator/markdown.go b/pkg/generator/markdown.go index 185b405..ce51eed 100644 --- a/pkg/generator/markdown.go +++ b/pkg/generator/markdown.go @@ -37,56 +37,85 @@ func (mdg markdownGenerator) Generate(action *types.CompositeAction) string { doc.WriteText(action.Description) doc.WriteNewLine() - if len(action.Inputs) != 0 { - var inputs [][]string - for _, inp := range action.Inputs { - inputs = append(inputs, []string{inp.Name, inp.Description, strconv.FormatBool(inp.Required), inp.Default}) - } + doc.WriteNewLine() + doc.WriteHeading("Inputs", 2) + if len(action.Inputs) != 0 { + mdg.generateInputTable(action, doc) + } else { + doc.WriteText("No inputs.") doc.WriteNewLine() - doc.WriteHeading("Inputs", 2) - _, _ = doc.WriteTable( - []string{"Name", "Description", "Required", "Default"}, - inputs, - ) } + doc.WriteNewLine() + doc.WriteHeading("Outputs", 2) + if len(action.Outputs) != 0 { - var outputs [][]string - for _, out := range action.Outputs { - outputs = append(outputs, []string{out.Name, out.Description, out.Value}) - } + mdg.generateOutputTable(action, doc) + } else { + doc.WriteText("No outputs.") + doc.WriteNewLine() + } + + doc.WriteNewLine() + doc.WriteHeading("External Actions", 2) + if len(action.Uses) != 0 { + mdg.generateExternalActionTable(action, doc) + } else { + doc.WriteText("No external actions.") doc.WriteNewLine() - doc.WriteHeading("Outputs", 2) - _, _ = doc.WriteTable( - []string{"Name", "Description", "Value"}, - outputs, + } + + return doc.Render() +} + +func (mdg markdownGenerator) generateInputTable(act *types.CompositeAction, doc *document.MarkdownDocument) { + columns := []string{"Name", "Description", "Required", "Default"} + + var rows [][]string + for _, inp := range act.Inputs { + rows = append( + rows, + []string{ + inp.Name, + inp.Description, + strconv.FormatBool(inp.Required), + document.FormatCode(inp.Default), + }, ) } - if len(action.Uses) != 0 { - var externalActions [][]string - for _, act := range action.Uses { - externalActions = append( - externalActions, - []string{ - document.CreateMarkdownLink(act.Name, act.GetLink()), - act.Creator, - act.Version, - act.StepName, - act.StepID, - }, - ) - } + _, _ = doc.WriteTable(columns, rows) +} - doc.WriteNewLine() - doc.WriteHeading("External Actions", 2) - _, _ = doc.WriteTable( - []string{"Name", "Creator", "Version", "Step Name", "Step ID"}, - externalActions, +func (mdg markdownGenerator) generateOutputTable(act *types.CompositeAction, doc *document.MarkdownDocument) { + columns := []string{"Name", "Description", "Value"} + + var rows [][]string + for _, out := range act.Outputs { + rows = append(rows, []string{out.Name, out.Description, document.FormatCode(out.Value)}) + } + + _, _ = doc.WriteTable(columns, rows) +} + +func (mdg markdownGenerator) generateExternalActionTable(act *types.CompositeAction, doc *document.MarkdownDocument) { + columns := []string{"Name", "Creator", "Version", "Step Name", "Step ID"} + + var rows [][]string + for _, act := range act.Uses { + rows = append( + rows, + []string{ + document.CreateMarkdownLink(act.Name, act.GetLink()), + act.Creator, + act.Version, + act.StepName, + act.StepID, + }, ) } - return doc.Render() + _, _ = doc.WriteTable(columns, rows) } diff --git a/pkg/generator/markdown_test.go b/pkg/generator/markdown_test.go index cb4a7fa..d771f73 100644 --- a/pkg/generator/markdown_test.go +++ b/pkg/generator/markdown_test.go @@ -183,6 +183,15 @@ func TestGenerateMarkdownFull(t *testing.T) { func getMarkdownNameDesc() string { return `# test also test + +## Inputs +No inputs. + +## Outputs +No outputs. + +## External Actions +No external actions. ` } @@ -193,8 +202,14 @@ also test ## Inputs | Name | Description | Required | Default | | --- | --- | --- | --- | -| a | a | false | a | -| b | b | true | | +| a | a | false | ` + "`a`" + ` | +| b | b | true | ` + "``" + ` | + +## Outputs +No outputs. + +## External Actions +No external actions. ` } @@ -202,11 +217,17 @@ func getMarkdownOutputs() string { return `# test also test +## Inputs +No inputs. + ## Outputs | Name | Description | Value | | --- | --- | --- | -| a | a | a | -| b | b | b | +| a | a | ` + "`a`" + ` | +| b | b | ` + "`b`" + ` | + +## External Actions +No external actions. ` } @@ -214,6 +235,12 @@ func getMarkdownExternal() string { return `# test also test +## Inputs +No inputs. + +## Outputs +No outputs. + ## External Actions | Name | Creator | Version | Step Name | Step ID | | --- | --- | --- | --- | --- | @@ -229,13 +256,16 @@ also test ## Inputs | Name | Description | Required | Default | | --- | --- | --- | --- | -| a | a | false | a | -| b | b | true | | +| a | a | false | ` + "`a`" + ` | +| b | b | true | ` + "``" + ` | ## Outputs | Name | Description | Value | | --- | --- | --- | -| a | a | a | -| b | b | b | +| a | a | ` + "`a`" + ` | +| b | b | ` + "`b`" + ` | + +## External Actions +No external actions. ` } diff --git a/test.md b/test.md new file mode 100644 index 0000000..cf223bf --- /dev/null +++ b/test.md @@ -0,0 +1,26 @@ +# Bump Version in Package.json & Release +Increments the version following semver conventions, updates the version in package.json and creates a release with a generated changelog. + +## Inputs +| Name | Description | Required | Default | +| --- | --- | --- | --- | +| git-chglog-version | git-chglog version to install. Defaults to latest. | false | `latest` | +| go-version | Go version to install. Defaults to ^1.17. | false | `^1.17` | +| tag-prefix | Prefix to add to the created tag. | true | `` | +| project-name | The project scope this change relates to | true | `` | +| project-path | The directory to the package.json to update | false | `.` | +| changelog-config | Path to the configuration file for git-chglog. | false | `.chglog/releaselog-config.yaml` | + +## Outputs +| Name | Description | Value | +| --- | --- | --- | +| new-tag | New tag created for the release. | `${{ steps.bump-version.outputs.new_tag }}` | + +## External Actions +| Name | Creator | Version | Step Name | Step ID | +| --- | --- | --- | --- | --- | +| [setup-go](https://github.com/actions/setup-go/tree/v2) | actions | v2 | Set up go | | +| [cache](https://github.com/actions/cache/tree/v2) | actions | v2 | Set up Go cache | | +| [github-tag-action](https://github.com/mathieudutour/github-tag-action/tree/v5.6) | mathieudutour | v5.6 | Bump version | bump-version | +| [git-auto-commit-action](https://github.com/stefanzweifel/git-auto-commit-action/tree/v4) | stefanzweifel | v4 | Commit version | auto-commit-action | +| [release-action](https://github.com/ncipollo/release-action/tree/v1) | ncipollo | v1 | Create release | |