@@ -103,6 +103,30 @@ func newPageContentOutput(p *pageState, po *pageOutput) (*pageContentOutput, err
103
103
return err
104
104
}
105
105
106
+ ctxCallback := func (cp2 * pageContentOutput ) {
107
+ cp .p .cmap .hasNonMarkdownShortcode = cp .p .cmap .hasNonMarkdownShortcode || cp2 .p .cmap .hasNonMarkdownShortcode
108
+ // Merge content placeholders
109
+ for k , v := range cp2 .contentPlaceholders {
110
+ cp .contentPlaceholders [k ] = v
111
+ }
112
+
113
+ if p .s .watching () {
114
+ for _ , s := range cp2 .p .shortcodeState .shortcodes {
115
+ for _ , templ := range s .templs {
116
+ dependencyTracker .Add (templ .(identity.Manager ))
117
+ }
118
+ }
119
+ }
120
+
121
+ // Transfer shortcode names so HasShortcode works for shortcodes from included pages.
122
+ cp .p .shortcodeState .transferNames (cp2 .p .shortcodeState )
123
+ if cp2 .p .pageOutputTemplateVariationsState .Load () == 2 {
124
+ cp .p .pageOutputTemplateVariationsState .Store (2 )
125
+ }
126
+ }
127
+
128
+ ctx = tpl .SetCallbackFunctionInContext (ctx , ctxCallback )
129
+
106
130
var hasVariants bool
107
131
cp .workContent , hasVariants , err = p .contentToRender (ctx , p .source .parsed , p .cmap , cp .contentPlaceholders )
108
132
if err != nil {
@@ -350,6 +374,63 @@ func (p *pageContentOutput) Fragments(ctx context.Context) *tableofcontents.Frag
350
374
return p .tableOfContents
351
375
}
352
376
377
+ func (p * pageContentOutput ) RenderShortcodes (ctx context.Context ) (template.HTML , error ) {
378
+ p .p .s .initInit (ctx , p .initToC , p .p )
379
+ source := p .p .source .parsed .Input ()
380
+ renderedShortcodes := p .contentPlaceholders
381
+ var insertPlaceholders bool
382
+ var hasVariants bool
383
+ var cb func (* pageContentOutput )
384
+ if v := tpl .GetCallbackFunctionFromContext (ctx ); v != nil {
385
+ if fn , ok := v .(func (* pageContentOutput )); ok {
386
+ insertPlaceholders = true
387
+ cb = fn
388
+ }
389
+ }
390
+ c := make ([]byte , 0 , len (source )+ (len (source )/ 10 ))
391
+ for _ , it := range p .p .cmap .items {
392
+ switch v := it .(type ) {
393
+ case pageparser.Item :
394
+ c = append (c , source [v .Pos ():v .Pos ()+ len (v .Val (source ))]... )
395
+ case pageContentReplacement :
396
+ // Ignore.
397
+ case * shortcode :
398
+ if ! insertPlaceholders || ! v .insertPlaceholder () {
399
+ // Insert the rendered shortcode.
400
+ renderedShortcode , found := renderedShortcodes [v .placeholder ]
401
+ if ! found {
402
+ // This should never happen.
403
+ panic (fmt .Sprintf ("rendered shortcode %q not found" , v .placeholder ))
404
+ }
405
+
406
+ b , more , err := renderedShortcode .renderShortcode (ctx )
407
+ if err != nil {
408
+ return "" , fmt .Errorf ("failed to render shortcode: %w" , err )
409
+ }
410
+ hasVariants = hasVariants || more
411
+ c = append (c , []byte (b )... )
412
+
413
+ } else {
414
+ // Insert the placeholder so we can insert the content after
415
+ // markdown processing.
416
+ c = append (c , []byte (v .placeholder )... )
417
+ }
418
+ default :
419
+ panic (fmt .Sprintf ("unknown item type %T" , it ))
420
+ }
421
+ }
422
+
423
+ if hasVariants {
424
+ p .p .pageOutputTemplateVariationsState .Store (2 )
425
+ }
426
+
427
+ if cb != nil {
428
+ cb (p )
429
+ }
430
+
431
+ return helpers .BytesToHTML (c ), nil
432
+ }
433
+
353
434
func (p * pageContentOutput ) TableOfContents (ctx context.Context ) template.HTML {
354
435
p .p .s .initInit (ctx , p .initToC , p .p )
355
436
return p .tableOfContentsHTML
0 commit comments