Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
✨ add sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Dec 14, 2017
1 parent 4d32f55 commit 2a9502b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/main/java/com/tale/controller/IndexController.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ public String page(@PathParam String cid, Request request) {
@GetRoute(value = {"page/:page", "page/:page.html"})
public String index(Request request, @PathParam int page, @Param(defaultValue = "12") int limit) {
page = page < 0 || page > TaleConst.MAX_PAGE ? 1 : page;
// Page<Contents> articles = new Contents().where("type", Types.ARTICLE).and("status", Types.PUBLISH).page(page, limit, "created desc");
// request.attribute("articles", articles);
if (page > 1) {
this.title(request, "第" + page + "页");
}
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/com/tale/utils/TaleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,24 +285,34 @@ public static String getRssXml(java.util.List<Contents> articles) throws FeedExc
static class Url {
String loc;
String lastmod;

public Url(String loc) {
this.loc = loc;
}
}

public static String getSitemapXml(List<Contents> articles) {
List<Url> urls = articles.stream()
.map(TaleUtils::parse)
.collect(Collectors.toList());
urls.add(new Url(Commons.site_url() + "/archives"));

String urlBody = urls.stream()
.map(url -> "<url><loc>" + url.loc + "</loc><lastmod>" + url.lastmod + "</lastmod></url>")
.map(url -> {
String s = "<url><loc>" + url.loc + "</loc>";
if (null != url.lastmod) {
s += "<lastmod>" + url.lastmod + "</lastmod>";
}
return s + "</url>";
})
.collect(Collectors.joining("\n"));

return SITEMAP_HEAD + urlBody;
return SITEMAP_HEAD + urlBody + "</urlset>";
}

private static Url parse(Contents contents) {
Url url = new Url();
url.loc = Commons.site_url() + "/article/" + contents.getCid();
url.lastmod = DateKit.toString(contents.getModified(), "YYYY-MM-DDThh:mmTZD");
Url url = new Url(Commons.site_url() + "/article/" + contents.getCid());
url.lastmod = DateKit.toString(contents.getModified(), "yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
return url;
}

Expand Down

0 comments on commit 2a9502b

Please sign in to comment.