An open API service indexing awesome lists of open source software.

https://github.com/prodigeris/html2plaintext


https://github.com/prodigeris/html2plaintext

Last synced: 6 months ago
JSON representation

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.


An image

`

// 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.


A beautiful photo

```

### 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