Skip to content

Commit

Permalink
Improve the rendering of subsequent links
Browse files Browse the repository at this point in the history
  • Loading branch information
tdemin committed Nov 7, 2020
1 parent 3d9314f commit 6ee9efa
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions internal/gemini/renderer.go
Expand Up @@ -25,6 +25,9 @@ func NewRenderer() Renderer {
return Renderer{}
}

// TODO: lists
// TODO: code renderer

func (r Renderer) link(w io.Writer, node *ast.Link, entering bool) {
if entering {
w.Write(linkPrefix)
Expand Down Expand Up @@ -108,7 +111,7 @@ func (r Renderer) heading(w io.Writer, node *ast.Heading, entering bool) {
}
}

func (r Renderer) paragraph(w io.Writer, node *ast.Paragraph, entering bool) {
func (r Renderer) paragraph(w io.Writer, node *ast.Paragraph, entering bool) (noNewLine bool) {
if entering {
children := node.Children
linkStack := make([]ast.Node, 0, len(children))
Expand All @@ -127,6 +130,18 @@ func (r Renderer) paragraph(w io.Writer, node *ast.Paragraph, entering bool) {
return false
}()
onlyElement := len(children) == 1 || onlyElementWithGoMarkdownFix
onlyElementIsLink := func() bool {
if len(children) >= 1 {
if _, ok := children[0].(*ast.Link); ok {
return true
}
if _, ok := children[0].(*ast.Image); ok {
return true
}
}
return false
}()
noNewLine = onlyElementIsLink
for _, child := range children {
// only render links text in the paragraph if they're
// combined with some other text on page
Expand All @@ -146,10 +161,14 @@ func (r Renderer) paragraph(w io.Writer, node *ast.Paragraph, entering bool) {
r.text(w, text)
}
}
w.Write(lineBreak)
if !onlyElementIsLink {
w.Write(lineBreak)
}
// render a links block after paragraph
if len(linkStack) > 0 {
w.Write(lineBreak)
if !onlyElementIsLink {
w.Write(lineBreak)
}
for _, link := range linkStack {
if link, ok := link.(*ast.Link); ok {
r.link(w, link, true)
Expand All @@ -161,6 +180,7 @@ func (r Renderer) paragraph(w io.Writer, node *ast.Paragraph, entering bool) {
}
}
}
return
}

func (r Renderer) code(w io.Writer, node *ast.Code, entering bool) {
Expand Down Expand Up @@ -191,9 +211,9 @@ func (r Renderer) RenderNode(w io.Writer, node ast.Node, entering bool) ast.Walk
noNewLine = false
case *ast.Paragraph:
// blockquote wraps paragraphs which makes for an extra render
if _, parentIsBlockQuote := node.Parent.(*ast.BlockQuote); !parentIsBlockQuote {
r.paragraph(w, node, entering)
noNewLine = false
_, parentIsBlockQuote := node.Parent.(*ast.BlockQuote)
if !parentIsBlockQuote {
noNewLine = r.paragraph(w, node, entering)
}
case *ast.Code:
// TODO: *ast.Code render is likely to be merged into paragraph
Expand Down

0 comments on commit 6ee9efa

Please sign in to comment.