Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search: generate full link from the server side #7070

Merged
merged 8 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions readthedocs/core/static-src/core/js/doc-embed/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,7 @@ function attach_elastic_search_query(data) {
}
}

// Creating the result from elements
var suffix = DOCUMENTATION_OPTIONS.FILE_SUFFIX;
// Since sphinx 2.2.1 FILE_SUFFIX is .html for all builders,
// and there is a new BUILDER option.
if ('BUILDER' in DOCUMENTATION_OPTIONS && DOCUMENTATION_OPTIONS.BUILDER === 'readthedocsdirhtml') {
suffix = '';
}
var link = doc.link + suffix + "?highlight=" + $.urlencode(query);
var link = doc.link + "?highlight=" + $.urlencode(query);

var item = $('<a>', {'href': link});

Expand Down
2 changes: 1 addition & 1 deletion readthedocs/core/static/core/js/readthedocs-doc-embed.js

Large diffs are not rendered by default.

52 changes: 37 additions & 15 deletions readthedocs/search/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from readthedocs.api.v2.permissions import IsAuthorizedToViewVersion
from readthedocs.builds.models import Version
from readthedocs.projects.constants import MKDOCS, SPHINX_HTMLDIR
from readthedocs.projects.models import HTMLFile, Project
from readthedocs.search import tasks, utils
from readthedocs.search.faceted_search import PageSearch
Expand All @@ -27,15 +28,28 @@ class PageSearchSerializer(serializers.Serializer):
version = serializers.CharField()
title = serializers.CharField()
path = serializers.CharField()
full_path = serializers.CharField()
link = serializers.SerializerMethodField()
highlight = serializers.SerializerMethodField()
inner_hits = serializers.SerializerMethodField()

def get_link(self, obj):
projects_url = self.context.get('projects_url')
if projects_url:
docs_url = projects_url[obj.project]
return docs_url + obj.path
project_data = self.context['projects_data'].get(obj.project)
if not project_data:
return None

docs_url, doctype = project_data
path = obj.full_path

# Generate an appropriate link for the doctype,
# And always end it with / so it goes directly to proxito.
if (
doctype in {SPHINX_HTMLDIR, MKDOCS} and
path == 'index.html' or path.endswith('/index.html')
):
path = path[:-len('index.html')].rstrip('/') + '/'
stsewd marked this conversation as resolved.
Show resolved Hide resolved

return docs_url + path

def get_highlight(self, obj):
highlight = getattr(obj.meta, 'highlight', None)
Expand Down Expand Up @@ -157,7 +171,7 @@ def validate_query_params(self):

def get_serializer_context(self):
context = super().get_serializer_context()
context['projects_url'] = self.get_all_projects_url()
context['projects_data'] = self.get_all_projects_data()
return context

def get_all_projects(self):
Expand Down Expand Up @@ -185,19 +199,17 @@ def get_all_projects(self):
all_projects.append(version.project)
return all_projects

def get_all_projects_url(self):
def get_all_projects_data(self):
"""
Return a dict containing the project slug and its version URL.

The dictionary contains the project and its subprojects . Each project's
slug is used as a key and the documentation URL for that project and
version as the value.
Return a dict containing the project slug and its version URL and version's doctype.

Example:
The dictionary contains the project and its subprojects. Each project's
slug is used as a key and a tuple with the documentation URL and doctype
from the version. Example:

{
"requests": "https://requests.readthedocs.io/en/latest/",
"requests-oauth": "https://requests-oauth.readthedocs.io/en/latest/",
"requests": ("https://requests.readthedocs.io/en/latest/", "sphinx"),
"requests-oauth": ("https://requests-oauth.readthedocs.io/en/latest/", "sphinx_htmldir"),
}

:rtype: dict
Expand All @@ -207,7 +219,17 @@ def get_all_projects_url(self):
projects_url = {}
stsewd marked this conversation as resolved.
Show resolved Hide resolved
for project in all_projects:
projects_url[project.slug] = project.get_docs_url(version_slug=version_slug)
return projects_url

versions_doctype = (
Version.objects
.filter(project__slug__in=projects_url.keys(), slug=version_slug)
.values_list('project__slug', 'documentation_type')
)
projects_data = {
project_slug: (projects_url[project_slug], doctype)
for project_slug, doctype in versions_doctype
}
return projects_data

def list(self, request, *args, **kwargs):
"""Overriding ``list`` method to record query in database."""
Expand Down