pipr.tools
Pipes
Clean Email Strip formatting from pasted email for clean plain text Clean AI Output Clean plain text from ChatGPT or AI output Decode & Format JWT Decode a JWT and pretty-print header and payload Word Frequency Count word frequency in text

extract-codeblocks Extract fenced code blocks from Markdown

</> Web
Input0 chars
Output0 chars

Flags

--lang string default:
--separator string default: ---

Examples

Extract code from Markdown fenced blocks

Usage
"Here's some code:

```js
const x = 1;
console.log(x);
```

A..." | extract-codeblocks

Pull code snippets from a ChatGPT response

Usage
"To fix the bug, update your config:

```yaml
server:
  port:..." | extract-codeblocks

Extract code examples from lesson notes or tutorials

Usage
"## Lesson 3: Functions

```python
def greet(name):
    retur..." | extract-codeblocks
View source
(input, opts = {})=>{
                const lang = (opts.lang || "").toLowerCase();
                const sep = opts.separator ?? "\n---\n";
                const blocks = [];
                const re = /^(`{3,}|~{3,})(\w*)\s*\n([\s\S]*?)^\1\s*$/gm;
                let m;
                while((m = re.exec(input)) !== null){
                    const blockLang = (m[2] || "").toLowerCase();
                    if (!lang || blockLang === lang) {
                        blocks.push(m[3].replace(/\n$/, ""));
                    }
                }
                if (!blocks.length) return "(no code blocks found)";
                return blocks.join(sep);
            }

Also useful for