Skip to content

Commit ae1f4df

Browse files
committed
Add custom e2e highlight tests
1 parent 72694d2 commit ae1f4df

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

cypress/e2e/simple-jekyll-search.cy.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,45 @@ describe('Simple Jekyll Search', () => {
6060
.should('exist');
6161
});
6262
});
63+
64+
describe('Highlighting Middleware', () => {
65+
it('should highlight search terms in results', () => {
66+
cy.get('#search-input')
67+
.type('Lorem');
68+
69+
cy.get('#results-container')
70+
.should('be.visible');
71+
72+
cy.get('#results-container .search-desc .search-highlight')
73+
.should('exist')
74+
.should('have.css', 'background-color', 'rgb(255, 255, 0)');
75+
76+
cy.get('#results-container .search-desc .search-highlight')
77+
.first()
78+
.should('contain', 'Lorem');
79+
});
80+
81+
it('should highlight multiple occurrences', () => {
82+
cy.get('#search-input')
83+
.type('test');
84+
85+
cy.get('#results-container')
86+
.should('be.visible');
87+
88+
cy.get('#results-container .search-desc .search-highlight')
89+
.should('have.length.at.least', 1);
90+
});
91+
92+
it('should escape HTML in search results', () => {
93+
cy.get('#search-input')
94+
.type('sed');
95+
96+
cy.get('#results-container')
97+
.should('be.visible');
98+
99+
cy.get('#results-container .search-desc')
100+
.should('exist')
101+
.and('not.contain', '<script>');
102+
});
103+
});
63104
});

docs/_includes/search.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@
77
<script src="{{ '/assets/js/simple-jekyll-search.min.js' | relative_url }}"></script>
88

99
<script>
10+
var highlightMiddleware = window.createHighlightTemplateMiddleware({
11+
className: 'search-highlight',
12+
maxLength: 200
13+
});
14+
1015
window.simpleJekyllSearch = new SimpleJekyllSearch({
1116
searchInput: document.getElementById('search-input'),
1217
resultsContainer: document.getElementById('results-container'),
1318
json: '{{ "/assets/data/search.json" | relative_url }}',
14-
searchResultTemplate: '<li><a href="{url}?query={query}" title="{desc}">{title}</a></li>',
19+
searchResultTemplate: '<li><a href="{url}?query={query}">{title}</a><p class="search-desc">{content}</p></li>',
20+
templateMiddleware: highlightMiddleware,
1521
noResultsText: 'No results found',
1622
limit: 10,
1723
strategy: 'wildcard',

docs/_sass/_custom.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,16 @@ $on-laptop: 800px;
6262

6363
.info-text {
6464
margin-top: 20px;
65+
}
66+
67+
.search-highlight {
68+
background-color: #ffff00;
69+
font-weight: bold;
70+
padding: 0 2px;
71+
}
72+
73+
.search-desc {
74+
font-size: 0.9em;
75+
color: $grey-color-dark;
76+
margin: 0.3em 0;
6577
}

0 commit comments

Comments
 (0)