# README
HTML Pipeline for Go
This is go version of html-pipeline
Other versions
- html-pipeline - Ruby
- html-pipeline.cr - Crystal
Usage
package main
import (
"fmt"
"github.com/PuerkitoBio/goquery"
pipeline "github.com/longbridgeapp/html-pipeline"
)
// ImageMaxWidthFilter a custom filter example
type ImageMaxWidthFilter struct{}
func (f ImageMaxWidthFilter) Call(doc *goquery.Document) (err error) {
doc.Find("img").Each(func(i int, node *goquery.Selection) {
node.SetAttr("style", `max-width: 100%`)
})
return
}
func main() {
pipe := pipeline.NewPipeline([]pipeline.Filter{
pipeline.MarkdownFilter{},
pipeline.SanitizationFilter{},
ImageMaxWidthFilter{},
pipeline.MentionFilter{
Prefix: "#",
Format: func(name string) string {
return fmt.Sprintf(`<a href="https://github.com/topic/%s">#%s</a>`, name, name)
},
},
pipeline.MentionFilter{
Prefix: "@",
Format: func(name string) string {
return fmt.Sprintf(`<a href="https://github.com/%s">@%s</a>`, name, name)
},
},
})
markdown := `# Hello world
 [Click me](javascript:alert)
This is #html-pipeline example, @huacnlee created.`
out, _ := pipe.Call(markdown)
fmt.Println(out)
/*
<h1>Hello world</h1>
<p><img alt="" style="max-width: 100%"/> Click me</p>
<p>This is <a href="https://github.com/topic/html-pipeline">#html-pipeline</a> example, <a href="https://github.com/huacnlee">@huacnlee</a> created.</p>
*/
}
https://play.golang.org/p/zB0T7KczdB4
Use for Plain Text case
Sometimes, you may want use html-pipeline to manage the Plain Text process.
For example:
- Match mentions, and then send notifications.
- Convert Mention / HashTag or other text into other format.
But in HTML mode, it will escape some chars ("
, '
, &
) ... We don't wants that.
So, there have NewPlainPipeline
method for you to create a plain mode pipeline without any escape.
NOTE: For secruity, this pipeline will remove all HTML tags
<.+?>
package main
import (
"fmt"
"github.com/longbridgeapp/html-pipeline"
)
func main() {
pipe := pipeline.NewPlainPipeline([]pipeline.Filter{
pipeline.MentionFilter{
Prefix: "#",
Format: func(name string) string {
return fmt.Sprintf(`[hashtag name="%s"]%s[/hashtag]`, name, name)
},
},
pipeline.MentionFilter{
Prefix: "@",
Format: func(name string) string {
return fmt.Sprintf(`[mention name="%s"]@%s[/mention]`, name, name)
},
},
})
text := `"Hello" & 'world' this <script>danger</script> is #html-pipeline created by @huacnlee.`
out, _ := pipe.Call(text)
fmt.Println(out)
// "Hello" & 'world' this danger is [hashtag name="html-pipeline"]html-pipeline[/hashtag] created by [mention name="huacnlee"]@huacnlee[/mention].
}
https://play.golang.org/p/vxKZU9jJi3u
Built-in filters
- SanitizationFilter - Use bluemonday default UGCPolicy to sanitize html
- MarkdownFilter - Use blackfriday to covert Markdown to HTML.
- MentionFilter - Match Mention or HashTag like Twitter.
- HTMLEscapeFilter - HTML Escape for plain text.
- SimpleFormatFilter - Format plain text for covert
\n\n
into paragraph, like Rails simple_format. - AutoCorrectFilter - Use AutoCorrect to automatically add spaces between CJK and English words.
- ImageProxyFilter - DEPRECATED A filter can match all
img
to replace src as proxy url with imageproxy. - ImageURLFilter - A filter can match
img
to replace with rules like (imageproxy, Ban URL, Thumb version ...). - ExternalLinkFilter a filter to match external links to add
rel="nofollow"
,target="_blank"
.
License
MIT License
# Functions
NewPipeline create pipeline with HTML mode.
NewPlainPipeline create pipeline with Plain mode (HTML tags will remove).
TraverseTextNodes map nested node to find all text node.
# Structs
AutoCorrectFilter Automatically add whitespace between CJK and half-width characters (alphabetical letters, numerical digits and symbols).
ExternalLinkFilter a filter to match external links to add rel="nofollow" target="_blank".
HTMLEscapeFilter HTML escape for Plain text.
ImageProxyFilter DEPRECATED, use ImageURLFilter replace img src for use image proxy.
ImageURLFilter will match image src and replace with custom format.
MarkdownFilter render Markdown with blackfriday.
MentionFilter mention with @ or # or other prefix.
Pipeline stuct.
SanitizationFilter use bluemonday default UGCPolicy to sanitize html.
SimpleFormatFilter covnert simple plain text into breakable html.
# Interfaces
Filter base filter interface.