Skip to content

Commit 02b979d

Browse files
committed
#573 add markdown filter to template engine
1 parent 79c425d commit 02b979d

File tree

2 files changed

+95
-51
lines changed

2 files changed

+95
-51
lines changed

cms-templates/src/main/java/com/condation/cms/templates/filter/impl/MarkdownFilter.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
package com.condation.cms.templates.filter.impl;
22

3+
/*-
4+
* #%L
5+
* cms-templates
6+
* %%
7+
* Copyright (C) 2023 - 2026 CondationCMS
8+
* %%
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public
20+
* License along with this program. If not, see
21+
* <http://www.gnu.org/licenses/gpl-3.0.html>.
22+
* #L%
23+
*/
24+
325
import com.condation.cms.api.feature.features.MarkdownRendererFeature;
426
import com.condation.cms.api.request.RequestContextScope;
527
import com.condation.cms.templates.filter.Filter;

cms-templates/src/test/java/com/condation/cms/templates/filter/FilterTest.java

Lines changed: 73 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21,94 +21,116 @@
2121
* <http://www.gnu.org/licenses/gpl-3.0.html>.
2222
* #L%
2323
*/
24-
24+
import com.condation.cms.api.feature.features.MarkdownRendererFeature;
25+
import com.condation.cms.api.markdown.MarkdownRenderer;
26+
import com.condation.cms.api.request.RequestContext;
27+
import com.condation.cms.api.request.RequestContextScope;
2528
import com.condation.cms.templates.filter.impl.DateFilter;
29+
import com.condation.cms.templates.filter.impl.MarkdownFilter;
2630
import com.condation.cms.templates.filter.impl.RawFilter;
2731
import java.text.SimpleDateFormat;
2832
import java.util.Date;
2933
import org.apache.commons.text.StringEscapeUtils;
3034
import org.assertj.core.api.Assertions;
3135
import org.junit.jupiter.api.BeforeEach;
3236
import org.junit.jupiter.api.Test;
37+
import org.mockito.Mockito;
3338

3439
public class FilterTest {
3540

36-
FilterRegistry registry = new FilterRegistry();
37-
FilterPipeline pipeline = new FilterPipeline(registry);
38-
39-
@BeforeEach
40-
public void setup() {
41-
// Register filters
42-
registry.register("raw", (input, params) -> input); // Raw does nothing
43-
registry.register("truncate", (input, params) -> {
44-
if (input instanceof String stringValue) {
45-
int length = params.length > 0 ? (Integer)params[0] : stringValue.length();
46-
return stringValue.length() > length ? stringValue.substring(0, length) + "..." : input;
47-
}
48-
return input;
49-
});
50-
51-
pipeline.addStep("raw");
52-
pipeline.addStep("truncate", 20);
53-
}
54-
55-
@Test
56-
void test() {
57-
Object result = pipeline.execute("Dies ist ein langer Text, der abgeschnitten werden sollte.");
58-
Assertions.assertThat(result).isEqualTo("Dies ist ein langer ...");
59-
}
60-
41+
FilterRegistry registry = new FilterRegistry();
42+
FilterPipeline pipeline = new FilterPipeline(registry);
43+
44+
@BeforeEach
45+
public void setup() {
46+
// Register filters
47+
registry.register("raw", (input, params) -> input); // Raw does nothing
48+
registry.register("truncate", (input, params) -> {
49+
if (input instanceof String stringValue) {
50+
int length = params.length > 0 ? (Integer) params[0] : stringValue.length();
51+
return stringValue.length() > length ? stringValue.substring(0, length) + "..." : input;
52+
}
53+
return input;
54+
});
55+
56+
pipeline.addStep("raw");
57+
pipeline.addStep("truncate", 20);
58+
}
59+
60+
@Test
61+
void test() {
62+
Object result = pipeline.execute("Dies ist ein langer Text, der abgeschnitten werden sollte.");
63+
Assertions.assertThat(result).isEqualTo("Dies ist ein langer ...");
64+
}
65+
6166
@Test
62-
void date() {
63-
67+
void date() {
68+
6469
FilterRegistry registry = new FilterRegistry();
6570
FilterPipeline pipeline = new FilterPipeline(registry);
6671
registry.register(DateFilter.NAME, new DateFilter());
67-
72+
6873
pipeline.addStep("date");
69-
70-
74+
7175
var date = new Date();
7276
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
7377
var formatted = format.format(date);
74-
75-
Object result = pipeline.execute(date);
76-
Assertions.assertThat(result).isEqualTo(formatted);
77-
}
78-
78+
79+
Object result = pipeline.execute(date);
80+
Assertions.assertThat(result).isEqualTo(formatted);
81+
}
82+
7983
@Test
80-
void date_custom_format() {
81-
84+
void date_custom_format() {
85+
8286
FilterRegistry registry = new FilterRegistry();
8387
FilterPipeline pipeline = new FilterPipeline(registry);
8488
registry.register(DateFilter.NAME, new DateFilter());
85-
89+
8690
pipeline.addStep("date", "MM/yyyy");
87-
88-
91+
8992
var date = new Date();
9093
SimpleDateFormat format = new SimpleDateFormat("MM/yyyy");
9194
var formatted = format.format(date);
92-
93-
Object result = pipeline.execute(date);
94-
Assertions.assertThat(result).isEqualTo(formatted);
95-
}
96-
95+
96+
Object result = pipeline.execute(date);
97+
Assertions.assertThat(result).isEqualTo(formatted);
98+
}
99+
97100
@Test
98-
void raw() {
99-
101+
void raw() {
100102
FilterRegistry registry = new FilterRegistry();
101103
FilterPipeline pipeline = new FilterPipeline(registry);
102104
registry.register(RawFilter.NAME, new RawFilter());
103-
105+
104106
pipeline.addStep("raw");
105-
107+
106108
String input = """
107109
<p>"We believe that great content is the foundation for successful communication and lasting connections. With Condation, we aim to create the base where ideas can grow and thrive. Our goal is to provide a solid, reliable platform that enables everyone to easily create, manage, and share their content—helping to change the world, one idea at a time."</p><p></p>
108110
""";
109111

110112
String escaped = StringEscapeUtils.ESCAPE_HTML4.translate(input);
111113
Object result = pipeline.execute(escaped);
112114
Assertions.assertThat(result).isEqualTo(input);
113-
}
115+
}
116+
117+
@Test
118+
void markdown() {
119+
FilterRegistry registry = new FilterRegistry();
120+
FilterPipeline pipeline = new FilterPipeline(registry);
121+
registry.register(MarkdownFilter.NAME, new MarkdownFilter());
122+
pipeline.addStep("markdown");
123+
124+
var markdownRenderer = Mockito.mock(MarkdownRenderer.class);
125+
Mockito.when(markdownRenderer.render(Mockito.any())).thenReturn("");
126+
127+
RequestContext context = new RequestContext();
128+
context.add(MarkdownRendererFeature.class, new MarkdownRendererFeature(markdownRenderer));
129+
ScopedValue.where(RequestContextScope.REQUEST_CONTEXT, context).run(() -> {
130+
String input = "input string";
131+
pipeline.execute(input);
132+
133+
Mockito.verify(markdownRenderer, Mockito.times(1)).render(input);
134+
});
135+
}
114136
}

0 commit comments

Comments
 (0)