From 1d6b9eca326544b5b5b63d77c12ec4c33f7365c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20R=C3=B8ed=20Hestvik?= Date: Tue, 4 Jun 2024 16:06:45 +0200 Subject: [PATCH 1/2] Fix markdown export --- packages/bygger/src/translations/utils.test.ts | 14 ++++++-------- .../StructuredHtml/StructuredHtmlElement.test.ts | 10 ++++++---- .../html/StructuredHtml/StructuredHtmlElement.ts | 5 +++-- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/packages/bygger/src/translations/utils.test.ts b/packages/bygger/src/translations/utils.test.ts index 2584598c0..fe8c48f85 100644 --- a/packages/bygger/src/translations/utils.test.ts +++ b/packages/bygger/src/translations/utils.test.ts @@ -501,23 +501,21 @@ describe('Skjema med globale oversettelser som inneholder linjeskift', () => { }, }, }; + it('fjerner linjeskift i tekster som skal eksporteres', () => { const eksport = getTextsAndTranslationsForForm(form, translations); - expect(eksport).toHaveLength(6); + expect(eksport).toHaveLength(2); expect(eksport[0].text).toBe('Veiledning'); expect(eksport[0].en).toBe('Guidance (Global Tekst)'); expect(eksport[0]['nn-NO']).toBe('Rettleiing (Global Tekst)'); - expect([eksport[1].text, eksport[2].text, eksport[3].text]).toEqual(['NAV sender svar. ', '
', ' Se ']); - expect([eksport[1].en, eksport[2].en, eksport[3].en]).toEqual(['NAV sends answers. ', '
', ' See ']); - expect([eksport[1]['nn-NO'], eksport[2]['nn-NO'], eksport[3]['nn-NO']]).toEqual([ - 'NAV sender svar. ', - '
', - ' Sjå ', - ]); + expect(eksport[1].text).toEqual('NAV sender svar.
Se [link](https://www.nav.no/person/).'); + expect(eksport[1].en).toEqual('NAV sends answers.
See [link](https://www.nav.no/person/).'); + expect(eksport[1]['nn-NO']).toEqual('NAV sender svar.
Sjå [lenke](https://www.nav.no/person/).'); }); }); + describe('testGetCSVfileHeaders', () => { it('Test headers with only origin form text', () => { const actual = getTextsAndTranslationsHeaders([] as FormioTranslationMap); diff --git a/packages/shared-components/src/util/html/StructuredHtml/StructuredHtmlElement.test.ts b/packages/shared-components/src/util/html/StructuredHtml/StructuredHtmlElement.test.ts index 0d0e74d72..33379b0f6 100644 --- a/packages/shared-components/src/util/html/StructuredHtml/StructuredHtmlElement.test.ts +++ b/packages/shared-components/src/util/html/StructuredHtml/StructuredHtmlElement.test.ts @@ -171,15 +171,17 @@ describe('StructuredHtmlElement', () => { const withLinkJsonMarkdownChildren = ( (withLinkJson.children[0] as HtmlAsJsonElement).children[0] as HtmlAsJsonElement ).children; - expect((withLinkJsonMarkdownChildren[0] as HtmlAsJsonTextElement).textContent).toBe('Punkt 1 med '); - expect((withLinkJsonMarkdownChildren[1] as HtmlAsJsonTextElement).textContent).toBe('[lenketekst](www.url.no)'); + expect((withLinkJsonMarkdownChildren[0] as HtmlAsJsonTextElement).textContent).toBe( + 'Punkt 1 med [lenketekst](www.url.no)', + ); }); it('converts strong to json with markdown', () => { const withStrongJson = withStrong.toJson(true); const withStrongJsonMarkdownChildren = (withStrongJson.children[0] as HtmlAsJsonElement).children; - expect((withStrongJsonMarkdownChildren[0] as HtmlAsJsonTextElement).textContent).toBe('Avsnitt med '); - expect((withStrongJsonMarkdownChildren[1] as HtmlAsJsonTextElement).textContent).toBe('**fet skrift**'); + expect((withStrongJsonMarkdownChildren[0] as HtmlAsJsonTextElement).textContent).toBe( + 'Avsnitt med **fet skrift**.', + ); }); it('does not convert strong to markdown when conversion was done without skipping within given tags', () => { diff --git a/packages/shared-components/src/util/html/StructuredHtml/StructuredHtmlElement.ts b/packages/shared-components/src/util/html/StructuredHtml/StructuredHtmlElement.ts index 078b7e848..ea590dd6e 100644 --- a/packages/shared-components/src/util/html/StructuredHtml/StructuredHtmlElement.ts +++ b/packages/shared-components/src/util/html/StructuredHtml/StructuredHtmlElement.ts @@ -160,13 +160,14 @@ class StructuredHtmlElement extends StructuredHtml { } toJson(getMarkdown?: boolean): HtmlAsJsonElement { + const markdown = getMarkdown && this.containsMarkdown ? this.markdown : undefined; return { id: this.id, type: 'Element', tagName: this.tagName, attributes: this.attributes, - children: (getMarkdown && this.containsMarkdown ? this.childrenAsMarkdown ?? [] : this.children).map((child) => - child.toJson(getMarkdown), + children: (markdown ? [new StructuredHtmlText(markdown, { isMarkdownText: true })] : this.children).map( + (child: StructuredHtml) => child.toJson(getMarkdown), ), }; } From b91e5d66fc57ce5494c566c7a7bddeac5a56eace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20R=C3=B8ed=20Hestvik?= Date: Wed, 5 Jun 2024 15:47:10 +0200 Subject: [PATCH 2/2] Refactor --- .../src/util/html/StructuredHtml/StructuredHtmlElement.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/shared-components/src/util/html/StructuredHtml/StructuredHtmlElement.ts b/packages/shared-components/src/util/html/StructuredHtml/StructuredHtmlElement.ts index ea590dd6e..da4c42c47 100644 --- a/packages/shared-components/src/util/html/StructuredHtml/StructuredHtmlElement.ts +++ b/packages/shared-components/src/util/html/StructuredHtml/StructuredHtmlElement.ts @@ -161,14 +161,14 @@ class StructuredHtmlElement extends StructuredHtml { toJson(getMarkdown?: boolean): HtmlAsJsonElement { const markdown = getMarkdown && this.containsMarkdown ? this.markdown : undefined; + const children = markdown ? [new StructuredHtmlText(markdown, { isMarkdownText: true })] : this.children; + return { id: this.id, type: 'Element', tagName: this.tagName, attributes: this.attributes, - children: (markdown ? [new StructuredHtmlText(markdown, { isMarkdownText: true })] : this.children).map( - (child: StructuredHtml) => child.toJson(getMarkdown), - ), + children: children.map((child: StructuredHtml) => child.toJson(getMarkdown)), }; }