mirror of
https://github.com/txtdot/txtdot
synced 2024-10-18 14:40:19 +03:00
refactor(plugins): searx rewrite to jsx
and fix pageno problem
This commit is contained in:
parent
3cee45b591
commit
c2e6475624
41
packages/plugins/src/components/searchers.tsx
Normal file
41
packages/plugins/src/components/searchers.tsx
Normal file
@ -0,0 +1,41 @@
|
||||
import { JSX } from '@txtdot/sdk';
|
||||
|
||||
export function PageFooter({
|
||||
page,
|
||||
previous,
|
||||
next,
|
||||
}: {
|
||||
page: number;
|
||||
previous: string | false;
|
||||
next: string | false;
|
||||
}) {
|
||||
return (
|
||||
<>
|
||||
{page !== 1 ? (
|
||||
<>
|
||||
<a href={previous}>Previous </a> |
|
||||
</>
|
||||
) : (
|
||||
<></>
|
||||
)}
|
||||
<a href={next}> Next</a>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export function ResultItem({
|
||||
url,
|
||||
title,
|
||||
content,
|
||||
}: {
|
||||
url: string;
|
||||
title: string;
|
||||
content: string;
|
||||
}) {
|
||||
return (
|
||||
<>
|
||||
<a href={url}>{title}</a>
|
||||
<p>{content}</p>
|
||||
</>
|
||||
);
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
import { Engine } from '@txtdot/sdk';
|
||||
|
||||
import { JSX } from '@txtdot/sdk';
|
||||
|
||||
const Habr = new Engine('Habr', 'Habr parser', ['*']);
|
||||
|
||||
Habr.route('*path', async (input, ro) => {
|
||||
return {
|
||||
content: <div>Test</div>,
|
||||
};
|
||||
});
|
||||
|
||||
export default Habr;
|
@ -1,6 +1,5 @@
|
||||
import StackOverflow from './stackoverflow';
|
||||
import Readability from './readability';
|
||||
import SearX from './searx';
|
||||
import Habr from './habr';
|
||||
|
||||
export { StackOverflow, Readability, SearX, Habr };
|
||||
export { StackOverflow, Readability, SearX };
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { Engine } from '@txtdot/sdk';
|
||||
import { Engine, JSX } from '@txtdot/sdk';
|
||||
import { HandlerInput, Route } from '@txtdot/sdk';
|
||||
import { parseHTML } from 'linkedom';
|
||||
import { PageFooter, ResultItem } from '../components/searchers';
|
||||
|
||||
const SearX = new Engine('SearX', "Engine for searching with 'SearXNG'", [
|
||||
'searx.*',
|
||||
@ -14,13 +15,19 @@ async function search(
|
||||
const search = ro.q.search;
|
||||
const page = parseInt(ro.q.pageno || '1');
|
||||
|
||||
const page_footer = `${
|
||||
page !== 1
|
||||
? `<a href="${ro.reverse({ search, pageno: page - 1 })}">Previous </a>|`
|
||||
: ''
|
||||
}<a href="${ro.reverse({ search, pageno: page + 1 })}"> Next</a>`;
|
||||
let previous: string | false;
|
||||
let next: string | false;
|
||||
|
||||
if (ro.q.pageno) {
|
||||
previous = ro.reverse({ search, pageno: page - 1 });
|
||||
next = ro.reverse({ search, pageno: page + 1 });
|
||||
} else {
|
||||
previous = false;
|
||||
next = `/search?q=${search}&pageno=${page + 1}`;
|
||||
}
|
||||
|
||||
const articles = Array.from(document.querySelectorAll('.result'));
|
||||
|
||||
const articles_parsed = articles.map((a) => {
|
||||
const parsed = {
|
||||
url:
|
||||
@ -35,21 +42,24 @@ async function search(
|
||||
};
|
||||
|
||||
return {
|
||||
html: `<a href="${parsed.url}">${parsed.title}</a><p>${parsed.content}</p><hr>`,
|
||||
html: <ResultItem {...parsed} />,
|
||||
text: `${parsed.title} (${parsed.url})\n${parsed.content}\n---\n\n`,
|
||||
};
|
||||
});
|
||||
|
||||
const content = `${articles_parsed
|
||||
.map((a) => a.html)
|
||||
.join('')}${page_footer}`;
|
||||
const content = (
|
||||
<>
|
||||
{articles_parsed.map((a) => a.html).join('')}
|
||||
<PageFooter page={page} previous={previous} next={next} />
|
||||
</>
|
||||
);
|
||||
|
||||
const textContent = articles_parsed.map((a) => a.text).join('');
|
||||
|
||||
return {
|
||||
content: content,
|
||||
textContent,
|
||||
title: `${search} - Searx - Page ${page}`,
|
||||
lang: document.documentElement.lang,
|
||||
};
|
||||
}
|
||||
|
@ -8,17 +8,25 @@ export namespace JSX {
|
||||
}
|
||||
|
||||
export function createElement(
|
||||
name: string,
|
||||
props: { [id: string]: string },
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
name: any,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
props: { [id: string]: any },
|
||||
...content: string[]
|
||||
) {
|
||||
props = props || {};
|
||||
const propsstr = Object.keys(props)
|
||||
.map((key) => {
|
||||
const value = props[key];
|
||||
if (key === 'className') return `class=${value}`;
|
||||
else return `${key}=${value}`;
|
||||
})
|
||||
.join(' ');
|
||||
return `<${name} ${propsstr}>${content.join('')}</${name}>`;
|
||||
if (typeof name === 'string') {
|
||||
props = props || {};
|
||||
const propsstr = Object.keys(props)
|
||||
.map((key) => {
|
||||
const value = props[key];
|
||||
if (key === 'className') return `class=${value}`;
|
||||
else return `${key}=${value}`;
|
||||
})
|
||||
.join(' ');
|
||||
return `<${name} ${propsstr}>${content.join('')}</${name}>`;
|
||||
} else if (typeof name === 'function') {
|
||||
return name(props, ...content);
|
||||
} else {
|
||||
return content.join('');
|
||||
}
|
||||
}
|
||||
|
@ -28,8 +28,8 @@ export class HandlerInput {
|
||||
export interface HandlerOutput {
|
||||
content: string;
|
||||
textContent: string;
|
||||
title?: string;
|
||||
lang?: string;
|
||||
title: string;
|
||||
lang: string;
|
||||
}
|
||||
|
||||
export interface EngineOutput {
|
||||
|
@ -73,14 +73,14 @@ export class Distributor {
|
||||
);
|
||||
|
||||
const purify = DOMPurify(dom);
|
||||
const content = purify.sanitize(output.content);
|
||||
const content = purify.sanitize(dom.document.toString());
|
||||
|
||||
return {
|
||||
content,
|
||||
textContent:
|
||||
output.textContent || dom.document.documentElement.textContent || '',
|
||||
title: output.title,
|
||||
lang: output.lang,
|
||||
title: output.title || dom.document.title,
|
||||
lang: output.lang || dom.document.documentElement.lang,
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user