Skip to content

Commit

Permalink
Use RoqUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
Matheus Cruz committed Oct 9, 2024
1 parent 4ff391d commit 39f5c81
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
package io.quarkiverse.roq.plugin.aliases.deployment;

import static io.quarkiverse.roq.frontmatter.deployment.FrontMatterJsonData.mergeParents;
import static io.quarkiverse.roq.frontmatter.deployment.Link.DEFAULT_PAGE_LINK_TEMPLATE;
import static io.quarkiverse.roq.frontmatter.deployment.data.RoqFrontMatterDataProcessor.LINK_KEY;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import io.quarkiverse.roq.frontmatter.deployment.Link;
import io.quarkiverse.roq.frontmatter.deployment.scan.RoqFrontMatterRawTemplateBuildItem;
import io.quarkiverse.roq.frontmatter.deployment.data.RoqFrontMatterTemplateBuildItem;
import io.quarkiverse.roq.frontmatter.runtime.RoqSiteConfig;
import io.quarkiverse.roq.frontmatter.runtime.model.RoqUrl;
import io.quarkiverse.roq.generator.deployment.items.SelectedPathBuildItem;
import io.quarkiverse.roq.plugin.aliases.deployment.items.RoqFrontMatterAliasesBuildItem;
import io.quarkiverse.roq.plugin.aliases.runtime.RoqFrontMatterAliasesRecorder;
Expand All @@ -40,7 +34,7 @@ FeatureBuildItem feature() {
}

@BuildStep
public void consumeTemplates(RoqSiteConfig config, List<RoqFrontMatterRawTemplateBuildItem> rawTemplates,
public void consumeTemplates(RoqSiteConfig config, List<RoqFrontMatterTemplateBuildItem> rawTemplates,
BuildProducer<RoqFrontMatterAliasesBuildItem> aliasesProducer,
BuildProducer<SelectedPathBuildItem> selectedPathsProducer,
BuildProducer<NotFoundPageDisplayableEndpointBuildItem> notFoundPageDisplayableEndpointProducer) {
Expand All @@ -49,27 +43,16 @@ public void consumeTemplates(RoqSiteConfig config, List<RoqFrontMatterRawTemplat
return;
}

final var byKey = rawTemplates.stream()
.collect(Collectors.toMap(RoqFrontMatterRawTemplateBuildItem::id, Function.identity()));

HashMap<String, String> aliasMap = new HashMap<>();
for (RoqFrontMatterRawTemplateBuildItem item : rawTemplates) {

final JsonObject data = mergeParents(item, byKey);
for (RoqFrontMatterTemplateBuildItem item : rawTemplates) {

Set<String> aliasesName = getAliases(data);
Set<String> aliasesName = getAliases(item.data());
if (aliasesName.isEmpty()) {
continue;
}

Link.PageLinkData pageLinkData = new Link.PageLinkData(item.info().baseFileName(), item.info().date(),
item.collection(), data);
final String targetLink = Link.pageLink(config.rootPath(), data.getString(LINK_KEY, DEFAULT_PAGE_LINK_TEMPLATE),
pageLinkData);

RoqUrl url = item.url();
for (String alias : aliasesName) {
String aliasLink = Link.pageLink(config.rootPath(), alias, pageLinkData);
aliasMap.put(aliasLink, targetLink);
aliasMap.put(alias, url.path());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,4 @@ void dispatchByType(BuildProducer<RoqFrontMatterPublishPageBuildItem> pagesProdu

}
}

private static JsonObject mergeParents(RoqFrontMatterRawTemplateBuildItem item,
Map<String, RoqFrontMatterRawTemplateBuildItem> byPath) {
Stack<JsonObject> fms = new Stack<>();
String parent = item.layout();
fms.add(item.data());
while (parent != null) {
if (byPath.containsKey(parent)) {
final RoqFrontMatterRawTemplateBuildItem parentItem = byPath.get(parent);
parent = parentItem.layout();
fms.push(parentItem.data());
} else {
parent = null;
}
}

JsonObject merged = new JsonObject();
while (!fms.empty()) {
merged.mergeIn(fms.pop());
}
return merged;
}
}

0 comments on commit 39f5c81

Please sign in to comment.