Skip to content

Commit

Permalink
close #95 use meta data to disable serach indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorsten Marx committed Nov 22, 2023
1 parent 4d26826 commit 91f8e37
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/

import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;

/**
*
Expand All @@ -31,5 +33,6 @@
public interface ModuleFileSystem {

Path resolve(String path);


Optional<Map<String, Object>> getMeta(String path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ public boolean isVisible(final String uri) {
var n = node.get();
return MetaData.isVisible(n);
}

@Override
public Optional<Map<String,Object>> 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) {
Expand Down
2 changes: 2 additions & 0 deletions cms-server/hosts/demo/content/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
title: Startseite
template: start.html
search:
index: false
---

# Demo Project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -99,6 +108,15 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
return FileVisitResult.CONTINUE;
}

private boolean shouldIndex (Path contentFile) {
Optional<Map<String, Object>> meta = moduleContext.getFileSystem().getMeta(PathUtil.toRelativeFile(contentFile, contentBase));

return (Boolean)((Map<String, Object>) 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']");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 91f8e37

Please sign in to comment.