Skip to content

Commit

Permalink
lglg.top 下的帖子链接渲染
Browse files Browse the repository at this point in the history
  • Loading branch information
bohanjun committed Jul 21, 2023
1 parent 8999a22 commit 5560bc8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
24 changes: 20 additions & 4 deletions packages/viewer/src/lib/luogu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,19 @@ export const getForumName = (forum: string) =>

export const judgementUrl = "https://www.luogu.com.cn/judgement";

const isHTTPorHTTPS = (url: URL) =>
(url.protocol === "https:" && ["", "443"].includes(url.port)) ||
(url.protocol === "http:" && ["", "80"].includes(url.port));

const isLuoguUrl = (url: URL) =>
url.hostname === "www.luogu.com.cn" &&
((url.protocol === "https:" && ["", "443"].includes(url.port)) ||
(url.protocol === "http:" && ["", "80"].includes(url.port)));
url.hostname === "www.luogu.com.cn" && isHTTPorHTTPS(url);

const isLDAHackUrl = (url: URL) =>
url.hostname === "www.luogu.com.co" && isHTTPorHTTPS(url);

const isLDAUrl = (url: URL) =>
["lglg.top", "cf.lglg.top", "hk.lglg.top"].includes(url.hostname) &&
isHTTPorHTTPS(url);

export function getUserIdFromUrl(target: URL) {
if (!isLuoguUrl(target)) return null;
Expand All @@ -43,7 +52,7 @@ export function getUserIdFromUrl(target: URL) {
}

export function getDiscussionIdFromUrl(target: URL) {
if (!isLuoguUrl(target)) return null;
if (!isLuoguUrl(target) || !isLDAHackUrl(target)) return null;
const discussionId = parseInt(
(target.pathname.startsWith("/discuss/") &&
target.pathname.split("/")[2]) ||
Expand All @@ -54,6 +63,13 @@ export function getDiscussionIdFromUrl(target: URL) {
return Number.isNaN(discussionId) ? null : discussionId;
}

export function getDiscussionIdFromLDAUrl(target: URL) {
if (!isLDAUrl(target) || !/^d+$/.test(target.pathname.split("/")[1]))
return null;
const discussionId = parseInt(target.pathname.split("/")[1], 10);
return Number.isNaN(discussionId) ? null : discussionId;
}

export function getDiscussionId(s: string) {
const id = parseInt(s, 10);
if (!Number.isNaN(id)) return id;
Expand Down
19 changes: 16 additions & 3 deletions packages/viewer/src/lib/serialize-reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Discussion, ReplyTakedown, User } from "@prisma/client";
import prisma from "@/lib/prisma";
import {
getDiscussionIdFromUrl,
getDiscussionIdFromLDAUrl,
getUserIdFromUrl,
getUserUrl,
getUserRealUrl,
Expand Down Expand Up @@ -89,9 +90,7 @@ export default async function serializeReply(
// element.classList.add("text-decoration-none", "link-teal");
element.setAttribute("href", getUserUrl(uid));
} else {
const mentionedDiscussionId = getDiscussionIdFromUrl(
new URL(element.getAttribute("href") as string),
);
const mentionedDiscussionId = getDiscussionIdFromUrl(urlAbsolute);
if (mentionedDiscussionId) {
discussions.push(mentionedDiscussionId);
discussionElements.push({
Expand All @@ -103,6 +102,20 @@ export default async function serializeReply(
mentionedDiscussionId.toString(),
);
element.setAttribute("href", getDiscussionUrl(mentionedDiscussionId));
} else {
const mentionedLDADiscussionId =
getDiscussionIdFromLDAUrl(urlAbsolute);
if (mentionedLDADiscussionId) {
discussions.push(mentionedLDADiscussionId);
discussionElements.push({
ele: element,
discussion: mentionedLDADiscussionId,
});
element.setAttribute(
"data-discussion-id",
mentionedLDADiscussionId.toString(),
);
}
}
}
} catch (e) {
Expand Down

0 comments on commit 5560bc8

Please sign in to comment.