Skip to content
This repository has been archived by the owner on Jan 23, 2022. It is now read-only.

Commit

Permalink
fix(docs): base url
Browse files Browse the repository at this point in the history
  • Loading branch information
labbati committed Aug 10, 2015
1 parent 9b2c199 commit 5a31339
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 3 deletions.
115 changes: 115 additions & 0 deletions spec/urlSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
describe('url rewriting', function() {

var rUrl = /(\/?#!\/.*|\/(api|anothersection)\/?(\?.*)*|\/index[^\.]*\.html.*)$/;


it('should rewrite a destination \'index.html\'', function() {
expect(
'protocol://some/path/index.html'
.replace(rUrl, '__replacement__')
).toEqual(
'protocol://some/path__replacement__'
);
});


it('should rewrite a destination containing the word \'index[*].html\' in the file name', function() {
expect(
'protocol://some/path/index-toberemoved.html'
.replace(rUrl, '__replacement__')
).toEqual(
'protocol://some/path__replacement__'
);
});


it('should rewrite a destination containing the word \'index[*].html\' in the file name event if a query string is provided', function() {
expect(
'protocol://some/path/index-toberemoved.html?p1=v1&p2=v2'
.replace(rUrl, '__replacement__')
).toEqual(
'protocol://some/path__replacement__'
);
});


it('should rewrite a destination ending on a path named \'api\'', function() {
expect(
'protocol://some/path/api'
.replace(rUrl, '__replacement__')
).toEqual(
'protocol://some/path__replacement__'
);
});


it('should rewrite a destination ending on a path named \'api\' with a trailing slash', function() {
expect(
'protocol://some/path/api/'
.replace(rUrl, '__replacement__')
).toEqual(
'protocol://some/path__replacement__'
);
});


it('should rewrite a destination ending on a path named with alternative section', function() {
expect(
'protocol://some/path/anothersection/'
.replace(rUrl, '__replacement__')
).toEqual(
'protocol://some/path__replacement__'
);
});


it('should rewrite a destination ending on a path named \'api\' even if the url has a query string provided', function() {
expect(
'protocol://some/path/api?p1=v1&p2=v2'
.replace(rUrl, '__replacement__')
).toEqual(
'protocol://some/path__replacement__'
);
});


it('should NOT rewrite a destination only because contain the word \'api\'', function() {
expect(
'protocol://some/path/path-containing-word-api?p1=v1&p2=v2'
.replace(rUrl, '__replacement__')
).toEqual(
'protocol://some/path/path-containing-word-api?p1=v1&p2=v2'
);
});


it('should NOT rewrite a destination only because starts with the word \'api\'', function() {
expect(
'protocol://some/path/api-path-containing?p1=v1&p2=v2'
.replace(rUrl, '__replacement__')
).toEqual(
'protocol://some/path/api-path-containing?p1=v1&p2=v2'
);
});


it('should rewrite a destination pointing to the root fragment if the path does not end with a trailing slash', function() {
expect(
'protocol://some/path#!/'
.replace(rUrl, '__replacement__')
).toEqual(
'protocol://some/path__replacement__'
);
});


it('should rewrite a destination pointing to the root fragment if the path ends with a trailing slash', function() {
expect(
'protocol://some/path/#!/'
.replace(rUrl, '__replacement__')
).toEqual(
'protocol://some/path__replacement__'
);
});

});
6 changes: 3 additions & 3 deletions src/templates/index.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
// before the base attribute is added, causing 404 and terribly slow loading of the docs app.
(function() {
var indexFile = (location.pathname.match(/\/(index[^\.]*\.html)/) || ['', ''])[1],
origin, baseUrl, rUrl = /(#!\/|\/<%= sections %>\/|index[^\.]*\.html).*$/,
origin, baseUrl, rUrl = /(\/?#!\/.*|\/(<%= sections %>)\/?(\?.*)*|\/index[^\.]*\.html.*)$/,
headEl = document.getElementsByTagName('head')[0],
sync = true;

if (location.href.slice(0, 7) == 'file://') {
baseUrl = location.href.replace(rUrl, indexFile);
baseUrl = location.href.replace(rUrl, '/' + indexFile);
} else {
origin = location.origin || (window.location.protocol + "//" + window.location.hostname +
(window.location.port ? ':' + window.location.port: ''));
baseUrl = origin + location.href.substr(origin.length).replace(rUrl, indexFile);
baseUrl = origin + location.href.substr(origin.length).replace(rUrl, '/' + indexFile);
}

addTag('base', {href: baseUrl});
Expand Down

0 comments on commit 5a31339

Please sign in to comment.