Skip to content

Code Blocks

Fenced code blocks can be validated for both their language specifier and content.

Code blocks match literally by default - both language and content must be identical. This is similar to how literal matchers in general work.

schema.mds
```rust
fn main() {}
```
input.md
```rust
fn main() {}
```
schema.mds
```rust
fn main() {}
```
input.md
```python
fn main() {}
```
schema.mds
```rust
fn main() {}
```
input.md
```rust
print('hello')
```

Use {label:/pattern/} in the language position to match language specifiers:

schema.mds
```{lang:/w+/}
fn main() {}
```
input.md
```rust
fn main() {}
```
Output:
{"lang":"rust"}
schema.mds
```{lang:/w+/}
fn main() {}
```
input.md
```javascript
fn main() {}
```
Output:
{"lang":"javascript"}
schema.mds
```{lang:/w+/}
fn main() {}
```
input.md
```rust
print('hello')
```

Use {id} in the code content to capture the entire code block without validation:

schema.mds
```rust
{code}
```
input.md
```rust
print('hello')
```
Output:
{"code":"print('hello')"}

You can validate code block content by specifying an executable to run. The code content is passed via stdin to the executable.

Use the syntax {id:!executable} to validate and capture code content:

schema.mds
```python
{code:!python -m py_compile -}
```
input.md
```python
print('hello')
```
Output:
{"code":"print('hello')"}
schema.mds
```python
{code:!python -m py_compile -}
```
input.md
```python
this is invalid python
```

The executable receives the code content via stdin. If the executable exits with code 0, the validation passes. Any other exit code indicates validation failure.

Match language with a pattern and capture content:

schema.mds
```{lang:/w+/}
{code}
```
input.md
```python
print('hello')
```
Output:
{"lang":"python","code":"print('hello')"}
schema.mds
```{lang:/w+/}
{code}
```
input.md
```rust
fn main() {}
```
Output:
{"lang":"rust","code":"fn main() {}"}
🚧 Not Implemented Yet

You can validate code block content using the same execution validation pattern as inline matchers. Use {id:!executable}:

schema.mds
```python
{code:!python -m py_compile -}
```
input.md
```python
print('hello')
```
Output:
{"code":"print('hello')"}
schema.mds
```python
{code:!python -m py_compile -}
```
input.md
```python
this is invalid python
```

The code content is passed via stdin to the executable. Exit code 0 means validation passes.

  • Language matchers support the same regex patterns as inline matchers
  • Content can only be captured with {id}, not validated with regex
  • Both language and content are optional in the schema and input