# get-hrefs

> Get all href urls from an HTML string

Latest version **4.0.0** (published 2019-03-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install get-hrefs
pnpm add get-hrefs
yarn add get-hrefs
bun add get-hrefs
```

Provides the command `get-hrefs`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2019-03-14 |
| First published | 2016-01-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.6.0 |
| Dependencies | 4 |
| Unpacked size | 14.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Joakim Carlstein |
| Maintainers | joakimbeng |
| Keywords | html, find, extract, links, urls, href, hrefs, get, string |

## Links

- npm: https://www.npmjs.com/package/get-hrefs
- Repository: https://github.com/joakimbeng/get-hrefs
- Homepage: https://github.com/joakimbeng/get-hrefs#readme
- Issues: https://github.com/joakimbeng/get-hrefs/issues
- npm.io page: https://npm.io/package/get-hrefs

## Dependencies (4)

- [meow](https://npm.io/package/meow.md) ^5.0.0
- [cheerio](https://npm.io/package/cheerio.md) ^1.0.0-rc.2
- [get-stdin](https://npm.io/package/get-stdin.md) ^6.0.0
- [normalize-url](https://npm.io/package/normalize-url.md) ^4.2.0

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 4.0.0 (latest) — 2019-03-14
- 3.0.0 — 2019-03-06
- 2.0.0 — 2017-09-14
- 1.0.3 — 2016-04-04
- 1.0.2 — 2016-01-07
- 1.0.1 — 2016-01-05
- 1.0.0 — 2016-01-05

## README

# get-hrefs

[![Build status][travis-image]][travis-url] [![NPM version][npm-image]][npm-url] [![XO code style][codestyle-image]][codestyle-url]

> Get all href urls from an HTML string

## Installation

Install `get-hrefs` using [npm](https://www.npmjs.com/):

```bash
npm install --save get-hrefs
```

## Usage

### Module usage

```javascript
const getHrefs = require('get-hrefs');

getHrefs(`
	<body>
		<a href="http://example.com">Example</a>
	</body>
`);
// ["http://example.com"]

getHrefs(`
	<head>
		<base href="http://example.com/path1/">
	</head>
	<body>
		<a href="path2/index.html">Example</a>
	</body>
`);
// ["http://example.com/path1/path2/index.html"]
```

### CLI usage

```bash
$> get-hrefs --help

Get all href urls from an HTML string

  Usage:
    get-hrefs <html file>
    cat <html file> | get-hrefs

  Options:
    -b, --base-url	Set baseUrl
    <all other flags are passed to normalize-url>

  Examples:
    curl -s example.com | get-hrefs
    echo '<a href="http://www.example.com">Link</a>' | get-hrefs --strip-w-w-w
```

## API

### `getHrefs(html, [options])`

| Name    | Type     | Description                           |
| ------- | -------- | ------------------------------------- |
| html    | `String` | The HTML string to extract hrefs from |
| options | `Object` | Optional options                      |

Returns: `Array<String>`, all unique and [normalized](https://github.com/sindresorhus/normalize-url) hrefs resolved from any provided [`baseUrl`](#optionsbaseurl) and [`<base href="...">`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base) in the HTML document.

#### options.baseUrl

Type: `String`  
Default: `""`

The baseUrl to use for relative hrefs. The module also takes [`<base ...>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base) tags into account.

#### options.allowedProtocols

Type: `Object`  
Default: `{"http": true, "https": true}`

Specifies which protocols to allow by setting their respective key (the protocol name without ":") in `allowedProtocols` to `true` (or to `false` to disable one of the defaults), e.g. `allowedProtocols: {tel: true, http: false}` will return only found URLs with the protocols `tel:` or `https:`.

#### options.`<any>`

All other options are passed to [`normalize-url`](https://github.com/sindresorhus/normalize-url). See its [options](https://github.com/sindresorhus/normalize-url#options) for alternatives.

## Related modules

- [get-urls](https://github.com/sindresorhus/get-urls) - Get all urls in a string

## License

MIT © [Joakim Carlstein](http://joakim.beng.se/)

[npm-url]: https://npmjs.org/package/get-hrefs
[npm-image]: https://badge.fury.io/js/get-hrefs.svg
[travis-url]: https://travis-ci.org/joakimbeng/get-hrefs
[travis-image]: https://travis-ci.org/joakimbeng/get-hrefs.svg?branch=master
[codestyle-url]: https://github.com/sindresorhus/xo
[codestyle-image]: https://img.shields.io/badge/code_style-XO-5ed9c7.svg?style=flat

---
_Source: https://npm.io/package/get-hrefs · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
