Skip to content

Commit

Permalink
Add ApplyBasicAccessibleMarkdown func
Browse files Browse the repository at this point in the history
  • Loading branch information
thebaer committed Dec 15, 2021
1 parent 789795d commit 2514905
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions posts/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ func ApplyBasicMarkdown(data []byte) string {
return outHTML
}

// ApplyBasicAccessibleMarkdown applies Markdown to the given data, rendering basic text formatting and preserving hard
// line breaks. It is meant for formatting text in small, multi-line UI elements, like user profile biographies.
func ApplyBasicAccessibleMarkdown(data []byte) string {
mdExtensions := 0 |
blackfriday.EXTENSION_STRIKETHROUGH |
blackfriday.EXTENSION_SPACE_HEADERS |
blackfriday.EXTENSION_HEADER_IDS |
blackfriday.EXTENSION_HARD_LINE_BREAK
htmlFlags := 0 |
blackfriday.HTML_SKIP_HTML |
blackfriday.HTML_USE_SMARTYPANTS |
blackfriday.HTML_SMARTYPANTS_DASHES

// Generate Markdown
md := blackfriday.Markdown([]byte(data), blackfriday.HtmlRenderer(htmlFlags, "", ""), mdExtensions)
// Strip out bad HTML
policy := bluemonday.UGCPolicy()
policy.AllowAttrs("class", "id").Globally()
outHTML := string(policy.SanitizeBytes(md))
outHTML = markeddownReg.ReplaceAllString(outHTML, "$1")
outHTML = strings.TrimRightFunc(outHTML, unicode.IsSpace)

return outHTML
}

// StripHTMLWithoutEscaping strips HTML tags with bluemonday's StrictPolicy, then unescapes the HTML
// entities added in by sanitizing the content.
func StripHTMLWithoutEscaping(content string) string {
Expand Down

0 comments on commit 2514905

Please sign in to comment.