Skip to content

Commit

Permalink
refactor: use bbob instead of bbcodejs (1point3acres) (#17396)
Browse files Browse the repository at this point in the history
* refactor: use bbob instead of bbcodejs (1point3acres)

* refactor: use bbob instead of bbcodejs (counter-strike)

* fix: minor fixes

* fix(route/counter-strike/news): handle optional attributes in customPreset
  • Loading branch information
TonyRL authored Nov 1, 2024
1 parent c8496f3 commit bf1083d
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 30 deletions.
4 changes: 3 additions & 1 deletion lib/routes/1point3acres/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import cache from '@/utils/cache';
import { rootUrl, apiRootUrl, types, ProcessThreads } from './utils';

export const route: Route = {
path: ['/post/:type?/:order?', '/thread/:type?/:order?'],
path: '/thread/:type?/:order?',
example: '/1point3acres/thread/hot',
parameters: { type: '帖子分类, 见下表,默认为 hot,即热门帖子', order: '排序方式,见下表,默认为空,即最新回复' },
name: '帖子',
categories: ['bbs'],
maintainers: ['EthanWng97', 'DIYgod', 'nczitzk'],
Expand Down
94 changes: 79 additions & 15 deletions lib/routes/1point3acres/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
import path from 'node:path';
import bbcode from 'bbcodejs';
import type { BBobCoreTagNodeTree } from '@bbob/types';
import bbobHTML from '@bbob/html';
import presetHTML5 from '@bbob/preset-html5';

const rootUrl = 'https://instant.1point3acres.com';
const apiRootUrl = 'https://api.1point3acres.com';
Expand All @@ -15,6 +17,17 @@ const types = {
hot: '热门帖子',
};

const swapLinebreak = (tree: BBobCoreTagNodeTree) =>
tree.walk((node) => {
if (typeof node === 'string' && node === '\n') {
return {
tag: 'br',
content: null,
};
}
return node;
});

const ProcessThreads = async (tryGet, apiUrl, order) => {
const response = await got({
method: 'get',
Expand All @@ -24,8 +37,6 @@ const ProcessThreads = async (tryGet, apiUrl, order) => {
},
});

const bbcodeParser = new bbcode.Parser();

const items = await Promise.all(
response.data.threads.map((item) => {
const result = {
Expand All @@ -48,20 +59,73 @@ const ProcessThreads = async (tryGet, apiUrl, order) => {
},
});

const data = detailResponse.data;
const thread = detailResponse.data.thread;

const customPreset = presetHTML5.extend((tags) => ({
...tags,
attach: (node, { render }) => {
const id = render(node.content);
const attachment = thread.attachment_list.find((a) => a.aid === Number.parseInt(id));

if (attachment.isimage) {
return {
tag: 'img',
attrs: {
src: attachment.url,
},
};
}

return {
tag: 'a',
attrs: {
href: `https://www.1point3acres.com/bbs/plugin.php?id=attachcenter:page&aid=${id}`,
rel: 'noopener',
target: '_blank',
},
content: `https://www.1point3acres.com/bbs/plugin.php?id=attachcenter:page&aid=${id}`,
};
},
url: (node) => {
const link = Object.keys(node.attrs as Record<string, string>)[0];
if (link.startsWith('https://link.1p3a.com/?url=')) {
const url = decodeURIComponent(link.replace('https://link.1p3a.com/?url=', ''));
return {
tag: 'a',
attrs: {
href: url,
rel: 'noopener',
target: '_blank',
},
content: node.content,
};
}

return {
tag: 'a',
attrs: {
href: link,
rel: 'noopener',
target: '_blank',
},
content: node.content,
};
},
}));

result.description = bbcodeParser.toHTML(data.thread.message_bbcode);
result.description = bbobHTML(thread.message_bbcode, [customPreset(), swapLinebreak]);

for (const a of data.thread.attachment_list) {
if (a.isimage === 1) {
result.description = result.description.replaceAll(
new RegExp(`\\[attach\\]${a.aid}\\[\\/attach\\]`, 'g'),
art(path.join(__dirname, 'templates/image.art'), {
url: a.url,
height: a.height,
width: a.width,
})
);
if (!thread.message_bbcode.includes('[attach]') && thread.attachment_list.length > 0) {
for (const a of thread.attachment_list) {
result.description +=
a.isimage === 1
? '<br>' +
art(path.join(__dirname, 'templates/image.art'), {
url: a.url,
height: a.height,
width: a.width,
})
: '';
}
}
} catch {
Expand Down
54 changes: 49 additions & 5 deletions lib/routes/counter-strike/news.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,56 @@
import { Route } from '@/types';
import type { BBobCoreTagNodeTree, PresetFactory } from '@bbob/types';

import got from '@/utils/got';
import { load } from 'cheerio';
import bbcode from 'bbcodejs';
import bbobHTML from '@bbob/html';
import presetHTML5 from '@bbob/preset-html5';
import { parseDate } from '@/utils/parse-date';

const swapLinebreak = (tree: BBobCoreTagNodeTree) =>
tree.walk((node) => {
if (typeof node === 'string' && node === '\n') {
return {
tag: 'br',
content: null,
};
}
return node;
});

const customPreset: PresetFactory = presetHTML5.extend((tags) => ({
...tags,
url: (node) => ({
tag: 'a',
attrs: {
href: Object.keys(node.attrs as Record<string, string>)[0],
rel: 'noopener',
target: '_blank',
},
content: node.content,
}),
video: (node, { render }) => ({
tag: 'video',
attrs: {
controls: '',
preload: 'metadata',
poster: node.attrs?.poster,
},
content: render(
Object.entries({
webm: 'video/webm',
mp4: 'video/mp4',
}).map(([key, type]) => ({
tag: 'source',
attrs: {
src: node.attrs?.[key],
type,
},
}))
),
}),
}));

export const handler = async (ctx) => {
const { category = 'all', language = 'english' } = ctx.req.param();
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 100;
Expand All @@ -25,14 +71,12 @@ export const handler = async (ctx) => {
},
});

const bbcodeParser = new bbcode.Parser();

const items = response.events
.filter((item) => (category === 'updates' ? item.event_type === 12 : item.event_type !== 12))
.filter((item) => (category === 'updates' ? item.event_type === 12 : item.event_type))
.slice(0, limit)
.map((item) => {
const title = item.event_name;
const description = bbcodeParser.toHTML(item.announcement_body.body);
const description = bbobHTML(item.announcement_body.body, [customPreset(), swapLinebreak]);
const guid = `counter-strike-news-${item.gid}`;

return {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
"*.yml": "eslint --cache --fix"
},
"dependencies": {
"@bbob/html": "4.1.1",
"@bbob/preset-html5": "4.1.1",
"@hono/node-server": "1.13.3",
"@hono/zod-openapi": "0.16.4",
"@notionhq/client": "2.2.15",
Expand All @@ -67,7 +69,6 @@
"@tonyrl/rand-user-agent": "2.0.81",
"aes-js": "3.1.2",
"art-template": "4.13.2",
"bbcodejs": "0.0.4",
"cheerio": "1.0.0",
"chrono-node": "2.7.7",
"city-timezones": "1.3.0",
Expand Down Expand Up @@ -137,6 +138,7 @@
"devDependencies": {
"@babel/preset-env": "7.26.0",
"@babel/preset-typescript": "7.26.0",
"@bbob/types": "4.1.1",
"@eslint/eslintrc": "3.1.0",
"@eslint/js": "9.13.0",
"@microsoft/eslint-formatter-sarif": "3.1.0",
Expand Down
72 changes: 64 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bf1083d

Please sign in to comment.