From f5d218197ae6685f0bb76b98877f41eff0d87227 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 31 May 2021 12:19:56 +0200 Subject: [PATCH 1/3] Markdown support for entity footer, public notes & terms --- app/Utils/HtmlEngine.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 1106fe7c01f5..757792140557 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -17,6 +17,7 @@ use App\Models\CreditInvitation; use App\Models\InvoiceInvitation; use App\Models\QuoteInvitation; use App\Models\RecurringInvoiceInvitation; +use App\Services\PdfMaker\Designs\Utilities\DesignHelpers; use App\Utils\Traits\MakesDates; use Exception; use Illuminate\Support\Facades\App; @@ -121,7 +122,7 @@ class HtmlEngine if ($this->entity_string == 'invoice' || $this->entity_string == 'recurring_invoice') { $data['$entity'] = ['value' => '', 'label' => ctrans('texts.invoice')]; $data['$number'] = ['value' => $this->entity->number ?: ' ', 'label' => ctrans('texts.invoice_number')]; - $data['$entity.terms'] = ['value' => $this->entity->terms ?: '', 'label' => ctrans('texts.invoice_terms')]; + $data['$entity.terms'] = ['value' => DesignHelpers::parseMarkdownToHtml($this->entity->terms ?: '') ?: '', 'label' => ctrans('texts.invoice_terms')]; $data['$terms'] = &$data['$entity.terms']; $data['$view_link'] = ['value' => ''.ctrans('texts.view_invoice').'', 'label' => ctrans('texts.view_invoice')]; $data['$view_url'] = ['value' => $this->invitation->getLink(), 'label' => ctrans('texts.view_invoice')]; @@ -200,7 +201,7 @@ class HtmlEngine $data['$invoice.custom2'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'invoice2', $this->entity->custom_value2, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'invoice2')]; $data['$invoice.custom3'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'invoice3', $this->entity->custom_value3, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'invoice3')]; $data['$invoice.custom4'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'invoice4', $this->entity->custom_value4, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'invoice4')]; - $data['$invoice.public_notes'] = ['value' => $this->entity->public_notes ?: '', 'label' => ctrans('texts.public_notes')]; + $data['$invoice.public_notes'] = ['value' => DesignHelpers::parseMarkdownToHtml($this->entity->public_notes ?: '') ?: '', 'label' => ctrans('texts.public_notes')]; $data['$entity.public_notes'] = &$data['$invoice.public_notes']; $data['$public_notes'] = &$data['$invoice.public_notes']; @@ -374,7 +375,7 @@ class HtmlEngine $data['$description'] = ['value' => '', 'label' => ctrans('texts.description')]; //$data['$entity_footer'] = ['value' => $this->client->getSetting("{$this->entity_string}_footer"), 'label' => '']; - $data['$entity_footer'] = ['value' => $this->entity->footer, 'label' => '']; + $data['$entity_footer'] = ['value' => DesignHelpers::parseMarkdownToHtml($this->entity->footer ?: ''), 'label' => '']; $data['$page_size'] = ['value' => $this->settings->page_size, 'label' => '']; $data['$page_layout'] = ['value' => property_exists($this->settings, 'page_layout') ? $this->settings->page_layout : 'Portrait', 'label' => '']; From c9334daa3f360b1573345644702ad16c487a7c93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 31 May 2021 13:47:22 +0200 Subject: [PATCH 2/3] Note for settings-controlled parsing of markdown --- app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php b/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php index 69be963e3631..b1f70f44f32f 100644 --- a/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php +++ b/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php @@ -321,6 +321,9 @@ document.addEventListener('DOMContentLoaded', function() { public static function parseMarkdownToHtml(string $markdown): ?string { + // Use setting to determinate if parsing should be done. + // 'parse_markdown_on_pdfs' + $converter = new CommonMarkConverter([ 'allow_unsafe_links' => false, ]); From d701125bf124470d5b3047f70892f1c9757818cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Mon, 31 May 2021 14:08:24 +0200 Subject: [PATCH 3/3] Markdown parsing for line items --- app/Services/PdfMaker/Design.php | 2 ++ .../PdfMaker/Designs/Utilities/DesignHelpers.php | 14 ++++++++++++++ app/Services/PdfMaker/PdfMakerUtilities.php | 5 ++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/Services/PdfMaker/Design.php b/app/Services/PdfMaker/Design.php index f24aebd1d148..e5ac416f8897 100644 --- a/app/Services/PdfMaker/Design.php +++ b/app/Services/PdfMaker/Design.php @@ -346,6 +346,8 @@ class Design extends BaseDesign $items = $this->transformLineItems($this->entity->line_items, $type); + $this->processMarkdownOnLineItems($items); + if (count($items) == 0) { return []; } diff --git a/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php b/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php index b1f70f44f32f..40da9a3e7225 100644 --- a/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php +++ b/app/Services/PdfMaker/Designs/Utilities/DesignHelpers.php @@ -330,4 +330,18 @@ document.addEventListener('DOMContentLoaded', function() { return $converter->convertToHtml($markdown); } + + public function processMarkdownOnLineItems(array &$items) + { + // Use setting to determinate if parsing should be done. + // 'parse_markdown_on_pdfs' + + foreach ($items as $key => $item) { + foreach ($item as $variable => $value) { + $item[$variable] = DesignHelpers::parseMarkdownToHtml($value ?? ''); + } + + $items[$key] = $item; + } + } } diff --git a/app/Services/PdfMaker/PdfMakerUtilities.php b/app/Services/PdfMaker/PdfMakerUtilities.php index df84bddaab17..bc7df97a6136 100644 --- a/app/Services/PdfMaker/PdfMakerUtilities.php +++ b/app/Services/PdfMaker/PdfMakerUtilities.php @@ -92,7 +92,10 @@ trait PdfMakerUtilities $contains_html = false; if (isset($child['content'])) { - $child['content'] = nl2br($child['content']); + // Commented cause it keeps adding
at the end, if markdown parsing is turned on. + // Should update with 'parse_markdown_on_pdfs' setting. + + // $child['content'] = nl2br($child['content']); } // "/\/[a-z]*>/i" -> checks for HTML-like tags: