Merge pull request #8 from TxtDot/remap-links
feat: change links to proxied
This commit is contained in:
commit
66af4a1a2f
@ -3,10 +3,11 @@ import { IHandlerOutput } from "./handler.interface";
|
||||
import { readability } from "./readability";
|
||||
import { JSDOM } from "jsdom";
|
||||
|
||||
type EngineFunction = (url: JSDOM) => Promise<IHandlerOutput>;
|
||||
type EngineFunction = (url: Document) => Promise<IHandlerOutput>;
|
||||
|
||||
export default async function handlePage(
|
||||
url: string,
|
||||
originalUrl: string,
|
||||
engine?: string
|
||||
): Promise<IHandlerOutput> {
|
||||
if (engine && engineList.indexOf(engine) === -1) {
|
||||
@ -14,14 +15,21 @@ export default async function handlePage(
|
||||
}
|
||||
|
||||
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) {
|
||||
return engines[engine](dom);
|
||||
return engines[engine](document);
|
||||
}
|
||||
|
||||
const host = new URL(url).hostname;
|
||||
return fallback[host](dom) || fallback["*"](dom);
|
||||
return fallback[host](document) || fallback["*"](document);
|
||||
}
|
||||
|
||||
interface Engines {
|
||||
@ -29,7 +37,7 @@ interface Engines {
|
||||
}
|
||||
|
||||
export const engines: Engines = {
|
||||
readability: readability,
|
||||
readability,
|
||||
};
|
||||
|
||||
export const engineList: string[] = Object.keys(engines);
|
||||
|
@ -1,9 +1,8 @@
|
||||
import { Readability } from "@mozilla/readability";
|
||||
import { JSDOM } from "jsdom";
|
||||
import { IHandlerOutput } from "./handler.interface";
|
||||
|
||||
export async function readability(dom: JSDOM): Promise<IHandlerOutput> {
|
||||
const reader = new Readability(dom.window.document);
|
||||
export async function readability(document: Document): Promise<IHandlerOutput> {
|
||||
const reader = new Readability(document);
|
||||
const parsed = reader.parse();
|
||||
|
||||
if (!parsed) {
|
||||
|
@ -2,6 +2,7 @@ import { FastifyInstance } from "fastify";
|
||||
|
||||
import { GetRequest } from "../types/requests";
|
||||
import handlePage from "../handlers/main";
|
||||
import { generateOriginUrl } from "../utils";
|
||||
|
||||
export default async function mainRoute(fastify: FastifyInstance) {
|
||||
fastify.get("/", async (request: GetRequest, reply) => {
|
||||
@ -18,7 +19,15 @@ export default async function mainRoute(fastify: FastifyInstance) {
|
||||
format = "html";
|
||||
}
|
||||
|
||||
const parsed = await handlePage(remoteUrl, engine);
|
||||
const parsed = await handlePage(
|
||||
remoteUrl,
|
||||
generateOriginUrl(
|
||||
request.protocol,
|
||||
request.hostname,
|
||||
request.originalUrl
|
||||
),
|
||||
engine
|
||||
);
|
||||
|
||||
if (format === "text") {
|
||||
return parsed.textContent;
|
||||
|
@ -1,10 +1,19 @@
|
||||
import { EngineRequest } from "../types/requests";
|
||||
import { FastifyInstance } from "fastify";
|
||||
import handlePage from "../handlers/main";
|
||||
import { generateOriginUrl } from "../utils";
|
||||
|
||||
export default async function parseRoute(fastify: FastifyInstance) {
|
||||
fastify.get("/parse", async (req: EngineRequest) => {
|
||||
const parsed = await handlePage(req.query.url, req.query.engine);
|
||||
fastify.get("/parse", async (request: EngineRequest) => {
|
||||
const parsed = await handlePage(
|
||||
request.query.url,
|
||||
generateOriginUrl(
|
||||
request.protocol,
|
||||
request.hostname,
|
||||
request.originalUrl
|
||||
),
|
||||
request.query.engine
|
||||
);
|
||||
|
||||
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