Files
RSSHub/lib/routes/obsidian/utils.ts
T
dependabot[bot]andTony 873618e99d chore(deps-dev): bump eslint-plugin-unicorn from 65.0.1 to 66.0.0 (#22274)
* chore(deps-dev): bump eslint-plugin-unicorn from 65.0.1 to 66.0.0

Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 65.0.1 to 66.0.0.
- [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases)
- [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v65.0.1...v66.0.0)

---
updated-dependencies:
- dependency-name: eslint-plugin-unicorn
  dependency-version: 66.0.0
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* style: fix unicorn-js/prefer-url-href

* style: oxlint disable

* style: fix unicorn-js/no-useless-else

* style: no-unsafe-string-replacement

* style: fix no-duplicate-loops

* style: fix prefer-number-is-safe-integer

* style: fix prefer-single-array-predicate

* style: fix no-object-methods-with-collections

* style: fix no-useless-boolean-cast

* style: fix no-useless-template-literals

* style: fix no-negated-array-predicate

* style: fix prefer-direct-iteration

* style: fix consistent-optional-chaining

* style: fix prefer-simple-sort-comparator

* style: fix prefer-global-number-constants

* style: fix prefer-type-literal-last

* style: fix require-array-sort-compare.

* style: fix prefer-early-return

* style: fix prefer-short-arrow-method

* style: fix prefer-array-from-map

* style: fix prefer-scoped-selector

* style: fix no-global-object-property-assignment

* style: fix no-unnecessary-global-this

* style: fix no-unreadable-object-destructuring

* style: fix no-invalid-argument-count

* style: fix prefer-smaller-scope

* style: fix prefer-iterator-to-array.

* style: fix prefer-object-iterable-methods

* style: fix typo

* style: fix max-nested-calls

* style: fix no-incorrect-template-string-interpolation

* style: fix no-break-in-nested-loop

* style: fix prefer-await

* style: fix no-computed-property-existence-check

* style: fix prefer-number-coercion

* style: fix no-empty-file

* style: update eslint unicorn v66 recommended preset

* style: add unicorn/prefer-spread to oxlint-disable comments

* style: fix no-array-reduce

* style: delete github/no-then as it's covered by unicorn/prefer-await

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Tony <TonyRL@users.noreply.github.com>
2026-06-16 14:43:42 +08:00

97 lines
3.0 KiB
TypeScript

import type { Data, DataItem } from '@/types';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
type CommunityEntryType = 'plugin' | 'theme';
type CommunitySearchConfig = {
apiKey: string;
url: string;
};
type CommunitySearchHit = {
document: {
authors?: string[];
github_created_at?: string;
github_updated_at?: string;
id: string;
latest_release_at?: string;
name: string;
short_desc?: string;
slug: string;
tags?: string[];
type: CommunityEntryType;
};
};
type CommunitySearchResponse = {
hits?: CommunitySearchHit[];
};
const hitsPerPage = 54;
const searchPageBaseUrl = 'https://community.obsidian.md/search';
const titleRegex = /([^/]+)\.md$/;
export async function buildCommunityFeed(type: CommunityEntryType): Promise<Data> {
const pageUrl = getSearchPageUrl(type);
const searchConfig = await getSearchConfig(pageUrl);
const data = await ofetch<CommunitySearchResponse>(`${searchConfig.url}/collections/entries/documents/search`, {
headers: {
'X-TYPESENSE-API-KEY': searchConfig.apiKey,
},
query: {
filter_by: `type:=${type}`,
per_page: hitsPerPage,
q: '*',
query_by: 'name,authors,short_desc',
sort_by: 'github_created_at:desc',
},
});
return {
title: `Obsidian ${type === 'plugin' ? 'Plugins' : 'Themes'}`,
link: pageUrl,
item: data.hits?.map(({ document }) => buildItem(document)) ?? [],
};
}
export function getTitle(path: string): string {
const match = path.match(titleRegex);
return match ? match[1] : '';
}
function getSearchPageUrl(type: CommunityEntryType) {
const url = new URL(searchPageBaseUrl);
url.searchParams.set('type', type);
url.searchParams.set('sort', 'created');
return url.href;
}
async function getSearchConfig(pageUrl: string): Promise<CommunitySearchConfig> {
const html = await ofetch<string>(pageUrl);
const match = html.match(/apiKey\\":\\"([^"\\]+)\\"[\s\S]*?url\\":\\"([^"\\]+)\\"/) ?? html.match(/"apiKey":"([^"]+)"[\s\S]*?"url":"([^"]+)"/);
if (!match) {
throw new Error('Unable to locate Obsidian community search API config');
}
return {
apiKey: match[1],
url: match[2].replaceAll(String.raw`\/`, '/'),
};
}
function buildItem(document: CommunitySearchHit['document']): DataItem {
return {
title: document.name,
description: document.short_desc,
link: `https://community.obsidian.md/${document.type}s/${document.slug}`,
guid: `${document.type}:${document.id}`,
pubDate: document.github_created_at ? parseDate(document.github_created_at) : undefined,
updated: document.latest_release_at ? parseDate(document.latest_release_at) : document.github_updated_at ? parseDate(document.github_updated_at) : undefined,
author: document.authors?.join(', '),
category: document.tags,
};
}