@@ -599,24 +599,48 @@ protected function nameToSlug($name)
599
599
}
600
600
601
601
/**
602
- * Render the page for viewing, Parsing and performing features such as page transclusion.
602
+ * Render the page for viewing
603
603
* @param Page $page
604
- * @param bool $ignorePermissions
605
- * @return mixed| string
604
+ * @param bool $blankIncludes
605
+ * @return string
606
606
*/
607
- public function renderPage (Page $ page , $ ignorePermissions = false )
607
+ public function renderPage (Page $ page , bool $ blankIncludes = false ) : string
608
608
{
609
609
$ content = $ page ->html ;
610
+
610
611
if (!config ('app.allow_content_scripts ' )) {
611
612
$ content = $ this ->escapeScripts ($ content );
612
613
}
613
614
614
- $ matches = [];
615
- preg_match_all ( " /{{@\s?([0-9].*?)}}/ " , $ content, $ matches );
616
- if ( count ( $ matches [ 0 ]) === 0 ) {
617
- return $ content ;
615
+ if ( $ blankIncludes ) {
616
+ $ content = $ this -> blankPageIncludes ( $ content );
617
+ } else {
618
+ $ content = $ this -> parsePageIncludes ( $ content) ;
618
619
}
619
620
621
+ return $ content ;
622
+ }
623
+
624
+ /**
625
+ * Remove any page include tags within the given HTML.
626
+ * @param string $html
627
+ * @return string
628
+ */
629
+ protected function blankPageIncludes (string $ html ) : string
630
+ {
631
+ return preg_replace ("/{{@\s?([0-9].*?)}}/ " , '' , $ html );
632
+ }
633
+
634
+ /**
635
+ * Parse any include tags "{{@<page_id>#section}}" to be part of the page.
636
+ * @param string $html
637
+ * @return mixed|string
638
+ */
639
+ protected function parsePageIncludes (string $ html ) : string
640
+ {
641
+ $ matches = [];
642
+ preg_match_all ("/{{@\s?([0-9].*?)}}/ " , $ html , $ matches );
643
+
620
644
$ topLevelTags = ['table ' , 'ul ' , 'ol ' ];
621
645
foreach ($ matches [1 ] as $ index => $ includeId ) {
622
646
$ splitInclude = explode ('# ' , $ includeId , 2 );
@@ -625,22 +649,22 @@ public function renderPage(Page $page, $ignorePermissions = false)
625
649
continue ;
626
650
}
627
651
628
- $ matchedPage = $ this ->getById ('page ' , $ pageId, false , $ ignorePermissions );
652
+ $ matchedPage = $ this ->getById ('page ' , $ pageId );
629
653
if ($ matchedPage === null ) {
630
- $ content = str_replace ($ matches [0 ][$ index ], '' , $ content );
654
+ $ html = str_replace ($ matches [0 ][$ index ], '' , $ html );
631
655
continue ;
632
656
}
633
657
634
658
if (count ($ splitInclude ) === 1 ) {
635
- $ content = str_replace ($ matches [0 ][$ index ], $ matchedPage ->html , $ content );
659
+ $ html = str_replace ($ matches [0 ][$ index ], $ matchedPage ->html , $ html );
636
660
continue ;
637
661
}
638
662
639
663
$ doc = new DOMDocument ();
640
664
$ doc ->loadHTML (mb_convert_encoding ('<body> ' .$ matchedPage ->html .'</body> ' , 'HTML-ENTITIES ' , 'UTF-8 ' ));
641
665
$ matchingElem = $ doc ->getElementById ($ splitInclude [1 ]);
642
666
if ($ matchingElem === null ) {
643
- $ content = str_replace ($ matches [0 ][$ index ], '' , $ content );
667
+ $ html = str_replace ($ matches [0 ][$ index ], '' , $ html );
644
668
continue ;
645
669
}
646
670
$ innerContent = '' ;
@@ -652,25 +676,22 @@ public function renderPage(Page $page, $ignorePermissions = false)
652
676
$ innerContent .= $ doc ->saveHTML ($ childNode );
653
677
}
654
678
}
655
- $ content = str_replace ($ matches [0 ][$ index ], trim ($ innerContent ), $ content );
679
+ $ html = str_replace ($ matches [0 ][$ index ], trim ($ innerContent ), $ html );
656
680
}
657
681
658
- return $ content ;
682
+ return $ html ;
659
683
}
660
684
661
685
/**
662
686
* Escape script tags within HTML content.
663
687
* @param string $html
664
- * @return mixed
688
+ * @return string
665
689
*/
666
- protected function escapeScripts (string $ html )
690
+ protected function escapeScripts (string $ html ) : string
667
691
{
668
692
$ scriptSearchRegex = '/<script.*?>.*?<\/script>/ms ' ;
669
693
$ matches = [];
670
694
preg_match_all ($ scriptSearchRegex , $ html , $ matches );
671
- if (count ($ matches ) === 0 ) {
672
- return $ html ;
673
- }
674
695
675
696
foreach ($ matches [0 ] as $ match ) {
676
697
$ html = str_replace ($ match , htmlentities ($ match ), $ html );
0 commit comments