String Utilities Strumento aggiornato 1 giorno fa

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.

Sempre gratuito Facile da usare Risultati immediati Riservato e sicuro

Utilizzo Markdown Code Block Extractor

Estrattore di blocchi di codice Markdown

Estrai i blocchi di codice Markdown racchiusi tra tag, i linguaggi di programmazione e il codice grezzo in un formato JSON strutturato.

Inserisci Markdown

Incolla il codice Markdown contenente blocchi di codice racchiusi tra parentesi quadre
Supporta i blocchi recintati utilizzando ``` e ~~~. La lingua viene ricavata dalla stringa informativa del fence.
Non ancora estratto
Stato dell'estrazione
0 blocchi
Blocchi di codice estratti

Blocchi estratti

Anteprima del testo e del codice per ogni blocco
# Lingua Codice
Non è stata ancora eseguita alcuna estrazione.
Suggerimento per l'estrazione del codice
Questo strumento estrae solo i blocchi di codice racchiusi tra tag. Il codice inline come `example` viene ignorato di proposito.
Link agli strumenti
Copiato negli appunti.
Aiutaci a migliorare questo strumento

Leggi il contenuto

Scopri come funziona lo strumento e quando utilizzarlo.

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

  1. 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.
  2. Press Extract.
  3. Read the Extracted Blocks table. Each row shows the block number, the language badge and a preview of the code.
  4. Check the status cards. Extraction status says "Code blocks extracted" or "No code blocks found", and Extracted code blocks gives the total.
  5. 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

Domande frequenti

Risposte concise alle domande che gli utenti si pongono prima di fidarsi del risultato.

Yes. Fences of three or more backticks and fences of three or more tildes are both recognised, and each block closes at the next fence of the same type.

It is read from the info string after the opening fence. Only the first word is kept, so a fence written as js title="app.js" is recorded as js. Blocks with no info string get null.

The tool extracts fenced blocks only. Inline code between single backticks is intentionally skipped, as are indented blocks without a fence.

Everything from the opening fence to the end of the document is captured as one block. A mismatch between the possible blocks count and what you expect is a good hint that a fence is missing.

No. The extraction runs in your browser with JavaScript, and the text is not sent to this site's server.

Download JSON saves the results as markdown-code-blocks.json, an array of objects with language and code keys.

Yes. The code value keeps every line between the fences exactly as written, including blank lines and leading spaces. Only Windows line endings are converted to plain line feeds.

Commenti (0)

Per discussioni tra utenti, casi limite e consigli aggiuntivi.
Lascia un commento
Il tuo commento sarà visibile a tutti una volta inviato.
Non ci sono ancora commenti. Lascia il primo commento!
Facci sapere!
Segnala un problema con questo strumento

Descrivi il problema che hai riscontrato, in modo che possiamo analizzarlo e migliorare lo strumento.

Ciò che aiuta di più
Includi i dati inseriti, il risultato atteso, il risultato effettivo, il browser/dispositivo utilizzato e, se utile, uno screenshot.
Il pulsante "Screenshot" acquisisce la pagina nel browser e allega l'immagine generata a questo modulo.
Screenshot a tutta pagina
Acquisito nel tuo browser con newisty.
Non è stato ancora acquisito alcun screenshot.
Strumento Relazione