1 <?php namespace BookStack\Entities\Tools;
3 use BookStack\Entities\Models\Page;
4 use BookStack\Entities\Tools\Markdown\CustomStrikeThroughExtension;
5 use BookStack\Facades\Theme;
6 use BookStack\Theming\ThemeEvents;
7 use BookStack\Util\HtmlContentFilter;
11 use League\CommonMark\CommonMarkConverter;
12 use League\CommonMark\Environment;
13 use League\CommonMark\Extension\Table\TableExtension;
14 use League\CommonMark\Extension\TaskList\TaskListExtension;
22 * PageContent constructor.
24 public function __construct(Page $page)
30 * Update the content of the page with new provided HTML.
32 public function setNewHTML(string $html)
34 $this->page->html = $this->formatHtml($html);
35 $this->page->text = $this->toPlainText();
36 $this->page->markdown = '';
40 * Update the content of the page with new provided Markdown content.
42 public function setNewMarkdown(string $markdown)
44 $this->page->markdown = $markdown;
45 $html = $this->markdownToHtml($markdown);
46 $this->page->html = $this->formatHtml($html);
47 $this->page->text = $this->toPlainText();
51 * Convert the given Markdown content to a HTML string.
53 protected function markdownToHtml(string $markdown): string
55 $environment = Environment::createCommonMarkEnvironment();
56 $environment->addExtension(new TableExtension());
57 $environment->addExtension(new TaskListExtension());
58 $environment->addExtension(new CustomStrikeThroughExtension());
59 $environment = Theme::dispatch(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $environment) ?? $environment;
60 $converter = new CommonMarkConverter([], $environment);
61 return $converter->convertToHtml($markdown);
65 * Formats a page's html to be tagged correctly within the system.
67 protected function formatHtml(string $htmlText): string
69 if ($htmlText == '') {
73 libxml_use_internal_errors(true);
74 $doc = new DOMDocument();
75 $doc->loadHTML(mb_convert_encoding($htmlText, 'HTML-ENTITIES', 'UTF-8'));
77 $container = $doc->documentElement;
78 $body = $container->childNodes->item(0);
79 $childNodes = $body->childNodes;
80 $xPath = new DOMXPath($doc);
82 // Set ids on top-level nodes
84 foreach ($childNodes as $index => $childNode) {
85 [$oldId, $newId] = $this->setUniqueId($childNode, $idMap);
86 if ($newId && $newId !== $oldId) {
87 $this->updateLinks($xPath, '#' . $oldId, '#' . $newId);
91 // Ensure no duplicate ids within child items
92 $idElems = $xPath->query('//body//*//*[@id]');
93 foreach ($idElems as $domElem) {
94 [$oldId, $newId] = $this->setUniqueId($domElem, $idMap);
95 if ($newId && $newId !== $oldId) {
96 $this->updateLinks($xPath, '#' . $oldId, '#' . $newId);
100 // Generate inner html as a string
102 foreach ($childNodes as $childNode) {
103 $html .= $doc->saveHTML($childNode);
110 * Update the all links to the $old location to instead point to $new.
112 protected function updateLinks(DOMXPath $xpath, string $old, string $new)
114 $old = str_replace('"', '', $old);
115 $matchingLinks = $xpath->query('//body//*//*[@href="'.$old.'"]');
116 foreach ($matchingLinks as $domElem) {
117 $domElem->setAttribute('href', $new);
122 * Set a unique id on the given DOMElement.
123 * A map for existing ID's should be passed in to check for current existence.
124 * Returns a pair of strings in the format [old_id, new_id]
126 protected function setUniqueId(\DOMNode $element, array &$idMap): array
128 if (get_class($element) !== 'DOMElement') {
132 // Stop if there's an existing valid id that has not already been used.
133 $existingId = $element->getAttribute('id');
134 if (strpos($existingId, 'bkmrk') === 0 && !isset($idMap[$existingId])) {
135 $idMap[$existingId] = true;
136 return [$existingId, $existingId];
139 // Create an unique id for the element
140 // Uses the content as a basis to ensure output is the same every time
141 // the same content is passed through.
142 $contentId = 'bkmrk-' . mb_substr(strtolower(preg_replace('/\s+/', '-', trim($element->nodeValue))), 0, 20);
143 $newId = urlencode($contentId);
146 while (isset($idMap[$newId])) {
147 $newId = urlencode($contentId . '-' . $loopIndex);
151 $element->setAttribute('id', $newId);
152 $idMap[$newId] = true;
153 return [$existingId, $newId];
157 * Get a plain-text visualisation of this page.
159 protected function toPlainText(): string
161 $html = $this->render(true);
162 return html_entity_decode(strip_tags($html));
166 * Render the page for viewing
168 public function render(bool $blankIncludes = false) : string
170 $content = $this->page->html;
172 if (!config('app.allow_content_scripts')) {
173 $content = HtmlContentFilter::removeScripts($content);
176 if ($blankIncludes) {
177 $content = $this->blankPageIncludes($content);
179 $content = $this->parsePageIncludes($content);
186 * Parse the headers on the page to get a navigation menu
188 public function getNavigation(string $htmlContent): array
190 if (empty($htmlContent)) {
194 libxml_use_internal_errors(true);
195 $doc = new DOMDocument();
196 $doc->loadHTML(mb_convert_encoding($htmlContent, 'HTML-ENTITIES', 'UTF-8'));
197 $xPath = new DOMXPath($doc);
198 $headers = $xPath->query("//h1|//h2|//h3|//h4|//h5|//h6");
200 return $headers ? $this->headerNodesToLevelList($headers) : [];
204 * Convert a DOMNodeList into an array of readable header attributes
205 * with levels normalised to the lower header level.
207 protected function headerNodesToLevelList(DOMNodeList $nodeList): array
209 $tree = collect($nodeList)->map(function ($header) {
210 $text = trim(str_replace("\xc2\xa0", '', $header->nodeValue));
211 $text = mb_substr($text, 0, 100);
214 'nodeName' => strtolower($header->nodeName),
215 'level' => intval(str_replace('h', '', $header->nodeName)),
216 'link' => '#' . $header->getAttribute('id'),
219 })->filter(function ($header) {
220 return mb_strlen($header['text']) > 0;
223 // Shift headers if only smaller headers have been used
224 $levelChange = ($tree->pluck('level')->min() - 1);
225 $tree = $tree->map(function ($header) use ($levelChange) {
226 $header['level'] -= ($levelChange);
230 return $tree->toArray();
234 * Remove any page include tags within the given HTML.
236 protected function blankPageIncludes(string $html) : string
238 return preg_replace("/{{@\s?([0-9].*?)}}/", '', $html);
242 * Parse any include tags "{{@<page_id>#section}}" to be part of the page.
244 protected function parsePageIncludes(string $html) : string
247 preg_match_all("/{{@\s?([0-9].*?)}}/", $html, $matches);
249 foreach ($matches[1] as $index => $includeId) {
250 $fullMatch = $matches[0][$index];
251 $splitInclude = explode('#', $includeId, 2);
253 // Get page id from reference
254 $pageId = intval($splitInclude[0]);
255 if (is_nan($pageId)) {
259 // Find page and skip this if page not found
260 $matchedPage = Page::visible()->find($pageId);
261 if ($matchedPage === null) {
262 $html = str_replace($fullMatch, '', $html);
266 // If we only have page id, just insert all page html and continue.
267 if (count($splitInclude) === 1) {
268 $html = str_replace($fullMatch, $matchedPage->html, $html);
272 // Create and load HTML into a document
273 $innerContent = $this->fetchSectionOfPage($matchedPage, $splitInclude[1]);
274 $html = str_replace($fullMatch, trim($innerContent), $html);
282 * Fetch the content from a specific section of the given page.
284 protected function fetchSectionOfPage(Page $page, string $sectionId): string
286 $topLevelTags = ['table', 'ul', 'ol'];
287 $doc = new DOMDocument();
288 libxml_use_internal_errors(true);
289 $doc->loadHTML(mb_convert_encoding('<body>'.$page->html.'</body>', 'HTML-ENTITIES', 'UTF-8'));
291 // Search included content for the id given and blank out if not exists.
292 $matchingElem = $doc->getElementById($sectionId);
293 if ($matchingElem === null) {
297 // Otherwise replace the content with the found content
298 // Checks if the top-level wrapper should be included by matching on tag types
300 $isTopLevel = in_array(strtolower($matchingElem->nodeName), $topLevelTags);
302 $innerContent .= $doc->saveHTML($matchingElem);
304 foreach ($matchingElem->childNodes as $childNode) {
305 $innerContent .= $doc->saveHTML($childNode);
308 libxml_clear_errors();
310 return $innerContent;