Skip to content

Commit

Permalink
Handle common shell block pre- and suffixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sourishkrout committed Oct 11, 2022
1 parent 5a15e36 commit 8b69983
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions internal/renderer/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var testCases = []string{
"doublecode",
"nocodeblock",
"equalvshash",
"symbols",
}

func TestParser_Renderer(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"document":[{"markdown":"# Leading dollar sign"},{"content":"$ deno install \\\n --allow-read --allow-write \\\n --allow-env --allow-net --allow-run \\\n --no-check \\\n -r -f https://deno.land/x/deploy/deployctl.ts\n","description":"# Leading dollar sign","language":"sh","lines":["deno install \\"," --allow-read --allow-write \\"," --allow-env --allow-net --allow-run \\"," --no-check \\"," -r -f https://deno.land/x/deploy/deployctl.ts"]},{"markdown":"# Trailing backslashes"}]}
11 changes: 11 additions & 0 deletions internal/renderer/testdata/symbols.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Leading dollar sign

```sh
$ deno install \
--allow-read --allow-write \
--allow-env --allow-net --allow-run \
--no-check \
-r -f https://deno.land/x/deploy/deployctl.ts
```

# Trailing backslashes
10 changes: 9 additions & 1 deletion internal/snippets/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@ func (s *Snippet) GetLines() []string {
var cmds []string

firstHasDollar := false
hasBslashSuffix := false
lines := strings.Split(s.Content, "\n")

for _, line := range lines {
if strings.HasPrefix(line, "$") {
firstHasDollar = true
line = strings.TrimLeft(line, "$")
} else if strings.HasSuffix(strings.TrimSpace(line), "\\") {
// really long shell command lines are often
// spaced out over multiple lines using \
firstHasDollar = false
hasBslashSuffix = true
} else if firstHasDollar {
// If the first line was prefixed with "$",
// then all commands should be as well.
Expand All @@ -48,7 +54,9 @@ func (s *Snippet) GetLines() []string {
continue
}

line = strings.TrimSpace(line)
if !hasBslashSuffix {
line = strings.TrimSpace(line)
}

if line == "" {
continue
Expand Down

0 comments on commit 8b69983

Please sign in to comment.