Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug where markdown headers in later lines weren't stripped. #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion markdown/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (e *Extractor) PlainText(input string) (*string, error) {
for _, fullTag := range listFullTag {
var plain = fullTag
if tag.StartRegex != nil {
plain = tag.StartRegex.ReplaceAll(plain, nil)
plain = tag.StartRegex.ReplaceAll(plain, tag.StartReplacement)
}
if tag.EndRegex != nil {
plain = tag.EndRegex.ReplaceAll(plain, nil)
Expand Down
1 change: 1 addition & 0 deletions markdown/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestExtract(t *testing.T) {
{"~~strikethrough~~", "strikethrough"},
{"# H1 \n*italic* **bold** `code` `not code [link](https://example.com) ![image](https://image.com/image.png) ~~strikethrough~~", "H1 \nitalic bold code `not code link image strikethrough"},
{"# H1 \n new line", "H1 \n new line"},
{"# H1 \n new line \n## H2 \n new line", "H1 \n new line \nH2 \n new line"},
{"*italic*", "italic"},
{"**bold**", "bold"},
{"`code`", "code"},
Expand Down
6 changes: 4 additions & 2 deletions markdown/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ type Tag struct {
Name string
FullRegex *regexp.Regexp
StartRegex *regexp.Regexp
StartReplacement []byte
EndRegex *regexp.Regexp
}

var tags = []Tag{
{
Name: "Header",
FullRegex: regexp.MustCompile(`^#{1,6}\s+(.*)`),
StartRegex: regexp.MustCompile(`^#{1,6}\s+`),
FullRegex: regexp.MustCompile(`(^|\n)#{1,6}\s+(.*)`),
StartRegex: regexp.MustCompile(`(^|\n)#{1,6}\s+`),
StartReplacement: []byte("$1"),
EndRegex: nil,
},
{
Expand Down
9 changes: 5 additions & 4 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package plaintext

import (
"testing"

"github.com/huantt/plaintext-extractor/html"
"github.com/huantt/plaintext-extractor/markdown"
"github.com/stretchr/testify/assert"
"testing"
)

func TestParseHtml(t *testing.T) {
tests := []struct {
input string
expected string
}{
{`<div>This is a <a href="https://example.com">link</a></div>`, "This is a link"},
{`<div>This is a <a href="https://example.com">link</a></div>`, "This is a link\n"},
}
for _, test := range tests {
extractor := NewHtmlExtractor()
Expand Down Expand Up @@ -44,8 +45,8 @@ func TestMultipleExtractors(t *testing.T) {
input string
expected string
}{
{"<div> html </div> *markdown*", "html markdown"},
{"<div> *markdown in html* </div>", "markdown in html"},
{"<div> html </div> *markdown*", "html\nmarkdown"},
{"<div> *markdown in html* </div>", "markdown in html\n"},
}
for _, test := range tests {
markdownExtractor := markdown.NewExtractor()
Expand Down