Skip to content

Commit 1b901a6

Browse files
committed
Merge branch 'scottnath-patterns-url' into dev
2 parents 8d52aa9 + 977983c commit 1b901a6

File tree

7 files changed

+48
-9
lines changed

7 files changed

+48
-9
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ As you can see, it's a much easier way of linking patterns to one another.
148148

149149
Coupled with exported css (much easier to extract with existing tools like [grunt-contrib-copy](https://github.com/gruntjs/grunt-contrib-copy)), pattern export can help to maintain the relevancy of the design system by directly placing partials in a directory of your choosing.
150150

151+
##### baseurl
152+
153+
If your instance of Pattern Lab lives in a subdirectory of your server, for instance on github pages (ex: yourusername.github.io/patterns-demo/), then add the baseurl here. The baseurl is everything after the hostname - ie: `patterns-demo`
154+
155+
```
156+
"baseurl" : "/patterns-demo"
157+
```
158+
159+
Default: blank
151160

152161
##### Verbose Mode
153162
`patternlab.json` is a file created for debugging purposes. Set `debug` to true in `.config.json` to see all the secrets.

builder/patternlab.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ var entity_encoder = new he();
174174
pattern.patternPartial = renderPattern(pattern.template, patternlab.data, patternlab.partials);
175175
}
176176

177+
// check for baseurl in config.json
178+
if(patternlab.config.baseurl){
179+
pattern.baseurl = patternlab.config.baseurl;
180+
}
181+
177182
//add footer info before writing
178183
var patternFooter = renderPattern(patternlab.footer, pattern);
179184

@@ -199,13 +204,18 @@ var entity_encoder = new he();
199204
patternlab.patternPaths = {};
200205
patternlab.viewAllPaths = {};
201206

207+
var baseurl;
208+
// check for baseurl in config.json
209+
if(patternlab.config.baseurl){
210+
baseurl = patternlab.config.baseurl;
211+
}
202212
//find mediaQueries
203213
var media_hunter = new mh();
204214
media_hunter.find_media_queries(patternlab);
205215

206216
//build the styleguide
207217
var styleguideTemplate = fs.readFileSync('./source/_patternlab-files/styleguide.mustache', 'utf8');
208-
var styleguideHtml = renderPattern(styleguideTemplate, {partials: patternlab.patterns});
218+
var styleguideHtml = renderPattern(styleguideTemplate, {partials: patternlab.patterns, baseurl: baseurl});
209219
fs.outputFileSync('./public/styleguide/html/styleguide.html', styleguideHtml);
210220

211221
//build the viewall pages
@@ -230,7 +240,7 @@ var entity_encoder = new he();
230240
}
231241

232242
var viewAllTemplate = fs.readFileSync('./source/_patternlab-files/viewall.mustache', 'utf8');
233-
var viewAllHtml = renderPattern(viewAllTemplate, {partials: viewAllPatterns, patternPartial: patternPartial});
243+
var viewAllHtml = renderPattern(viewAllTemplate, {partials: viewAllPatterns, patternPartial: patternPartial, baseurl: baseurl});
234244
fs.outputFileSync('./public/patterns/' + pattern.flatPatternPath + '/index.html', viewAllHtml);
235245
}
236246
}

config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@
3232
"homepage-emergency" : "inprogress"
3333
},
3434
"patternExportKeys": [],
35-
"patternExportDirectory": "./pattern_exports/"
35+
"patternExportDirectory": "./pattern_exports/",
36+
"baseurl" : ""
3637
}

public/styleguide/js/postmessage.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,26 @@ function receiveIframeMessage(event) {
8686
if (data.path !== undefined) {
8787

8888
if (patternPartial !== "") {
89-
89+
90+
if(baseurl){
91+
// create main site url
92+
var siteurl = window.location.protocol+"//"+window.location.host;
93+
// add a trailing slash if it doesn't exist
94+
siteurl = siteurl.replace(/\/?$/, '/');
95+
// remove a begining slash for baseurl if it exists
96+
baseurl = baseurl.replace(/^\//, '');
97+
// connect baseurl to siteurl
98+
siteurl = siteurl + baseurl;
99+
// add a trailing slash if it doesn't exist
100+
siteurl = siteurl.replace(/\/?$/, '/');
101+
// build our final path
102+
path = siteurl+data.path+'?'+Date.now();
103+
} else {
90104
// handle patterns and the view all page
91-
var re = /patterns\/(.*)$/;
92-
path = window.location.protocol+"//"+window.location.host+window.location.pathname.replace(re,'')+data.path+'?'+Date.now();
105+
var re = /patterns\/(.*)$/;
106+
path = window.location.protocol+"//"+window.location.host+window.location.pathname.replace(re,'')+data.path+'?'+Date.now();
107+
}
108+
93109
window.location.replace(path);
94110

95111
} else {

source/_patternlab-files/pattern-header-footer/footer.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
var patternPartial = "{{ patternGroup }}-{{ patternName }}";
1212
var lineage = [{{{ lineage }}}];
1313
var lineageR = [{{{ lineageR }}}];
14-
var patternState = "{{patternState}}"
14+
var patternState = "{{patternState}}";
15+
var baseurl = "{{{baseurl}}}";
1516
var cssEnabled = false; //TODO
1617
</script>
1718

source/_patternlab-files/styleguide.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@
5353

5454
<!-- JS to hook everything together and do annotations -->
5555
<script>
56-
// handle injection of items from PHP
56+
// handle injection of items from patternlab.js
5757
var patternPartial = "";
5858
var lineage = "";
59+
var baseurl = "{{{baseurl}}}";
5960
</script>
6061
<script src="../../styleguide/js/vendor/jwerty.js"></script>
6162
<script src="../../styleguide/js/vendor/prism.js"></script>

source/_patternlab-files/viewall.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@
5353

5454
<!-- JS to hook everything together and do annotations -->
5555
<script>
56-
// handle injection of items from PHP
56+
// handle injection of items from patternlab.js
5757
var patternPartial = "{{ patternPartial }}";
5858
var lineage = "";
59+
var baseurl = "{{{baseurl}}}";
5960
</script>
6061
<script src="../../styleguide/js/vendor/jwerty.js"></script>
6162
<script src="../../styleguide/js/postmessage.js"></script>

0 commit comments

Comments
 (0)