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
32 changes: 29 additions & 3 deletions assets/js/search-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@
threshold: 0.4,
};

const INTERNAL_SEARCH_PATTERNS = [
/https:\/\/blogs\.comphy-lab\.org\/Private-ToDo/i,
/https:\/\/blogs\.comphy-lab\.org\/0_ToDo/i,
/^Private todo blog public/i,
/^0_todo blog public/i,
];

function isInternalSearchEntry(entry) {
const entryIdentityText = [entry?.title, entry?.url]
.filter(Boolean)
.join(" ");

return INTERNAL_SEARCH_PATTERNS.some((pattern) =>
pattern.test(entryIdentityText)
);
}

function sanitizeSearchData(data) {
return data.filter((entry) => !isInternalSearchEntry(entry));
}

/**
* Loads the search database from the server
* @returns {Promise<Object[]>} Promise that resolves to search data array
Expand Down Expand Up @@ -61,9 +82,14 @@
);
}

searchData = data;
console.log(`Search database loaded: ${data.length} items`);
return data;
const sanitizedData = sanitizeSearchData(data);

searchData = sanitizedData;
console.log(
`Search database loaded: ${sanitizedData.length} items ` +
`(${data.length - sanitizedData.length} internal items filtered)`
);
return sanitizedData;
} catch (error) {
console.warn("Could not load search database:", error.message);
searchData = []; // Set empty array to prevent further attempts
Expand Down
80 changes: 80 additions & 0 deletions tests/search-manager.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
describe("search-manager sanitization", () => {
beforeEach(() => {
jest.resetModules();
jest.clearAllMocks();
document.body.innerHTML = "";
delete window.SearchManager;
delete window.searchDatabaseForCommandPalette;
});

it("filters internal backlog pages from search results", async () => {
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: () =>
Promise.resolve([
{
title: "Private todo blog public - Elasticity and viscoelasticity",
url:
"https://blogs.comphy-lab.org/Private-ToDo-Blog-public/" +
"#elasticity-and-viscoelasticity",
content: "internal planning item",
priority: 3,
},
{
title: "0_todo blog public",
url: "https://blogs.comphy-lab.org/0_ToDo-Blog-public/",
content: "internal backlog page",
priority: 3,
},
{
title: "Teaching - Teaching: Methods",
url: "https://comphy-lab.org/teaching/#teaching",
content: "public teaching overview",
priority: 3,
},
]),
});

require("../assets/js/search-manager.js");

const data = await window.SearchManager.loadSearchDatabase();

expect(data).toHaveLength(1);
expect(data[0].title).toBe("Teaching - Teaching: Methods");
expect(data[0].url).toBe("https://comphy-lab.org/teaching/#teaching");
});

it("keeps public pages whose content references internal backlog URLs", async () => {
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: () =>
Promise.resolve([
{
title: "Skill - Badge Shapes",
url: "https://comphy-lab.org/.agents/skills/add-paper/SKILL/#badge-shapes",
content:
"Example badge config referencing https://blogs.comphy-lab.org/0_ToDo-Blog-public/ for documentation only.",
priority: 3,
},
{
title: "Private todo blog public - Elasticity and viscoelasticity",
url:
"https://blogs.comphy-lab.org/Private-ToDo-Blog-public/" +
"#elasticity-and-viscoelasticity",
content: "internal planning item",
priority: 3,
},
]),
});

require("../assets/js/search-manager.js");

const data = await window.SearchManager.loadSearchDatabase();

expect(data).toHaveLength(1);
expect(data[0].title).toBe("Skill - Badge Shapes");
expect(data[0].url).toBe(
"https://comphy-lab.org/.agents/skills/add-paper/SKILL/#badge-shapes"
);
});
});
Loading