Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jan 2, 2019
1 parent 7a8f0d8 commit f3f5487
Show file tree
Hide file tree
Showing 12 changed files with 659 additions and 207 deletions.
41 changes: 19 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[![NPM Status](https://img.shields.io/npm/dm/css-urls.svg?style=flat-square)](https://www.npmjs.org/package/css-urls)
[![Donate](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square)](https://paypal.me/Kikobeats)

> Get all URLs referenced from stylesheet files
> Get all URLs inside stylesheets
## Install

Expand All @@ -18,41 +18,38 @@ $ npm install css-urls --save

## Usage

### From single URL

```js
(async () => {
const { urls, meta } = await cssUrls('https://kikobeats.com/styles.css')
})()
```

### From a collection of URLs

```js
(async () => {
const { urls, meta } = await cssUrls([
'https://elenatorro.github.io/build/assets/style.css',
'https://kikobeats.com/styles.css'
])
const got = require('got')
const htmlUrls = require('html-urls')

;(async () => {
const url = process.argv[2]
if (!url) throw new TypeError('Need to provide an url as first argument.')
const { body } = await got(url)
const links = cssUrls({ text: body, html })
links.forEach(({ url, normalizedUrl }) => console.log(normalizedUrl))
})()
```

## API

### cssUrls(url, [options])
### cssUrls({url, text})

#### url

*Required*<br>
Type: `string`|`array`
Type: `string`

The target URL(s) for extracting urls referenced
The target URL(s) for extracting URLs referenced.

#### options
#### text

*Required*<br>
Type: `string`

Type: `object`
The

Use it for providing [html-get#options](https://github.com/Kikobeats/html-get#options).
The target URL(s) for extracting URLs referenced.

## Related

Expand Down
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "css-urls",
"description": "et all URLs referenced from stylesheet files",
"description": "Get all URLs inside stylesheets",
"homepage": "https://documentup.com/Kikobeats/css-urls",
"version": "1.0.0",
"main": "src/index.js",
Expand Down Expand Up @@ -29,10 +29,8 @@
"urls"
],
"dependencies": {
"@metascraper/helpers": "~3.11.8",
"aigle": "~1.12.0",
"css-url-parser": "~1.1.3",
"html-get": "~1.0.7"
"@metascraper/helpers": "~4.8.5",
"css-url-parser": "~1.1.3"
},
"devDependencies": {
"ava": "latest",
Expand All @@ -44,7 +42,6 @@
"lint-staged": "latest",
"nyc": "latest",
"prettier-standard": "latest",
"puppeteer": "latest",
"standard": "latest",
"standard-markdown": "latest"
},
Expand Down
58 changes: 5 additions & 53 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,12 @@
'use strict'

const { normalizeUrl } = require('@metascraper/helpers')
const parseCssUrls = require('css-url-parser')
const getHTML = require('html-get')
const { URL } = require('url')
const aigle = require('aigle')
const path = require('path')

const { getUrl } = require('@metascraper/helpers')

const REGEX_URL_CSS = /^\.css$/i

const isCss = str => REGEX_URL_CSS.test(path.extname(str) || str)

const fromHTML = (url, html) => {
module.exports = ({ text, url }) => {
const { origin: baseUrl } = new URL(url)
const originalUrls = parseCssUrls(html)
const urls = originalUrls.map(url => getUrl(baseUrl, url))
return { originalUrls, urls }
}

const cssUrls = async (url, opts) => {
const { html } = await getHTML(url, opts)
return fromHTML(url, html)
}

module.exports = async (urls, opts) => {
const collection = [].concat(urls)

const iterator = async (acc, url) => {
if (!isCss(url)) return acc
const { urls, originalUrls } = await cssUrls(url, opts)
acc.urls = new Set(...acc.urls, urls)
if (urls.length > 0) {
acc.meta[url] = urls.map((url, index) => ({
url,
originalUrl: originalUrls[index]
}))
}
return acc
}

const data = await aigle.reduce(collection, iterator, {
urls: new Set(),
meta: {}
})
return { urls: Array.from(data.urls), meta: data.meta }
}

module.exports.isCss = isCss

module.exports.html = (url, html) => {
const { urls, originalUrls } = fromHTML(url, html)
let buffer = html
urls.forEach((url, index) => {
const regex = new RegExp(originalUrls[index], 'gi')
buffer = buffer.replace(regex, url)
})
return { html: buffer, urls, originalUrls }
const urls = parseCssUrls(text)
const normalizedUrl = urls.map(url => normalizeUrl(baseUrl, url))
return { urls, normalizedUrl }
}
Loading

0 comments on commit f3f5487

Please sign in to comment.