3 namespace BookStack\Entities\Tools;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Entities\Tools\Markdown\CustomListItemRenderer;
7 use BookStack\Entities\Tools\Markdown\CustomStrikeThroughExtension;
8 use BookStack\Exceptions\ImageUploadException;
9 use BookStack\Facades\Theme;
10 use BookStack\Theming\ThemeEvents;
11 use BookStack\Uploads\ImageRepo;
12 use BookStack\Util\HtmlContentFilter;
16 use Illuminate\Support\Str;
17 use League\CommonMark\Block\Element\ListItem;
18 use League\CommonMark\CommonMarkConverter;
19 use League\CommonMark\Environment;
20 use League\CommonMark\Extension\Table\TableExtension;
21 use League\CommonMark\Extension\TaskList\TaskListExtension;
28 * PageContent constructor.
30 public function __construct(Page $page)
36 * Update the content of the page with new provided HTML.
38 public function setNewHTML(string $html)
40 $html = $this->extractBase64ImagesFromHtml($html);
41 $this->page->html = $this->formatHtml($html);
42 $this->page->text = $this->toPlainText();
43 $this->page->markdown = '';
47 * Update the content of the page with new provided Markdown content.
49 public function setNewMarkdown(string $markdown)
51 $markdown = $this->extractBase64ImagesFromMarkdown($markdown);
52 $this->page->markdown = $markdown;
53 $html = $this->markdownToHtml($markdown);
54 $this->page->html = $this->formatHtml($html);
55 $this->page->text = $this->toPlainText();
59 * Convert the given Markdown content to a HTML string.
61 protected function markdownToHtml(string $markdown): string
63 $environment = Environment::createCommonMarkEnvironment();
64 $environment->addExtension(new TableExtension());
65 $environment->addExtension(new TaskListExtension());
66 $environment->addExtension(new CustomStrikeThroughExtension());
67 $environment = Theme::dispatch(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $environment) ?? $environment;
68 $converter = new CommonMarkConverter([], $environment);
70 $environment->addBlockRenderer(ListItem::class, new CustomListItemRenderer(), 10);
72 return $converter->convertToHtml($markdown);
76 * Convert all base64 image data to saved images.
78 protected function extractBase64ImagesFromHtml(string $htmlText): string
80 if (empty($htmlText) || strpos($htmlText, 'data:image') === false) {
84 $doc = $this->loadDocumentFromHtml($htmlText);
85 $container = $doc->documentElement;
86 $body = $container->childNodes->item(0);
87 $childNodes = $body->childNodes;
88 $xPath = new DOMXPath($doc);
90 // Get all img elements with image data blobs
91 $imageNodes = $xPath->query('//img[contains(@src, \'data:image\')]');
92 foreach ($imageNodes as $imageNode) {
93 $imageSrc = $imageNode->getAttribute('src');
94 $newUrl = $this->base64ImageUriToUploadedImageUrl($imageSrc);
95 $imageNode->setAttribute('src', $newUrl);
98 // Generate inner html as a string
100 foreach ($childNodes as $childNode) {
101 $html .= $doc->saveHTML($childNode);
108 * Convert all inline base64 content to uploaded image files.
110 protected function extractBase64ImagesFromMarkdown(string $markdown)
113 preg_match_all('/!\[.*?]\(.*?(data:image\/.*?)[)"\s]/', $markdown, $matches);
115 foreach ($matches[1] as $base64Match) {
116 $newUrl = $this->base64ImageUriToUploadedImageUrl($base64Match);
117 $markdown = str_replace($base64Match, $newUrl, $markdown);
124 * Parse the given base64 image URI and return the URL to the created image instance.
125 * Returns an empty string if the parsed URI is invalid or causes an error upon upload.
127 protected function base64ImageUriToUploadedImageUrl(string $uri): string
129 $imageRepo = app()->make(ImageRepo::class);
130 $imageInfo = $this->parseBase64ImageUri($uri);
132 // Validate extension and content
133 if (empty($imageInfo['data']) || !$imageRepo->imageExtensionSupported($imageInfo['extension'])) {
137 // Save image from data with a random name
138 $imageName = 'embedded-image-' . Str::random(8) . '.' . $imageInfo['extension'];
141 $image = $imageRepo->saveNewFromData($imageName, $imageInfo['data'], 'gallery', $this->page->id);
142 } catch (ImageUploadException $exception) {
150 * Parse a base64 image URI into the data and extension.
151 * @return array{extension: array, data: string}
153 protected function parseBase64ImageUri(string $uri): array
155 [$dataDefinition, $base64ImageData] = explode(',', $uri, 2);
156 $extension = strtolower(preg_split('/[\/;]/', $dataDefinition)[1] ?? '');
158 'extension' => $extension,
159 'data' => base64_decode($base64ImageData) ?: '',
164 * Formats a page's html to be tagged correctly within the system.
166 protected function formatHtml(string $htmlText): string
168 if (empty($htmlText)) {
172 $doc = $this->loadDocumentFromHtml($htmlText);
173 $container = $doc->documentElement;
174 $body = $container->childNodes->item(0);
175 $childNodes = $body->childNodes;
176 $xPath = new DOMXPath($doc);
178 // Set ids on top-level nodes
180 foreach ($childNodes as $index => $childNode) {
181 [$oldId, $newId] = $this->setUniqueId($childNode, $idMap);
182 if ($newId && $newId !== $oldId) {
183 $this->updateLinks($xPath, '#' . $oldId, '#' . $newId);
187 // Ensure no duplicate ids within child items
188 $idElems = $xPath->query('//body//*//*[@id]');
189 foreach ($idElems as $domElem) {
190 [$oldId, $newId] = $this->setUniqueId($domElem, $idMap);
191 if ($newId && $newId !== $oldId) {
192 $this->updateLinks($xPath, '#' . $oldId, '#' . $newId);
196 // Generate inner html as a string
198 foreach ($childNodes as $childNode) {
199 $html .= $doc->saveHTML($childNode);
206 * Update the all links to the $old location to instead point to $new.
208 protected function updateLinks(DOMXPath $xpath, string $old, string $new)
210 $old = str_replace('"', '', $old);
211 $matchingLinks = $xpath->query('//body//*//*[@href="' . $old . '"]');
212 foreach ($matchingLinks as $domElem) {
213 $domElem->setAttribute('href', $new);
218 * Set a unique id on the given DOMElement.
219 * A map for existing ID's should be passed in to check for current existence.
220 * Returns a pair of strings in the format [old_id, new_id].
222 protected function setUniqueId(\DOMNode $element, array &$idMap): array
224 if (get_class($element) !== 'DOMElement') {
228 // Stop if there's an existing valid id that has not already been used.
229 $existingId = $element->getAttribute('id');
230 if (strpos($existingId, 'bkmrk') === 0 && !isset($idMap[$existingId])) {
231 $idMap[$existingId] = true;
233 return [$existingId, $existingId];
236 // Create an unique id for the element
237 // Uses the content as a basis to ensure output is the same every time
238 // the same content is passed through.
239 $contentId = 'bkmrk-' . mb_substr(strtolower(preg_replace('/\s+/', '-', trim($element->nodeValue))), 0, 20);
240 $newId = urlencode($contentId);
243 while (isset($idMap[$newId])) {
244 $newId = urlencode($contentId . '-' . $loopIndex);
248 $element->setAttribute('id', $newId);
249 $idMap[$newId] = true;
251 return [$existingId, $newId];
255 * Get a plain-text visualisation of this page.
257 protected function toPlainText(): string
259 $html = $this->render(true);
261 return html_entity_decode(strip_tags($html));
265 * Render the page for viewing.
267 public function render(bool $blankIncludes = false): string
269 $content = $this->page->html ?? '';
271 if (!config('app.allow_content_scripts')) {
272 $content = HtmlContentFilter::removeScripts($content);
275 if ($blankIncludes) {
276 $content = $this->blankPageIncludes($content);
278 $content = $this->parsePageIncludes($content);
285 * Parse the headers on the page to get a navigation menu.
287 public function getNavigation(string $htmlContent): array
289 if (empty($htmlContent)) {
293 $doc = $this->loadDocumentFromHtml($htmlContent);
294 $xPath = new DOMXPath($doc);
295 $headers = $xPath->query('//h1|//h2|//h3|//h4|//h5|//h6');
297 return $headers ? $this->headerNodesToLevelList($headers) : [];
301 * Convert a DOMNodeList into an array of readable header attributes
302 * with levels normalised to the lower header level.
304 protected function headerNodesToLevelList(DOMNodeList $nodeList): array
306 $tree = collect($nodeList)->map(function ($header) {
307 $text = trim(str_replace("\xc2\xa0", '', $header->nodeValue));
308 $text = mb_substr($text, 0, 100);
311 'nodeName' => strtolower($header->nodeName),
312 'level' => intval(str_replace('h', '', $header->nodeName)),
313 'link' => '#' . $header->getAttribute('id'),
316 })->filter(function ($header) {
317 return mb_strlen($header['text']) > 0;
320 // Shift headers if only smaller headers have been used
321 $levelChange = ($tree->pluck('level')->min() - 1);
322 $tree = $tree->map(function ($header) use ($levelChange) {
323 $header['level'] -= ($levelChange);
328 return $tree->toArray();
332 * Remove any page include tags within the given HTML.
334 protected function blankPageIncludes(string $html): string
336 return preg_replace("/{{@\s?([0-9].*?)}}/", '', $html);
340 * Parse any include tags "{{@<page_id>#section}}" to be part of the page.
342 protected function parsePageIncludes(string $html): string
345 preg_match_all("/{{@\s?([0-9].*?)}}/", $html, $matches);
347 foreach ($matches[1] as $index => $includeId) {
348 $fullMatch = $matches[0][$index];
349 $splitInclude = explode('#', $includeId, 2);
351 // Get page id from reference
352 $pageId = intval($splitInclude[0]);
353 if (is_nan($pageId)) {
357 // Find page and skip this if page not found
358 /** @var ?Page $matchedPage */
359 $matchedPage = Page::visible()->find($pageId);
360 if ($matchedPage === null) {
361 $html = str_replace($fullMatch, '', $html);
365 // If we only have page id, just insert all page html and continue.
366 if (count($splitInclude) === 1) {
367 $html = str_replace($fullMatch, $matchedPage->html, $html);
371 // Create and load HTML into a document
372 $innerContent = $this->fetchSectionOfPage($matchedPage, $splitInclude[1]);
373 $html = str_replace($fullMatch, trim($innerContent), $html);
380 * Fetch the content from a specific section of the given page.
382 protected function fetchSectionOfPage(Page $page, string $sectionId): string
384 $topLevelTags = ['table', 'ul', 'ol'];
385 $doc = $this->loadDocumentFromHtml($page->html);
387 // Search included content for the id given and blank out if not exists.
388 $matchingElem = $doc->getElementById($sectionId);
389 if ($matchingElem === null) {
393 // Otherwise replace the content with the found content
394 // Checks if the top-level wrapper should be included by matching on tag types
396 $isTopLevel = in_array(strtolower($matchingElem->nodeName), $topLevelTags);
398 $innerContent .= $doc->saveHTML($matchingElem);
400 foreach ($matchingElem->childNodes as $childNode) {
401 $innerContent .= $doc->saveHTML($childNode);
404 libxml_clear_errors();
406 return $innerContent;
410 * Create and load a DOMDocument from the given html content.
412 protected function loadDocumentFromHtml(string $html): DOMDocument
414 libxml_use_internal_errors(true);
415 $doc = new DOMDocument();
416 $html = '<body>' . $html . '</body>';
417 $doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));