Skip to content

Commit

Permalink
Move the content root marker to the <html> element
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Aug 11, 2023
1 parent 818895a commit 8e730ae
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
10 changes: 5 additions & 5 deletions sphinx/builders/html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
from sphinx.util.i18n import format_date
from sphinx.util.inventory import InventoryFile
from sphinx.util.matching import DOTFILES, Matcher, patmatch
from sphinx.util.osutil import copyfile, ensuredir, os_path, relative_uri
from sphinx.util.osutil import SEP, copyfile, ensuredir, os_path, relative_uri
from sphinx.util.tags import Tags
from sphinx.writers.html import HTMLWriter
from sphinx.writers.html5 import HTML5Translator
Expand Down Expand Up @@ -360,8 +360,7 @@ def add_css_file(self, filename: str, **kwargs: Any) -> None:

def init_js_files(self) -> None:
self.script_files = []
self.add_js_file('documentation_options.js', id="documentation_options",
data_url_root='', priority=200)
self.add_js_file('documentation_options.js', priority=200)
self.add_js_file('doctools.js', priority=200)
self.add_js_file('sphinx_highlight.js', priority=200)

Expand Down Expand Up @@ -1075,6 +1074,9 @@ def hasdoc(name: str) -> bool:
self.add_sidebars(pagename, ctx)
ctx.update(addctx)

# 'blah.html' should have content_root = './' not ''.
ctx['content_root'] = (f'..{SEP}' * default_baseuri.count(SEP)) or f'.{SEP}'

# revert script_files and css_files
self.script_files[:] = self._script_files
self.css_files[:] = self._css_files
Expand Down Expand Up @@ -1233,8 +1235,6 @@ def js_tag(js: JavaScript) -> str:
if value is not None:
if key == 'body':
body = value
elif key == 'data_url_root':
attrs.append(f'data-url_root="{pathto("", resource=True)}"')
else:
attrs.append(f'{key}="{html.escape(value, True)}"')
if js.filename:
Expand Down
2 changes: 1 addition & 1 deletion sphinx/themes/basic/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ <h3>{{ _('Navigation') }}</h3>
{%- if html_tag %}
{{ html_tag }}
{%- else %}
<html{% if language is not none %} lang="{{ language }}"{% endif %}>
<html{% if language is not none %} lang="{{ language }}"{% endif %} data-content_root="{{ content_root }}">
{%- endif %}
<head>
<meta charset="{{ encoding }}" />
Expand Down
1 change: 0 additions & 1 deletion sphinx/themes/basic/static/documentation_options.js_t
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var DOCUMENTATION_OPTIONS = {
URL_ROOT: document.getElementById("documentation_options").getAttribute('data-url_root'),
VERSION: '{{ release|e }}',
LANGUAGE: '{{ language }}',
COLLAPSE_INDEX: false,
Expand Down
6 changes: 3 additions & 3 deletions sphinx/themes/basic/static/searchtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ const _escapeRegExp = (string) =>

const _displayItem = (item, searchTerms) => {
const docBuilder = DOCUMENTATION_OPTIONS.BUILDER;
const docUrlRoot = DOCUMENTATION_OPTIONS.URL_ROOT;
const docFileSuffix = DOCUMENTATION_OPTIONS.FILE_SUFFIX;
const docLinkSuffix = DOCUMENTATION_OPTIONS.LINK_SUFFIX;
const showSearchSummary = DOCUMENTATION_OPTIONS.SHOW_SEARCH_SUMMARY;
const contentRoot = document.documentElement.dataset.content_root;

const [docName, title, anchor, descr, score, _filename] = item;

Expand All @@ -75,11 +75,11 @@ const _displayItem = (item, searchTerms) => {
if (dirname.match(/\/index\/$/))
dirname = dirname.substring(0, dirname.length - 6);
else if (dirname === "index/") dirname = "";
requestUrl = docUrlRoot + dirname;
requestUrl = contentRoot + dirname;
linkUrl = requestUrl;
} else {
// normal html builders
requestUrl = docUrlRoot + docName + docFileSuffix;
requestUrl = contentRoot + docName + docFileSuffix;
linkUrl = docName + docLinkSuffix;
}
let linkEl = listItem.appendChild(document.createElement("a"));
Expand Down

1 comment on commit 8e730ae

@tuannv-github
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
image
This occured when using search feature.

Please sign in to comment.