Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

refactor: rename Parse and implement Simple markdown function (#641)

Guilherme Oenning21c5379ee51b
diff --git a/app/handlers/apiv1/invite.go b/app/handlers/apiv1/invite.go
index 09d8ebe8e..d925655b7 100644
--- a/app/handlers/apiv1/invite.go
+++ b/app/handlers/apiv1/invite.go
@@ -23,7 +23,7 @@ func SendSampleInvite() web.HandlerFunc {
input.Model.Message = strings.Replace(input.Model.Message, app.InvitePlaceholder, "*the link to accept invitation will be here*", -1)
to := email.NewRecipient(c.User().Name, c.User().Email, email.Params{
"subject": input.Model.Subject,
- "message": markdown.Parse(input.Model.Message),
+ "message": markdown.Full(input.Model.Message),
})
err := c.Services().Emailer.Send(c, "invite_email", email.Params{}, c.Tenant().Name, to)
if err != nil {
diff --git a/app/pkg/markdown/markdown.go b/app/pkg/markdown/markdown.go
index 6e1f6f3ed..ed0fc1e14 100644
--- a/app/pkg/markdown/markdown.go
+++ b/app/pkg/markdown/markdown.go
@@ -7,7 +7,8 @@ import (
"github.com/russross/blackfriday"
)
-var mdExtns = 0 | blackfriday.EXTENSION_TABLES |
+var mdExtns = 0 |
+ blackfriday.EXTENSION_TABLES |
blackfriday.EXTENSION_AUTOLINK |
blackfriday.EXTENSION_FENCED_CODE |
blackfriday.EXTENSION_TITLEBLOCK |
@@ -16,25 +17,43 @@ var mdExtns = 0 | blackfriday.EXTENSION_TABLES |
blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
blackfriday.EXTENSION_HARD_LINE_BREAK
-var htmlExtns = 0 |
+var simpleHTMLExtensions = 0 |
blackfriday.HTML_USE_XHTML |
blackfriday.HTML_USE_SMARTYPANTS |
+ blackfriday.HTML_SMARTYPANTS_FRACTIONS |
+ blackfriday.HTML_SMARTYPANTS_DASHES |
blackfriday.HTML_SKIP_IMAGES |
+ blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
+
+var simpleRenderer = SimpleRenderer(simpleHTMLExtensions)
+
+// Simple turns a markdown into HTML using few rules
+func Simple(input string) template.HTML {
+ output := blackfriday.Markdown([]byte(input), simpleRenderer, mdExtns)
+
+ return template.HTML(strings.TrimSpace(string(output)))
+}
+
+var fullHTMLExtensions = 0 |
+ blackfriday.HTML_USE_XHTML |
+ blackfriday.HTML_USE_SMARTYPANTS |
blackfriday.HTML_SMARTYPANTS_FRACTIONS |
blackfriday.HTML_SMARTYPANTS_DASHES |
blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
-// Parse given markdown input into html with all enabled features
-func Parse(input string) template.HTML {
- renderer := blackfriday.HtmlRenderer(htmlExtns, "", "")
- output := blackfriday.Markdown([]byte(input), renderer, mdExtns)
+var fullRenderer = blackfriday.HtmlRenderer(fullHTMLExtensions, "", "")
+
+// Full turns a markdown into HTML using all rules
+func Full(input string) template.HTML {
+ output := blackfriday.Markdown([]byte(input), fullRenderer, mdExtns)
return template.HTML(strings.TrimSpace(string(output)))
}
+var textRenderer = TextRenderer()
+
// PlainText parses given markdown input and return only the text
func PlainText(input string) string {
- renderer := TextRenderer()
- output := blackfriday.Markdown([]byte(input), renderer, mdExtns)
+ output := blackfriday.Markdown([]byte(input), textRenderer, mdExtns)
return strings.TrimSpace(string(output))
}
diff --git a/app/pkg/markdown/markdown_test.go b/app/pkg/markdown/markdown_test.go
index 11260c36c..7eb9d1548 100644
--- a/app/pkg/markdown/markdown_test.go
+++ b/app/pkg/markdown/markdown_test.go
@@ -8,12 +8,12 @@ import (
"github.com/getfider/fider/app/pkg/markdown"
)
-func TestParseMarkdown(t *testing.T) {
+func TestFullMarkdown(t *testing.T) {
RegisterT(t)
for input, expected := range map[string]string{
"# Hello World": `<h1>Hello World</h1>`,
- "![](http://example.com/hello.jpg)": `<p></p>`,
+ "![](http://example.com/hello.jpg)": `<p><img src="http://example.com/hello.jpg" alt="" /></p>`,
"Go to http://example.com/hello.jpg": `<p>Go to <a href="http://example.com/hello.jpg">http://example.com/hello.jpg</a></p>`,
`
- **Option 1**
@@ -37,7 +37,7 @@ This will allow to send and receive SMS and get the IMEI No. in our app.</p>
<p>Thanks!</p>`,
} {
- output := markdown.Parse(input)
+ output := markdown.Full(input)
Expect(output).Equals(template.HTML(expected))
}
}
@@ -46,11 +46,12 @@ func TestPlainTextMarkdown(t *testing.T) {
RegisterT(t)
for input, expected := range map[string]string{
- "**Hello World**": `Hello World`,
- "[My Image](http://example.com/hello.jpg)": `My Image`,
- "Go to http://example.com/hello.jpg": `Go to http://example.com/hello.jpg`,
- "~~Option 3~~": `Option 3`,
- "# Hello World": `Hello World`,
+ "**Hello World**": `Hello World`,
+ "[My Link](http://example.com/)": `My Link`,
+ "![My Image](http://example.com/hello.jpg)": ``,
+ "Go to http://example.com/hello.jpg": `Go to http://example.com/hello.jpg`,
+ "~~Option 3~~": `Option 3`,
+ "# Hello World": `Hello World`,
`# Hello World
How are you?`: `Hello World
How are you?`,
@@ -65,3 +66,30 @@ How are you?`,
Expect(output).Equals(expected)
}
}
+
+func TestSimpleMarkdown(t *testing.T) {
+ RegisterT(t)
+
+ for input, expected := range map[string]string{
+ "**Hello World**": `<p><strong>Hello World</strong></p>`,
+ "[My Link](http://example.com/)": `<p><a href="http://example.com/">My Link</a></p>`,
+ "![My Image](http://example.com/hello.jpg)": `<p></p>`,
+ "Go to http://example.com/hello.jpg": `<p>Go to <a href="http://example.com/hello.jpg">http://example.com/hello.jpg</a></p>`,
+ "~~Option 3~~": `<p><del>Option 3</del></p>`,
+ "# Hello World": `<p>Hello World</p>`,
+ "### Hello World": `<p>Hello World</p>`,
+ "Check this out: `HEEEY`": "<p>Check this out: <code>HEEEY</code></p>",
+ `# Hello World
+How are you?`: `<p>Hello World</p>
+
+<p>How are you?</p>`,
+ `Hello World
+
+How are you?`: `<p>Hello World</p>
+
+<p>How are you?</p>`,
+ } {
+ output := markdown.Simple(input)
+ Expect(output).Equals(template.HTML(expected))
+ }
+}
diff --git a/app/pkg/markdown/simple_renderer.go b/app/pkg/markdown/simple_renderer.go
new file mode 100644
index 000000000..84ea24674
--- /dev/null
+++ b/app/pkg/markdown/simple_renderer.go
@@ -0,0 +1,194 @@
+package markdown
+
+import (
+ "bytes"
+
+ "github.com/russross/blackfriday"
+)
+
+// Ensure we implement the Blackfriday Markdown Renderer interface
+var _ blackfriday.Renderer = (*renderer)(nil)
+
+// renderer renders Markdown to plain-text meant for listings and excerpts,
+// and implements the blackfriday.Renderer interface.
+//
+// Many of the methods are stubs with no output to prevent output of HTML markup.
+type proxy struct {
+ renderer blackfriday.Renderer
+}
+
+// SimpleRenderer renders most HTML tags, except title
+func SimpleRenderer(flags int) proxy {
+ return proxy{
+ renderer: blackfriday.HtmlRenderer(flags, "", ""),
+ }
+}
+
+// Blocklevel callbacks
+
+// BlockCode is the code tag callback.
+func (p proxy) BlockCode(out *bytes.Buffer, text []byte, land string) {
+ p.renderer.BlockCode(out, text, land)
+}
+
+// BlockQuote is the quote tag callback.
+func (p proxy) BlockQuote(out *bytes.Buffer, text []byte) {
+ p.renderer.BlockQuote(out, text)
+}
+
+// BlockHtml is the HTML tag callback.
+func (p proxy) BlockHtml(out *bytes.Buffer, text []byte) {
+ p.renderer.BlockHtml(out, text)
+}
+
+// Header is the header tag callback.
+func (p proxy) Header(out *bytes.Buffer, text func() bool, level int, id string) {
+ p.Paragraph(out, text)
+}
+
+// HRule is the horizontal rule tag callback.
+func (p proxy) HRule(out *bytes.Buffer) {
+ p.renderer.HRule(out)
+}
+
+// List is the list tag callback.
+func (p proxy) List(out *bytes.Buffer, text func() bool, flags int) {
+ p.renderer.List(out, text, flags)
+}
+
+// ListItem is the list item tag callback.
+func (p proxy) ListItem(out *bytes.Buffer, text []byte, flags int) {
+ p.renderer.ListItem(out, text, flags)
+}
+
+// Paragraph is the paragraph tag callback. This renders simple paragraph text
+// into plain text, such that summaries can be easily generated.
+func (p proxy) Paragraph(out *bytes.Buffer, text func() bool) {
+ p.renderer.Paragraph(out, text)
+}
+
+// Table is the table tag callback.
+func (p proxy) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {
+ p.renderer.Table(out, header, body, columnData)
+}
+
+// TableRow is the table row tag callback.
+func (p proxy) TableRow(out *bytes.Buffer, text []byte) {
+ p.renderer.TableRow(out, text)
+}
+
+// TableHeaderCell is the table header cell tag callback.
+func (p proxy) TableHeaderCell(out *bytes.Buffer, text []byte, flags int) {
+ p.renderer.TableHeaderCell(out, text, flags)
+}
+
+// TableCell is the table cell tag callback.
+func (p proxy) TableCell(out *bytes.Buffer, text []byte, flags int) {
+ p.renderer.TableCell(out, text, flags)
+}
+
+// Footnotes is the foot notes tag callback.
+func (p proxy) Footnotes(out *bytes.Buffer, text func() bool) {
+ p.renderer.Footnotes(out, text)
+}
+
+// FootnoteItem is the footnote item tag callback.
+func (p proxy) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
+ p.renderer.FootnoteItem(out, name, text, flags)
+}
+
+// TitleBlock is the title tag callback.
+func (p proxy) TitleBlock(out *bytes.Buffer, text []byte) {
+ p.renderer.TitleBlock(out, text)
+}
+
+// Spanlevel callbacks
+
+// AutoLink is the autolink tag callback.
+func (p proxy) AutoLink(out *bytes.Buffer, link []byte, kind int) {
+ p.renderer.AutoLink(out, link, kind)
+}
+
+// CodeSpan is the code span tag callback. Outputs a simple Markdown version
+// of the code span.
+func (p proxy) CodeSpan(out *bytes.Buffer, text []byte) {
+ p.renderer.CodeSpan(out, text)
+}
+
+// DoubleEmphasis is the double emphasis tag callback. Outputs a simple
+// plain-text version of the input.
+func (p proxy) DoubleEmphasis(out *bytes.Buffer, text []byte) {
+ p.renderer.DoubleEmphasis(out, text)
+}
+
+// Emphasis is the emphasis tag callback. Outputs a simple plain-text
+// version of the input.
+func (p proxy) Emphasis(out *bytes.Buffer, text []byte) {
+ p.renderer.Emphasis(out, text)
+}
+
+// Image is the image tag callback.
+func (p proxy) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
+ p.renderer.Image(out, link, title, alt)
+}
+
+// LineBreak is the line break tag callback.
+func (p proxy) LineBreak(out *bytes.Buffer) {
+ p.renderer.LineBreak(out)
+}
+
+// Link is the link tag callback. Outputs a simple plain-text version
+// of the input.
+func (p proxy) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
+ p.renderer.Link(out, link, title, content)
+}
+
+// RawHtmlTag is the raw HTML tag callback.
+func (p proxy) RawHtmlTag(out *bytes.Buffer, tag []byte) {
+ p.renderer.RawHtmlTag(out, tag)
+}
+
+// TripleEmphasis is the triple emphasis tag callback. Outputs a simple plain-text
+// version of the input.
+func (p proxy) TripleEmphasis(out *bytes.Buffer, text []byte) {
+ p.renderer.TripleEmphasis(out, text)
+}
+
+// StrikeThrough is the strikethrough tag callback.
+func (p proxy) StrikeThrough(out *bytes.Buffer, text []byte) {
+ p.renderer.StrikeThrough(out, text)
+}
+
+// FootnoteRef is the footnote ref tag callback.
+func (p proxy) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
+ p.renderer.FootnoteRef(out, ref, id)
+}
+
+// Lowlevel callbacks
+
+// Entity callback. Outputs a simple plain-text version of the input.
+func (p proxy) Entity(out *bytes.Buffer, entity []byte) {
+ p.renderer.Entity(out, entity)
+}
+
+// NormalText callback. Outputs a simple plain-text version of the input.
+func (p proxy) NormalText(out *bytes.Buffer, text []byte) {
+ p.renderer.NormalText(out, text)
+}
+
+// Header and footer
+
+// DocumentHeader callback.
+func (p proxy) DocumentHeader(out *bytes.Buffer) {
+ p.renderer.DocumentHeader(out)
+}
+
+// DocumentFooter callback.
+func (p proxy) DocumentFooter(out *bytes.Buffer) {
+ p.renderer.DocumentFooter(out)
+}
+
+// GetFlags returns zero.
+func (p proxy) GetFlags() int {
+ return p.renderer.GetFlags()
+}
diff --git a/app/pkg/markdown/renderer.go b/app/pkg/markdown/text_renderer.go
similarity index 100%
rename from app/pkg/markdown/renderer.go
rename to app/pkg/markdown/text_renderer.go
diff --git a/app/pkg/web/renderer.go b/app/pkg/web/renderer.go
index 9edff9627..2c4beb655 100644
--- a/app/pkg/web/renderer.go
+++ b/app/pkg/web/renderer.go
@@ -25,7 +25,7 @@ var templateFunctions = template.FuncMap{
return crypto.MD5(input)
},
"markdown": func(input string) template.HTML {
- return markdown.Parse(input)
+ return markdown.Full(input)
},
}
diff --git a/app/tasks/tasks.go b/app/tasks/tasks.go
index d8f8cbd00..17a6509c4 100644
--- a/app/tasks/tasks.go
+++ b/app/tasks/tasks.go
@@ -101,7 +101,7 @@ func NotifyAboutNewPost(post *models.Post) worker.Task {
"title": post.Title,
"tenantName": c.Tenant().Name,
"userName": c.User().Name,
- "content": markdown.Parse(post.Description),
+ "content": markdown.Simple(post.Description),
"postLink": linkWithText(fmt.Sprintf("#%d", post.Number), c.BaseURL(), "/posts/%d/%s", post.Number, post.Slug),
"view": linkWithText("View it on your browser", c.BaseURL(), "/posts/%d/%s", post.Number, post.Slug),
"change": linkWithText("change your notification settings", c.BaseURL(), "/settings"),
@@ -147,7 +147,7 @@ func NotifyAboutNewComment(post *models.Post, comment *models.NewComment) worker
"title": post.Title,
"tenantName": c.Tenant().Name,
"userName": c.User().Name,
- "content": markdown.Parse(comment.Content),
+ "content": markdown.Simple(comment.Content),
"postLink": linkWithText(fmt.Sprintf("#%d", post.Number), c.BaseURL(), "/posts/%d/%s", post.Number, post.Slug),
"view": linkWithText("View it on your browser", c.BaseURL(), "/posts/%d/%s", post.Number, post.Slug),
"unsubscribe": linkWithText("unsubscribe from it", c.BaseURL(), "/posts/%d/%s", post.Number, post.Slug),
@@ -208,7 +208,7 @@ func NotifyAboutStatusChange(post *models.Post, prevStatus models.PostStatus) wo
"title": post.Title,
"postLink": linkWithText(fmt.Sprintf("#%d", post.Number), c.BaseURL(), "/posts/%d/%s", post.Number, post.Slug),
"tenantName": c.Tenant().Name,
- "content": markdown.Parse(post.Response.Text),
+ "content": markdown.Simple(post.Response.Text),
"status": post.Status.Name(),
"duplicate": duplicate,
"view": linkWithText("View it on your browser", c.BaseURL(), "/posts/%d/%s", post.Number, post.Slug),
@@ -255,7 +255,7 @@ func NotifyAboutDeletedPost(post *models.Post) worker.Task {
params := email.Params{
"title": post.Title,
"tenantName": c.Tenant().Name,
- "content": markdown.Parse(post.Response.Text),
+ "content": markdown.Simple(post.Response.Text),
"change": linkWithText("change your notification settings", c.BaseURL(), "/settings"),
}
@@ -276,7 +276,7 @@ func SendInvites(subject, message string, invitations []*models.UserInvitation)
url := fmt.Sprintf("%s/invite/verify?k=%s", c.BaseURL(), invite.VerificationKey)
toMessage := strings.Replace(message, app.InvitePlaceholder, string(url), -1)
to[i] = email.NewRecipient("", invite.Email, email.Params{
- "message": markdown.Parse(toMessage),
+ "message": markdown.Full(toMessage),
})
}
return c.Services().Emailer.BatchSend(c, "invite_email", email.Params{