Allow tags in post template, Fix blog index

This commit is contained in:
MrLetsplay 2024-02-17 20:26:23 +01:00
parent 4db5029118
commit 636edfb44c
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
2 changed files with 26 additions and 15 deletions

View File

@ -12,6 +12,7 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -163,6 +164,7 @@ public class MdBlog {
}) })
.collect(Collectors.joining(", "))); .collect(Collectors.joining(", ")));
}) })
.filter(Objects::nonNull)
.collect(Collectors.joining("\n\n"))); .collect(Collectors.joining("\n\n")));
index.getBodyNode().appendChild(new MdRenderer().render(MdParser.parse(indexMd))); index.getBodyNode().appendChild(new MdRenderer().render(MdParser.parse(indexMd)));
@ -244,8 +246,6 @@ public class MdBlog {
System.out.println("Update"); System.out.println("Update");
indexTemplates.clear(); indexTemplates.clear();
System.out.println(posts);
Files.walk(POSTS_PATH) Files.walk(POSTS_PATH)
.filter(Files::isRegularFile) .filter(Files::isRegularFile)
.filter(f -> f.getFileName().toString().endsWith(Post.FILE_EXTENSION)) .filter(f -> f.getFileName().toString().endsWith(Post.FILE_EXTENSION))

View File

@ -3,30 +3,31 @@ package me.mrletsplay.mdblog.blog;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.security.MessageDigest; import java.util.stream.Collectors;
import java.security.NoSuchAlgorithmException;
import me.mrletsplay.mdblog.markdown.MdParser; import me.mrletsplay.mdblog.markdown.MdParser;
import me.mrletsplay.mdblog.markdown.MdRenderer; import me.mrletsplay.mdblog.markdown.MdRenderer;
import me.mrletsplay.mdblog.template.Template; import me.mrletsplay.mdblog.template.Template;
import me.mrletsplay.mdblog.template.Templates; import me.mrletsplay.mdblog.template.Templates;
import me.mrletsplay.mdblog.util.TimeFormatter; import me.mrletsplay.mdblog.util.TimeFormatter;
import me.mrletsplay.mrcore.http.HttpUtils;
import me.mrletsplay.simplehttpserver.dom.html.HtmlDocument; import me.mrletsplay.simplehttpserver.dom.html.HtmlDocument;
import me.mrletsplay.simplehttpserver.dom.html.HtmlElement;
public class Post { public class Post {
public static final String FILE_EXTENSION = ".md"; public static final String FILE_EXTENSION = ".md";
private static final MessageDigest MD_5; //private static final MessageDigest MD_5;
private static final MdRenderer RENDERER = new MdRenderer(); private static final MdRenderer RENDERER = new MdRenderer();
static { // static {
try { // try {
MD_5 = MessageDigest.getInstance("MD5"); // MD_5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) { // } catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e); // throw new RuntimeException(e);
} // }
} // }
private Path filePath; private Path filePath;
// private String checksum; TODO currently unused, post also needs to update when templates change // private String checksum; TODO currently unused, post also needs to update when templates change
@ -61,15 +62,25 @@ public class Post {
this.metadata = PostMetadata.load(spl[0]); this.metadata = PostMetadata.load(spl[0]);
HtmlDocument document = new HtmlDocument(); String postMd = templates.render(Template.POST,
document.getBodyNode().appendChild(RENDERER.render(MdParser.parse(templates.render(Template.POST,
"content", spl[1], "content", spl[1],
"title", metadata.title(), "title", metadata.title(),
"author", metadata.author(), "author", metadata.author(),
"description", metadata.description(), "description", metadata.description(),
"date", TimeFormatter.toDateOnly(metadata.date()), "date", TimeFormatter.toDateOnly(metadata.date()),
"date_time", TimeFormatter.toDateAndTime(metadata.date()), "date_time", TimeFormatter.toDateAndTime(metadata.date()),
"date_relative", TimeFormatter.toRelativeTime(metadata.date()))))); "date_relative", TimeFormatter.toRelativeTime(metadata.date()),
"tags", metadata.tags().stream()
.map(t -> {
HtmlElement link = new HtmlElement("a");
link.setText(t);
link.setAttribute("href", "./?tag=" + HttpUtils.urlEncode(t));
return link.toString();
})
.collect(Collectors.joining(", ")));
HtmlDocument document = new HtmlDocument();
document.getBodyNode().appendChild(RENDERER.render(MdParser.parse(postMd)));
document.setTitle(metadata.title()); document.setTitle(metadata.title());
document.setDescription(metadata.description()); document.setDescription(metadata.description());
document.addStyleSheet("_/style/base.css"); document.addStyleSheet("_/style/base.css");