Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ GitHub Action that generates beautiful, easy-to-read markdown tables with just a

## Usage

Add the `Inputs` and/or `Outputs` and/or `Secrets` (only supported by reusable workflows) [`H2` header](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#headers) to any markdown file.
Add the `Inputs` and/or `Outputs`
and/or `Secrets` (only supported by reusable workflows)
and/or `Description` (only supported by actions)
[`H2` header](https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet#headers) to any markdown file.

```yaml
...
Expand All @@ -61,21 +64,21 @@ Add the `Inputs` and/or `Outputs` and/or `Secrets` (only supported by reusable w

<!-- AUTO-DOC-INPUT:START - Do not remove or modify this section -->

| INPUT | TYPE | REQUIRED | DEFAULT | DESCRIPTION |
|-------------------------|--------|----------|----------------|--------------------------------------------------------------------------------------------------|
| bin\_path | string | false | | Path to the auto-doc binary |
| col\_max\_width | string | false | `"1000"` | Max width of a column |
| col\_max\_words | string | false | `"5"` | Max number of words per line <br>in a column |
| filename | string | false | `"action.yml"` | Path to the yaml file |
| input\_columns | string | false | | List of action.yml **input** columns names <br>to display, default (display all columns) |
| markdown\_links | string | false | `"true"` | Boolean indicating whether to output input, <br>output and secret names as markdown <br>links |
| output | string | false | `"README.md"` | Path to the output file |
| output\_columns | string | false | | List of action.yml **output** column names <br>to display, default (display all columns) |
| reusable | string | false | | Boolean Indicating whether the file is <br>a reusable workflow |
| reusable\_input\_columns | string | false | | List of reusable workflow **input** column <br>names to display, default (display all columns) |
| reusable\_output\_columns | string | false | | List of reusable workflow **output** column <br>names to display, default (display all columns) |
| reusable\_secret\_columns | string | false | | List of reusable workflow **secret** column <br>names to display, default (display all columns) |
| version | string | false | | The version number to run |
| INPUT | TYPE | REQUIRED | DEFAULT | DESCRIPTION |
|---------------------------|--------|----------|----------------|-------------------------------------------------------------------------------------------------|
| bin\_path | string | false | | Path to the auto-doc binary |
| col\_max\_width | string | false | `"1000"` | Max width of a column |
| col\_max\_words | string | false | `"5"` | Max number of words per line <br>in a column |
| filename | string | false | `"action.yml"` | Path to the yaml file |
| input\_columns | string | false | | List of action.yml **input** columns names <br>to display, default (display all columns) |
| markdown\_links | string | false | `"true"` | Boolean indicating whether to output input, <br>output and secret names as markdown <br>links |
| output | string | false | `"README.md"` | Path to the output file |
| output\_columns | string | false | | List of action.yml **output** column names <br>to display, default (display all columns) |
| reusable | string | false | | Boolean Indicating whether the file is <br>a reusable workflow |
| reusable\_input\_columns | string | false | | List of reusable workflow **input** column <br>names to display, default (display all columns) |
| reusable\_output\_columns | string | false | | List of reusable workflow **output** column <br>names to display, default (display all columns) |
| reusable\_secret\_columns | string | false | | List of reusable workflow **secret** column <br>names to display, default (display all columns) |
| version | string | false | | The version number to run |

<!-- AUTO-DOC-INPUT:END -->

Expand Down
9 changes: 9 additions & 0 deletions internal/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ package internal

import "fmt"

// DescriptionHeader represents the markdown header of description
const DescriptionHeader = "## Description"

// InputsHeader represents the markdown header of inputs
const InputsHeader = "## Inputs"

Expand All @@ -39,6 +42,12 @@ const PipeSeparator = "|"
// NewLineSeparator used for splitting lines
const NewLineSeparator = "\n"

// DescriptionAutoDocStart is the start of the description
var DescriptionAutoDocStart = fmt.Sprintf(AutoDocStart, "DESCRIPTION")

// DescriptionAutoDocEnd is the end of the description
var DescriptionAutoDocEnd = fmt.Sprintf(AutoDocEnd, "DESCRIPTION")

// InputAutoDocStart is the start of the input
var InputAutoDocStart = fmt.Sprintf(AutoDocStart, "INPUT")

Expand Down
60 changes: 55 additions & 5 deletions internal/types/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Action struct {
OutputColumns []string
Inputs map[string]ActionInput `yaml:"inputs,omitempty"`
Outputs map[string]ActionOutput `yaml:"outputs,omitempty"`
Description string `yaml:"description,omitempty"`
InputMarkdownLinks bool
}

Expand All @@ -72,7 +73,7 @@ func (a *Action) GetData() error {
}

// WriteDocumentation write the table to the output file
func (a *Action) WriteDocumentation(inputTable, outputTable *strings.Builder) error {
func (a *Action) WriteDocumentation(inputTable, outputTable, description *strings.Builder) error {
var err error
input, err := os.ReadFile(a.OutputFileName)
if err != nil {
Expand All @@ -81,13 +82,36 @@ func (a *Action) WriteDocumentation(inputTable, outputTable *strings.Builder) er

var output []byte

hasInputsData, indices := utils.HasBytesInBetween(
hasDescriptionData, indices := utils.HasBytesInBetween(
input,
[]byte(internal.DescriptionAutoDocStart),
[]byte(internal.DescriptionAutoDocEnd),
)
output = input

descriptionStr := strings.TrimSpace(description.String())

if hasDescriptionData {
output = utils.ReplaceBytesInBetween(output, indices, []byte(descriptionStr))
} else {
re := regexp.MustCompile(fmt.Sprintf("(?m)^%s", internal.DescriptionHeader))
output = re.ReplaceAllFunc(input, func(match []byte) []byte {
if bytes.HasPrefix(match, []byte(internal.DescriptionHeader)) {
if descriptionStr != "" {
return []byte(fmt.Sprintf("%s\n\n%v", internal.DescriptionHeader, descriptionStr))
}
return []byte(internal.DescriptionHeader)
}
return match
})
}

hasInputsData, indices := utils.HasBytesInBetween(
output,
[]byte(internal.InputAutoDocStart),
[]byte(internal.InputAutoDocEnd),
)

output = input
inputsStr := strings.TrimSpace(inputTable.String())

if hasInputsData {
Expand All @@ -105,7 +129,6 @@ func (a *Action) WriteDocumentation(inputTable, outputTable *strings.Builder) er
return match
})
}

hasOutputsData, indices := utils.HasBytesInBetween(
output,
[]byte(internal.OutputAutoDocStart),
Expand Down Expand Up @@ -150,6 +173,11 @@ func (a *Action) RenderOutput() error {
return err
}

descriptionOutput, err := renderDescription(a.Description)
if err != nil {
return err
}

inputTableOutput, err := renderActionInputTableOutput(a.Inputs, a.InputColumns, a.InputMarkdownLinks, maxWidth, maxWords)
if err != nil {
return err
Expand All @@ -160,14 +188,36 @@ func (a *Action) RenderOutput() error {
return err
}

err = a.WriteDocumentation(inputTableOutput, outputTableOutput)
err = a.WriteDocumentation(inputTableOutput, outputTableOutput, descriptionOutput)
if err != nil {
return err
}

return nil
}

// renderDescription
func renderDescription(description string) (*strings.Builder, error) {
descriptionOutput := &strings.Builder{}
_, err := fmt.Fprintln(descriptionOutput, internal.DescriptionAutoDocStart)
if err != nil {
return descriptionOutput, err
}

_, err = fmt.Fprintln(descriptionOutput)
if err != nil {
return descriptionOutput, err
}
descriptionOutput.WriteString(description)
descriptionOutput.WriteString("\n")

_, err = fmt.Fprint(descriptionOutput, internal.DescriptionAutoDocEnd)
if err != nil {
return descriptionOutput, err
}
return descriptionOutput, nil
}

// renderActionOutputTableOutput renders the action input table

func renderActionInputTableOutput(inputs map[string]ActionInput, inputColumns []string, markdownLinks bool, maxWidth int, maxWords int) (*strings.Builder, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

Test text ## Inputs

## Description
<!-- AUTO-DOC-DESCRIPTION:START - Do not remove or modify this section -->
No description.
<!-- AUTO-DOC-DESCRIPTION:END -->

## Inputs

<!-- AUTO-DOC-INPUT:START - Do not remove or modify this section -->
Expand Down
6 changes: 6 additions & 0 deletions test/README-action-empty-markers.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# test/action

## Description

<!-- AUTO-DOC-DESCRIPTION:START - Do not remove or modify this section -->
No description.
<!-- AUTO-DOC-DESCRIPTION:END -->

Test text ## Inputs

## Inputs
Expand Down