diff --git a/cms-api/src/main/java/com/github/thmarx/cms/api/ModuleFileSystem.java b/cms-api/src/main/java/com/github/thmarx/cms/api/ModuleFileSystem.java index 51488f9e..660b6ebe 100644 --- a/cms-api/src/main/java/com/github/thmarx/cms/api/ModuleFileSystem.java +++ b/cms-api/src/main/java/com/github/thmarx/cms/api/ModuleFileSystem.java @@ -23,6 +23,8 @@ */ import java.nio.file.Path; +import java.util.Map; +import java.util.Optional; /** * @@ -31,5 +33,6 @@ public interface ModuleFileSystem { Path resolve(String path); - + + Optional> getMeta(String path); } diff --git a/cms-filesystem/src/main/java/com/github/thmarx/cms/filesystem/FileSystem.java b/cms-filesystem/src/main/java/com/github/thmarx/cms/filesystem/FileSystem.java index 25b134e8..0f89bb28 100644 --- a/cms-filesystem/src/main/java/com/github/thmarx/cms/filesystem/FileSystem.java +++ b/cms-filesystem/src/main/java/com/github/thmarx/cms/filesystem/FileSystem.java @@ -104,6 +104,16 @@ public boolean isVisible(final String uri) { var n = node.get(); return MetaData.isVisible(n); } + + @Override + public Optional> getMeta(final String uri) { + var node = metaData.byUri(uri); + if (node.isEmpty()) { + return Optional.empty(); + } + var n = node.get(); + return Optional.of(n.data()); + } public void shutdown() { if (fileWatcher != null) { diff --git a/cms-server/hosts/demo/content/index.md b/cms-server/hosts/demo/content/index.md index f8800c91..42e32979 100644 --- a/cms-server/hosts/demo/content/index.md +++ b/cms-server/hosts/demo/content/index.md @@ -1,6 +1,8 @@ --- title: Startseite template: start.html +search: + index: false --- # Demo Project diff --git a/modules/search-module/src/main/java/com/github/thmarx/cms/modules/search/extension/FileIndexingVisitor.java b/modules/search-module/src/main/java/com/github/thmarx/cms/modules/search/extension/FileIndexingVisitor.java index 08d6019f..2666f2a0 100644 --- a/modules/search-module/src/main/java/com/github/thmarx/cms/modules/search/extension/FileIndexingVisitor.java +++ b/modules/search-module/src/main/java/com/github/thmarx/cms/modules/search/extension/FileIndexingVisitor.java @@ -22,6 +22,7 @@ * #L% */ import com.github.thmarx.cms.api.CMSModuleContext; +import com.github.thmarx.cms.api.ModuleFileSystem; import com.github.thmarx.cms.api.utils.PathUtil; import com.github.thmarx.cms.api.utils.SectionUtil; import com.github.thmarx.cms.modules.search.IndexDocument; @@ -33,6 +34,7 @@ import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.Collections; +import java.util.Map; import java.util.Optional; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -65,7 +67,14 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO try { log.trace("indexing file {}", file.getFileName().toString()); + + if (!shouldIndex(file)) { + log.trace("indexing is disabled for this file"); + return FileVisitResult.CONTINUE; + } + var uri = PathUtil.toURI(file, contentBase); + var content = getContent(file); if (content.isPresent()) { @@ -99,6 +108,15 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO return FileVisitResult.CONTINUE; } + private boolean shouldIndex (Path contentFile) { + Optional> meta = moduleContext.getFileSystem().getMeta(PathUtil.toRelativeFile(contentFile, contentBase)); + + return (Boolean)((Map) meta + .orElse(Map.of()) + .getOrDefault("search", Map.of())) + .getOrDefault("index", Boolean.TRUE); + } + private boolean noindex (final Document document) { Element meta = document.selectFirst("head meta[name='robots']"); diff --git a/modules/search-module/src/main/java/com/github/thmarx/cms/modules/search/extension/SearchLifecycleExtension.java b/modules/search-module/src/main/java/com/github/thmarx/cms/modules/search/extension/SearchLifecycleExtension.java index ae959c03..2a600c7a 100644 --- a/modules/search-module/src/main/java/com/github/thmarx/cms/modules/search/extension/SearchLifecycleExtension.java +++ b/modules/search-module/src/main/java/com/github/thmarx/cms/modules/search/extension/SearchLifecycleExtension.java @@ -69,7 +69,11 @@ protected void reindexContext() { var contentPath = getContext().getFileSystem().resolve("content"); try { searchEngine.clear(); - Files.walkFileTree(contentPath, new FileIndexingVisitor(contentPath, SearchLifecycleExtension.searchEngine, getContext())); + Files.walkFileTree(contentPath, new FileIndexingVisitor( + contentPath, + SearchLifecycleExtension.searchEngine, + getContext() + )); searchEngine.commit(); } catch (IOException e) { log.error(null, e);