url-convert

0.1.1 • Public • Published

url-convert

License: MIT

0 dependencies. Convert any URL on links, images, scripts, etc to absolute URLs in an HTML source.

Install

npm install url-convert
yarn add url-convert

Usage

import urlConvert from "url-convert";
 
const htmlString = `<div>
    <a href="/a"></a>
    <img src="/img" alt="" />
    <script src="/script"></script>
    <link rel="stylesheet" href="./link" />
  </div>
  `;
const baseUrl = "https://zenghongtu.com";
 
urlConvert({ htmlString, baseUrl });
/*`<div>
    <a href="https://zenghongtu.com/a"></a>
    <img src="https://zenghongtu.com/img" alt="">
    <script src="https://zenghongtu.com/script"></script>
    <link rel="stylesheet" href="https://zenghongtu.com/link">
  </div>
  `;
*/
import urlConvert from "url-convert";
 
const htmlString = `<div>
    <a href="/a"></a>
    <img src="/img" alt="" />
    <script src="/script"></script>
    <link rel="stylesheet" href="./link" />
  </div>
  `;
const baseUrl = "https://zenghongtu.com";
const handler = src => {
  return src + "?raw=true";
};
 
urlConvert({ htmlString, baseUrl, handler });
/*<div>
    <a href="https://zenghongtu.com/a?raw=true"></a>
    <img src="https://zenghongtu.com/img?raw=true" alt="">
    <script src="https://zenghongtu.com/script?raw=true"></script>
    <link rel="stylesheet" href="https://zenghongtu.com/link?raw=true">
  </div>
*/

License

MIT © zenghongtu

Readme

Keywords

Package Sidebar

Install

npm i url-convert

Weekly Downloads

2

Version

0.1.1

License

MIT

Unpacked Size

21.8 kB

Total Files

14

Last publish

Collaborators

  • zenghongtu