Markdown Code Block Extractor
Extract every fenced code block from a Markdown document. The tool reads backtick and tilde fences, records the language from each info string, and returns the code as a JSON array you can review in a table, copy, or download as markdown-code-blocks.json. Everything runs in your browser.
Use Markdown Code Block Extractor
Input Markdown
Extracted Blocks
| # | Language | Code |
|---|---|---|
|
No extraction has been run yet.
|
||
Read Content
Pull every fenced code sample out of a Markdown file
Technical writing is full of code. A single README might hold install commands, a config file, a short PHP class and a curl request, each sitting inside its own fenced block. The Markdown Code Block Extractor reads a Markdown document, finds every fenced block, and returns the code together with the language tag in a clean JSON array. You can scan the results in a table, copy the JSON, or download it as a file for further processing.
It is useful whenever the code matters more than the prose around it: testing documentation snippets, building a snippet library, moving examples into a test suite, or simply checking which languages a long guide actually uses.
What counts as a code block
The extractor looks for fences, the lines that open and close a block:
- Backtick fences: three or more backticks, as in
```. - Tilde fences: three or more tildes, as in
~~~.
The text that follows the opening fence on the same line is the info string. The tool takes the first word of it as the language, so ```js title="app.js" is recorded as js. A fence with no info string gets a language of null in the JSON and shows as "(none)" in the table.
Leading spaces before a fence are tolerated, so fences inside list items are picked up. A block is closed by the next fence line of the same kind: a backtick block ends at the next backtick fence, a tilde block at the next tilde fence. If a block is never closed, everything from the opening fence to the end of the document is captured as that block, which is the same way most Markdown renderers treat an unclosed fence.
Two kinds of code are deliberately left out. Inline code such as `npm install` inside a sentence is ignored, and so are indented code blocks made with four leading spaces and no fence. The tip at the bottom of the page states the inline rule plainly: this tool extracts fenced code blocks only.
The JSON format
Each block becomes one object with two keys, in the order the blocks appear in your document:
[{"language": "bash", "code": "npm install\nnpm run build"}, {"language": null, "code": "plain text block"}]
The code value is the exact text between the fences with the original line breaks and indentation preserved. Windows line endings are normalised to plain line feeds first. The fence lines themselves are not included.
Using the tool
- Paste your Markdown into the Input Markdown editor, or press Paste in the header to pull it from your clipboard. The footer shows the character count and a rough "possible blocks" figure that counts fence lines and halves the result.
- Press Extract.
- Read the Extracted Blocks table. Each row shows the block number, the language badge and a preview of the code.
- Check the status cards. Extraction status says "Code blocks extracted" or "No code blocks found", and Extracted code blocks gives the total.
- Press Copy JSON to copy the array, or Download JSON to save it as
markdown-code-blocks.json.
Two smaller buttons sit on the input card: Copy Input copies what you pasted, and Clear empties the input, the table and the JSON. Editing the input after an extraction resets the results, so the table always matches the text currently in the editor. Press Extract again after any change.
Where the work happens
Extraction runs entirely in your browser in plain JavaScript, line by line. Your Markdown is not uploaded to this site's server, which makes the tool safe for internal docs and unreleased code samples. There is no server-side size limit; very large documents are limited only by what your browser and the editor handle comfortably.
Things people do with the output
Testing the examples in your docs
Documentation rots when code samples stop working. Feed the JSON into a small script that picks every python or bash block and runs it in a sandbox. Failures point straight at the snippet that needs fixing.
Building a snippet library
Extract blocks from a collection of notes or blog posts, filter by language, and import them into a snippet manager or a team knowledge base.
Auditing language tags
Syntax highlighting depends on the info string. Running a long guide through the extractor quickly shows blocks with no language or with inconsistent tags like sh, shell and bash side by side, so you can standardise them.
Preparing training or evaluation data
Researchers and teams building prompt sets often need code separated from prose. The array is already in a shape most scripts can read directly.
Copying a single command
Sometimes you just want the commands from an install guide without scrolling. The table gives you every block in one place.
Tips for accurate results
- Always tag your fences with a language. It makes the JSON far more useful and improves highlighting everywhere the Markdown is rendered.
- If the possible blocks count is not a whole match for the number you expect, look for a missing closing fence. An odd number of fence lines usually means one block swallows the rest of the file.
- When you need to show a fence inside a code block, use the other fence type for the outer block, for example a tilde block that contains backtick lines. That keeps the inner fence from closing the outer block early.
- Download the JSON when you plan to process it with a script. Copying from the editor works too, but a file avoids clipboard size limits on very long outputs.
What to avoid
- Do not expect indented code blocks to appear. Convert them to fenced blocks first if you need them extracted.
- Do not nest a backtick fence of four backticks around one of three and expect the inner one to survive. The tool compares only the first three characters of a fence, so any backtick fence line closes an open backtick block.
- Do not use the table preview as your source of truth for exact characters. Copy from the Raw JSON editor or the downloaded file, which hold the code exactly as extracted.
- Do not paste secrets you would not want in a clipboard history. The tool itself keeps nothing, but copying puts the text on your clipboard.
Limitations
- Fenced blocks only; inline code and four-space indented blocks are ignored.
- Only the first word of the info string is kept. Attributes such as titles or line highlights after the language are dropped.
- Closing fences are matched by type, not by length.
- Code inside HTML
<pre>tags embedded in the Markdown is not detected. - The output format is JSON only. There is no option to export each block as a separate file.
Related tools
If you want the opposite result, a document with the code removed, try Markdown Remove Code Blocks. To list headings so you can tell which section each snippet belongs to, use the Markdown Heading Extractor.
Frequently Asked Questions
FAQ
Comments (0)
Describe the issue you encountered so we can investigate and improve the tool.