Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions __tests__/plugins/formatters.core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,26 @@ loader.paths('f-macro-ctx-%N.html').forEach((path) => {
test(`apply macro ctx - ${path}`, () => loader.execute(path));
});

test('line-breaks', () => {
const ctx = new Context({});

let vars = variables('');
Core['line-breaks'].apply([], vars, ctx);
expect(vars[0].get()).toEqual('');

vars = variables(' \n ');
Core['line-breaks'].apply([], vars, ctx);
expect(vars[0].get()).toEqual(' <br/> ');

vars = variables('A\nB\n\n\nC\nD');
Core['line-breaks'].apply([], vars, ctx);
expect(vars[0].get()).toEqual('A<br/>B<br/><br/><br/>C<br/>D');
});

loader.paths('f-line-breaks-%N.html').forEach((path) => {
test(`line-breaks - ${path}`, () => loader.execute(path));
});

loader.paths('f-output-%N.html').forEach((path) => {
test(`output - ${path}`, () => loader.execute(path));
});
Expand Down
26 changes: 26 additions & 0 deletions __tests__/plugins/resources/f-line-breaks-1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
:JSON
{
"quotes": [
"| \n |",
"",
"\"abc\"",
"\"A\nB\nC\"",
"\"\nLong\n\nquote\n\nwith\n\nseveral\n\n\nline\n\nbreaks\n\""
]
}

:TEMPLATE
{.repeated section quotes}
{@|line-breaks}
{.end}

:OUTPUT
| <br/> |



"abc"

"A<br/>B<br/>C"

"<br/>Long<br/><br/>quote<br/><br/>with<br/><br/>several<br/><br/><br/>line<br/><br/>breaks<br/>"
12 changes: 12 additions & 0 deletions src/plugins/formatters.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,17 @@ export class KeyByFormatter extends Formatter {
}
}

const NEWLINE = /\n/g;

export class LineBreaksFormatter extends Formatter {
apply(args: string[], vars: Variable[], ctx: Context): void {
const first = vars[0];
const value = first.node.asString();
const replacement = value.replace(NEWLINE, '<br/>');
first.set(replacement);
}
}

export class LookupFormatter extends Formatter {
apply(args: string[], vars: Variable[], ctx: Context): void {
const first = vars[0];
Expand Down Expand Up @@ -419,6 +430,7 @@ export const CORE_FORMATTERS: FormatterTable = {
json: new JsonFormatter(),
'json-pretty': new JsonPretty(),
'key-by': new KeyByFormatter(),
'line-breaks': new LineBreaksFormatter(),
lookup: new LookupFormatter(),
mod: new ModFormatter(),
output: new OutputFormatter(),
Expand Down