337 lines
5.8 KiB
Go
Raw Normal View History

2024-03-31 19:01:47 +00:00
package markdown
2024-03-31 21:08:01 +00:00
import (
"html"
"strings"
)
var (
headerStart = []string{"<h1>", "<h2>", "<h3>", "<h4>", "<h5>", "<h6>"}
headerEnd = []string{"</h1>", "</h2>", "</h3>", "</h4>", "</h5>", "</h6>"}
)
2024-04-02 08:44:47 +00:00
// renderer represents a Markdown to HTML renderer.
2024-04-01 16:10:52 +00:00
type renderer struct {
2024-04-02 08:44:47 +00:00
out []byte
2024-04-01 18:59:32 +00:00
paragraphLevel int
quoteLevel int
listLevel int
tableLevel int
2024-04-01 19:27:10 +00:00
codeLines int
2024-04-01 18:59:32 +00:00
tableHeaderWritten bool
2024-04-01 19:27:10 +00:00
inCodeBlock bool
2024-04-01 16:10:52 +00:00
}
2024-03-31 19:01:47 +00:00
// Render creates HTML from the supplied markdown text.
func Render(markdown string) string {
2024-03-31 21:08:01 +00:00
var (
2024-04-01 16:10:52 +00:00
r renderer
2024-03-31 21:08:01 +00:00
i = 0
lineStart = 0
)
2024-04-02 08:44:47 +00:00
r.out = make([]byte, 0, nextPowerOf2(uint32(len(markdown)+4)))
2024-03-31 21:08:01 +00:00
for {
if i > len(markdown) {
2024-04-01 18:04:20 +00:00
r.closeAll()
2024-04-01 16:10:52 +00:00
for range r.quoteLevel {
2024-04-02 08:44:47 +00:00
r.WriteString("</blockquote>")
2024-04-01 16:10:52 +00:00
}
2024-04-02 08:44:47 +00:00
return string(r.out)
2024-03-31 21:08:01 +00:00
}
if i != len(markdown) && markdown[i] != '\n' {
i++
continue
}
line := markdown[lineStart:i]
lineStart = i + 1
i++
2024-04-01 16:10:52 +00:00
r.processLine(line)
}
}
2024-03-31 21:08:01 +00:00
2024-04-01 16:10:52 +00:00
func (r *renderer) processLine(line string) {
2024-04-01 19:27:10 +00:00
if r.inCodeBlock {
if strings.HasPrefix(line, "```") {
2024-04-02 08:44:47 +00:00
r.WriteString("</code></pre>")
2024-04-01 19:27:10 +00:00
r.inCodeBlock = false
r.codeLines = 0
} else {
if r.codeLines != 0 {
2024-04-02 08:44:47 +00:00
r.WriteByte('\n')
2024-04-01 19:27:10 +00:00
}
2024-04-02 08:44:47 +00:00
r.WriteString(html.EscapeString(line))
2024-04-01 19:27:10 +00:00
r.codeLines++
}
return
}
2024-04-01 16:10:52 +00:00
newQuoteLevel := 0
2024-03-31 21:08:01 +00:00
2024-04-01 16:10:52 +00:00
for strings.HasPrefix(line, ">") {
line = strings.TrimSpace(line[1:])
newQuoteLevel++
}
2024-03-31 21:08:01 +00:00
2024-04-01 16:10:52 +00:00
if newQuoteLevel > r.quoteLevel {
r.closeParagraphs()
2024-03-31 21:08:01 +00:00
2024-04-01 16:10:52 +00:00
for range newQuoteLevel - r.quoteLevel {
2024-04-02 08:44:47 +00:00
r.WriteString("<blockquote>")
2024-03-31 21:08:01 +00:00
}
2024-04-01 16:10:52 +00:00
} else if newQuoteLevel < r.quoteLevel {
r.closeParagraphs()
for range r.quoteLevel - newQuoteLevel {
2024-04-02 08:44:47 +00:00
r.WriteString("</blockquote>")
2024-04-01 16:10:52 +00:00
}
}
r.quoteLevel = newQuoteLevel
2024-04-01 18:04:20 +00:00
if len(line) == 0 {
r.closeAll()
return
}
switch line[0] {
case '#':
r.closeAll()
2024-04-01 16:10:52 +00:00
space := strings.IndexByte(line, ' ')
if space > 0 && space <= 6 {
2024-04-02 08:44:47 +00:00
r.WriteString(headerStart[space-1])
2024-04-01 16:10:52 +00:00
r.writeText(line[space+1:])
2024-04-02 08:44:47 +00:00
r.WriteString(headerEnd[space-1])
2024-04-01 16:10:52 +00:00
}
return
2024-04-01 18:04:20 +00:00
case '-', '*':
2024-04-01 16:10:52 +00:00
r.closeParagraphs()
2024-04-01 18:04:20 +00:00
line = strings.TrimSpace(line[1:])
if r.listLevel == 0 {
2024-04-02 08:44:47 +00:00
r.WriteString("<ul>")
2024-04-01 18:04:20 +00:00
r.listLevel++
}
2024-04-02 08:44:47 +00:00
r.WriteString("<li>")
2024-04-01 18:04:20 +00:00
r.writeText(line)
2024-04-02 08:44:47 +00:00
r.WriteString("</li>")
2024-04-01 16:10:52 +00:00
return
2024-04-01 18:59:32 +00:00
2024-04-01 19:27:10 +00:00
case '`':
if strings.HasPrefix(line, "```") {
language := line[3:]
if !r.inCodeBlock {
if language != "" {
2024-04-02 08:44:47 +00:00
r.WriteString("<pre><code class=\"language-")
r.WriteString(html.EscapeString(language))
r.WriteString("\">")
2024-04-01 19:27:10 +00:00
} else {
2024-04-02 08:44:47 +00:00
r.WriteString("<pre><code>")
2024-04-01 19:27:10 +00:00
}
r.inCodeBlock = true
}
return
}
2024-04-01 18:59:32 +00:00
case '|':
r.closeParagraphs()
line = line[1:]
if r.tableLevel == 0 {
2024-04-02 08:44:47 +00:00
r.WriteString("<table><thead>")
2024-04-01 18:59:32 +00:00
r.tableLevel++
}
column := 0
for {
pipe := strings.IndexByte(line, '|')
if pipe == -1 {
2024-04-02 08:44:47 +00:00
r.WriteString("</tr>")
2024-04-01 18:59:32 +00:00
return
}
content := strings.TrimSpace(line[:pipe])
if strings.HasPrefix(content, "---") {
2024-04-02 08:44:47 +00:00
r.WriteString("</thead><tbody>")
2024-04-01 18:59:32 +00:00
r.tableHeaderWritten = true
return
}
if column == 0 {
2024-04-02 08:44:47 +00:00
r.WriteString("<tr>")
2024-04-01 18:59:32 +00:00
}
if r.tableHeaderWritten {
2024-04-02 08:44:47 +00:00
r.WriteString("<td>")
2024-04-01 18:59:32 +00:00
r.writeText(content)
2024-04-02 08:44:47 +00:00
r.WriteString("</td>")
2024-04-01 18:59:32 +00:00
} else {
2024-04-02 08:44:47 +00:00
r.WriteString("<th>")
2024-04-01 18:59:32 +00:00
r.writeText(content)
2024-04-02 08:44:47 +00:00
r.WriteString("</th>")
2024-04-01 18:59:32 +00:00
}
line = line[pipe+1:]
column++
}
2024-04-01 16:10:52 +00:00
}
if r.paragraphLevel == 0 {
2024-04-02 08:44:47 +00:00
r.WriteString("<p>")
2024-04-01 16:10:52 +00:00
r.paragraphLevel++
r.writeText(line)
return
}
2024-04-02 08:44:47 +00:00
r.WriteByte(' ')
2024-04-01 16:10:52 +00:00
r.writeText(line)
}
2024-04-01 18:04:20 +00:00
// closeAll closes all open tags.
func (r *renderer) closeAll() {
r.closeLists()
r.closeParagraphs()
2024-04-01 18:59:32 +00:00
r.closeTables()
2024-04-01 18:04:20 +00:00
}
2024-04-01 16:10:52 +00:00
// closeParagraphs closes open paragraphs.
func (r *renderer) closeParagraphs() {
for range r.paragraphLevel {
2024-04-02 08:44:47 +00:00
r.WriteString("</p>")
2024-03-31 21:08:01 +00:00
}
2024-04-01 16:10:52 +00:00
r.paragraphLevel = 0
2024-03-31 19:01:47 +00:00
}
2024-04-01 09:54:14 +00:00
2024-04-01 18:04:20 +00:00
// closeLists closes open lists.
func (r *renderer) closeLists() {
for range r.listLevel {
2024-04-02 08:44:47 +00:00
r.WriteString("</ul>")
2024-04-01 18:04:20 +00:00
}
r.listLevel = 0
}
2024-04-01 18:59:32 +00:00
// closeTables closes open tables.
func (r *renderer) closeTables() {
for range r.tableLevel {
2024-04-02 08:44:47 +00:00
r.WriteString("</tbody></table>")
2024-04-01 18:59:32 +00:00
}
r.tableLevel = 0
r.tableHeaderWritten = false
}
2024-04-01 16:10:52 +00:00
// writeText converts inline markdown to HTML.
func (r *renderer) writeText(markdown string) {
2024-04-01 09:54:14 +00:00
var (
i = 0
tokenStart = 0
)
var (
textStart = -1
textEnd = -1
urlStart = -1
parentheses = 0
)
for {
2024-04-01 16:10:52 +00:00
if i == len(markdown) {
2024-04-02 08:44:47 +00:00
r.WriteString(html.EscapeString(markdown[tokenStart:]))
2024-04-01 09:54:14 +00:00
return
}
2024-04-01 16:10:52 +00:00
c := markdown[i]
2024-04-01 09:54:14 +00:00
switch c {
case '[':
2024-04-02 08:44:47 +00:00
r.WriteString(html.EscapeString(markdown[tokenStart:i]))
2024-04-01 09:54:14 +00:00
tokenStart = i
textStart = i
case ']':
textEnd = i
case '(':
if parentheses == 0 {
urlStart = i
}
parentheses++
case ')':
parentheses--
if parentheses == 0 && textStart >= 0 && textEnd >= 0 && urlStart >= 0 {
2024-04-01 16:10:52 +00:00
linkText := markdown[textStart+1 : textEnd]
linkURL := markdown[urlStart+1 : i]
2024-04-01 09:54:14 +00:00
2024-04-02 08:44:47 +00:00
r.WriteString("<a href=\"")
r.WriteString(sanitizeURL(linkURL))
r.WriteString("\">")
r.WriteString(html.EscapeString(linkText))
r.WriteString("</a>")
2024-04-01 09:54:14 +00:00
textStart = -1
textEnd = -1
urlStart = -1
tokenStart = i + 1
}
}
i++
}
}
2024-04-01 18:04:20 +00:00
// sanitizeURL makes a URL safe to use as the value for a `href` attribute.
2024-04-01 16:10:52 +00:00
func sanitizeURL(linkURL string) string {
2024-04-01 17:34:02 +00:00
linkURL = strings.TrimSpace(linkURL)
2024-04-01 09:54:14 +00:00
if strings.HasPrefix(strings.ToLower(linkURL), "javascript:") {
return ""
}
return html.EscapeString(linkURL)
}
2024-04-02 08:44:47 +00:00
// WriteByte adds a single byte to the output.
func (r *renderer) WriteByte(b byte) error {
r.out = append(r.out, b)
return nil
}
// WriteString adds a string to the output.
func (r *renderer) WriteString(text string) (int, error) {
r.out = append(r.out, text...)
return len(text), nil
}
// nextPowerOf2 calculates the next 32-bit power of 2.
func nextPowerOf2(x uint32) uint32 {
x--
x |= x >> 1
x |= x >> 2
x |= x >> 4
x |= x >> 8
x |= x >> 16
x++
return x
}