From 0be85fc7f9ce10775e331bca13362da3b02ee843 Mon Sep 17 00:00:00 2001 From: Timur Demin Date: Mon, 9 Aug 2021 12:00:48 +0500 Subject: [PATCH] Handle H4-H6 in accordance with Gemini spec 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. --- internal/gemini/renderer.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/internal/gemini/renderer.go b/internal/gemini/renderer.go index 4731475..81e23da 100644 --- a/internal/gemini/renderer.go +++ b/internal/gemini/renderer.go @@ -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)