Skip to content

Commit

Permalink
Handle H4-H6 in accordance with Gemini spec
Browse files Browse the repository at this point in the history
Unlike Markdown, which has 6 levels of headings, Gemini spec (p. 5.5.1)
only allows up to 3 shebang characters before a space. This adds an extra
space before levels 4-6 in order to comply with the spec.

Fixes #1.
  • Loading branch information
tdemin committed Aug 9, 2021
1 parent 2ea0b7d commit 0be85fc
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions internal/gemini/renderer.go
Expand Up @@ -91,14 +91,25 @@ func (r Renderer) blockquote(w io.Writer, node *ast.BlockQuote, entering bool) {
}
}

const gemtextHeadingLevelLimit = 3

func (r Renderer) heading(w io.Writer, node *ast.Heading, entering bool) {
if entering {
// pad headings with the relevant number of #-s
heading := make([]byte, node.Level+1)
// pad headings with the relevant number of #-s; Gemini spec allows 3 at
// maximum before the space, therefore add one after 3 and keep padding
bufLength := node.Level + 1
spaceNeeded := node.Level > gemtextHeadingLevelLimit
if spaceNeeded {
bufLength++
}
heading := make([]byte, bufLength)
heading[len(heading)-1] = ' '
for i := 0; i < len(heading)-1; i++ {
heading[i] = '#'
}
if spaceNeeded {
heading[gemtextHeadingLevelLimit] = ' '
}
w.Write(heading)
for _, text := range node.Children {
w.Write(text.AsLeaf().Literal)
Expand Down

0 comments on commit 0be85fc

Please sign in to comment.