Skip to content

Commit 8d15990

Browse files
committed
docs: format code snippets in markdown files
1 parent 70c77d5 commit 8d15990

File tree

5 files changed

+224
-133
lines changed

5 files changed

+224
-133
lines changed

.github/workflows/lint.yaml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ jobs:
6161
runs-on: ubuntu-latest
6262
steps:
6363
- uses: actions/checkout@v4
64-
- uses: DavidAnson/markdownlint-cli2-action@v20
64+
65+
- name: Setup Go
66+
uses: actions/setup-go@v5
67+
with:
68+
go-version: stable
69+
70+
- name: Setup mdsf
71+
uses: hougesen/[email protected]
72+
73+
- name: Setup formatters
74+
run: |
75+
# goimports
76+
go install golang.org/x/tools/cmd/goimports@latest
77+
# shfmt https://github.com/mvdan/sh#shfmt
78+
go install mvdan.cc/sh/v3/cmd/shfmt@latest
79+
# taplo https://taplo.tamasfe.dev/cli/installation/binary.html
80+
curl -fsSL https://github.com/tamasfe/taplo/releases/latest/download/taplo-full-linux-x86_64.gz | gzip -d - | install -m 755 /dev/stdin /usr/local/bin/taplo
81+
82+
- name: Verify files format using markdownlint-cli2
83+
uses: DavidAnson/markdownlint-cli2-action@v20
6584
with:
6685
config: .markdownlint-cli2.yaml
86+
87+
- name: Verify code snippets using mdsf
88+
run: mdsf verify --on-missing-language-definition ignore --on-missing-tool-binary fail-fast .

DEVELOPING.md

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ We can set the banned identifier by using the TOML configuration file:
6161

6262
```toml
6363
[rule.ban-struct-name]
64-
arguments = ["Foo"]
64+
arguments = ["Foo"]
6565
```
6666

