Skip to content

Commit e304501

Browse files
inital commit of v2
1 parent 67fcf58 commit e304501

File tree

183 files changed

+13589
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

183 files changed

+13589
-0
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: "\U0001F41B Bug"
5+
labels: bug
6+
assignees: ''
7+
8+
---
9+
10+
**Describe the bug**
11+
A clear and concise description of what the bug is.
12+
13+
**HTML Input**
14+
```html
15+
<h1>Title</h1>
16+
```
17+
18+
19+
**Generated Markdown**
20+
````markdown
21+
# Title
22+
````
23+
24+
**Expected Markdown**
25+
````markdown
26+
# Title!!!
27+
````
28+
29+
**Additional context**
30+
Add any other context about the problem here. For example, if you changed the default options or used a plugin. Also adding the version from the `go.mod` is helpful.

.github/dependabot.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Please see the documentation for all configuration options:
2+
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
3+
4+
version: 2
5+
updates:
6+
- package-ecosystem: "gomod"
7+
directory: "/"
8+
schedule:
9+
interval: "weekly"

.github/workflows/go.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
11+
# Test the latest go version
12+
# and upload the test coverage.
13+
test_latest:
14+
name: Go latest stable
15+
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Setup Go
19+
uses: actions/setup-go@v5
20+
with:
21+
go-version: 'stable'
22+
check-latest: true
23+
24+
- name: Checkout code
25+
uses: actions/checkout@v4
26+
27+
- name: Build
28+
run: go build -v .
29+
30+
- name: Test
31+
run: go test ./... -v -race -coverprofile=coverage.txt -covermode=atomic
32+
33+
# - uses: codecov/codecov-action@v4
34+
# with:
35+
# files: ./coverage.txt
36+
# token: ${{ secrets.CODECOV_TOKEN }}
37+
38+
# Test the latest three golang version
39+
# on different operating systems.
40+
test_versions:
41+
strategy:
42+
matrix:
43+
go: ['1.22']
44+
os: [ubuntu-latest, macos-latest, windows-latest]
45+
name: Go ${{ matrix.go }} on ${{ matrix.os }}
46+
47+
runs-on: ${{ matrix.os }}
48+
steps:
49+
- name: Setup Go
50+
uses: actions/setup-go@v5
51+
with:
52+
go-version: ${{ matrix.go }}
53+
54+
- name: Checkout code
55+
uses: actions/checkout@v4
56+
57+
- name: Test
58+
run: go test ./... -v -race -cover

.github/workflows/release.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: goreleaser
2+
3+
on:
4+
push:
5+
tags:
6+
- '*'
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
goreleaser:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Setup Go
16+
uses: actions/setup-go@v5
17+
with:
18+
go-version: 'stable'
19+
check-latest: true
20+
21+
- name: Checkout
22+
uses: actions/checkout@v4
23+
with:
24+
fetch-depth: 0
25+
26+
- name: Run GoReleaser
27+
uses: goreleaser/goreleaser-action@v6
28+
with:
29+
distribution: goreleaser
30+
version: 'latest'
31+
args: release --clean
32+
env:
33+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34+
35+
36+

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
# - - - - - General - - - - - #
3+
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, build with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
.DS_Store
18+
19+
20+
21+
# - - - - - Project Specific - - - - - #
22+
23+
NOTES.md
24+
.tmp
25+
26+
27+
dist/

.goreleaser.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
# The lines below are called `modelines`. See `:help modeline`
3+
# Feel free to remove those if you don't want/need to use them.
4+
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
5+
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
6+
7+
version: 2
8+
9+
before:
10+
hooks:
11+
- go mod tidy
12+
13+
builds:
14+
- env:
15+
- CGO_ENABLED=0
16+
goos:
17+
- linux
18+
- windows
19+
- darwin
20+
21+
# Note: We only use goreleaser for the CLI,
22+
# so we have to go into the "cli" directory.
23+
dir: cli
24+
binary: html2markdown
25+
26+
archives:
27+
- format: tar.gz
28+
# this name template makes the OS and Arch compatible with the results of `uname`.
29+
name_template: >-
30+
{{ .ProjectName }}_
31+
{{- title .Os }}_
32+
{{- if eq .Arch "amd64" }}x86_64
33+
{{- else if eq .Arch "386" }}i386
34+
{{- else }}{{ .Arch }}{{ end }}
35+
{{- if .Arm }}v{{ .Arm }}{{ end }}
36+
# use zip for windows archives
37+
format_overrides:
38+
- goos: windows
39+
format: zip
40+
41+
changelog:
42+
sort: asc
43+
filters:
44+
exclude:
45+
- "^docs:"
46+
- "^test:"

CONTRIBUTING.md

