Extract Markdown Code Blocks Node
Overview
The Extract Markdown Code Blocks Node is used to extract code blocks from a Markdown text. It extracts all code blocks and the specified languages for each block. The node outputs the first matched block, all matched blocks, and the languages specified for the blocks.
A markdown code block is defined as a code block that is surrounded by three backticks on each side. For example:
Here is some JavaScript code:
```javascript
console.log('Hello, world!');
```
This node is useful when working with LLMs that have been trained extensively on replying with markdown data.
- Inputs
- Outputs
- Editor Settings
Inputs
Title | Data Type | Description | Default Value | Notes |
---|---|---|---|---|
Input | string | The Markdown text from which to extract code blocks | (required) | The input will be coerced into a string if it is not a string |
Outputs
Title | Data Type | Description | Notes |
---|---|---|---|
First Block | string | The first code block found in the input Markdown text | If no code block is found, this output will not be ran |
All Blocks | string[] | All code blocks found in the input Markdown text | If no code block is found, this output will be an empty array |
Languages | string[] | The languages specified for each code block in the input Markdown text, in the order they appear | If no language is specified for a code block, the corresponding element in the array will be an empty string |
Editor Settings
This node has no configurable editor settings.
Example 1: Extract code blocks from a Markdown text
Create a Text Node and set the text to the following Markdown text:
Here is some JavaScript code:
```javascript
console.log('Hello, world!');
```
And here is some Python code:
```python
print('Hello, world!')
```
That's it!Create an Extract Markdown Code Blocks Node and connect the Text Node to its
Input
input.Run the graph. The
First Block
output of the Extract Markdown Code Blocks Node should beconsole.log('Hello, world!');
, theAll Blocks
output should be an array containingconsole.log('Hello, world!');
andprint('Hello, world!')
, and theLanguages
output should be an array containingjavascript
andpython
.
Error Handling
The Extract Markdown Code Blocks Node will not error under normal circumstances. If the input text does not contain any code blocks, the First Block
output will not be ran, and the All Blocks
and Languages
outputs will be empty arrays.
FAQ
Q: What happens if a code block does not specify a language?
A: The corresponding element in the Languages
output array will be an empty string.
Q: What happens if the input text contains non-Markdown text?
A: The node will ignore any non-Markdown text and only extract code blocks. If the input text does not contain any code blocks, the First Block
output will not be ran, and the All Blocks
and Languages
outputs will be empty arrays.
See Also
- Extract JSON Node
- Extract YAML Node
- Extract Object Path Node
- Extract with Regex Node
- Text Node
- Split Text Node