Skip to main content

Extract Markdown Code Blocks Node

Extract Markdown Code Blocks Node Screenshot

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

TitleData TypeDescriptionDefault ValueNotes
InputstringThe Markdown text from which to extract code blocks(required)The input will be coerced into a string if it is not a string

Example 1: Extract code blocks from a Markdown text

  1. 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!
  2. Create an Extract Markdown Code Blocks Node and connect the Text Node to its Input input.

  3. Run the graph. The First Block output of the Extract Markdown Code Blocks Node should be console.log('Hello, world!');, the All Blocks output should be an array containing console.log('Hello, world!'); and print('Hello, world!'), and the Languages output should be an array containing javascript and python.

Extract Markdown Code Blocks Node Example 1

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