Skip to content

Commit

Permalink
Fix base_url in error pages. (mkdocs#1599)
Browse files Browse the repository at this point in the history
Closes mkdocs#1598.
  • Loading branch information
waylan authored Aug 21, 2018
1 parent da3a252 commit 1a5c4a7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
8 changes: 5 additions & 3 deletions mkdocs/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def get_context(nav, files, config, page=None, base_url=''):
'nav': nav,
'pages': files.documentation_pages(),

'base_url': base_url.rstrip('/'),
'base_url': base_url,

'extra_css': extra_css,
'extra_javascript': extra_javascript,
Expand All @@ -78,8 +78,10 @@ def _build_template(name, template, files, config, nav):
if utils.is_error_template(name):
# Force absolute URLs in the nav of error pages and account for the
# possability that the docs root might be different than the server root.
# See https://github.com/mkdocs/mkdocs/issues/77
base_url = utils.urlparse(config['site_url']).path
# See https://github.com/mkdocs/mkdocs/issues/77.
# However, if site_url is not set, assume the docs root and server root
# are the same. See https://github.com/mkdocs/mkdocs/issues/1598.
base_url = utils.urlparse(config['site_url'] or '/').path
else:
base_url = utils.get_relative_url('.', name)

Expand Down
18 changes: 15 additions & 3 deletions mkdocs/contrib/search/templates/search/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@ function getSearchTermFromLocation() {
}
}

function joinUrl (base, path) {
if (path.substring(0, 1) === "/") {
// path starts with `/`. Thus it is absolute.
return path;
}
if (base.substring(base.length-1) === "/") {
// base ends with `/`
return base + path;
}
return base + "/" + path;
}

function formatResult (location, title, summary) {
return '<article><h3><a href="' + base_url + '/' + location + '">'+ title + '</a></h3><p>' + summary +'</p></article>';
return '<article><h3><a href="' + joinUrl(base_url, location) + '">'+ title + '</a></h3><p>' + summary +'</p></article>';
}

function displayResults (results) {
Expand Down Expand Up @@ -67,7 +79,7 @@ function onWorkerMessage (e) {
if (!window.Worker) {
console.log('Web Worker API not supported');
// load index in main thread
$.getScript(base_url + "/search/worker.js").done(function () {
$.getScript(joinUrl(base_url, "search/worker.js")).done(function () {
console.log('Loaded worker');
init();
window.postMessage = function (msg) {
Expand All @@ -78,7 +90,7 @@ if (!window.Worker) {
});
} else {
// Wrap search in a web worker
var searchWorker = new Worker(base_url + "/search/worker.js");
var searchWorker = new Worker(joinUrl(base_url, "search/worker.js"));
searchWorker.postMessage({init: true});
searchWorker.onmessage = onWorkerMessage;
}
8 changes: 4 additions & 4 deletions mkdocs/tests/build_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,22 @@ def test_context_base_url_relative_no_page_use_directory_urls(self):
def test_context_base_url_absolute_no_page(self):
cfg = load_config(use_directory_urls=False)
context = build.get_context(mock.Mock(), mock.Mock(), cfg, base_url='/')
self.assertEqual(context['base_url'], '')
self.assertEqual(context['base_url'], '/')

def test_context_base_url__absolute_no_page_use_directory_urls(self):
cfg = load_config()
context = build.get_context(mock.Mock(), mock.Mock(), cfg, base_url='/')
self.assertEqual(context['base_url'], '')
self.assertEqual(context['base_url'], '/')

def test_context_base_url_absolute_nested_no_page(self):
cfg = load_config(use_directory_urls=False)
context = build.get_context(mock.Mock(), mock.Mock(), cfg, base_url='/foo/')
self.assertEqual(context['base_url'], '/foo')
self.assertEqual(context['base_url'], '/foo/')

def test_context_base_url__absolute_nested_no_page_use_directory_urls(self):
cfg = load_config()
context = build.get_context(mock.Mock(), mock.Mock(), cfg, base_url='/foo/')
self.assertEqual(context['base_url'], '/foo')
self.assertEqual(context['base_url'], '/foo/')

def test_context_extra_css_js_from_homepage(self):
nav_cfg = [
Expand Down

0 comments on commit 1a5c4a7

Please sign in to comment.