Skip to content

Commit 92c0286

Browse files
committed
Add line-breaks formatter and test cases.
1 parent 9888f2a commit 92c0286

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

__tests__/plugins/formatters.core.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,26 @@ loader.paths('f-macro-ctx-%N.html').forEach((path) => {
462462
test(`apply macro ctx - ${path}`, () => loader.execute(path));
463463
});
464464

465+
test('line-breaks', () => {
466+
const ctx = new Context({});
467+
468+
let vars = variables('');
469+
Core['line-breaks'].apply([], vars, ctx);
470+
expect(vars[0].get()).toEqual('');
471+
472+
vars = variables(' \n ');
473+
Core['line-breaks'].apply([], vars, ctx);
474+
expect(vars[0].get()).toEqual(' <br/> ');
475+
476+
vars = variables('A\nB\n\n\nC\nD');
477+
Core['line-breaks'].apply([], vars, ctx);
478+
expect(vars[0].get()).toEqual('A<br/>B<br/><br/><br/>C<br/>D');
479+
});
480+
481+
loader.paths('f-line-breaks-%N.html').forEach((path) => {
482+
test(`line-breaks - ${path}`, () => loader.execute(path));
483+
});
484+
465485
loader.paths('f-output-%N.html').forEach((path) => {
466486
test(`output - ${path}`, () => loader.execute(path));
467487
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
:JSON
2+
{
3+
"quotes": [
4+
"| \n |",
5+
"",
6+
"\"abc\"",
7+
"\"A\nB\nC\"",
8+
"\"\nLong\n\nquote\n\nwith\n\nseveral\n\n\nline\n\nbreaks\n\""
9+
]
10+
}
11+
12+
:TEMPLATE
13+
{.repeated section quotes}
14+
{@|line-breaks}
15+
{.end}
16+
17+
:OUTPUT
18+
| <br/> |
19+
20+
21+
22+
"abc"
23+
24+
"A<br/>B<br/>C"
25+
26+
"<br/>Long<br/><br/>quote<br/><br/>with<br/><br/>several<br/><br/><br/>line<br/><br/>breaks<br/>"

src/plugins/formatters.core.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,17 @@ export class KeyByFormatter extends Formatter {
243243
}
244244
}
245245

246+
const NEWLINE = /\n/g;
247+
248+
export class LineBreaksFormatter extends Formatter {
249+
apply(args: string[], vars: Variable[], ctx: Context): void {
250+
const first = vars[0];
251+
const value = first.node.asString();
252+
const replacement = value.replace(NEWLINE, '<br/>');
253+
first.set(replacement);
254+
}
255+
}
256+
246257
export class LookupFormatter extends Formatter {
247258
apply(args: string[], vars: Variable[], ctx: Context): void {
248259
const first = vars[0];
@@ -419,6 +430,7 @@ export const CORE_FORMATTERS: FormatterTable = {
419430
json: new JsonFormatter(),
420431
'json-pretty': new JsonPretty(),
421432
'key-by': new KeyByFormatter(),
433+
'line-breaks': new LineBreaksFormatter(),
422434
lookup: new LookupFormatter(),
423435
mod: new ModFormatter(),
424436
output: new OutputFormatter(),

0 commit comments

Comments
 (0)