Files
NodeBB/src/controllers/osd.js

58 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-03-06 20:07:06 +01:00
'use strict';
2019-08-21 23:02:50 -04:00
const xml = require('xml');
const nconf = require('nconf');
2017-03-06 20:07:06 +01:00
2019-08-21 23:02:50 -04:00
const plugins = require('../plugins');
const meta = require('../meta');
2017-03-06 20:07:06 +01:00
2017-03-06 21:00:20 +01:00
module.exports.handle = function (req, res, next) {
if (plugins.hooks.hasListeners('filter:search.query')) {
2017-09-21 23:55:46 +02:00
res.type('application/opensearchdescription+xml').send(generateXML());
2017-03-06 20:07:06 +01:00
} else {
next();
}
};
function generateXML() {
return xml([{
OpenSearchDescription: [
2021-02-04 00:13:32 -07:00
{
_attr: {
xmlns: 'http://a9.com/-/spec/opensearch/1.1/',
'xmlns:moz': 'http://www.mozilla.org/2006/browser/search/',
},
},
2017-09-21 23:55:46 +02:00
{ ShortName: trimToLength(String(meta.config.title || meta.config.browserTitle || 'NodeBB'), 16) },
{ Description: trimToLength(String(meta.config.description || ''), 1024) },
{ InputEncoding: 'UTF-8' },
2021-02-04 00:13:32 -07:00
{
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`,
},
2017-03-06 20:24:00 +01:00
},
2021-02-04 00:13:32 -07:00
},
2021-02-03 23:59:08 -07:00
{ 'moz:SearchForm': `${nconf.get('url')}/search` },
2017-03-06 20:07:06 +01:00
],
}], { declaration: true, indent: '\t' });
}
2017-09-21 23:55:46 +02:00
function trimToLength(string, length) {
return string.trim().substring(0, length).trim();
}