Whitespace-only changes.

README.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# html-to-markdown
2+
3+
> [!WARNING]
4+
> This is an **early experimental version** of the library.
5+
>
6+
> We encourage testing and bug reporting. However, please note:
7+
>
8+
> - Not production-ready
9+
> - Default options are well-tested, but custom configurations have limited coverage
10+
> - Functionality is currently restricted
11+
> - Focus is on stabilization and core features
12+
> - No compatibility guarantee
13+
> - Only use `htmltomarkdown.ConvertString()` and `htmltomarkdown.ConvertNode()` from the root package. They are _unlikely_ to change.
14+
> - Other functions and nested packages are _very like_ to change.
15+
16+
---
17+
18+
## Golang Library
19+
20+
```go
21+
package main
22+
23+
import (
24+
"fmt"
25+
"log"
26+
27+
htmltomarkdown "github.com/JohannesKaufmann/html-to-markdown/v2"
28+
)
29+
30+
func main() {
31+
input := `<strong>Bold Text</strong>`
32+
33+
markdown, err := htmltomarkdown.ConvertString(input)
34+
if err != nil {
35+
log.Fatal(err)
36+
}
37+
fmt.Println(markdown)
38+
// Output: **Bold Text**
39+
}
40+
```
41+
42+
- 🧑‍💻 [Example code, basics](/examples/basics/main.go)
43+
44+
The function `htmltomarkdown.ConvertString()` is just a small wrapper around `converter.NewConverter()` and `commonmark.NewCommonmarkPlugin()`. If you want more control, use the following:
45+
46+
```go
47+
package main
48+
49+
import (
50+
"fmt"
51+
"log"
52+
53+
"github.com/JohannesKaufmann/html-to-markdown/v2/converter"
54+
"github.com/JohannesKaufmann/html-to-markdown/v2/plugin/commonmark"
55+
)
56+
57+
func main() {
58+
input := `<strong>Bold Text</strong>`
59+
60+
conv := converter.NewConverter(
61+
converter.WithPlugins(
62+
commonmark.NewCommonmarkPlugin(
63+
commonmark.WithStrongDelimiter("__"),
64+
// ...additional configurations for the plugin
65+
),
66+
),
67+
)
68+
69+
markdown, err := conv.ConvertString(input)
70+
if err != nil {
71+
log.Fatal(err)
72+
}
73+
fmt.Println(markdown)
74+
// Output: __Bold Text__
75+
}
76+
```
77+
78+
- 🧑‍💻 [Example code, options](/examples/options/main.go)
79+
80+
> [!NOTE]
81+
> If you use `NewConverter` directly make sure to also **register the commonmark plugin**.
82+
83+
---
84+
85+
---
86+
87+
## CLI - Using it on the command line
88+
89+
Using the Golang library provides the most customization, while the CLI is the simplest way to get started.
90+
91+
### Installation
92+
93+
Download the pre-compiled binaries from the [releases page](https://github.com/JohannesKaufmann/html-to-markdown/releases) and copy them to the desired location.
94+
95+
```bash
96+
html2markdown --version
97+
```
98+
99+
> [!NOTE]
100+
> Make sure that `--version` prints `2.X.X` as there is a different CLI for V2 of the converter.
101+
102+
## Usage
103+
104+
```bash
105+
$ echo "<strong>important</strong>" | html2markdown
106+
107+
**important**
108+
```
109+
110+
```text
111+
$ curl --no-progress-meter http://example.com | html2markdown
112+
113+
# Example Domain
114+
115+
This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.
116+
117+
[More information...](https://www.iana.org/domains/example)
118+
```
119+
120+
_(The cli does not support every option yet. Over time more customization will be added)_

SECURITY.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Security Policy
2+
3+
## Reporting a Vulnerability
4+
5+
Please report (suspected) security vulnerabilities to johannes@joina.de with the subject _"Security html-to-markdown"_ and you will receive a response within 48 hours.
6+

cli/cmd/cli_run.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cmd
2+
3+
func Run(
4+
stdin ReadWriterWithStat,
5+
stdout ReadWriterWithStat,
6+
stderr ReadWriterWithStat,
7+
8+
osArgs []string,
9+
10+
release Release,
11+
) {
12+
13+
cli := CLI{
14+
Stdin: stdin,
15+
Stdout: stdout,
16+
Stderr: stderr,
17+
18+
OsArgs: osArgs,
19+
20+
Release: release,
21+
}
22+
23+
// - - - - - init - - - - - //
24+
if err := cli.Init(); err != nil {
25+
panic(err)
26+
}
27+
28+
// - - - - - exec - - - - - //
29+
cli.Execute()
30+
}

0 commit comments

Comments
 (0)