npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

posthtml-base-url

v3.1.5

Published

PostHTML plugin for prepending a base string to attribute values.

Downloads

37,851

Readme

Version Build License Downloads

Introduction

This PostHTML plugin can prepend a string to various HTML attribute values and CSS property values.

Input:

<img src="test.jpg">

Output:

<img src="https://example.com/test.jpg">

Works on the following attributes:

  • src=""
  • href=""
  • srcset=""
  • poster=""
  • background=""

... and the following CSS properties:

  • background: url()
  • background-image: url()
  • @font-face { src: url() }

Both <style> tags and style="" attributes are supported.

CSS property values with multiple url() sources are supported as well.

Install

npm i posthtml posthtml-base-url

Usage

import posthtml from 'posthtml'
import baseUrl from 'posthtml-base-url'

posthtml([
  baseUrl({
    url: 'https://example.com', 
    tags: ['img']
  })
])
  .process('<img src="test.jpg">')
  .then(result => console.log(result.html))

Result:

<img src="https://example.com/test.jpg">

Absolute URLs

If the target attribute value is an URL, the plugin will not modify it.

If the prefix string to prepend to the target attribute value is an URL, the two strings will be concatenated.

Relative paths

If both the prefix and the attribute value are relative paths, the plugin will intelligently join the paths instead of simply concatenating them.

Options

You can configure what to prepend to which attribute values.

url

Type: string
Default: ''

The string to prepend to the attribute value.

allTags

Type: boolean
Default: false

The plugin is opt-in, meaning that by default it doesn't affect any tag.

When you set allTags to true, the plugin will prepend your url to all attribute values in all the tags that it supports.

styleTag

Type: boolean
Default: false

When set to true, the plugin will prepend your url to all url() sources in <style> tags.

inlineCss

Type: boolean
Default: false

When set to true, the plugin will prepend your url to all url() sources in style="" attributes.

tags

Type: array|object
Default: defaultTags (object)

Define a list of tags and their attributes to handle.

When using the tags option, the plugin will only handle those tags.

Array tags

To replace all known attributes for a list of tags, use the array format:

posthtml([
  baseUrl({
    url: 'https://example.com',
    tags: ['img', 'script'],
  })
])
  .process(
    `<a href="foo/bar.html">
      <img src="img.jpg" srcset="img-HD.jpg 2x,img-xs.jpg 100w">
    </a>
    
    <script src="javascript.js"></script>`
  )
  .then(result => console.log(result.html))

Result:

<a href="foo/bar.html">
  <img src="https://example.com/img.jpg" srcset="https://example.com/img-HD.jpg 2x, https://example.com/img-xs.jpg 100w">
</a>

<script src="https://example.com/javascript.js"></script>

Object tags

You may use an object for granular control over how specific attributes should be handled:

posthtml([
  baseUrl({
    url: 'https://foo.com/',
    tags: {
      img: {
        src: true,
        srcset: 'https://bar.com/',
      },
    },
  })
])
  .process(
    `<a href="foo/bar.html">
      <img src="img.jpg" srcset="img-HD.jpg 2x, img-xs.jpg 100w">
    </a>`
  )
  .then(result => console.log(result.html))

Result:

<a href="foo/bar.html">
  <img src="https://foo.com/img.jpg" srcset="https://bar.com/img-HD.jpg 2x, https://bar.com/img-xs.jpg 100w">
</a>

You may set the value of an attribute to true and the plugin will use the url option value - we did that above for the src attribute.

attributes

Type: object
Default: {}

Key-value pairs of attributes and what to prepend to them.

Example:

posthtml([
  baseUrl({
    attributes: {
      'data-url': 'https://example.com/',
    }
  })
])
  .process('<div data-url="foo/bar.html"></div>')
  .then(result => console.log(result.html))

Result:

<div data-url="https://example.com/foo/bar.html"></div>