{"id":50531637,"url":"https://github.com/aripalo/rehype-external-link-title","last_synced_at":"2026-06-03T14:02:22.538Z","repository":{"id":355126125,"uuid":"1215769591","full_name":"aripalo/rehype-external-link-title","owner":"aripalo","description":"rehype-external-link-title","archived":false,"fork":false,"pushed_at":"2026-04-20T18:12:16.000Z","size":112,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-02T02:15:51.658Z","etag":null,"topics":["external-links","hast","html","link-title","metadata","rehype","rehype-plugin","title","tooltip","unified"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aripalo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-04-20T08:34:11.000Z","updated_at":"2026-04-20T18:05:49.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/aripalo/rehype-external-link-title","commit_stats":null,"previous_names":["aripalo/rehype-external-link-title"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/aripalo/rehype-external-link-title","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aripalo%2Frehype-external-link-title","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aripalo%2Frehype-external-link-title/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aripalo%2Frehype-external-link-title/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aripalo%2Frehype-external-link-title/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aripalo","download_url":"https://codeload.github.com/aripalo/rehype-external-link-title/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aripalo%2Frehype-external-link-title/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33867802,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-03T02:00:06.370Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["external-links","hast","html","link-title","metadata","rehype","rehype-plugin","title","tooltip","unified"],"created_at":"2026-06-03T14:02:20.971Z","updated_at":"2026-06-03T14:02:22.529Z","avatar_url":"https://github.com/aripalo.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# rehype-external-link-title\n\n[**rehype**][rehype] plugin that fetches the page `\u003ctitle\u003e` of every external\nlink in your HTML and writes it to the link's `title` attribute (so users see\nthe destination's real name when they hover the link), with a pluggable\ncaching layer.\n\n## Contents\n\n- [What is this?](#what-is-this)\n- [When should I use this?](#when-should-i-use-this)\n- [Install](#install)\n- [Use](#use)\n- [API](#api)\n  - [`unified().use(rehypeExternalLinkTitle[, options])`](#unifieduserehypeexternallinktitle-options)\n  - [`Options`](#options)\n  - [`Cache`](#cache)\n  - [Built-in caches](#built-in-caches)\n- [Examples](#examples)\n- [Types](#types)\n- [Compatibility](#compatibility)\n- [Security](#security)\n- [License](#license)\n\n## What is this?\n\nThis is a [unified][] ([rehype][]) plugin. It walks the [hast][] tree, finds\nexternal `\u003ca\u003e` elements (by default: anchors whose `href` starts with `http://`\nor `https://`), fetches each unique URL, parses the `\u003ctitle\u003e` element from the\nresponse, and stores it on the anchor as a `title` attribute.\n\nTo avoid hammering remote servers (and to keep your build times reasonable),\nresults are persisted to a cache. The cache is **pluggable**: a default\n[lowdb][]-backed JSON file is provided out of the box, and you can swap in\nyour own backend (Redis, KV, in-memory, etc.) by implementing a tiny\ntwo-method interface.\n\n## When should I use this?\n\nUse this plugin if you publish content with many external references — blog\nposts, link round-ups, documentation — and you want hover tooltips to display\nthe actual page title rather than the raw URL.\n\nYou probably **shouldn't** use it if:\n\n- Your build runs in a sandbox without outbound network access.\n- You don't trust the remote pages and don't want to render their titles\n  (consider [`rehype-sanitize`][rehype-sanitize] downstream regardless — see\n  [Security](#security)).\n- Build performance is more important than hover-over UX (the first build\n  fetches every link; subsequent builds are cache hits).\n\n## Install\n\nThis package is [ESM only][esm]. In Node.js (version 18+):\n\n```sh\nnpm install rehype-external-link-title\n```\n\n```sh\npnpm add rehype-external-link-title\n```\n\n## Use\n\nSay we have the following input HTML:\n\n```html\n\u003cp\u003eRead more on \u003ca href=\"https://example.com\"\u003eexample.com\u003c/a\u003e.\u003c/p\u003e\n```\n\n…and a script `example.js`:\n\n```js\nimport {unified} from 'unified'\nimport rehypeParse from 'rehype-parse'\nimport rehypeStringify from 'rehype-stringify'\nimport rehypeExternalLinkTitle from 'rehype-external-link-title'\n\nconst file = await unified()\n  .use(rehypeParse, {fragment: true})\n  .use(rehypeExternalLinkTitle)\n  .use(rehypeStringify)\n  .process('\u003cp\u003e\u003ca href=\"https://example.com\"\u003eexample.com\u003c/a\u003e\u003c/p\u003e')\n\nconsole.log(String(file))\n```\n\n…running `node example.js` yields (assuming the page's title is `Example Domain`):\n\n```html\n\u003cp\u003e\u003ca href=\"https://example.com\" title=\"Example Domain\" data-title-updated-at=\"2026-04-19T00:00:00.000Z\"\u003eexample.com\u003c/a\u003e\u003c/p\u003e\n```\n\n## API\n\nThis package exports the named identifiers `lowdbCache`, `memoryCache`, and the\nTypeScript types `Cache`, `CacheEntry`, `FetchOptions`, `LinkPredicate`, and\n`Options`. The default export is `rehypeExternalLinkTitle`.\n\n### `unified().use(rehypeExternalLinkTitle[, options])`\n\nAdds page titles to external `\u003ca\u003e` elements as `title` attributes, with caching.\n\n###### Parameters\n\n- `options` ([`Options`](#options), optional) — configuration\n\n###### Returns\n\nAsync transform.\n\n### `Options`\n\nConfiguration (TypeScript type).\n\n| Field              | Type                                  | Default                              | Description                                                                                                                                            |\n| ------------------ | ------------------------------------- | ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------ |\n| `cache`            | `string \\| Cache`                     | built-in `lowdbCache()`              | `undefined`: lowdb at `./db.titles.json`. `string`: lowdb at the given path. `Cache`: your own implementation.                                          |\n| `ttl`              | `number`                              | `Infinity`                           | TTL for **successful** entries, in ms.                                                                                                                 |\n| `failureTtl`       | `number`                              | `86_400_000` (24 h)                  | TTL for **failed** entries (`title === null`), in ms. Use `0` to never cache failures, `Infinity` to cache forever.                                    |\n| `test`             | `(href, node) =\u003e boolean`             | `http(s)://...`                      | Predicate deciding which `\u003ca\u003e` elements to process.                                                                                                    |\n| `attribute`        | `string`                              | `'title'`                            | Attribute name written on the link element.                                                                                                            |\n| `includeUpdatedAt` | `boolean`                             | `true`                               | Whether to also write `data-title-updated-at` (ISO timestamp).                                                                                         |\n| `concurrency`      | `number`                              | `8`                                  | Maximum concurrent outbound fetches per transformer invocation.                                                                                        |\n| `fetch`            | `FetchOptions`                        | see below                            | Options forwarded to the internal HTTP client (`timeout`, `userAgent`, `signal`).                                                                      |\n\n### `Cache`\n\nThe plugin treats the cache as a dumb async key/value store. TTL/staleness\nhandling is performed by the plugin itself, so cache implementations stay\ntrivial:\n\n```ts\nexport interface CacheEntry {\n  title: string | null  // `null` = \"we tried and got nothing\"\n  updatedAt: string     // ISO-8601\n}\n\nexport interface Cache {\n  get(url: string): Promise\u003cCacheEntry | undefined\u003e | CacheEntry | undefined\n  set(url: string, entry: CacheEntry): Promise\u003cvoid\u003e | void\n  delete?(url: string): Promise\u003cvoid\u003e | void  // optional\n}\n```\n\nBoth sync and async return values are supported, so a `Map`-backed cache or a\nRedis-backed cache are equally easy to write.\n\n### Built-in caches\n\n```ts\nimport {lowdbCache, memoryCache} from 'rehype-external-link-title/cache'\n\nconst persistent = lowdbCache({path: '.cache/titles.json'})\nconst ephemeral  = memoryCache()\n```\n\n- **`lowdbCache(options?: {path?: string})`** — JSON file backed by [lowdb][].\n  The file is opened lazily on first use (no top-level I/O).\n- **`memoryCache()`** — `Map`-backed; useful for tests or short-lived processes.\n\n## Examples\n\n### Custom cache path\n\n```ts\nunified().use(rehypeExternalLinkTitle, {cache: '.cache/external-link-titles.json'})\n```\n\n### Refetch every entry older than a week\n\n```ts\nunified().use(rehypeExternalLinkTitle, {ttl: 7 * 24 * 60 * 60 * 1000})\n```\n\n### Bring your own cache (Redis-style)\n\n```ts\nimport type {Cache, CacheEntry} from 'rehype-external-link-title'\n\nconst redisCache: Cache = {\n  async get(url) {\n    const raw = await redis.get(`title:${url}`)\n    return raw ? (JSON.parse(raw) as CacheEntry) : undefined\n  },\n  async set(url, entry) {\n    await redis.set(`title:${url}`, JSON.stringify(entry))\n  },\n  async delete(url) {\n    await redis.del(`title:${url}`)\n  },\n}\n\nunified().use(rehypeExternalLinkTitle, {cache: redisCache})\n```\n\n### Custom User-Agent\n\n```ts\nunified().use(rehypeExternalLinkTitle, {\n  fetch: {userAgent: 'MyCoolBlog/1.0 (+https://example.com/about)'},\n})\n```\n\n### Process only a subset of links\n\n```ts\nunified().use(rehypeExternalLinkTitle, {\n  test: (href) =\u003e href.startsWith('https://en.wikipedia.org/'),\n})\n```\n\n## Types\n\nThis package is fully typed with [TypeScript][]. It exports the additional\ntypes `Options`, `Cache`, `CacheEntry`, `FetchOptions`, and `LinkPredicate`.\n\n## Compatibility\n\nCompatible with maintained versions of Node.js (\u003e=18). Works with `unified`\nversion 11+.\n\n## Security\n\nThis plugin sets the `title` attribute on `\u003ca\u003e` elements based on data fetched\nfrom third-party servers. While `title` is generally not an XSS vector\n(browsers do not interpret it as HTML), you should still pair this plugin with\n[`rehype-sanitize`][rehype-sanitize] downstream, configured to allow the\n`title` attribute on anchors:\n\n```ts\nimport rehypeSanitize, {defaultSchema} from 'rehype-sanitize'\n\nunified()\n  .use(rehypeExternalLinkTitle)\n  .use(rehypeSanitize, {\n    ...defaultSchema,\n    attributes: {\n      ...defaultSchema.attributes,\n      a: [...(defaultSchema.attributes?.a ?? []), 'title'],\n    },\n  })\n```\n\nThe HTML returned by remote servers is sanitized internally with [DOMPurify][]\n(stripped down to `\u003chtml\u003e`/`\u003chead\u003e`/`\u003ctitle\u003e` only) before the title is\nextracted, so malicious script tags in the source page are discarded before\nparsing.\n\n## License\n\n[MIT][license] © [Ari Palo][author]\n\n[unified]: https://github.com/unifiedjs/unified\n[rehype]: https://github.com/rehypejs/rehype\n[hast]: https://github.com/syntax-tree/hast\n[rehype-sanitize]: https://github.com/rehypejs/rehype-sanitize\n[lowdb]: https://github.com/typicode/lowdb\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n[typescript]: https://www.typescriptlang.org\n[dompurify]: https://github.com/cure53/DOMPurify\n[license]: ./LICENSE\n[author]: https://aripalo.technology\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faripalo%2Frehype-external-link-title","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faripalo%2Frehype-external-link-title","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faripalo%2Frehype-external-link-title/lists"}