add lists support to markdown
Ted Unangst tedu@tedunangst.com
Tue, 12 Nov 2019 14:45:39 -0500
4 files changed,
32 insertions(+),
0 deletions(-)
M
docs/changelog.txt
→
docs/changelog.txt
@@ -2,6 +2,8 @@ changelog
-- next ++ Lists supported in markdown. + + Rewrite admin console to avoid large dependencies. + "Bug" fixes.
M
docs/honk.5
→
docs/honk.5
@@ -46,6 +46,11 @@ /* triple tick code blocks support syntax highlighting */
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
M
markitzero.go
→
markitzero.go
@@ -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 @@ s = re_bolder.ReplaceAllString(s, "$1<b>$2</b>$3")
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, "<img x>", "<img x>", -1) s = re_imgfix.ReplaceAllStringFunc(s, func(string) string {@@ -105,6 +117,7 @@ // some final fixups
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 }
M
markitzero_test.go
→
markitzero_test.go
@@ -94,3 +94,15 @@ input := `an image <img alt="caption" src="https://example.com/wherever"> and linked [<img src="there">](example.com)`
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) +}