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>
This commit is contained in:
@@ -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
lib/search/algolia-search.js
Normal file
29
lib/search/algolia-search.js
Normal file
@@ -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')
|
||||
}))
|
||||
}
|
||||
@@ -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',
|
||||
|
||||
Reference in New Issue
Block a user