在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:JohannesKaufmann/html-to-markdown开源软件地址:https://github.com/JohannesKaufmann/html-to-markdown开源编程语言:Go 100.0%开源软件介绍:html-to-markdownConvert HTML into Markdown with Go. It is using an HTML Parser to avoid the use of Installation
Usageimport md "github.com/JohannesKaufmann/html-to-markdown"
converter := md.NewConverter("", true, nil)
html = `<strong>Important</strong>`
markdown, err := converter.ConvertString(html)
if err != nil {
log.Fatal(err)
}
fmt.Println("md ->", markdown) If you are already using goquery you can pass a selection to markdown, err := converter.Convert(selec) Using it on the command lineIf you want to make use of OptionsThe third parameter to For example you can change the character that is around a bold text (" opt := &md.Options{
StrongDelimiter: "__", // default: **
// ...
}
converter := md.NewConverter("", true, opt) For all the possible options look at godocs and for a example look at the example. Adding Rulesconverter.AddRules(
md.Rule{
Filter: []string{"del", "s", "strike"},
Replacement: func(content string, selec *goquery.Selection, opt *md.Options) *string {
// You need to return a pointer to a string (md.String is just a helper function).
// If you return nil the next function for that html element
// will be picked. For example you could only convert an element
// if it has a certain class name and fallback if not.
content = strings.TrimSpace(content)
return md.String("~" + content + "~")
},
},
// more rules
) For more information have a look at the example add_rules. Using PluginsIf you want plugins (github flavored markdown like striketrough, tables, ...) you can pass it to import "github.com/JohannesKaufmann/html-to-markdown/plugin"
// Use the `GitHubFlavored` plugin from the `plugin` package.
converter.Use(plugin.GitHubFlavored()) Or if you only want to use the converter.Use(plugin.Strikethrough("")) For more information have a look at the example github_flavored. Writing PluginsHave a look at the plugin folder for a reference implementation. The most basic one is Strikethrough. SecurityThis library produces markdown that is readable and can be changed by humans. Once you convert this markdown back to HTML (e.g. using goldmark or blackfriday) you need to be careful of malicious content. This library does NOT sanitize untrusted content. Use an HTML sanitizer such as bluemonday before displaying the HTML in the browser. Other Methods
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论