6767
With the snippet above we:
@@ -89,22 +89,28 @@ type Formatter interface {
8989

9090
### Lint Markdown files
9191

92-
We are using [markdownlint](https://github.com/DavidAnson/markdownlint) for checking Markdown files.
92+
We use [markdownlint](https://github.com/DavidAnson/markdownlint) and [mdsf](https://github.com/hougesen/mdsf) to check Markdown files.
93+
`markdownlint` verifies document formatting, such as line length and empty lines, while `mdsf` is responsible for formatting code snippets.
9394

9495
1. Install [markdownlint-cli2](https://github.com/DavidAnson/markdownlint-cli2#install).
95-
2. Run the following command:
96+
2. Install [mdsf](https://mdsf.mhouge.dk/#installation) and formatters:
97+
- [goimports](https://pkg.go.dev/golang.org/x/tools/cmd/goimports) for `go`: `go install golang.org/x/tools/cmd/goimports@latest`
98+
- [shfmt](https://github.com/mvdan/sh#shfmt) for `sh, shell, bash`: `go install mvdan.cc/sh/v3/cmd/shfmt@latest`
99+
- [taplo](https://taplo.tamasfe.dev/cli/installation/binary.html) for `toml`
100+
3. Run the following command to check formatting:
96101

97102
```sh
98-
$ markdownlint-cli2 .
99-
Finding: *.{md,markdown} *.md
100-
Found:
101-
CODE_OF_CONDUCT.md
102-
CONTRIBUTING.md
103-
DEVELOPING.md
104-
README.md
105-
RULES_DESCRIPTIONS.md
106-
Linting: 5 file(s)
107-
Summary: 0 error(s)
103+
markdownlint-cli2 .
108104
```
109105

110-
The tool automatically uses the config file [.markdownlint-cli2.yaml](./.markdownlint-cli2.yaml).
106+
_The `markdownlint-cli2` tool automatically uses the config file [.markdownlint-cli2.yaml](./.markdownlint-cli2.yaml)._
107+
\
108+
4. Run the following commands to verify and format code snippets:
109+
110+
```sh
111+
mdsf verify .
112+
```
113+
114+
```sh
115+
mdsf format .
116+
```

README.md

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ A volume must be mounted to share the current repository with the container.
106106
Please refer to the [bind mounts Docker documentation](https://docs.docker.com/storage/bind-mounts/)
107107

108108
```bash
109-
docker run -v "$(pwd)":/var/<repository> ghcr.io/mgechev/revive:v1.3.7 -config /var/<repository>/revive.toml -formatter stylish ./var/kidle/...
109+
docker run -v "$(pwd)":/var/YOUR_REPOSITORY ghcr.io/mgechev/revive:v1.3.7 -config /var/YOUR_REPOSITORY/revive.toml -formatter stylish ./var/YOUR_REPOSITORY/...
110110
```
111111

112112
- `-v` is for the volume
@@ -238,6 +238,7 @@ Using comments, you can disable the linter for the entire file or only a range o
238238
//revive:disable
239239
240240
func Public() {}
241+
241242
//revive:enable
242243
```
243244

@@ -251,8 +252,9 @@ You can do the same on a rule level. In case you want to disable only a particul
251252
```go
252253
//revive:disable:unexported-return
253254
func Public() private {
254-
return private
255+
return private
255256
}
257+
256258
//revive:enable:unexported-return
257259
```
258260

@@ -278,7 +280,7 @@ in the configuration. You can set the severity (defaults to _warning_) of the vi
278280

279281
```toml
280282
[directive.specify-disable-reason]
281-
severity = "error"
283+
severity = "error"
282284
```
283285

284286
### Configuration
@@ -305,11 +307,11 @@ warningCode = 0
305307
# Configuration of the `cyclomatic` rule. Here we specify that
306308
# the rule should fail if it detects code with higher complexity than 10.
307309
[rule.cyclomatic]
308-
arguments = [10]
310+
arguments = [10]
309311

310312
# Sets the severity of the `package-comments` rule to "error".
311313
[rule.package-comments]
312-
severity = "error"
314+
severity = "error"
313315
```
314316

315317
By default `revive` will enable only the linting rules that are named in the configuration file.
@@ -328,7 +330,7 @@ For example:
328330

329331
```toml
330332
[rule.line-length-limit]
331-
Disabled = true
333+
Disabled = true
332334
```
333335

334336
When enabling all rules you still need/can provide specific configurations for rules.
@@ -346,29 +348,29 @@ enableAllRules = true
346348

347349
# Disabled rules
348350
[rule.blank-imports]
349-
Disabled = true
351+
Disabled = true
350352
[rule.file-header]
351-
Disabled = true
353+
Disabled = true
352354
[rule.max-public-structs]
353-
Disabled = true
355+
Disabled = true
354356
[rule.line-length-limit]
355-
Disabled = true
357+
Disabled = true
356358
[rule.function-length]
357-
Disabled = true
359+
Disabled = true
358360
[rule.banned-characters]
359-
Disabled = true
361+
Disabled = true
360362

361363
# Rule tuning
362364
[rule.argument-limit]
363-
Arguments = [5]
365+
Arguments = [5]
364366
[rule.cyclomatic]
365-
Arguments = [10]
367+
Arguments = [10]
366368
[rule.cognitive-complexity]
367-
Arguments = [7]
369+
Arguments = [7]
368370
[rule.function-result-limit]
369-
Arguments = [3]
371+
Arguments = [3]
370372
[rule.error-strings]
371-
Arguments = ["mypackage.Error"]
373+
Arguments = ["mypackage.Error"]
372374
```
373375

374376
### Default Configuration
@@ -440,9 +442,9 @@ errorCode = 0
440442
warningCode = 0
441443

442444
[rule.blank-imports]
443-
Exclude=["**/*.pb.go"]
445+
Exclude = ["**/*.pb.go"]
444446
[rule.context-as-argument]
445-
Exclude=["src/somepkg/*.go", "TEST"]
447+
Exclude = ["src/somepkg/*.go", "TEST"]
446448
```
447449

448450
You can use the following exclude patterns
@@ -559,7 +561,7 @@ in `golint` but optionally, you can relax it (see [golint/lint/issues/89](https:
559561

560562
```toml
561563
[rule.var-naming]
562-
arguments = [["ID"], ["VM"]]
564+
arguments = [["ID"], ["VM"]]
563565
```
564566

565567
This way, revive will not warn for an identifier called `customId` but will warn that `customVm` should be called `customVM`.
@@ -656,7 +658,9 @@ func (f myRule) Name() string {
656658
return "myRule"
657659
}
658660

659-
func (f myRule) Apply(*lint.File, lint.Arguments) []lint.Failure { ... }
661+
func (f myRule) Apply(*lint.File, lint.Arguments) []lint.Failure {
662+
// ...
663+
}
660664
```
661665

662666
You can still go further and use `revive` without its CLI, as part of your library, or your CLI:
@@ -665,39 +669,39 @@ You can still go further and use `revive` without its CLI, as part of your libra
665669
package mylib
666670

667671
import (
668-
"github.com/mgechev/revive/cli"
669-
"github.com/mgechev/revive/revivelib"
672+
"github.com/mgechev/revive/config"
670673
"github.com/mgechev/revive/lint"
674+
"github.com/mgechev/revive/revivelib"
671675
)
672676

673677
// Error checking removed for clarity
674678
func LintMyFile(file string) {
675-
conf, _:= config.GetConfig("../defaults.toml")
679+
conf, _ := config.GetConfig("../defaults.toml")
676680

677681
revive, _ := revivelib.New(
678-
conf, // Configuration file
679-
true, // Set exit status
680-
2048, // Max open files
682+
conf, // Configuration file
683+
true, // Set exit status
684+
2048, // Max open files
681685

682686
// Then add as many extra rules as you need
683687
revivelib.NewExtraRule(&myRule{}, lint.RuleConfig{}),
684688
)
685689

686690
failuresChan, err := revive.Lint(
687-
revivelib.Include(file),
688-
revivelib.Exclude("./fixtures"),
689-
// You can use as many revivelib.Include or revivelib.Exclude as required
690-
)
691-
if err != nil {
692-
panic("Shouldn't have failed: " + err.Error())
693-
}
694-
695-
// Now let's return the formatted errors
691+
revivelib.Include(file),
692+
revivelib.Exclude("./fixtures"),
693+
// You can use as many revivelib.Include or revivelib.Exclude as required
694+
)
695+
if err != nil {
696+
panic("Shouldn't have failed: " + err.Error())
697+
}
698+
699+
// Now let's return the formatted errors
696700
failures, exitCode, _ := revive.Format("stylish", failuresChan)
697701

698-
// failures is the string with all formatted lint error messages
699-
// exit code is 0 if no errors, 1 if errors (unless config options change it)
700-
// ... do something with them
702+
// failures is the string with all formatted lint error messages
703+
// exit code is 0 if no errors, 1 if errors (unless config options change it)
704+
// ... do something with them
701705
}
702706

703707
type myRule struct{}
@@ -706,7 +710,9 @@ func (f myRule) Name() string {
706710
return "myRule"
707711
}
708712

709-
func (f myRule) Apply(*lint.File, lint.Arguments) []lint.Failure { ... }
713+
func (f myRule) Apply(*lint.File, lint.Arguments) []lint.Failure {
714+
// ...
715+
}
710716
```
711717

712718
### Custom Formatter
@@ -733,8 +739,8 @@ Here's a basic performance benchmark on MacBook Pro Early 2013 run on Kubernetes
733739

734740
### golint
735741

736-
```shell
737-
time golint kubernetes/... > /dev/null
742+
```zsh
743+
$ time golint kubernetes/... > /dev/null
738744

739745
real 0m54.837s
740746
user 0m57.844s
@@ -743,9 +749,9 @@ sys 0m9.146s
743749

744750
### revive's speed
745751

746-
```shell
752+
```zsh
747753
# no type checking
748-
time revive -config untyped.toml kubernetes/... > /dev/null
754+
$ time revive -config untyped.toml kubernetes/... > /dev/null
749755

750756
real 0m8.471s
751757
user 0m40.721s
@@ -754,9 +760,9 @@ sys 0m3.262s
754760

755761
Keep in mind that if you use rules that require type checking, the performance may drop to 2x faster than `golint`:
756762

757-
```shell
763+
```zsh
758764
# type checking enabled
759-
time revive kubernetes/... > /dev/null
765+
$ time revive kubernetes/... > /dev/null
760766

761767
real 0m26.211s
762768
user 2m6.708s

0 commit comments

Comments
 (0)