Merge branch 'main' into start-surfing
This commit is contained in:
commit
847ab55a82
@ -3,10 +3,11 @@ import { IHandlerOutput } from "./handler.interface";
|
|||||||
import { readability } from "./readability";
|
import { readability } from "./readability";
|
||||||
import { JSDOM } from "jsdom";
|
import { JSDOM } from "jsdom";
|
||||||
|
|
||||||
type EngineFunction = (url: JSDOM) => Promise<IHandlerOutput>;
|
type EngineFunction = (url: Document) => Promise<IHandlerOutput>;
|
||||||
|
|
||||||
export default async function handlePage(
|
export default async function handlePage(
|
||||||
url: string,
|
url: string,
|
||||||
|
originalUrl: string,
|
||||||
engine?: string
|
engine?: string
|
||||||
): Promise<IHandlerOutput> {
|
): Promise<IHandlerOutput> {
|
||||||
if (engine && engineList.indexOf(engine) === -1) {
|
if (engine && engineList.indexOf(engine) === -1) {
|
||||||
@ -14,14 +15,21 @@ export default async function handlePage(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const response = await axios.get(url);
|
const response = await axios.get(url);
|
||||||
const dom = new JSDOM(response.data, { url: url });
|
const document = new JSDOM(response.data, { url: url }).window.document;
|
||||||
|
const UrlParsed = new URL(originalUrl);
|
||||||
|
|
||||||
|
[...document.getElementsByTagName("a")].forEach((link) => {
|
||||||
|
link.href = `${UrlParsed.origin}/?url=${link.href}${
|
||||||
|
engine && `&engine=${engine}`
|
||||||
|
}`;
|
||||||
|
});
|
||||||
|
|
||||||
if (engine) {
|
if (engine) {
|
||||||
return engines[engine](dom);
|
return engines[engine](document);
|
||||||
}
|
}
|
||||||
|
|
||||||
const host = new URL(url).hostname;
|
const host = new URL(url).hostname;
|
||||||
return fallback[host](dom) || fallback["*"](dom);
|
return fallback[host](document) || fallback["*"](document);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Engines {
|
interface Engines {
|
||||||
@ -29,7 +37,7 @@ interface Engines {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const engines: Engines = {
|
export const engines: Engines = {
|
||||||
readability: readability,
|
readability,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const engineList: string[] = Object.keys(engines);
|
export const engineList: string[] = Object.keys(engines);
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
import { Readability } from "@mozilla/readability";
|
import { Readability } from "@mozilla/readability";
|
||||||
import { JSDOM } from "jsdom";
|
|
||||||
import { IHandlerOutput } from "./handler.interface";
|
import { IHandlerOutput } from "./handler.interface";
|
||||||
|
|
||||||
export async function readability(dom: JSDOM): Promise<IHandlerOutput> {
|
export async function readability(document: Document): Promise<IHandlerOutput> {
|
||||||
const reader = new Readability(dom.window.document);
|
const reader = new Readability(document);
|
||||||
const parsed = reader.parse();
|
const parsed = reader.parse();
|
||||||
|
|
||||||
if (!parsed) {
|
if (!parsed) {
|
||||||
|
@ -2,6 +2,7 @@ import { FastifyInstance } from "fastify";
|
|||||||
|
|
||||||
import { GetRequest } from "../types/requests";
|
import { GetRequest } from "../types/requests";
|
||||||
import handlePage from "../handlers/main";
|
import handlePage from "../handlers/main";
|
||||||
|
import { generateOriginUrl } from "../utils";
|
||||||
|
|
||||||
export default async function mainRoute(fastify: FastifyInstance) {
|
export default async function mainRoute(fastify: FastifyInstance) {
|
||||||
fastify.get("/", async (request: GetRequest, reply) => {
|
fastify.get("/", async (request: GetRequest, reply) => {
|
||||||
@ -18,7 +19,15 @@ export default async function mainRoute(fastify: FastifyInstance) {
|
|||||||
format = "html";
|
format = "html";
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsed = await handlePage(remoteUrl, engine);
|
const parsed = await handlePage(
|
||||||
|
remoteUrl,
|
||||||
|
generateOriginUrl(
|
||||||
|
request.protocol,
|
||||||
|
request.hostname,
|
||||||
|
request.originalUrl
|
||||||
|
),
|
||||||
|
engine
|
||||||
|
);
|
||||||
|
|
||||||
if (format === "text") {
|
if (format === "text") {
|
||||||
return parsed.textContent;
|
return parsed.textContent;
|
||||||
|
@ -1,10 +1,19 @@
|
|||||||
import { EngineRequest } from "../types/requests";
|
import { EngineRequest } from "../types/requests";
|
||||||
import { FastifyInstance } from "fastify";
|
import { FastifyInstance } from "fastify";
|
||||||
import handlePage from "../handlers/main";
|
import handlePage from "../handlers/main";
|
||||||
|
import { generateOriginUrl } from "../utils";
|
||||||
|
|
||||||
export default async function parseRoute(fastify: FastifyInstance) {
|
export default async function parseRoute(fastify: FastifyInstance) {
|
||||||
fastify.get("/parse", async (req: EngineRequest) => {
|
fastify.get("/parse", async (request: EngineRequest) => {
|
||||||
const parsed = await handlePage(req.query.url, req.query.engine);
|
const parsed = await handlePage(
|
||||||
|
request.query.url,
|
||||||
|
generateOriginUrl(
|
||||||
|
request.protocol,
|
||||||
|
request.hostname,
|
||||||
|
request.originalUrl
|
||||||
|
),
|
||||||
|
request.query.engine
|
||||||
|
);
|
||||||
|
|
||||||
return parsed;
|
return parsed;
|
||||||
});
|
});
|
||||||
|
7
src/utils.ts
Normal file
7
src/utils.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
export function generateOriginUrl(
|
||||||
|
protocol: string,
|
||||||
|
host: string,
|
||||||
|
originalUrl: string
|
||||||
|
): string {
|
||||||
|
return `${protocol}://${host}${originalUrl}`;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user