Skip to content

Commit 753131c

Browse files
committed
[Vale] Lint "sh" codeblocks
1 parent 87d206a commit 753131c

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
# Warning: cloudflare.ShellCodeBlocks
3+
#
4+
# Checks that all `sh` codeblocks contain at least one `$`, to indicate a command.
5+
#
6+
# For a list of all options, see https://vale.sh/docs/topics/styles/
7+
extends: script
8+
message: "**Warning**: `sh` codeblocks must include a `$` character at the beginning of a command, otherwise it will be unselectable. For more information, refer to [Code block guidelines](https://developers.cloudflare.com/style-guide/formatting/code-block-guidelines/)"
9+
level: warning
10+
scope: raw
11+
script: |
12+
text := import("text")
13+
matches := []
14+
15+
open := false
16+
closed := false
17+
dollar := false
18+
blockText := []
19+
20+
reset := func() {
21+
open = false
22+
closed = false
23+
dollar = false
24+
blockText = []
25+
}
26+
27+
for line in text.split(scope, "\n") {
28+
trimmed := text.trim_space(line)
29+
30+
if !open && trimmed == "```sh" { open = true }
31+
32+
if open {
33+
blockText = append(blockText, line)
34+
if !dollar && text.contains(trimmed, "$") { dollar = true }
35+
if trimmed == "```" { closed = true }
36+
}
37+
38+
if open && closed {
39+
if !dollar {
40+
block := text.join(blockText, "\n")
41+
start := text.index(scope, block)
42+
matches = append(matches, { begin: start, end: start + len(block) })
43+
}
44+
45+
reset()
46+
}
47+
}

0 commit comments

Comments
 (0)