https://github.com/prodigeris/html2plaintext
https://github.com/prodigeris/html2plaintext
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/prodigeris/html2plaintext
- Owner: prodigeris
- Created: 2025-08-20T13:31:37.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-08-20T13:32:28.000Z (11 months ago)
- Last Synced: 2025-09-23T14:59:24.856Z (10 months ago)
- Language: Go
- Size: 3.91 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# html2plaintext
A Go package that converts HTML to plaintext with configurable options.
## Features
- Convert HTML to clean, readable plaintext
- Extract text from links (removes href, keeps link text)
- Skip or convert images based on configuration
- Extract SEO meta tags and title (optional)
- Clean formatting with proper spacing and line breaks
- Ignore script and style tags
## Installation
```bash
go get github.com/prodigeris/html2plaintext
```
## Usage
### As a Library
```go
package main
import (
"fmt"
"github.com/prodigeris/html2plaintext"
)
func main() {
html := `
Page Title
Hello World
This is a link.
`
// Convert with default options (body only, skip images)
opts := html2plaintext.Options{
OnlyBody: true, // Extract body content only
ImageToAlt: false, // Skip images
}
plaintext := html2plaintext.Convert(html, opts)
fmt.Println(plaintext)
}
```
### As a Command-Line Tool
```bash
# Build the tool
go build -o html2plaintext cmd/main.go
# Convert HTML file
./html2plaintext -input page.html
# Include meta tags and title
./html2plaintext -body-only=false -input page.html
# Convert images to alt text
./html2plaintext -image-alt -input page.html
# Pipe HTML content
curl -s https://example.com | ./html2plaintext
# Show help
./html2plaintext -help
```
## Options
### `Options` struct
- `OnlyBody` (bool): When `true`, extracts content from the body tag only. When `false`, also includes title and SEO meta tags (description, keywords, Open Graph tags).
- `ImageToAlt` (bool): When `true`, converts images to their alt text. When `false`, skips images entirely.
## Example Output
### Input HTML:
```html
Example Page
Welcome
This is a link.
```
### Output with `OnlyBody: true, ImageToAlt: false`:
```
Welcome
This is a link.
```
### Output with `OnlyBody: false, ImageToAlt: true`:
```
Example Page
This is a description
Welcome
This is a link.
A beautiful photo
```
## License
MIT