fix(route): Typst universe route removes duplicated packages and fix description (#15664)

* fix: package list source and title

* edit: remove duplicated packages and get rendered page

* Revert "edit: remove duplicated packages and get rendered page"

This reverts commit 711d8941c8098ffc077195587f6452349d7bf784.

* fix: remove duplicated packages

* fix: image links

* edit: fix image path after render

* fix: Incomplete URL substring sanitization

* fix: coding sty;e
This commit is contained in:
Yigong Hu 2024-05-25 16:16:11 +01:00 committed by GitHub
parent 94ffb50814
commit 1f334be4f8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 34 additions and 8 deletions

View File

@ -26,6 +26,25 @@ interface Context {
an: { exports: Array<Package> };
}
const GITHUBRAW_BASE = 'https://raw.githubusercontent.com';
const PKG_GITHUB_BASE = `${GITHUBRAW_BASE}/typst/packages/main/packages/preview`;
function fixImageSrc(src: string, env: Package) {
if (src.includes('://')) {
if (src.startsWith('https://typst.app/universe/package')) {
src = src.replaceAll('https://typst.app/universe/package', `${PKG_GITHUB_BASE}/${env.name}/${env.version}`);
} else if (src.startsWith('https://github.com/') && src.match(/\.(jpeg|jpg|gif|png|bmp|webp)$/gi)?.length) {
src = src.replace('https://github.com/', `${GITHUBRAW_BASE}/`);
}
} else {
const suffix = src.startsWith('/') ? '' : '/';
const package_base = `${PKG_GITHUB_BASE}/${env.name}/${env.version}${suffix}`;
const url = new URL(src, package_base);
src = url.toString();
}
return src;
}
export const route: Route = {
path: '/universe',
categories: ['program-update'],
@ -39,7 +58,7 @@ export const route: Route = {
name: 'Universe',
maintainers: ['HPDell'],
handler: async () => {
const targetUrl = 'https://typst.app/universe/search?kind=packages%2Ctemplates&packages=last-published';
const targetUrl = 'https://typst.app/universe/search?kind=packages%2Ctemplates&packages=last-updated';
const page = await ofetch(targetUrl);
const $ = load(page);
const script = $('script')
@ -58,18 +77,25 @@ export const route: Route = {
displayErrors: true,
});
const md = markdownit('commonmark');
const items = context.an.exports
.sort((a, b) => b.updatedAt - a.updatedAt)
.map((item) => ({
title: `${item.name} | ${item.description}`,
const items = context.an.exports.sort((a, b) => a.updatedAt - b.updatedAt);
const groups = new Map(items.map((it) => [it.name, it]));
const pkgs = [...groups.values()].map((item) => {
const $ = load(md.render(item.readme));
$('img').each((i, el) => {
const src = el.attribs.src;
el.attribs.src = fixImageSrc(src, item);
});
return {
title: `${item.name} (${item.version}) | ${item.description}`,
link: `https://typst.app/universe/package/${item.name}`,
description: md.render(item.readme),
description: $.html(),
pubDate: parseDate(item.updatedAt, 'X'),
}));
};
});
return {
title: 'Typst universe',
link: targetUrl,
item: items,
item: pkgs,
};
} else {
return {