Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix markdown export #1191

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions packages/bygger/src/translations/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. ', '<br>', ' Se ']);
expect([eksport[1].en, eksport[2].en, eksport[3].en]).toEqual(['NAV sends answers. ', '<br>', ' See ']);
expect([eksport[1]['nn-NO'], eksport[2]['nn-NO'], eksport[3]['nn-NO']]).toEqual([
'NAV sender svar. ',
'<br>',
' Sjå ',
]);
expect(eksport[1].text).toEqual('NAV sender svar. <br> Se [link](https://www.nav.no/person/).');
expect(eksport[1].en).toEqual('NAV sends answers. <br> See [link](https://www.nav.no/person/).');
expect(eksport[1]['nn-NO']).toEqual('NAV sender svar. <br> Sjå [lenke](https://www.nav.no/person/).');
});
});

describe('testGetCSVfileHeaders', () => {
it('Test headers with only origin form text', () => {
const actual = getTextsAndTranslationsHeaders([] as FormioTranslationMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
),
};
}
Expand Down