You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.3 KiB
JavaScript

8 years ago
'use strict';
const xml = require('xml');
const nconf = require('nconf');
8 years ago
const plugins = require('../plugins');
const meta = require('../meta');
8 years ago
8 years ago
module.exports.handle = function (req, res, next) {
if (plugins.hooks.hasListeners('filter:search.query')) {
res.type('application/opensearchdescription+xml').send(generateXML());
8 years ago
} else {
next();
}
};
function generateXML() {
return xml([{
OpenSearchDescription: [
{
_attr: {
xmlns: 'http://a9.com/-/spec/opensearch/1.1/',
'xmlns:moz': 'http://www.mozilla.org/2006/browser/search/',
},
},
{ ShortName: trimToLength(String(meta.config.title || meta.config.browserTitle || 'NodeBB'), 16) },
{ Description: trimToLength(String(meta.config.description || ''), 1024) },
{ InputEncoding: 'UTF-8' },
{
Image: [
{
_attr: {
width: '16',
height: '16',
type: 'image/x-icon',
},
},
`${nconf.get('url')}/favicon.ico`,
],
},
{
Url: {
_attr: {
type: 'text/html',
method: 'get',
template: `${nconf.get('url')}/search?term={searchTerms}&in=titlesposts`,
},
8 years ago
},
},
{ 'moz:SearchForm': `${nconf.get('url')}/search` },
8 years ago
],
}], { declaration: true, indent: '\t' });
}
function trimToLength(string, length) {
return string.trim().substring(0, length).trim();
}