Skip to content

Commit

Permalink
feat: renderer uses articles from snapshot if possible
Browse files Browse the repository at this point in the history
So all components get the latest articles.
  • Loading branch information
raoulvdberge committed Aug 16, 2024
1 parent bade88e commit d5e9d0e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public String getAssetsOutputPath() {
public String getRelativePagePath(final Path from, final Path to) {
return from.relativize(to).toString().replace(".adoc", ".html");
}

@Override
public String toString() {
return name + "@" + version;
}
}
62 changes: 44 additions & 18 deletions src/main/java/com/refinedmods/refinedsites/render/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,27 @@ public void render(final Site site) {
for (final Component component : site.getComponents()) {
renderComponentPre(component);
}
for (final Component component : site.getComponents()) {
renderComponent(component, assetsPath, site, sitemapIndex);
final var components = site.getComponents()
.stream()
.collect(Collectors.groupingBy(Component::getName));
for (final var entry : components.entrySet()) {
log.info("Rendering versions of component {}", entry.getKey());
final List<Component> componentVersions = entry.getValue();
final Component snapshotComponent = componentVersions.stream()
.filter(c -> c.getVersion().snapshot())
.findFirst()
.orElse(null);
List<ArticleRender> articles = null;
if (snapshotComponent != null) {
articles = renderComponent(snapshotComponent, assetsPath, site, sitemapIndex, null);
log.info("Reusing {} articles from snapshot in other components", articles.size());
}
for (final Component component : componentVersions) {
if (component == snapshotComponent) {
continue;
}
renderComponent(component, assetsPath, site, sitemapIndex, articles);
}
}
sitemapIndex.write();
Files.writeString(outputPath.resolve("robots.txt"), "Sitemap: " + url + "/sitemap_index.xml");
Expand Down Expand Up @@ -149,10 +168,11 @@ private void renderComponentPre(final Component component) {
component.setSlug(componentSlug);
}

private void renderComponent(final Component component,
final Path assetsOutputPath,
final Site site,
final SitemapIndexGenerator sitemapIndex)
private List<ArticleRender> renderComponent(final Component component,
final Path assetsOutputPath,
final Site site,
final SitemapIndexGenerator sitemapIndex,
@Nullable final List<ArticleRender> articles)
throws IOException {
log.info("Rendering component {}", component);
final Path componentOutputPath = getComponentOutputPath(component);
Expand Down Expand Up @@ -192,18 +212,9 @@ private void renderComponent(final Component component,
infosByPageType.computeIfAbsent(singleInfo.type(), k -> new ArrayList<>()).add(singleInfo);
}
prepareNavigationItems(component.getNavigationItems(), pageInfo);
final List<ArticleRender> articles = infosByPageType.getOrDefault("article", Collections.emptyList())
.stream()
.map(info -> new ArticleRender(
info.title(),
info.description(),
info.relativePath(),
info.date().orElse(LocalDate.EPOCH)
))
.sorted(Comparator.comparing(ArticleRender::getDate).reversed())
.toList();

writeRssFeed(component, sitemapBaseUrl, articles, componentOutputPath);
final List<ArticleRender> theArticles = articles == null ? getArticles(infosByPageType) : articles;
writeRssFeed(component, sitemapBaseUrl, theArticles, componentOutputPath);

for (final Path pagePath : component.getPages()) {
renderPage(
Expand All @@ -217,13 +228,28 @@ private void renderComponent(final Component component,
releaseMatchingComponentVersion,
sitemapBaseUrl,
componentSitemap,
articles
theArticles
);
}
if (componentSitemap != null) {
componentSitemap.write();
sitemapIndex.addUrl(sitemapBaseUrl + "/sitemap.xml", renderDate);
}

return theArticles;
}

private static List<ArticleRender> getArticles(final Map<String, List<PageInfo>> infosByPageType) {
return infosByPageType.getOrDefault("article", Collections.emptyList())
.stream()
.map(info -> new ArticleRender(
info.title(),
info.description(),
info.relativePath(),
info.date().orElse(LocalDate.EPOCH)
))
.sorted(Comparator.comparing(ArticleRender::getDate).reversed())
.toList();
}

private static void writeRssFeed(final Component component,
Expand Down

0 comments on commit d5e9d0e

Please sign in to comment.