@@ -62,44 +62,27 @@ func RenderExtended(ops []byte, customFormats func(string, *Op) Formatter) (html
6262 // Open the a block element, write its body, and close it to move on only when the ending "\n" of the block is reached.
6363 if strings .IndexByte (o .Data , '\n' ) != - 1 {
6464
65- if o .Data == "\n " { // Write a block element and flush the temporary buffer.
65+ // Extract text from between the block-terminating line feeds and write each part as its own Op.
66+ split := strings .Split (o .Data , "\n " )
6667
67- // Avoid empty paragraphs and "\n" in the output.
68- if tempBuf .Len () == 0 {
69- o .Data = "<br>"
70- } else {
71- o .Data = ""
72- }
73-
74- o .writeBlock (fs , tempBuf , finalBuf , fms )
75-
76- } else { // Extract the block-terminating line feeds and write each part as its own Op.
77-
78- split := strings .Split (o .Data , "\n " )
68+ for i := range split {
7969
80- for i := range split {
70+ o . Data = split [ i ]
8171
82- o .Data = split [i ]
72+ // If the current o.Data still has an "\n" following (its not the last in split), then it ends a block.
73+ if i < len (split )- 1 {
8374
84- // If the current o.Data still has an "\n" following (its not the last in split), then it ends a block.
85- if i < len (split )- 1 {
75+ o .writeBlock (fs , tempBuf , finalBuf , fms )
8676
87- // Avoid having empty paragraphs.
88- if tempBuf .Len () == 0 && o .Data == "" {
89- o .Data = "<br>"
90- }
77+ } else if o .Data != "" { // If the last element in split is just "" then the last character in the rawOp is "\n".
9178
92- o .writeBlock (fs , tempBuf , finalBuf , fms )
93-
94- } else if o .Data != "" { // If the last element in split is just "" then the last character in the rawOp is "\n".
95- o .writeInline (fs , tempBuf , fms )
96- }
79+ o .writeInline (fs , tempBuf , fms )
9780
9881 }
9982
10083 }
10184
102- } else { // We are just adding stuff inline.
85+ } else {
10386 o .writeInline (fs , tempBuf , fms )
10487 }
10588
@@ -154,19 +137,6 @@ type Op struct {
154137func (o * Op ) writeBlock (fs * formatState , tempBuf * bytes.Buffer , finalBuf * bytes.Buffer , newFms []* Format ) {
155138
156139 // Close the inline formats opened within the block to the tempBuf and block formats of wrappers to finalBuf.
157- //wrapFs := &formatState{make([]*Format, 0, 2)}
158- //inlineFs := &formatState{make([]*Format, 0, 2)}
159- //for i := range fs.open {
160- // if fs.open[i].wrap {
161- // wrapFs.add(fs.open[i])
162- // } else {
163- // inlineFs.add(fs.open[i])
164- // }
165- //}
166- //wrapFs.closePrevious(finalBuf, o, true)
167- //inlineFs.closePrevious(tempBuf, o, true)
168- //fs.open = append(wrapFs.open, inlineFs.open...) // There may be something left open.
169- //fs.closePrevious(tempBuf, o, true)
170140 closedTemp := & formatState {}
171141
172142 for i := len (fs .open ) - 1 ; i >= 0 ; i -- { // Start with the last format opened.
@@ -208,11 +178,6 @@ func (o *Op) writeBlock(fs *formatState, tempBuf *bytes.Buffer, finalBuf *bytes.
208178 style string
209179 }
210180
211- // At least a format from the Op.Type should be set.
212- if len (newFms ) == 0 {
213- return
214- }
215-
216181 // Merge all formats into a single tag.
217182 for i := range newFms {
218183 fm := newFms [i ]
@@ -222,9 +187,7 @@ func (o *Op) writeBlock(fs *formatState, tempBuf *bytes.Buffer, finalBuf *bytes.
222187 switch fm .Place {
223188 case Tag :
224189 // If an opening tag is not specified by the Op insert type, it may be specified by an attribute.
225- if v != "" {
226- block .tagName = v // Override whatever value is set.
227- }
190+ block .tagName = v // Override whatever value is set.
228191 case Class :
229192 block .classes = append (block .classes , v )
230193 case Style :
@@ -239,6 +202,11 @@ func (o *Op) writeBlock(fs *formatState, tempBuf *bytes.Buffer, finalBuf *bytes.
239202 }
240203 }
241204
205+ // Avoid empty paragraphs and "\n" in the output for text blocks.
206+ if o .Data == "" && block .tagName == "p" && tempBuf .Len () == 0 {
207+ o .Data = "<br>"
208+ }
209+
242210 if block .tagName != "" {
243211 finalBuf .WriteByte ('<' )
244212 finalBuf .WriteString (block .tagName )
@@ -258,9 +226,6 @@ func (o *Op) writeBlock(fs *formatState, tempBuf *bytes.Buffer, finalBuf *bytes.
258226 closeTag (finalBuf , block .tagName )
259227 }
260228
261- // Write out the closes by FormatWrapper formats, starting from the last written.
262- //fs.closePrevious(finalBuf, o, true)
263-
264229 tempBuf .Reset ()
265230
266231}
@@ -368,6 +333,8 @@ func (o *Op) getFormatter(keyword string, customFormats func(string, *Op) Format
368333 sf .t = "sub"
369334 }
370335 return sf
336+ case "code-block" :
337+ return & codeBlockFormat {o }
371338 }
372339
373340 return nil
0 commit comments