Ejemplo n.º 1
0
func htmlFromMarkdown(md string) string {
	if len(md) == 0 {
		return md
	}

	// Render markdown to HTML
	//
	htmlFlags := 0 |
		blackfriday.HTML_USE_XHTML |
		blackfriday.HTML_USE_SMARTYPANTS |
		blackfriday.HTML_SMARTYPANTS_FRACTIONS |
		blackfriday.HTML_SMARTYPANTS_LATEX_DASHES

	markdownExtensions := 0 |
		blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
		blackfriday.EXTENSION_FENCED_CODE |
		blackfriday.EXTENSION_STRIKETHROUGH |
		blackfriday.EXTENSION_SPACE_HEADERS

	// Leading whitespace can trigger Markdown features like
	// code blocks so let's avoid that by trimming the input
	//
	ret := string(blackfriday.MarkdownOptions(
		[]byte(strings.TrimSpace(md)),
		blackfriday.HtmlRenderer(htmlFlags, "", ""),
		blackfriday.Options{Extensions: markdownExtensions}))

	// Remove wrapping paragraph tags
	//
	ret = strings.TrimSpace(ret)
	if strings.HasPrefix(ret, "<p>") && strings.HasSuffix(ret, "</p>") {
		ret = ret[3 : len(ret)-4]
	}

	// Reinstate possible leading/trailing whitespace
	// (the markdown compiler will have removed it)
	//
	if leadingSpaces := util.LeadingWhitespace(md); 0 < len(leadingSpaces) {
		ret = leadingSpaces + ret
	}
	if trailingSpaces := util.TrailingWhitespace(md); 0 < len(trailingSpaces) {
		ret = ret + trailingSpaces
	}

	return ret
}
Ejemplo n.º 2
0
func TestLeadingWhitespace(t *testing.T) {
	ass := func(expected string, given string) {
		assert.Equal(t, expected, util.LeadingWhitespace(given), given)
	}

	ass("", "")
	ass("", "Moro")
	ass(" ", " Moro")
	ass("   ", "   Moro")
	ass("\t", "\tMoro")
	ass(" \t ", " \t Moro")
	ass("", ".   Moro")
	ass("", "xx   Moro")
	ass("", "Moro ")
	ass("", "Moro  ")
	ass("", "Moro  xx")
}