Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F10528932
render.go
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Subscribers
None
render.go
View Options
package
posts
import
(
"github.com/microcosm-cc/bluemonday"
"github.com/writeas/saturday"
"regexp"
"strings"
"unicode"
)
var
(
blockReg
=
regexp
.
MustCompile
(
"<(ul|ol|blockquote)>\n"
)
endBlockReg
=
regexp
.
MustCompile
(
"</([a-z]+)>\n</(ul|ol|blockquote)>"
)
markeddownReg
=
regexp
.
MustCompile
(
"<p>(.+)</p>"
)
)
func
ApplyMarkdown
(
data
[]
byte
)
string
{
mdExtensions
:=
0
|
blackfriday
.
EXTENSION_TABLES
|
blackfriday
.
EXTENSION_FENCED_CODE
|
blackfriday
.
EXTENSION_AUTOLINK
|
blackfriday
.
EXTENSION_STRIKETHROUGH
|
blackfriday
.
EXTENSION_SPACE_HEADERS
|
blackfriday
.
EXTENSION_HEADER_IDS
htmlFlags
:=
0
|
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
))
// Strip newlines on certain block elements that render with them
outHTML
=
blockReg
.
ReplaceAllString
(
outHTML
,
"<$1>"
)
outHTML
=
endBlockReg
.
ReplaceAllString
(
outHTML
,
"</$1></$2>"
)
return
outHTML
}
func
ApplyBasicMarkdown
(
data
[]
byte
)
string
{
mdExtensions
:=
0
|
blackfriday
.
EXTENSION_STRIKETHROUGH
|
blackfriday
.
EXTENSION_SPACE_HEADERS
|
blackfriday
.
EXTENSION_HEADER_IDS
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
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Apr 6, 7:17 AM (8 h, 17 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3186003
Attached To
rWC Write.as Web Core
Event Timeline
Log In to Comment