Skip to content

Commit c51f539

Browse files
heiskrchiedo
andauthored
Move search results to use relative urls (#17411)
* Move search results to use relative urls * ..and now we have real mark tags instead of em tags Co-authored-by: Chiedo John <2156688+chiedo@users.noreply.github.com>
1 parent 7f41681 commit c51f539

3 files changed

Lines changed: 31 additions & 28 deletions

File tree

javascripts/search.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,6 @@ function tmplSearchResult ({ url, breadcrumbs, heading, title, content }) {
263263
function markify (text) {
264264
const { mark } = tags
265265
return text
266-
.split(/<\/?em>/g)
266+
.split(/<\/?mark>/g)
267267
.map((el, i) => i % 2 ? mark(el) : el)
268268
}

lib/search/algolia-search.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const algoliasearch = require('algoliasearch')
2+
const { get } = require('lodash')
3+
const { namePrefix } = require('./config')
4+
5+
// https://www.algolia.com/apps/ZI5KPY1HBE/dashboard
6+
// This API key is public. There's also a private API key for writing to the Algolia API
7+
const searchClient = algoliasearch('ZI5KPY1HBE', '685df617246c3a10abba589b4599288f')
8+
9+
module.exports = async function loadAlgoliaResults ({ version, language, query, limit }) {
10+
const indexName = `${namePrefix}-${version}-${language}`
11+
const index = searchClient.initIndex(indexName)
12+
13+
// allows "phrase queries" and "prohibit operator"
14+
// https://www.algolia.com/doc/api-reference/api-parameters/advancedSyntax/
15+
const { hits } = await index.search(query, {
16+
hitsPerPage: limit,
17+
advancedSyntax: true,
18+
highlightPreTag: '<mark>',
19+
highlightPostTag: '</mark>'
20+
})
21+
22+
return hits.map(hit => ({
23+
url: hit.objectID,
24+
breadcrumbs: get(hit, '_highlightResult.breadcrumbs.value'),
25+
heading: get(hit, '_highlightResult.heading.value'),
26+
title: get(hit, '_highlightResult.title.value'),
27+
content: get(hit, '_highlightResult.content.value')
28+
}))
29+
}

middleware/search.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,10 @@
11
const express = require('express')
2-
const algoliasearch = require('algoliasearch')
3-
const { namePrefix } = require('../lib/search/config')
42
const languages = new Set(Object.keys(require('../lib/languages')))
53
const versions = require('../lib/search/versions')
6-
const { get } = require('lodash')
4+
const loadAlgoliaResults = require('../lib/search/algolia-search')
75

86
const router = express.Router()
97

10-
// https://www.algolia.com/apps/ZI5KPY1HBE/dashboard
11-
// This API key is public. There's also a private API key for writing to the Algolia API
12-
const searchClient = algoliasearch('ZI5KPY1HBE', '685df617246c3a10abba589b4599288f')
13-
14-
async function loadAlgoliaResults ({ version, language, query, limit }) {
15-
const indexName = `${namePrefix}-${version}-${language}`
16-
const index = searchClient.initIndex(indexName)
17-
18-
// allows "phrase queries" and "prohibit operator"
19-
// https://www.algolia.com/doc/api-reference/api-parameters/advancedSyntax/
20-
const { hits } = await index.search(query, {
21-
hitsPerPage: limit,
22-
advancedSyntax: true
23-
})
24-
25-
return hits.map(hit => ({
26-
url: hit.url,
27-
breadcrumbs: get(hit, '_highlightResult.breadcrumbs.value'),
28-
heading: get(hit, '_highlightResult.heading.value'),
29-
title: get(hit, '_highlightResult.title.value'),
30-
content: get(hit, '_highlightResult.content.value')
31-
}))
32-
}
33-
348
router.get('/', async (req, res) => {
359
res.set({
3610
'surrogate-control': 'private, no-store',

0 commit comments

Comments
 (0)