Files
NodeBB/src/controllers/osd.js

50 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: [
2017-09-21 23:55:46 +02:00
{ _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',
] },
2017-03-06 20:43:06 +01:00
{ Url: {
2017-03-06 20:07:06 +01:00
_attr: {
2017-03-06 20:23:34 +01:00
type: 'text/html',
method: 'get',
2017-03-06 20:24:00 +01:00
template: nconf.get('url') + '/search?term={searchTerms}&in=titlesposts',
},
2017-03-06 20:43:06 +01:00
} },
2017-09-21 23:55:46 +02: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();
}