From 7f196b2e2cc985c42f8436b8ef8d550aae4d1462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dorian=20Oszcz=C4=99da?= Date: Wed, 23 Oct 2024 22:58:14 +0100 Subject: [PATCH] feat: Add the ability to specify a custom definition link. --- CHANGELOG.md | 10 +++++++--- package-lock.json | 4 ++-- package.json | 2 +- src/constants/links.ts | 15 ++++++++++++++- src/index.ts | 7 ++++++- src/options.ts | 17 ++++++++++++++--- test/index.spec.ts | 1 + 7 files changed, 45 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c5ad1f3..7e4c1b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,14 +1,18 @@ +# 0.0.2 + +- Added option to override the links used by the scraper to allow specifying custom URLs (in case of proxies, etc.). + # 0.0.1-patch.3 -- Fix package having been published without re-compilation via `tsc`. +- Fixed package having been published without re-compilation via `tsc`. # 0.0.1-patch.2 -- Fix oversight involving the parser only parsing 1 part of speech per etymology. +- Fixed oversight involving the parser only parsing 1 part of speech per etymology. # 0.0.1-patch.1 -- Switch from Rome to Biome, following the package having been renamed. +- Switched from Rome to Biome, following the package having been renamed. # 0.0.1 diff --git a/package-lock.json b/package-lock.json index bed4ad1..48f2c65 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "wiktionary-scraper", - "version": "0.0.1+1", + "version": "0.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "wiktionary-scraper", - "version": "0.0.1+1", + "version": "0.0.2", "license": "MIT", "dependencies": { "cheerio": "^1.0.0-rc.12" diff --git a/package.json b/package.json index 9d61de5..838c4d9 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "wiktionary-scraper", "description": "A lightweight scraper to fetch information about words in various languages from Wiktionary.", "license": "MIT", - "version": "0.0.1-patch.3", + "version": "0.0.2", "main": "./dist/index.js", "types": "./dist/index.d.ts", "type": "module", diff --git a/src/constants/links.ts b/src/constants/links.ts index 6c91002..a8ff826 100644 --- a/src/constants/links.ts +++ b/src/constants/links.ts @@ -1,6 +1,19 @@ import { ScraperOptions } from "../options.js"; -export default { +/** Defines the links used in querying Wiktionary. */ +interface Links { + /** + * Generates a link pointing to a word entry. + * + * @defaultValue `https://${options.siteLanguage}.wiktionary.org/wiki/${word}#${options.lemmaLanguage}` + */ + definition: (word: string, options: ScraperOptions) => string; +} + +const links: Links = { definition: (word: string, options: ScraperOptions): string => `https://${options.siteLanguage}.wiktionary.org/wiki/${word}#${options.lemmaLanguage}`, }; + +export type { Links }; +export default links; diff --git a/src/index.ts b/src/index.ts index 6c0f22c..f970936 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,13 +10,18 @@ const defaultScraperOptions: ScraperOptions = { siteLanguage: "en", userAgent: "wiktionary-scraper (github.com/vxern/wiktionary-scraper)", followRedirect: false, + links, } as const; export async function get( lemma: string, options: Partial = defaultScraperOptions, ): Promise { - const optionsFilled: ScraperOptions = { ...defaultScraperOptions, ...options }; + const optionsFilled: ScraperOptions = { + ...defaultScraperOptions, + ...options, + links: { ...defaultScraperOptions.links, ...options.links }, + }; let response; try { diff --git a/src/options.ts b/src/options.ts index 7e2c315..9afa408 100644 --- a/src/options.ts +++ b/src/options.ts @@ -1,3 +1,5 @@ +import type { Links } from "./constants/links"; + export type SiteLanguage = "en"; /** Defines the available options for getting a lemma from the dictionary. */ @@ -5,14 +7,14 @@ export interface ScraperOptions { /** * Specifies the language of the lemma. * - * @defaultValue `"English"` + * @defaultValue "English" */ lemmaLanguage: string; /** * Specifies the language of the website. * - * @defaultValue `"en"` + * @defaultValue "en" */ siteLanguage: SiteLanguage; @@ -21,12 +23,21 @@ export interface ScraperOptions { * * Set to {@link undefined} to omit the header. * - * @defaultValue `"wiktionary-scraper (github.com/vxern/wiktionary-scraper)"` + * @defaultValue "wiktionary-scraper (github.com/vxern/wiktionary-scraper)" */ userAgent: string | undefined; /** * Whether the scraper should follow redirects to similar terms if the term does not exist. + * + * @defaultValue false */ followRedirect: boolean; + + /** + * Specifies the links to query. + * + * @defaultValue links + */ + links: Links; } diff --git a/test/index.spec.ts b/test/index.spec.ts index 18a6501..71820a9 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -22,6 +22,7 @@ describe("The parser", () => { }); it("returns `undefined` for redirects when following redirects is disabled.", async () => { + // This would redirect to "Germany" otherwise. const results = await Wiktionary.get("germany"); expect(results).to.be.undefined;