From 1ccf6840244b9a65885e1b2ef579167b79cbf538 Mon Sep 17 00:00:00 2001 From: moki Date: Sat, 14 Oct 2023 22:00:27 +0400 Subject: [PATCH] feat(xlf/renderer/xlf-md): em syntax --- .../renderer-inline.test.ts.snap | 2 ++ src/xlf/renderer/xlf-md/index.ts | 11 ++++++++ .../renderer/xlf-md/renderer-inline.test.ts | 28 +++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/src/xlf/renderer/xlf-md/__snapshots__/renderer-inline.test.ts.snap b/src/xlf/renderer/xlf-md/__snapshots__/renderer-inline.test.ts.snap index db02198d..69619d8d 100644 --- a/src/xlf/renderer/xlf-md/__snapshots__/renderer-inline.test.ts.snap +++ b/src/xlf/renderer/xlf-md/__snapshots__/renderer-inline.test.ts.snap @@ -1,5 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`renders xlf to markdown renders em wrapped in tags 1`] = `"Предложение номер *один*."`; + exports[`renders xlf to markdown renders plain text 1`] = `"Sentence about something. Another sentence."`; exports[`renders xlf to markdown renders strong wrapped in tags 1`] = `"Предложение номер **один**."`; diff --git a/src/xlf/renderer/xlf-md/index.ts b/src/xlf/renderer/xlf-md/index.ts index 944ceda6..e513342d 100644 --- a/src/xlf/renderer/xlf-md/index.ts +++ b/src/xlf/renderer/xlf-md/index.ts @@ -17,6 +17,7 @@ class XLFMDRenderer { tag: this.tag.bind(this), g: this.g.bind(this), strong: this.strong.bind(this), + em: this.em.bind(this), }; } @@ -75,6 +76,16 @@ class XLFMDRenderer { return token.equivText ?? '**'; } + + em(token: XLFToken): string { + assert(isXLFTagToken(token)); + token as XLFTagToken; + if (!token?.equivText) { + throw new Error(`token: ${token} missing equiv-text`); + } + + return token.equivText ?? '*'; + } } export {XLFMDRenderer}; diff --git a/src/xlf/renderer/xlf-md/renderer-inline.test.ts b/src/xlf/renderer/xlf-md/renderer-inline.test.ts index 24f06307..430608e5 100644 --- a/src/xlf/renderer/xlf-md/renderer-inline.test.ts +++ b/src/xlf/renderer/xlf-md/renderer-inline.test.ts @@ -40,4 +40,32 @@ describe('renders xlf to markdown', () => { const rendered = renderer.render(tokens); expect(rendered).toMatchSnapshot(); }); + + it('renders em wrapped in tags', () => { + const renderer = new XLFMDRenderer(); + const tokens: Array = [ + {type: 'tag', data: 'target', nodeType: 'open'}, + {type: 'text', data: 'Предложение номер '}, + { + type: 'tag', + data: 'g', + nodeType: 'open', + syntax: 'em', + equivText: '*', + }, + {type: 'text', data: 'один'}, + { + type: 'tag', + data: 'g', + nodeType: 'close', + syntax: 'em', + equivText: '*', + }, + {type: 'text', data: '.'}, + {type: 'tag', data: 'target', nodeType: 'close'}, + ]; + + const rendered = renderer.render(tokens); + expect(rendered).toMatchSnapshot(); + }); });