Skip to content

Commit

Permalink
Fixes for designs that use markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo124 committed Jan 14, 2025
1 parent d35545b commit 5f553b6
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 37 deletions.
46 changes: 36 additions & 10 deletions app/Services/Pdf/PdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1996,23 +1996,49 @@ public function updateElementProperty($element, string $attribute, ?string $valu
public function createElementContent($element, $children): self
{
foreach ($children as $child) {

$contains_html = false;
$contains_markdown = false;
$child['content'] = $child['content'] ?? '';

if ($child['element'] !== 'script') {
if ($this->service->company->markdown_enabled && array_key_exists('content', $child)) {
$child['content'] = str_replace('<br>', "\r", ($child['content'] ?? ''));
$child['content'] = $this->commonmark->convert($child['content'] ?? ''); //@phpstan-ignore-line
}
}
$lines = explode("\n", $child['content']);
$contains_markdown = false;

if (isset($child['content'])) {
if (isset($child['is_empty']) && $child['is_empty'] === true) {
foreach ($lines as $line) {
$trimmed = ltrim($line);
if (empty($trimmed)) {
continue;
}

$contains_html = preg_match('#(?<=<)\w+(?=[^<]*?>)#', $child['content'], $m) != 0;
$first_char = substr($trimmed, 0, 1);

if (
$first_char === '#' || // Headers
$first_char === '>' || // Blockquotes
$first_char === '-' || // Lists
$first_char === '*' || // Lists/Bold
$first_char === '_' || // Italic
$first_char === '`' || // Code
$first_char === '[' || // Links
str_contains($trimmed, '**') // Bold (special case)
) {
$contains_markdown = true;
break;
}
}

if ($this->service->company->markdown_enabled && $contains_markdown && $child['element'] !== 'script') {
$child['content'] = str_ireplace('<br>', "\r", $child['content']);
$child['content'] = $this->commonmark->convert($child['content']); //@phpstan-ignore-line
}

if (isset($child['is_empty']) && $child['is_empty'] === true) {
continue;
}

$contains_html = str_contains($child['content'], '<') && str_contains($child['content'], '>');


if ($contains_html) {

// If the element contains the HTML, we gonna display it as is. Backend is going to
Expand All @@ -2029,7 +2055,7 @@ public function createElementContent($element, $children): self
// .. in case string doesn't contain any HTML, we'll just return
// raw $content

$_child = $this->document->createElement($child['element'], isset($child['content']) ? htmlspecialchars($child['content']) : '');
$_child = $this->document->createElement($child['element'], htmlspecialchars($child['content']));
}

$element->appendChild($_child);
Expand Down
46 changes: 34 additions & 12 deletions app/Services/PdfMaker/PdfMakerUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,46 @@ public function createElementContent($element, $children)
{
foreach ($children as $child) {
$contains_html = false;
$contains_markdown = false;
$child['content'] = $child['content'] ?? '';

if ($child['element'] !== 'script') {
if (array_key_exists('process_markdown', $this->data) && array_key_exists('content', $child) && $this->data['process_markdown']) {
$child['content'] = str_replace('<br>', "\r", ($child['content'] ?? ''));
$child['content'] = $this->commonmark->convert($child['content'] ?? ''); //@phpstan-ignore-line
}
}
$lines = explode("\n", $child['content']);
$contains_markdown = false;

if (isset($child['content'])) {
if (isset($child['is_empty']) && $child['is_empty'] === true) {
foreach ($lines as $line) {
$trimmed = ltrim($line);
if (empty($trimmed)) {
continue;
}

$contains_html = preg_match('#(?<=<)\w+(?=[^<]*?>)#', $child['content'], $m) != 0;
$first_char = substr($trimmed, 0, 1);

if (
$first_char === '#' || // Headers
$first_char === '>' || // Blockquotes
$first_char === '-' || // Lists
$first_char === '*' || // Lists/Bold
$first_char === '_' || // Italic
$first_char === '`' || // Code
$first_char === '[' || // Links
str_contains($trimmed, '**') // Bold (special case)
) {
$contains_markdown = true;
break;
}
}

if (isset($this->data['process_markdown']) && $this->data['process_markdown'] && $contains_markdown &&$child['element'] !== 'script') {
$child['content'] = str_replace('<br>', "\r", $child['content']);
$child['content'] = $this->commonmark->convert($child['content']); //@phpstan-ignore-line
}

if (isset($child['is_empty']) && $child['is_empty'] === true) {
continue;
}

$contains_html = str_contains($child['content'], '<') && str_contains($child['content'], '>');

if ($contains_html) {
// If the element contains the HTML, we gonna display it as is. Backend is going to
// encode it for us, preventing any errors on the processing stage.
Expand All @@ -118,9 +142,7 @@ public function createElementContent($element, $children)
$_child->nodeValue = htmlspecialchars($child['content']);
} else {
// .. in case string doesn't contain any HTML, we'll just return
// raw $content.

$_child = $this->document->createElement($child['element'], isset($child['content']) ? htmlspecialchars($child['content']) : '');
$_child = $this->document->createElement($child['element'], htmlspecialchars($child['content']));
}

$element->appendChild($_child);
Expand Down
21 changes: 6 additions & 15 deletions app/Services/Template/TemplateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1575,23 +1575,14 @@ public function createElementContent($element, $children): self

foreach ($children as $child) {
$contains_html = false;
$child['content'] = $child['content'] ?? '';

//06-11-2023 for some reason this parses content as HTML
// if ($child['element'] !== 'script') {
// if ($this->company->markdown_enabled && array_key_exists('content', $child)) {
// $child['content'] = str_replace('<br>', "\r", $child['content']);
// $child['content'] = $this->commonmark->convert($child['content'] ?? '');
// }
// }

if (isset($child['content'])) {
if (isset($child['is_empty']) && $child['is_empty'] === true) {
continue;
}

$contains_html = preg_match('#(?<=<)\w+(?=[^<]*?>)#', $child['content'], $m) != 0;
if (isset($child['is_empty']) && $child['is_empty'] === true) {
continue;
}

$contains_html = str_contains($child['content'], '<') && str_contains($child['content'], '>');

if ($contains_html) {
// If the element contains the HTML, we gonna display it as is. Backend is going to
// encode it for us, preventing any errors on the processing stage.
Expand All @@ -1605,7 +1596,7 @@ public function createElementContent($element, $children): self
} else {
// .. in case string doesn't contain any HTML, we'll just return
// raw $content.
$_child = $this->document->createElement($child['element'], isset($child['content']) ? $child['content'] : '');
$_child = $this->document->createElement($child['element'], $child['content']);
}

$element->appendChild($_child);
Expand Down

0 comments on commit 5f553b6

Please sign in to comment.