Skip to content

Commit

Permalink
Merge pull request #3 from vxern/2-it-should-be-possible-to-override-…
Browse files Browse the repository at this point in the history
…the-url-being-queried

feat: Add the ability to specify a custom definition link.
  • Loading branch information
vxern authored Oct 23, 2024
2 parents ff108c3 + 7f196b2 commit 0a69086
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 11 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 14 additions & 1 deletion src/constants/links.ts
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ScraperOptions> = defaultScraperOptions,
): Promise<Entry[] | undefined> {
const optionsFilled: ScraperOptions = { ...defaultScraperOptions, ...options };
const optionsFilled: ScraperOptions = {
...defaultScraperOptions,
...options,
links: { ...defaultScraperOptions.links, ...options.links },
};

let response;
try {
Expand Down
17 changes: 14 additions & 3 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import type { Links } from "./constants/links";

export type SiteLanguage = "en";

/** Defines the available options for getting a lemma from the dictionary. */
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;

Expand All @@ -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;
}
1 change: 1 addition & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0a69086

Please sign in to comment.