Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions src/main/java/run/halo/sitemap/ChangeFreqEnum.java

This file was deleted.

10 changes: 1 addition & 9 deletions src/main/java/run/halo/sitemap/SitemapBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,11 @@ public String withXMLTemplate(String content) {
* @see <a href="https://www.w3schools.com/xml/el_sequence.asp">el_sequence</a>
*/
public LinkedHashMap<String, String> normalizeSitemapEntry(SitemapEntry sitemapEntry) {
LinkedHashMap<String, String> sitemapEntryMap = new LinkedHashMap<>(4, 1);
// Return keys in following order
LinkedHashMap<String, String> sitemapEntryMap = new LinkedHashMap<>(2, 1);
sitemapEntryMap.put("loc", sitemapEntry.getLoc());
if (StringUtils.isNotBlank(sitemapEntry.getLastmod())) {
sitemapEntryMap.put("lastmod", sitemapEntry.getLastmod());
}
ChangeFreqEnum changefreq = sitemapEntry.getChangefreq();
if (changefreq != null) {
sitemapEntryMap.put("changefreq", changefreq.name().toLowerCase());
}
if (sitemapEntry.getPriority() != null) {
sitemapEntryMap.put("priority", sitemapEntry.getPriority().toString());
}
return sitemapEntryMap;
}

Expand Down
5 changes: 1 addition & 4 deletions src/main/java/run/halo/sitemap/SitemapEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ public class SitemapEntry {
* <p>Parent tag for each URL entry. The remaining tags are children of this tag.</p>
* required.
*/
@NonNull
private String loc;

private String lastmod;

private ChangeFreqEnum changefreq;

private Double priority;
}
66 changes: 1 addition & 65 deletions src/main/java/run/halo/sitemap/SitemapGeneratorOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Set;
import lombok.Builder;
Expand All @@ -26,92 +25,29 @@ public class SitemapGeneratorOptions {
@NonNull
private URL siteUrl;

@Builder.Default
private String fileNamePrefix = "sitemap";

@Builder.Default
private boolean allowEmptySitemap = false;

@Builder.Default
private boolean allowMultipleSitemaps = true;

@Builder.Default
private DateTimeFormatter dateTimeFormatter = W3cDatetimeFormat.SECOND_FORMATTER;

/**
* Split large sitemap into multiple files by specifying sitemap size. Default 5000.
*/
@Builder.Default
private int sitemapSize = 5000;

@Builder.Default
private boolean autoValidate = false;

@Builder.Default
private boolean gzip = false;

@Builder.Default
private ChangeFreqEnum changefreq = ChangeFreqEnum.DAILY;

/**
* How to assign sitemap priorities:
* <pre>
* 1.0-0.8
* Homepage, product information, landing pages.
*
* 0.7-0.4
* News articles, some weather services, blog posts, category pages, pages that no site would be complete without.
*
* 0.3-0.0
* FAQs, outdated info, old press releases, completely static pages that are still relevant enough to keep from deleting entirely.
* </pre>
*
* @see
* <a href="https://www.contentpowered.com/blog/xml-sitemap-priority-changefreq/">xml-sitemap-priority-changefreq</a>
* @see
* <a href="https://slickplan.com/blog/xml-sitemap-priority-changefreq">xml-sitemap-priority-changefreq</a>
*/
@Builder.Default
private double priority = 0.7;

/**
* <p>Array of relative paths (wildcard pattern supported) to exclude from listing on sitemap
* .xml or sitemap-*.xml.</p>
*
* <p>e.g.: ['/page-0', '/page-*', '/private/*'].</p>
* Apart from this option next-sitemap also offers a custom transform option which could be
* used to exclude urls that match specific patterns
*/
private Set<String> exclude;

/**
* Generate index sitemaps. Default true.
*/
@Builder.Default
private boolean generateIndexSitemap = true;

public SitemapEntry transform(UrlEntryMeta context) {
String escapedUrl = UrlUtils.escapeSitemapUrl(context.getUrl());
String loc = UrlUtils.toURI(escapedUrl).normalize().toASCIIString();
if (!PathUtils.isAbsoluteUri(loc)) {
loc = getSiteUri().resolve(escapedUrl).normalize().toASCIIString();
}

var builder = SitemapEntry.builder()
.loc(loc)
.changefreq(changefreq);

if (context.getPriority() != null) {
builder.priority(context.getPriority());
} else {
builder.priority(priority);
}
var builder = SitemapEntry.builder().loc(loc);

if (context.getLastModifiedTime() != null) {
builder.lastmod(
W3cDatetimeFormat.format(context.getLastModifiedTime(), dateTimeFormatter));
} else {
builder.lastmod(W3cDatetimeFormat.format(Instant.now(), dateTimeFormatter));
}

return builder.build();
Expand Down
10 changes: 1 addition & 9 deletions src/test/java/run/halo/sitemap/SitemapBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ void buildSitemapXml() {
entries.add(SitemapEntry.builder()
.loc("https://halo.run/about")
.lastmod("2022-11-12T13:57:43.898+0800")
.changefreq(ChangeFreqEnum.DAILY)
.priority(0.7)
.build());
entries.add(SitemapEntry.builder()
.loc("https://halo.run/categories")
.lastmod("2022-11-12T13:57:43.898+0800")
.changefreq(ChangeFreqEnum.DAILY)
.priority(0.7)
.build());
String s = new SitemapBuilder().buildSitemapXml(entries);
assertEquals("""
Expand All @@ -41,16 +37,12 @@ void buildSitemapXml() {
xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<url><loc>https://halo.run/about</loc>
<lastmod>2022-11-12T13:57:43.898+0800</lastmod>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>
<url><loc>https://halo.run/categories</loc>
<lastmod>2022-11-12T13:57:43.898+0800</lastmod>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>

</urlset>
""", s);
}
}
}
17 changes: 7 additions & 10 deletions src/test/java/run/halo/sitemap/SitemapGeneratorOptionsTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package run.halo.sitemap;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -19,27 +20,23 @@ void transform() throws MalformedURLException {
SitemapGeneratorOptions options = SitemapGeneratorOptions.builder()
.siteUrl(new URL("https://halo.run"))
.build();

SitemapEntry entry = options.transform(new UrlEntryMeta("/about"));
assertEquals("https://halo.run/about", entry.getLoc());
assertEquals(ChangeFreqEnum.DAILY, entry.getChangefreq());
assertEquals(0.7, entry.getPriority());
assertNull(entry.getLastmod());

entry = options.transform(new UrlEntryMeta("/archives"));
assertEquals("https://halo.run/archives", entry.getLoc());
assertEquals(ChangeFreqEnum.DAILY, entry.getChangefreq());
assertEquals(0.7, entry.getPriority());
assertNull(entry.getLastmod());

entry = options.transform(new UrlEntryMeta("/categories/ümlat/>&>中"));
assertEquals("https://halo.run/categories/%C3%BCmlat/&gt;&amp;&gt;%E4%B8%AD",
entry.getLoc());
assertEquals(ChangeFreqEnum.DAILY, entry.getChangefreq());
assertEquals(0.7, entry.getPriority());
assertNull(entry.getLastmod());

entry = options.transform(new UrlEntryMeta("https://guqing.xyz/hello-中国<>&"));
assertEquals("https://guqing.xyz/hello-%E4%B8%AD%E5%9B%BD&lt;&gt;&amp;",
entry.getLoc());

assertEquals(ChangeFreqEnum.DAILY, entry.getChangefreq());
assertEquals(0.7, entry.getPriority());
assertNull(entry.getLastmod());
}
}
}
Loading