@@ -317,8 +317,8 @@ func RenderEmoji(
317
317
return ctx .postProcess (rawHTML )
318
318
}
319
319
320
- var byteBodyTag = [] byte ( "<body>" )
321
- var byteBodyTagClosing = [] byte ( "</body>" )
320
+ const byteBodyTag = "<body>"
321
+ const byteBodyTagClosing = "</body>"
322
322
323
323
func (ctx * postProcessCtx ) postProcess (rawHTML []byte ) ([]byte , error ) {
324
324
if ctx .procs == nil {
@@ -327,9 +327,9 @@ func (ctx *postProcessCtx) postProcess(rawHTML []byte) ([]byte, error) {
327
327
328
328
// give a generous extra 50 bytes
329
329
res := make ([]byte , 0 , len (rawHTML )+ 50 )
330
- res = append (res , byteBodyTag ... )
330
+ res = append (res , "<html><body>" ... )
331
331
res = append (res , rawHTML ... )
332
- res = append (res , byteBodyTagClosing ... )
332
+ res = append (res , "</body></html>" ... )
333
333
334
334
// parse the HTML
335
335
nodes , err := html .ParseFragment (bytes .NewReader (res ), nil )
@@ -341,6 +341,31 @@ func (ctx *postProcessCtx) postProcess(rawHTML []byte) ([]byte, error) {
341
341
ctx .visitNode (node , true )
342
342
}
343
343
344
+ newNodes := make ([]* html.Node , 0 , len (nodes ))
345
+
346
+ for _ , node := range nodes {
347
+ if node .Data == "html" {
348
+ node = node .FirstChild
349
+ for node != nil && node .Data != "body" {
350
+ node = node .NextSibling
351
+ }
352
+ }
353
+ if node == nil {
354
+ continue
355
+ }
356
+ if node .Data == "body" {
357
+ child := node .FirstChild
358
+ for child != nil {
359
+ newNodes = append (newNodes , child )
360
+ child = child .NextSibling
361
+ }
362
+ } else {
363
+ newNodes = append (newNodes , node )
364
+ }
365
+ }
366
+
367
+ nodes = newNodes
368
+
344
369
// Create buffer in which the data will be placed again. We know that the
345
370
// length will be at least that of res; to spare a few alloc+copy, we
346
371
// reuse res, resetting its length to 0.
@@ -353,9 +378,7 @@ func (ctx *postProcessCtx) postProcess(rawHTML []byte) ([]byte, error) {
353
378
}
354
379
}
355
380
356
- // remove initial parts - because Render creates a whole HTML page.
357
381
res = buf .Bytes ()
358
- res = res [bytes .Index (res , byteBodyTag )+ len (byteBodyTag ) : bytes .LastIndex (res , byteBodyTagClosing )]
359
382
360
383
// Everything done successfully, return parsed data.
361
384
return res , nil
0 commit comments