Skip to content

Commit 167ae5f

Browse files
authored
Add canonical URLs to all generated pages (#459)
Closes #458 Signed-off-by: Alexander Schwartz <[email protected]>
1 parent 05e2e20 commit 167ae5f

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/main/java/org/keycloak/webbuilder/utils/FreeMarker.java

+14-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class FreeMarker {
1818

1919
private Configuration cfg;
2020
private Map<String, Object> globalAttributes;
21+
private boolean isPublish;
22+
private File targetDir;
2123

2224
public FreeMarker(File webSrcDir) throws IOException {
2325
cfg = new Configuration(Configuration.VERSION_2_3_24);
@@ -46,14 +48,18 @@ public void writeFile(String template, File targetDir, String output) throws Exc
4648
}
4749

4850
public void writeFile(Map<String, Object> attr, String template, File targetDir, String output) throws Exception {
49-
Map<String, Object> attributes;
51+
Map<String, Object> attributes = new HashMap<>();
52+
attributes.putAll(globalAttributes);
5053
if (attr != null) {
51-
attributes = new HashMap<>();
52-
attributes.putAll(globalAttributes);
5354
attributes.putAll(attr);
54-
} else {
55-
attributes = globalAttributes;
5655
}
56+
String canonical = new File(targetDir, output).toURI().toString().substring(this.targetDir.toURI().toString().length());
57+
if (isPublish) {
58+
canonical = canonical.replaceAll("\\.html$", "");
59+
canonical = canonical.replaceAll("/index$", "");
60+
canonical = canonical.replaceAll("^index$", "");
61+
}
62+
attributes.put("canonical", attributes.get("root") + canonical);
5763

5864
Template downloadTemplate = cfg.getTemplate(template);
5965

@@ -88,6 +94,9 @@ public void init(Context context) {
8894
globalAttributes.put("archive", false);
8995

9096
globalAttributes.put("links", context.getLinks());
97+
98+
isPublish = context.config().isPublish();
99+
targetDir = context.getTargetDir();
91100
}
92101

93102
}

templates/template.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<link href="${links.getResource('bootstrap/dist/css/bootstrap.min.css')}" rel="stylesheet">
1717
<link href="${links.getResource('@fortawesome/fontawesome-free/css/all.min.css')}" rel="stylesheet">
1818
<link href="${links.getResource('css/keycloak.css')}" rel="stylesheet">
19+
<link rel="canonical" href="${ canonical }">
1920

2021
<link rel="shortcut icon" href="${links.getResource('favicon.ico')}">
2122

0 commit comments

Comments
 (0)