Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show matched tags in search suggestion #903

Merged
merged 21 commits into from
Dec 3, 2021
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
63 changes: 47 additions & 16 deletions src/www/_layouts/_header-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,39 +48,46 @@ const headerSearchJS = () => {
const query = e.target.value;
autoCompleteSuggestions.innerHTML = '';
if (query.length > 2) {
const matchedPages = pages.filter(page => {
let hasMatch = false;
if (
page.name.toLowerCase().includes(e.target.value.toLowerCase()) ||
(page.parent && page.parent.toLowerCase().includes(e.target.value.toLowerCase())) ||
(page.grandparent &&
page.grandparent.toLowerCase().includes(e.target.value.toLowerCase())) ||
(page.tags && page.tags.toLowerCase().includes(e.target.value.toLowerCase()))
const matchedPages = [[], [], []];

pages.forEach(page => {
if (page.name.toLowerCase().includes(query.toLowerCase())) {
matchedPages[0].push(page);
} else if (page.tags && page.tags.toLowerCase().includes(query.toLowerCase())) {
matchedPages[1].push(page);
} else if (
(page.parent && page.parent.toLowerCase().includes(query.toLowerCase())) ||
(page.grandparent && page.grandparent.toLowerCase().includes(query.toLowerCase()))
) {
hasMatch = true;
matchedPages[2].push(page);
}
return hasMatch;
});

if (matchedPages.length) {
if (!matchedPages.every(arr => arr.length === 0)) {
autoCompleteSuggestions.style.display = 'block';
matchedPages.forEach(page => {

const createSuggestionLink = page => {
// Construct page text node
const text = document.createTextNode(page.name);
const suggestionText = document.createElement('strong');
suggestionText.classList.add('wmnds-col-1');
suggestionText.appendChild(text);

const suggestionInfo = document.createElement('div');
suggestionInfo.classList.add('wmnds-grid', 'wmnds-grid--spacing-md-2-md');

// Construct breadcrumbs node
const crumbs = document.createTextNode(
`${page.grandparent ? `${page.grandparent} > ` : ''}${
page.parent ? `${page.parent} > ` : ''
}${page.name}`
);
const suggestionCrumbs = document.createElement('span');
const suggestionCrumbs = document.createElement('div');
suggestionCrumbs.classList.add('wmnds-col-1');
suggestionCrumbs.appendChild(crumbs);
suggestionInfo.appendChild(suggestionCrumbs);

const url = str => str.toLowerCase().replace(' ', '-');
const url = str => str.toLowerCase().replaceAll(' ', '-');
const suggestionHref =
page.href ||
`/${page.grandparent ? `${url(page.grandparent)}/` : ''}${
Expand All @@ -92,9 +99,33 @@ const headerSearchJS = () => {
suggestionLink.setAttribute('tabindex', '0');
suggestionLink.classList.add('wmnds-autocomplete-suggestions__li', 'wmnds-col-1');
suggestionLink.appendChild(suggestionText);
suggestionLink.appendChild(suggestionCrumbs);
suggestionLink.appendChild(suggestionInfo);

if (page.tags) {
const tags = page.tags.split(', ');
const matchedTags = tags.filter(tag => tag.includes(query));
if (matchedTags.length > 0) {
const tagText = document.createTextNode(
`Tag${matchedTags.length > 1 ? 's' : ''}: ${matchedTags.join(', ')}`
);
const suggestionTags = document.createElement('div');
suggestionCrumbs.classList.add('wmnds-col-md-2-3');
suggestionTags.classList.add(
'wmnds-col-md-1-3',
'wmnds-col-1',
'wmnds-text-align-right'
);
suggestionTags.appendChild(tagText);
suggestionInfo.appendChild(suggestionTags);
}
}

return suggestionLink;
};

autoCompleteSuggestions.appendChild(suggestionLink);
matchedPages.forEach(arrayOfMatches => {
const suggestions = arrayOfMatches.map(page => createSuggestionLink(page));
suggestions.forEach(suggestion => autoCompleteSuggestions.appendChild(suggestion));
});
} else {
autoCompleteSuggestions.style.display = 'none';
Expand Down
2 changes: 1 addition & 1 deletion src/www/pages/styles/utility-classes/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
{# Text Align #}
<h2>Text Align</h2>
<p>
Aligns text in the intended direction.
Aligns text in the intended direction. These classes can also be used to align <a href="/components/buttons">buttons</a>.
</p>

<pre class="wmnds-col-1 wmnds-col-md-1-2">
Expand Down