diff --git a/scripts/md-exec.md b/scripts/md-exec.md index b1c75f0..13ecf98 100644 --- a/scripts/md-exec.md +++ b/scripts/md-exec.md @@ -219,6 +219,17 @@ blah EOF ``` +A heredoc body may itself contain a line starting with ` ``` ` (e.g. a +heredoc that writes out markdown) without being mistaken for the end of +the code block: +```sh {empty_output} +$ cat > /tmp/md-exec-fence.txt << 'EOF' +before +``` +after +EOF +``` + --- ## Global Directives diff --git a/scripts/md-exec.py b/scripts/md-exec.py index 749c144..de78588 100644 --- a/scripts/md-exec.py +++ b/scripts/md-exec.py @@ -189,8 +189,13 @@ def parse(self): stripped = line.strip() - # Detect Code Snippets - if stripped.startswith("```"): + # Detect Code Snippets. Skip fence detection while inside an + # open heredoc body -- the heredoc content may legitimately + # contain a line starting with ``` (e.g. a heredoc that writes + # out markdown), and treating it as a fence boundary would cut + # the code block short mid-heredoc, leaving the shell command + # truncated and the heredoc unterminated. + if stripped.startswith("```") and heredoc_delimiter is None: if in_block: # End of block: save pending test if current_cmd: