Skip to content

Commit

Permalink
Move search results to use relative urls (github#17411)
Browse files Browse the repository at this point in the history
* Move search results to use relative urls

* ..and now we have real mark tags instead of em tags

Co-authored-by: Chiedo John <[email protected]>
  • Loading branch information
heiskr and chiedo authored Jan 21, 2021
1 parent 7f41681 commit c51f539
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
2 changes: 1 addition & 1 deletion javascripts/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,6 @@ function tmplSearchResult ({ url, breadcrumbs, heading, title, content }) {
function markify (text) {
const { mark } = tags
return text
.split(/<\/?em>/g)
.split(/<\/?mark>/g)
.map((el, i) => i % 2 ? mark(el) : el)
}
29 changes: 29 additions & 0 deletions lib/search/algolia-search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const algoliasearch = require('algoliasearch')
const { get } = require('lodash')
const { namePrefix } = require('./config')

// https://www.algolia.com/apps/ZI5KPY1HBE/dashboard
// This API key is public. There's also a private API key for writing to the Algolia API
const searchClient = algoliasearch('ZI5KPY1HBE', '685df617246c3a10abba589b4599288f')

module.exports = async function loadAlgoliaResults ({ version, language, query, limit }) {
const indexName = `${namePrefix}-${version}-${language}`
const index = searchClient.initIndex(indexName)

// allows "phrase queries" and "prohibit operator"
// https://www.algolia.com/doc/api-reference/api-parameters/advancedSyntax/
const { hits } = await index.search(query, {
hitsPerPage: limit,
advancedSyntax: true,
highlightPreTag: '<mark>',
highlightPostTag: '</mark>'
})

return hits.map(hit => ({
url: hit.objectID,
breadcrumbs: get(hit, '_highlightResult.breadcrumbs.value'),
heading: get(hit, '_highlightResult.heading.value'),
title: get(hit, '_highlightResult.title.value'),
content: get(hit, '_highlightResult.content.value')
}))
}
28 changes: 1 addition & 27 deletions middleware/search.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,10 @@
const express = require('express')
const algoliasearch = require('algoliasearch')
const { namePrefix } = require('../lib/search/config')
const languages = new Set(Object.keys(require('../lib/languages')))
const versions = require('../lib/search/versions')
const { get } = require('lodash')
const loadAlgoliaResults = require('../lib/search/algolia-search')

const router = express.Router()

// https://www.algolia.com/apps/ZI5KPY1HBE/dashboard
// This API key is public. There's also a private API key for writing to the Algolia API
const searchClient = algoliasearch('ZI5KPY1HBE', '685df617246c3a10abba589b4599288f')

async function loadAlgoliaResults ({ version, language, query, limit }) {
const indexName = `${namePrefix}-${version}-${language}`
const index = searchClient.initIndex(indexName)

// allows "phrase queries" and "prohibit operator"
// https://www.algolia.com/doc/api-reference/api-parameters/advancedSyntax/
const { hits } = await index.search(query, {
hitsPerPage: limit,
advancedSyntax: true
})

return hits.map(hit => ({
url: hit.url,
breadcrumbs: get(hit, '_highlightResult.breadcrumbs.value'),
heading: get(hit, '_highlightResult.heading.value'),
title: get(hit, '_highlightResult.title.value'),
content: get(hit, '_highlightResult.content.value')
}))
}

router.get('/', async (req, res) => {
res.set({
'surrogate-control': 'private, no-store',
Expand Down

0 comments on commit c51f539

Please sign in to comment.