Skip to content

Commit

Permalink
Add api docs to search index
Browse files Browse the repository at this point in the history
  • Loading branch information
wileymc committed Dec 21, 2024
1 parent 1234884 commit e713399
Show file tree
Hide file tree
Showing 4 changed files with 3,415 additions and 45 deletions.
93 changes: 52 additions & 41 deletions components/DocSearch/DocSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,49 +77,60 @@ export const DocSearch: React.FC = () => {
score += 10;
}

// Search in headings
item.headings.forEach((heading: { text: string; level: number }) => {
if (
searchTerms.every((term) => heading.text.toLowerCase().includes(term))
) {
score += 5;
matches.push({ text: heading.text, type: "heading" });
}
});

// Search in content
const contentLower = item.content.toLowerCase();
const allTermsInContent = searchTerms.every((term) =>
contentLower.includes(term)
);

if (allTermsInContent) {
score += 1;

// Find the best matching content snippet
const snippetLength = 150;
let bestSnippetScore = 0;
let bestSnippet = "";

for (let i = 0; i < contentLower.length - snippetLength; i += 50) {
const snippet = item.content.slice(i, i + snippetLength);
const snippetLower = snippet.toLowerCase();
let snippetScore = 0;

searchTerms.forEach((term) => {
const count = (snippetLower.match(new RegExp(term, "g")) || [])
.length;
snippetScore += count;
});

if (snippetScore > bestSnippetScore) {
bestSnippetScore = snippetScore;
bestSnippet = snippet;
// Search in headings (if they exist)
if (item.headings && item.headings.length > 0) {
item.headings.forEach((heading: { text: string; level: number }) => {
if (
searchTerms.every((term) =>
heading.text.toLowerCase().includes(term)
)
) {
score += 5;
matches.push({ text: heading.text, type: "heading" });
}
}
});
}

// Search in content (if it exists)
if (item.content) {
const contentLower = item.content.toLowerCase();
const allTermsInContent = searchTerms.every((term) =>
contentLower.includes(term)
);

if (allTermsInContent) {
score += 1;

// Find the best matching content snippet
const snippetLength = 150;
let bestSnippetScore = 0;
let bestSnippet = "";

if (bestSnippet) {
matches.push({ text: bestSnippet.trim(), type: "content" });
// If content is shorter than snippet length, use whole content
if (item.content.length <= snippetLength) {
bestSnippet = item.content;
} else {
for (let i = 0; i < contentLower.length - snippetLength; i += 50) {
const snippet = item.content.slice(i, i + snippetLength);
const snippetLower = snippet.toLowerCase();
let snippetScore = 0;

searchTerms.forEach((term) => {
const count = (snippetLower.match(new RegExp(term, "g")) || [])
.length;
snippetScore += count;
});

if (snippetScore > bestSnippetScore) {
bestSnippetScore = snippetScore;
bestSnippet = snippet;
}
}
}

if (bestSnippet) {
matches.push({ text: bestSnippet.trim(), type: "content" });
}
}
}

Expand Down
Loading

0 comments on commit e713399

Please sign in to comment.