markdownextractor

package
v1.4.4-alpha1202-diff-... Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2025 License: AGPL-3.0 Imports: 3 Imported by: 0

README

Markdown Code Block Extractor

A robust Go package for extracting code blocks from Markdown text. It supports both backtick and tilde delimited code blocks with language specifications.

Features

  • Supports both backtick (```) and tilde (~~~) delimiters
  • Handles nested code blocks
  • Supports mixed delimiters
  • Preserves code block metadata (language, position)
  • Handles special characters and edge cases

Usage

err := markdownextractor.ExtractMarkdownCode(markdown, func(typeName string, code string, startOffset int, endOffset int) {
    fmt.Printf("Type: %s\nCode: %s\nPosition: %d-%d\n", typeName, code, startOffset, endOffset)
})

Examples

Basic Code Block
# Title

```go
func main() {
    fmt.Println("Hello")
}
```
Multiple Code Blocks
# Multiple Code Blocks

```python
print("Hello")
```

Some text

```javascript
console.log("World")
```
Empty Type Code Block
```
plain text
```
Nested Code Blocks
```markdown
`code` inside code block
```
Code Block with Spaces
```  go  
func main() {}
```
Code Block with Special Characters
```go
func main() {
    // 中文注释
    fmt.Println("特殊字符: ~!@#$%^&*()_+")
}
```
Code Block with Line Breaks in Type
```go
python
func main() {}
```
Empty Code Block
```go

```
Code Block with Only Spaces
```go
    
```
Code Block with Backticks in Content
```go
fmt.Println("```")
```
Mixed Delimiters
```markdown
~~~python
print("Nested with different delimiters")
~~~
```
Malformed Backticks
```go
func main() {
    fmt.Println("Hello")
}
``

Error Handling

The package returns ErrUnclosedCodeBlock when it encounters an unclosed code block.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Documentation

Overview

Package markdownextractor provides functionality for extracting code blocks from markdown text. It supports both backtick and tilde delimited code blocks with language specifications.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnclosedCodeBlock = errors.New("unclosed code block")
)

Common errors

Functions

func ExtractMarkdownCode

func ExtractMarkdownCode(markdown string, callback func(typeName string, code string, startOffset int, endOffset int)) (string, error)

ExtractMarkdownCode parses markdown text and extracts code blocks. For each code block found, it calls the callback function with: - typeName: the language/type specified for the code block - code: the content of the code block - startOffset: the starting position of the code content in the original text - endOffset: the ending position of the code content in the original text Returns the original markdown text and any error encountered during processing

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL