diff --git a/__tests__/plugins/formatters.core.test.ts b/__tests__/plugins/formatters.core.test.ts
index 3357a92..5f32bcd 100644
--- a/__tests__/plugins/formatters.core.test.ts
+++ b/__tests__/plugins/formatters.core.test.ts
@@ -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('
');
+
+ vars = variables('A\nB\n\n\nC\nD');
+ Core['line-breaks'].apply([], vars, ctx);
+ expect(vars[0].get()).toEqual('A
B
C
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));
});
diff --git a/__tests__/plugins/resources/f-line-breaks-1.html b/__tests__/plugins/resources/f-line-breaks-1.html
new file mode 100644
index 0000000..16c6c5a
--- /dev/null
+++ b/__tests__/plugins/resources/f-line-breaks-1.html
@@ -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
+|
|
+
+
+
+"abc"
+
+"A
B
C"
+
+"
Long
quote
with
several
line
breaks
"
diff --git a/src/plugins/formatters.core.ts b/src/plugins/formatters.core.ts
index 14c1c29..48689d7 100644
--- a/src/plugins/formatters.core.ts
+++ b/src/plugins/formatters.core.ts
@@ -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, '
');
+ first.set(replacement);
+ }
+}
+
export class LookupFormatter extends Formatter {
apply(args: string[], vars: Variable[], ctx: Context): void {
const first = vars[0];
@@ -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(),