1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-07-19 13:19:42 +02:00

add short body

This commit is contained in:
Taly 2022-08-23 14:12:15 +03:00
parent cb877398a6
commit 03370dd066
5 changed files with 80 additions and 21 deletions

View file

@ -34,10 +34,13 @@ class Search {
case 'list': case 'list':
blockContent = block.data.items.join(' '); blockContent = block.data.items.join(' ');
break; break;
default:
return;
} }
const blockWords: string[] = blockContent const blockWords: string[] = blockContent
// @todo get text from inline code elements and remove html tags .replace(/<[^>]*>?/gm, '')
// lowercase all words // lowercase all words
.toLowerCase() .toLowerCase()
@ -99,36 +102,59 @@ class Search {
const returnPages = pages.filter(page => foundPages.some(({ id }) => id === page._id)) const returnPages = pages.filter(page => foundPages.some(({ id }) => id === page._id))
.map(page => { .map(page => {
let shortBody = '...'; let shortBody = '';
let score = 1; let flag = false;
let section = '';
let ratio = 0;
page.body.blocks.forEach((block: any) => { page.body.blocks.forEach((block: any) => {
if (flag) return;
let blockContent = ''; let blockContent = '';
switch (block.type) { switch (block.type) {
case 'header': case 'header':
blockContent = block.data.text; blockContent = block.data.text;
ratio = 1;
section = blockContent;
break; break;
// case 'paragraph': case 'paragraph':
// blockContent = block.data.text blockContent = block.data.text
// break; ratio = 0.5;
// break;
// case 'list':
// blockContent = block.data.items.join(' '); case 'list':
// break; blockContent = block.data.items.join(' ');
ratio = 0.5;
break;
default:
return;
} }
blockContent = blockContent
.replace(/<[^>]*>?/gm, '');
// .toLowerCase();
searchWords.forEach(word => { searchWords.forEach(word => {
blockContent = blockContent.replace(word, `<span class="search-word">${word}</span>`); // blockContent = blockContent.replace(word, `<span class="search-word">${word}</span>`);
if (flag) return;
if (blockContent.toLowerCase().indexOf(word) !== -1) {
shortBody = this.highlightSubstring(blockContent, word);
flag = true;
}
}) })
// shortBody += blockContent;
}); });
return { return {
...page, ...page,
shortBody shortBody,
anchor: section.replace(/\s+/g, '-').toLowerCase(),
section,
}; };
}); });
@ -136,12 +162,12 @@ class Search {
// --------- START test --------- // --------- START test ---------
//
const uniqWords = [...new Set(pagesWords.flatMap(page => page.words))].sort(); const uniqWords = [...new Set(pagesWords.flatMap(page => page.words))].sort();
//
uniqWords.forEach(word => { // uniqWords.forEach(word => {
console.log(word); // console.log(word);
}) // })
// --------- END test --------- // --------- END test ---------
@ -158,6 +184,12 @@ class Search {
return pages; return pages;
} }
private highlightSubstring(text: string, word: string) {
const wordRegExp = new RegExp(word, "ig");
return text.replace(wordRegExp, '<span class="search-word">$&</span>');
}
} }
export default Search; export default Search;

View file

@ -32,6 +32,8 @@ router.get('/search', async (req: Request, res: Response) => {
uri: page.uri, uri: page.uri,
// body: page.body, // body: page.body,
// parent: page.parent, // parent: page.parent,
section: page.section,
anchor: page.anchor,
shortBody: page.shortBody, shortBody: page.shortBody,
}; };
}); });

View file

@ -35,6 +35,7 @@ export default class Search {
searchResultItem: 'search-result-item', searchResultItem: 'search-result-item',
searchResultItemTitle: 'search-result-item__title', searchResultItemTitle: 'search-result-item__title',
searchResultItemSection: 'search-result-item__section',
searchResultItemDescription: 'search-result-item__description', searchResultItemDescription: 'search-result-item__description',
blur: 'blurred', blur: 'blurred',
@ -122,6 +123,7 @@ export default class Search {
} catch (e) {} } catch (e) {}
this.nodes.searchInput.focus(); this.nodes.searchInput.focus();
this.nodes.searchInput.select();
} }
createDebouncedSearch() { createDebouncedSearch() {
@ -150,19 +152,28 @@ export default class Search {
// this.nodes.searchResultWrapper.appendChild(suggestionsWrapper); // this.nodes.searchResultWrapper.appendChild(suggestionsWrapper);
data.result.pages.forEach(page => { data.result.pages.forEach(page => {
const url = `/${page.uri}` + (page.section ? `#${page.anchor}` : '');
const result = document.createElement('a'); const result = document.createElement('a');
result.classList.add(this.CSS.searchResultItem); result.classList.add(this.CSS.searchResultItem);
result.setAttribute('href', `/${page.uri}`); result.setAttribute('href', url);
const title = document.createElement('div'); const title = document.createElement('div');
title.classList.add(this.CSS.searchResultItemTitle); title.classList.add(this.CSS.searchResultItemTitle);
title.innerHTML = page.title; title.innerHTML = page.title;
result.appendChild(title); result.appendChild(title);
if (page.section !== page.title) {
const section = document.createElement('span');
section.classList.add(this.CSS.searchResultItemSection);
section.innerHTML = `${page.section}`;
title.appendChild(section);
}
const description = document.createElement('div'); const description = document.createElement('div');
description.classList.add(this.CSS.searchResultItemDescription); description.classList.add(this.CSS.searchResultItemDescription);
description.innerHTML = `${page.shortBody}`; description.innerHTML = `${page.shortBody}`;
// result.appendChild(description); result.appendChild(description);
this.nodes.searchResultWrapper.appendChild(result); this.nodes.searchResultWrapper.appendChild(result);
}); });

View file

@ -83,12 +83,26 @@
font-weight: 500; font-weight: 500;
} }
&__section {
opacity: 0.7;
&:before {
content: '•';
margin: 0 5px;
text-align: center;
}
}
&__description { &__description {
margin-top: 8px; margin-top: 8px;
line-height: 1.5;
} }
} }
} }
&-word {
color: var(--color-link-active);
}
} }
.noscroll { .noscroll {

View file

@ -4183,7 +4183,7 @@ nise@^5.1.0:
node-cache@^5.1.2: node-cache@^5.1.2:
version "5.1.2" version "5.1.2"
resolved "https://registry.npmjs.org/node-cache/-/node-cache-5.1.2.tgz" resolved "https://registry.yarnpkg.com/node-cache/-/node-cache-5.1.2.tgz#f264dc2ccad0a780e76253a694e9fd0ed19c398d"
integrity sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg== integrity sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg==
dependencies: dependencies:
clone "2.x" clone "2.x"