String Utilities Công cụ đã được cập nhật 3 ngày trước

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.

Luôn miễn phí Dễ sử dụng Kết quả ngay lập tức Riêng tư và an toàn

Sử dụng Markdown Code Block Extractor

Công cụ trích xuất khối mã Markdown

Trích xuất các khối mã Markdown được bao quanh, các ngôn ngữ lập trình và mã thô thành định dạng JSON có cấu trúc.

Nhập Markdown

Dán mã Markdown chứa các khối mã được bao quanh bởi dấu ngoặc vuông
Hỗ trợ các khối có hàng rào bằng cách sử dụng ``` và ~~~. Ngôn ngữ được đọc từ chuỗi thông tin hàng rào.
Chưa được trích xuất
Tình trạng khai thác
0 khối
Các đoạn mã được trích xuất

Các khối đã trích xuất

Xem trước ngôn ngữ và mã nguồn cho từng khối
# Ngôn ngữ Mã
Chưa có quá trình trích xuất nào được thực hiện.
Mẹo trích xuất mã
Công cụ này chỉ trích xuất các khối mã được bao quanh. Mã nhúng như `example` được cố ý bỏ qua.
Liên kết công cụ
Đã sao chép vào khay nhớ tạm.
Hãy giúp cải thiện công cụ này

Đọc nội dung

Tìm hiểu cách thức hoạt động của công cụ này và khi nào nên sử dụng nó.

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

Câu hỏi thường gặp

Các câu trả lời ngắn gọn cho những thắc mắc mà người dùng thường đặt ra trước khi tin tưởng vào kết quả.

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.

Bình luận (0)

Dành cho các cuộc thảo luận của người dùng, các trường hợp ngoại lệ và các mẹo bổ sung.
Để lại bình luận
Bình luận của bạn sẽ được hiển thị công khai sau khi gửi.
Chưa có bình luận nào. Hãy là người đầu tiên bình luận!
Hãy cho chúng tôi biết!
Báo cáo sự cố liên quan đến công cụ này

Hãy mô tả vấn đề mà bạn gặp phải để chúng tôi có thể tìm hiểu và cải thiện công cụ này.

Điều gì hữu ích nhất
Vui lòng cung cấp thông tin về dữ liệu đầu vào, kết quả mong đợi, kết quả thực tế, trình duyệt/thiết bị và ảnh chụp màn hình (nếu có thể).
Nút chụp màn hình sẽ chụp lại trang web trong trình duyệt và đính kèm hình ảnh được tạo ra vào biểu mẫu này.
Ảnh chụp màn hình toàn trang
Được ghi lại trên trình duyệt của bạn bằng newisty.
Chưa có ảnh chụp màn hình nào được chụp.
Công cụ Báo cáo