~samwhited/blogsync

241048ca535ab7cf9a58e3c3ede5b2eafd420617 — Sam Whited 1 year, 9 months ago 0cf27a4
blogsync: support more list types and footnotes

Adds support for more markdown list types, including footnotes and
footnote lists, enumerated lists, and definition lists.
2 files changed, 50 insertions(+), 7 deletions(-)

M markdown.go
M publish.go
M markdown.go => markdown.go +39 -4
@@ 20,8 20,10 @@ import (
// core markdown standard instead of just handling newlines differently in the
// web WYSIWYG editor.
type unwrapRenderer struct {
	listLevel  int
	quoteLevel int
	listLevel    int
	quoteLevel   int
	listType     blackfriday.ListType
	htmlRenderer *blackfriday.HTMLRenderer

	debug *log.Logger
}


@@ 75,7 77,13 @@ func (rend *unwrapRenderer) RenderNode(w io.Writer, node *blackfriday.Node, ente
	return blackfriday.GoToNext
}

func (*unwrapRenderer) renderLink(w io.Writer, node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
func (rend *unwrapRenderer) renderLink(w io.Writer, node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
	// Write.as doesn't support the footnote extension to markdown, so if this is
	// a footnote render it as HTML instead.
	if node.LinkData.NoteID != 0 {
		return rend.htmlRenderer.RenderNode(w, node, entering)
	}

	if entering {
		fmt.Fprintf(w, "[")
		return blackfriday.GoToNext


@@ 96,6 104,19 @@ func (*unwrapRenderer) renderImage(w io.Writer, node *blackfriday.Node, entering
}

func (rend *unwrapRenderer) renderList(w io.Writer, node *blackfriday.Node, entering bool) blackfriday.WalkStatus {

	// Footnotes aren't supported by write.as, so render the remaining nodes on
	// this branch with the HTML renderer.
	if node.IsFootnotesList ||
		rend.listType&blackfriday.ListTypeTerm == blackfriday.ListTypeTerm ||
		rend.listType&blackfriday.ListTypeDefinition == blackfriday.ListTypeDefinition {
		node.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
			return rend.htmlRenderer.RenderNode(w, node, entering)
		})
		return blackfriday.SkipChildren
	}

	rend.listType = node.ListData.ListFlags
	if entering {
		rend.listLevel++
		if rend.listLevel == 1 {


@@ 112,13 133,27 @@ func (rend *unwrapRenderer) renderList(w io.Writer, node *blackfriday.Node, ente
}

func (rend *unwrapRenderer) renderItem(w io.Writer, node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
	if node.IsFootnotesList ||
		rend.listType&blackfriday.ListTypeTerm == blackfriday.ListTypeTerm ||
		rend.listType&blackfriday.ListTypeDefinition == blackfriday.ListTypeDefinition {
		//node.Walk(func(node *blackfriday.Node, entering bool) blackfriday.WalkStatus {
		//	return rend.htmlRenderer.RenderNode(w, node, entering)
		//})
		return blackfriday.SkipChildren
	}

	if !entering {
		return blackfriday.GoToNext
	}
	for i := 0; i < rend.listLevel; i++ {
		fmt.Fprintf(w, "  ")
	}
	fmt.Fprintf(w, "* ")
	switch {
	case rend.listType&blackfriday.ListTypeOrdered == blackfriday.ListTypeOrdered:
		fmt.Fprintf(w, "1. ")
	default:
		fmt.Fprintf(w, "* ")
	}
	return blackfriday.GoToNext
}


M publish.go => publish.go +11 -3
@@ 150,9 150,17 @@ Expects an API token to be exported as $%s.`, envToken),
					return nil
				}
				body = bytes.TrimSpace(body)
				body = blackfriday.Run(body, blackfriday.WithRenderer(&unwrapRenderer{
					debug: debug,
				}))
				body = blackfriday.Run(body,
					blackfriday.WithNoExtensions(),
					blackfriday.WithExtensions(
						blackfriday.CommonExtensions|blackfriday.Footnotes,
					),
					blackfriday.WithRenderer(&unwrapRenderer{
						debug: debug,
						htmlRenderer: blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{
							Flags: blackfriday.FootnoteReturnLinks,
						}),
					}))

				var bodyBuf strings.Builder
				err = compiledTmpl.Execute(&bodyBuf, tmplData{