---
version: '3'

tasks:
  markdown:
    summary: |
      # Render Markdown with Glow

      [Glow]() provides pleasently formatted markdown
      in the terminal. This task allows you to pass in data to be formatted
      by Glow as the MARKDOWN variable or a CLI argument if the MARKDOWN
      variable is undefined. In both cases, this task will first check if
      the variable is a markdown file. It will render the file in this case.
      It can also render markdown passed in as a string.

      **Example with CLI and markdown file:**
      task log:markdown -- path/to/markdown.md

      **Example with MARKDOWN variable and inlined markdown:**
      ```
      mytask:
        cmds:
          - task: :log:markdown
            vars:
              MARKDOWN: |
                # Markdown Title

                Paragraph `code` [Link to cool website](https://megabyte.space).
      ```

      If passing in inlined markdown via the MARKDOWN library, you should ensure
      that the inlined string is properly escaped so there are no clashes with single
      quotes (e.g. ''). You can escape your strings using Task's built-in templating
      engine by using the following syntax:

      ```
      {{'{{'}} replace "'" "\'" .MY_VARIABLE {{'}}'}}
      ```
    cmds:
      - |
        TMP="$(mktemp)"
        {{if .MARKDOWN}}
        if [ -f '{{.MARKDOWN}}' ]; then
          cp '{{.MARKDOWN}}' "$TMP"
        else
          tee "$TMP" < '{{.MARKDOWN}}'
        fi
        .config/log md "$TMP"
        {{else}}
        {{if .CLI_ARGS}}
        if [ -f '{{.CLI_ARGS}}' ]; then
          cp '{{.CLI_ARGS}}' "$TMP"
        else
          tee "$TMP" < '{{replace "'" "\'" .CLI_ARGS}}'
        fi
        .config/log md "$TMP"
        {{else}}
        .config/log error 'A CLI argument or the MARKDOWN variable must be passed in.'
        {{end}}
        {{end}}
