add lists support to markdown

This commit is contained in:
Ted Unangst 2019-11-12 14:45:39 -05:00
parent 46e6d99eda
commit d31cc36a75
4 changed files with 32 additions and 0 deletions

View File

@ -2,6 +2,8 @@ changelog
-- next
+ Lists supported in markdown.
+ Rewrite admin console to avoid large dependencies.
+ "Bug" fixes.

View File

@ -46,6 +46,11 @@ Inline `code fragments` with single ticks.
int main() { return 0; }
```
.Ed
.It lists
Lists of items starting with either
.Sq +
or
.Sq - .
.It images
Inline images with img tags.
.Bd -literal

View File

@ -32,6 +32,7 @@ var re_quoter = regexp.MustCompile(`(?m:^> (.*)\n?)`)
var re_link = regexp.MustCompile(`.?.?https?://[^\s"]+[\w/)!]`)
var re_zerolink = regexp.MustCompile(`\[([^]]*)\]\(([^)]*\)?)\)`)
var re_imgfix = regexp.MustCompile(`<img ([^>]*)>`)
var re_lister = regexp.MustCompile(`((^|\n)(\+|-).*)+\n?`)
var lighter = synlight.New(synlight.Options{Format: synlight.HTML})
@ -78,6 +79,17 @@ func markitzero(s string) string {
s = re_italicer.ReplaceAllString(s, "$1<i>$2</i>$3")
s = re_quoter.ReplaceAllString(s, "<blockquote>$1</blockquote><p>")
s = re_lister.ReplaceAllStringFunc(s, func(m string) string {
m = strings.Trim(m, "\n")
items := strings.Split(m, "\n")
r := "<ul>"
for _, item := range items {
r += "<li>" + strings.Trim(item[1:], " ")
}
r += "</ul><p>"
return r
})
// restore images
s = strings.Replace(s, "&lt;img x&gt;", "<img x>", -1)
s = re_imgfix.ReplaceAllStringFunc(s, func(string) string {
@ -105,6 +117,7 @@ func markitzero(s string) string {
s = strings.Replace(s, "\n", "<br>", -1)
s = strings.Replace(s, "<br><blockquote>", "<blockquote>", -1)
s = strings.Replace(s, "<br><pre>", "<pre>", -1)
s = strings.Replace(s, "<br><ul>", "<ul>", -1)
s = strings.Replace(s, "<p><br>", "<p>", -1)
return s
}

View File

@ -94,3 +94,15 @@ func TestImagelink(t *testing.T) {
output := `an image <img alt="caption" src="https://example.com/wherever"> and linked <a href="example.com"><img src="there"></a>`
doonezerotest(t, input, output)
}
func TestLists(t *testing.T) {
input := `hello
+ a list
+ the list
para
- singleton`
output := `hello<ul><li>a list<li>the list</ul><p>para<ul><li>singleton</ul><p>`
doonezerotest(t, input, output)
}