28 lines
546 B
Go
28 lines
546 B
Go
|
|
package markdown
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"github.com/PuerkitoBio/goquery"
|
||
|
|
)
|
||
|
|
|
||
|
|
type htmlTransform func(ctx context.Context, dom *goquery.Document) error
|
||
|
|
|
||
|
|
func applyTransforms(ctx context.Context, inHTML string, transforms []htmlTransform) string {
|
||
|
|
dom, err := goquery.NewDocumentFromReader(strings.NewReader(inHTML))
|
||
|
|
if err != nil {
|
||
|
|
return inHTML
|
||
|
|
}
|
||
|
|
for _, transform := range transforms {
|
||
|
|
if err := transform(ctx, dom); err != nil {
|
||
|
|
return inHTML
|
||
|
|
}
|
||
|
|
}
|
||
|
|
res, err := dom.Html()
|
||
|
|
if err != nil {
|
||
|
|
return inHTML
|
||
|
|
}
|
||
|
|
return res
|
||
|
|
}
|