url-convert
v0.1.1
Published
0 dependencies. Convert any URL on links, images, scripts, etc to absolute URLs in an HTML source.
Downloads
7
Readme
url-convert
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