@ -11,7 +11,6 @@
package writefreely
import (
"bytes"
"fmt"
"github.com/microcosm-cc/bluemonday"
stripmd "github.com/writeas/go-strip-markdown"
@ -31,7 +30,7 @@ var (
endBlockReg = regexp . MustCompile ( "</([a-z]+)>\n</(ul|ol|blockquote)>" )
youtubeReg = regexp . MustCompile ( "(https?://www.youtube.com/embed/[a-zA-Z0-9\\-_]+)(\\?[^\t\n\f\r \"']+)?" )
titleElementReg = regexp . MustCompile ( "</?h[1-6]>" )
hashtagReg = regexp . MustCompile ( ` (?m)(^| )#([\p { L}\p { M}\d]+) ` )
hashtagReg = regexp . MustCompile ( ` {{ \ [ \ [ \ | \ | ( [ ^ | ] + ) \ | \ | \ ] \ ] }} ` )
markeddownReg = regexp . MustCompile ( "<p>(.+)</p>" )
)
@ -40,22 +39,10 @@ func (p *Post) formatContent(c *Collection, isOwner bool) {
if ! isSingleUser {
baseURL = "/" + c . Alias + "/"
}
newCon := hashtagReg . ReplaceAllFunc ( [ ] byte ( p . Content ) , func ( b [ ] byte ) [ ] byte {
// Ensure we only replace "hashtags" that have already been extracted.
// `hashtagReg` catches everything, including any hash on the end of a
// URL, so we rely on p.Tags as the final word on whether or not to link
// a tag.
for _ , t := range p . Tags {
if strings . TrimSpace ( string ( b ) ) == "#" + t {
return bytes . Replace ( b , [ ] byte ( "#" + t ) , [ ] byte ( "<a href=\"" + baseURL + "tag:" + t + "\" class=\"hashtag\"><span>#</span><span class=\"p-category\">" + t + "</span></a>" ) , - 1 )
}
}
return b
} )
p . HTMLTitle = template . HTML ( applyBasicMarkdown ( [ ] byte ( p . Title . String ) ) )
p . HTMLContent = template . HTML ( applyMarkdown ( [ ] byte ( newCon ) ) )
if exc := strings . Index ( string ( newCon ) , "<!--more-->" ) ; exc > - 1 {
p . HTMLExcerpt = template . HTML ( applyMarkdown ( [ ] byte ( newCon [ : exc ] ) ) )
p . HTMLContent = template . HTML ( applyMarkdown ( [ ] byte ( p . Content ) , baseURL ) )
if exc := strings . Index ( string ( p . Content ) , "<!--more-->" ) ; exc > - 1 {
p . HTMLExcerpt = template . HTML ( applyMarkdown ( [ ] byte ( p . Content [ : exc ] ) , baseURL ) )
}
}
@ -63,11 +50,11 @@ func (p *PublicPost) formatContent(isOwner bool) {
p . Post . formatContent ( & p . Collection . Collection , isOwner )
}
func applyMarkdown ( data [ ] byte ) string {
return applyMarkdownSpecial ( data , false )
func applyMarkdown ( data [ ] byte , baseURL string ) string {
return applyMarkdownSpecial ( data , false , baseURL )
}
func applyMarkdownSpecial ( data [ ] byte , skipNoFollow bool ) string {
func applyMarkdownSpecial ( data [ ] byte , skipNoFollow bool , baseURL string ) string {
mdExtensions := 0 |
blackfriday . EXTENSION_TABLES |
blackfriday . EXTENSION_FENCED_CODE |
@ -79,8 +66,16 @@ func applyMarkdownSpecial(data []byte, skipNoFollow bool) string {
blackfriday . HTML_USE_SMARTYPANTS |
blackfriday . HTML_SMARTYPANTS_DASHES
if baseURL != "" {
htmlFlags |= blackfriday . HTML_HASHTAGS
}
// Generate Markdown
md := blackfriday . Markdown ( [ ] byte ( data ) , blackfriday . HtmlRenderer ( htmlFlags , "" , "" ) , mdExtensions )
if baseURL != "" {
// Replace special text generated by Markdown parser
md = [ ] byte ( hashtagReg . ReplaceAll ( md , [ ] byte ( "<a href=\"" + baseURL + "tag:$1\" class=\"hashtag\"><span>#</span><span class=\"p-category\">$1</span></a>" ) ) )
}
// Strip out bad HTML
policy := getSanitizationPolicy ( )
policy . RequireNoFollowOnLinks ( ! skipNoFollow )