Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

get-hrefs

joakimbeng7.3kMIT4.0.0

Get all href urls from an HTML string

html, find, extract, links, urls, href, hrefs, get, string

readme

get-hrefs

Build status NPM version XO code style

Get all href urls from an HTML string

Installation

Install get-hrefs using npm:

npm install --save get-hrefs

Usage

Module usage

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

$> 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 hrefs resolved from any provided baseUrl and <base href="..."> in the HTML document.

options.baseUrl

Type: String
Default: ""

The baseUrl to use for relative hrefs. The module also takes <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. See its options for alternatives.

License

MIT © Joakim Carlstein

changelog

Change Log

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

4.0.0 (2019-03-14)

Features

  • add allowedProtocols option (7fb4cd7)

BREAKING CHANGES

  • by default only URLs with "http" or "https" protocols will be returned, to enable more e.g. "ftp" use the new "allowedProtocols" option.

3.0.0 (2019-03-06)

Bug Fixes

  • url: don't use the global URL constructor just yet (e5e984e)

Features

  • normalize: normalize relative urls without base url (d45c71b), closes #4

BREAKING CHANGES

  • normalize: get-hrefs is rewritten using the latest JavaScript syntax which is not supported in NodeJS versions below 8.6.0. Also get-hrefs does not strip www by default anymore, to get the old behaviour the "stripWWW" option must be set

2.0.0 (2017-09-14)

Features

  • options: pass options to normalize-url (fixes #2) (72ac455)

BREAKING CHANGES

  • options: use ES2015 syntax