forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package html | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/microcosm-cc/bluemonday" | ||
"github.com/redpanda-data/benthos/v4/public/bloblang" | ||
) | ||
|
||
func init() { | ||
stripHTMLSpec := bloblang.NewPluginSpec(). | ||
Category("String Manipulation"). | ||
Description(`Attempts to remove all HTML tags from a target string.`). | ||
Example("", `root.stripped = this.value.strip_html()`, | ||
[2]string{ | ||
`{"value":"<p>the plain <strong>old text</strong></p>"}`, | ||
`{"stripped":"the plain old text"}`, | ||
}). | ||
Example("It's also possible to provide an explicit list of element types to preserve in the output.", | ||
`root.stripped = this.value.strip_html(["article"])`, | ||
[2]string{ | ||
`{"value":"<article><p>the plain <strong>old text</strong></p></article>"}`, | ||
`{"stripped":"<article>the plain old text</article>"}`, | ||
}). | ||
Param(bloblang.NewAnyParam("preserve").Description("An optional array of element types to preserve in the output.").Optional()) | ||
|
||
if err := bloblang.RegisterMethodV2( | ||
"strip_html", stripHTMLSpec, | ||
func(args *bloblang.ParsedParams) (bloblang.Method, error) { | ||
p := bluemonday.NewPolicy() | ||
|
||
var tags []any | ||
if rawArgs := args.AsSlice(); len(rawArgs) > 0 { | ||
tags, _ = rawArgs[0].([]any) | ||
} | ||
|
||
if len(tags) > 0 { | ||
tagStrs := make([]string, len(tags)) | ||
for i, ele := range tags { | ||
var ok bool | ||
if tagStrs[i], ok = ele.(string); !ok { | ||
return nil, fmt.Errorf("invalid arg at index %v: expected string, got %T", i, ele) | ||
} | ||
} | ||
p = p.AllowElements(tagStrs...) | ||
} | ||
|
||
return bloblang.StringMethod(func(s string) (any, error) { | ||
return p.Sanitize(s), nil | ||
}), nil | ||
}, | ||
); err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package html | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/redpanda-data/benthos/v4/public/bloblang" | ||
) | ||
|
||
func TestStripHTMLNoArgs(t *testing.T) { | ||
e, err := bloblang.Parse(`root = this.strip_html()`) | ||
require.NoError(t, err) | ||
|
||
res, err := e.Query(`<div>meow</div>`) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, "meow", res) | ||
} | ||
|
||
func TestStripHTMLWithArgs(t *testing.T) { | ||
e, err := bloblang.Parse(`root = this.strip_html(["strong","h1"])`) | ||
require.NoError(t, err) | ||
|
||
res, err := e.Query(`<div> | ||
<h1>meow</h1> | ||
<p>hello world this is <strong>some</strong> text. | ||
</div>`) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, ` | ||
<h1>meow</h1> | ||
hello world this is <strong>some</strong> text. | ||
`, res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters