Skip to content

Instantly share code, notes, and snippets.

@DanCouper
Created June 3, 2021 11:35
Show Gist options
  • Save DanCouper/8756cc220fc709836cfd1b065566bc5e to your computer and use it in GitHub Desktop.
Save DanCouper/8756cc220fc709836cfd1b065566bc5e to your computer and use it in GitHub Desktop.
Examples of executable (via `zx`, using v1.14.1) markdown scripts. First one works, next two do not for described reasons

Expected Behavior

Only code I want to execute should execute.

Actual Behavior

Anything in either a fenced block or indented by 4 spaces/tab blindly executes.

Steps to Reproduce the Problem

  1. Save text of this issue as a .md file
  2. Execute it using zx

I am finding the ability of zx to execute scripts written as .md files very useful from a documentation PoV.

The actual executable part works well, ex:

$`echo "Example script execution in fenced block with a js language tag"`;
$`echo "Example script execution in a fenced block without any language tag"`;
$`echo "Example script execution in indented block"`;

But if I want to document some piece of code within the markdown file, I have an issue.

Expected Behavior

Only code I want to execute should execute.

Actual Behavior

Anything in either a fenced block or indented by 4 spaces/tab blindly executes.

Steps to Reproduce the Problem

  1. Save text of this issue as a .md file
  2. Execute it using zx

I am finding the ability of zx to execute scripts written as .md files very useful from a documentation PoV.

The actual executable part works well, ex:

$`echo "Example script execution in fenced block with a js language tag"`;
$`echo "Example script execution in a fenced block without any language tag"`;
$`echo "Example script execution in indented block"`;

But if I want to document some piece of code within the markdown file, I have an issue. So for example, maybe I want to explain how to run it via some aliased command in my npm scripts block:

yarn runscript ./scripts/example.md

But that will just cause the script to explode.

I understand why, see the tiny .md file parser:

Expected Behavior

Only code I want to execute should execute.

Actual Behavior

Anything in either a fenced block or indented by 4 spaces/tab blindly executes.

Steps to Reproduce the Problem

  1. Save text of this issue as a .md file
  2. Execute it using zx

I am finding the ability of zx to execute scripts written as .md files very useful from a documentation PoV.

The actual executable part works well, ex:

$`echo "Example script execution in fenced block with a js language tag"`;
$`echo "Example script execution in a fenced block without any language tag"`;
$`echo "Example script execution in indented block"`;

But if I want to document some piece of code within the markdown file, I have an issue. So for example, maybe I want to explain how to run it via some aliased command in my npm scripts block. But that will just cause the script to explode.

I understand why, see the tiny .md file parser:

So when I just glanced at that function, I thought I could use a different language tag (well, anything except js).

yarn runscript ./scripts/example.md

And the little parser manages to ignore it. But then the parsing breaks and it tries to execute this line here instead. Looking at the function, I see why, and I'm currently [naïvely] dealing with this by patching the package, removing the ability to parse indented code blocks as part of the output, then just indenting any examples.

Specifications

  • Version: all
  • Platform: all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment