From 7bd9b415c3cc372e03462d8a7c031b3e858a4cc9 Mon Sep 17 00:00:00 2001
From: Anastasia Kryukova <anastasia.kryukova@protonmail.com>
Date: Mon, 10 Mar 2025 19:00:30 +0100
Subject: [PATCH] cut trailing spaces before rendering text with markdown, add
 test

---
 cypress/e2e/e2e/solid-display.cy.ts           | 17 +++++++++++++++++
 examples/data/markdown-text.jsonld            | 19 +++++++++++++++++++
 examples/e2e/solid-display.html               |  9 +++++++++
 .../markdownMixin.ts                          |  3 ++-
 4 files changed, 47 insertions(+), 1 deletion(-)
 create mode 100644 examples/data/markdown-text.jsonld

diff --git a/cypress/e2e/e2e/solid-display.cy.ts b/cypress/e2e/e2e/solid-display.cy.ts
index 4183a038..3874cc66 100644
--- a/cypress/e2e/e2e/solid-display.cy.ts
+++ b/cypress/e2e/e2e/solid-display.cy.ts
@@ -355,4 +355,21 @@ describe('solid-display', function () {
       .find('solid-display-value-label')
       .should('have.class', 'solid-display-value-label emailClass');
   });
+
+  it('should render bold text without extra spaces', () => {
+    cy.get(
+      'solid-display#display-bold-no-whitespaces solid-display-value-markdown-label',
+    )
+      .find('p')
+      .as('text');
+
+    cy.get('@text').should(
+      'contain.text',
+      "La Société d'Economie Mixte Energies Renouvelables 36",
+    );
+    cy.get('@text').should(
+      'contain.html',
+      "<strong>La Société d'Economie Mixte Energies Renouvelables 36</strong>",
+    );
+  });
 });
diff --git a/examples/data/markdown-text.jsonld b/examples/data/markdown-text.jsonld
new file mode 100644
index 00000000..d04d0d61
--- /dev/null
+++ b/examples/data/markdown-text.jsonld
@@ -0,0 +1,19 @@
+{
+  "@id": "/examples/data/markdown-text.jsonld",
+  "creation_date": "2024-11-25T16:06:33.485578Z",
+  "update_date": "2025-02-19T15:57:17.092848Z",
+  "citizen_project": {
+    "@id": "https://api.ep-contributions.startinblox.com/citizenprojects/33/",
+    "@type": "energiepartagee:citizen_project"
+  },
+  "is_featured": false,
+  "crowdfunding_url": null,
+  "general_description": "**     La Société d'Economie Mixte Energies Renouvelables 36            **(SEMER)",
+  "star_initiative_briefing": null,
+  "stakes_description": null,
+  "replicability": null,
+  "additional_informations": null,
+  "@type": "energiepartagee:communication_profile",
+  "permissions": ["view"],
+  "@context": "https://cdn.startinblox.com/owl/context.jsonld"
+}
diff --git a/examples/e2e/solid-display.html b/examples/e2e/solid-display.html
index 7d9f3669..64a4974b 100644
--- a/examples/e2e/solid-display.html
+++ b/examples/e2e/solid-display.html
@@ -328,6 +328,15 @@
       widget-email="solid-display-value-label"
   ></solid-display>
 
+  <h3>Render bold text without extra spaces</h3>
+  <solid-display
+    id="display-bold-no-whitespaces"
+    data-src="/examples/data/markdown-text.jsonld"
+    fields="general_description"
+    label-general_description="Texte en markdown : "   
+    widget-general_description="solid-display-value-markdown-label"
+  ></solid-display>
+
     <script>
       const log = document.getElementById('log-event');
       document.getElementById('display-event').addEventListener('widgetRendered', (e) => {
diff --git a/src/new-widgets/valueTransformationMixins/markdownMixin.ts b/src/new-widgets/valueTransformationMixins/markdownMixin.ts
index 7b2382b5..c9299aca 100644
--- a/src/new-widgets/valueTransformationMixins/markdownMixin.ts
+++ b/src/new-widgets/valueTransformationMixins/markdownMixin.ts
@@ -31,7 +31,8 @@ const MarkdownMixin = {
         },
       });
 
-      const html = md.render(value);
+      const cleanedText = value.replace(/\*\*\s*(.*?)\s*\*\*/g, '**$1**');
+      const html = md.render(cleanedText);
       newValue = unsafeHTML(html);
     }
 
-- 
GitLab