Skip to content

Instantly share code, notes, and snippets.

@y4code
Created September 13, 2022 02:39
Show Gist options
  • Save y4code/cea45907ef5647b14754263367841304 to your computer and use it in GitHub Desktop.
Save y4code/cea45907ef5647b14754263367841304 to your computer and use it in GitHub Desktop.
convert HTML to Markdown
package main
import (
md "github.com/JohannesKaufmann/html-to-markdown"
"io/ioutil"
"log"
"strings"
)
var converter = md.NewConverter("", true, nil)
func main() {
files, err := ioutil.ReadDir("./HTML")
if err != nil {
panic(err)
}
htmlCount := 0
for _, file := range files {
if strings.HasSuffix(file.Name(), ".html") {
htmlCount++
log.Println("Processing file", htmlCount, file.Name())
html, err := ioutil.ReadFile("./HTML/" + file.Name())
if err != nil {
panic(err)
}
s, err := convert(string(html))
if err != nil {
panic(err)
}
err = ioutil.WriteFile("./MD/"+file.Name()[:len(file.Name())-5]+".md", []byte(s), 0644)
if err != nil {
panic(err)
}
}
}
}
func convert(html string) (string, error) {
markdown, err := converter.ConvertString(html)
if err != nil {
return "", err
}
return markdown, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment