Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
markdalgleish committed Mar 14, 2017
1 parent bfa4173 commit 0fda156
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var RawSource = require('webpack-sources/lib/RawSource');
var evaluate = require('eval');
var path = require('path');
var cheerio = require('cheerio');
var url = require('url');
var Promise = require('bluebird');

function StaticSiteGeneratorWebpackPlugin(options) {
Expand Down Expand Up @@ -91,7 +92,10 @@ function renderPaths(crawl, userLocals, paths, render, assets, webpackStats, com
compilation.assets[assetName] = new RawSource(rawSource);

if (crawl) {
var relativePaths = relativePathsFromHtml(rawSource);
var relativePaths = relativePathsFromHtml({
source: rawSource,
path: key
});

return renderPaths(crawl, userLocals, relativePaths, render, assets, webpackStats, compilation);
}
Expand Down Expand Up @@ -170,16 +174,34 @@ function makeObject(key, value) {
return obj;
}

function relativePathsFromHtml(html) {
function relativePathsFromHtml(options) {
var html = options.source;
var currentPath = options.path;

var $ = cheerio.load(html);

return $('a[href^="/"]')
.map(function(i, el) {
return $(el).attr('href');
})
.get()
.filter(function(url) {
return url.indexOf('//') === -1;
.map(function(href) {
if (href.indexOf('//') === 0) {
return null
}

var parsed = url.parse(href);

if (parsed.protocol) {
return null;
}

return parsed.path.indexOf('/') === 0 ?
parsed.path :
url.resolve(currentPath, href);
})
.filter(function(href) {
return href != null;
});
}

Expand Down

0 comments on commit 0fda156

Please sign in to comment.