From 8b69983f72dcb69aa74c35a0f8cf0ca7d363b5f5 Mon Sep 17 00:00:00 2001 From: Sebastian Tiedtke Date: Tue, 11 Oct 2022 10:44:24 -0400 Subject: [PATCH] Handle common shell block pre- and suffixes --- internal/renderer/json_test.go | 1 + .../testdata/.snapshots/TestParser_Renderer-symbols | 1 + internal/renderer/testdata/symbols.md | 11 +++++++++++ internal/snippets/snippet.go | 10 +++++++++- 4 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 internal/renderer/testdata/.snapshots/TestParser_Renderer-symbols create mode 100644 internal/renderer/testdata/symbols.md diff --git a/internal/renderer/json_test.go b/internal/renderer/json_test.go index a0c480039..210d59485 100644 --- a/internal/renderer/json_test.go +++ b/internal/renderer/json_test.go @@ -21,6 +21,7 @@ var testCases = []string{ "doublecode", "nocodeblock", "equalvshash", + "symbols", } func TestParser_Renderer(t *testing.T) { diff --git a/internal/renderer/testdata/.snapshots/TestParser_Renderer-symbols b/internal/renderer/testdata/.snapshots/TestParser_Renderer-symbols new file mode 100644 index 000000000..04a6cf4bc --- /dev/null +++ b/internal/renderer/testdata/.snapshots/TestParser_Renderer-symbols @@ -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"}]} diff --git a/internal/renderer/testdata/symbols.md b/internal/renderer/testdata/symbols.md new file mode 100644 index 000000000..f981f7a8b --- /dev/null +++ b/internal/renderer/testdata/symbols.md @@ -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 diff --git a/internal/snippets/snippet.go b/internal/snippets/snippet.go index 008943077..8a980e2b4 100644 --- a/internal/snippets/snippet.go +++ b/internal/snippets/snippet.go @@ -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. @@ -48,7 +54,9 @@ func (s *Snippet) GetLines() []string { continue } - line = strings.TrimSpace(line) + if !hasBslashSuffix { + line = strings.TrimSpace(line) + } if line == "" { continue