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

Wrong search hyperlinks to page sections for inherited classes #416

Open
josip-milic opened this issue Dec 11, 2022 · 1 comment
Open

Wrong search hyperlinks to page sections for inherited classes #416

josip-milic opened this issue Dec 11, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@josip-milic
Copy link

Hello, I'm using pdoc3 with lunr_search, it helps me a lot and produces very nice documentation without much effort, many thanks to the developers.
I just have one problem with it. Search results show found documentation part for both the original class and the classes that inherit it. This is fine and all of them actually link to the same part in the documentation for the original class, but the section parts in the links (the #<section name> parts) actually contain the names of the child classes instead of the parent class that actually contains the documentation which results in not being able to jump to the correct section of the page when clicking on the link (in short, each result with the child class leads to the right page, but it's unable to jump to the right part of it).

Here is a demonstration:

Structure:

module_example
- __init__.py
- class1.py
- class2.py

Content of 'class1.py'

class Class1:
    def __init__(self, a):
        self.a = a

    def get_a(self):
        """This method returns value of `a`

        Returns: value of `a`

        """
        return self.a

Content of 'class2.py'

from module_example.class1 import Class1


class Class2(Class1):
    pass

Creation of html pages:

pdoc3 --html module_example -c lunr_search={} -o ./docs

Opening 'http://localhost:63342/project/docs/module_example/' shows the generated pages for both classes.
When searching for 'get_a', it shows two results

module_example.class1.Class1.get_a()
http://localhost:63342/project/docs/module_example/class1.html#module_example.class1.Class1.get_a
module_example.class2.Class2.get_a()
http://localhost:63342/project/docs/module_example/class1.html#module_example.class2.Class2.get_a

The first link is fine and it leads to the correct section of the page.
The second link leads to a non-existing section of the page. The generated link should be the same as the first one.

In this case, the first link leads to the parent class which is fine, but when having a larger project, the parent class is not necessarily at the top of the search results. Also, in this case, the searched method is clearly visible, but if the class has many methods, it requires one additional awkward step of searching again on the page for the method.

If there is no easy fix for this, is there an existing option that would mitigate this, such as showing only the result for the parent class?

@kernc
Copy link
Member

kernc commented Dec 11, 2022

The link is constructed here:

const url = URLS[dobj.url] + '#' + dobj.ref;

dobj.url is assigned here from dobj.module:

pdoc/pdoc/cli.py

Lines 391 to 414 in 80af5d4

def recursive_add_to_index(dobj):
info = {
'ref': dobj.refname,
'url': to_url_id(dobj.module),
}
if index_docstrings:
info['doc'] = trim_docstring(dobj.docstring)
if isinstance(dobj, pdoc.Function):
info['func'] = 1
index.append(info)
for member_dobj in getattr(dobj, 'doc', {}).values():
recursive_add_to_index(member_dobj)
@lru_cache()
def to_url_id(module):
url = module.url()
if url not in url_cache:
url_cache[url] = len(url_cache)
return url_cache[url]
index: List[Dict] = []
url_cache: Dict[str, int] = {}
for top_module in modules:
recursive_add_to_index(top_module)

which points to the parent module for members that aren't overridden.

I'm not sure what is best to do here. Why not just skip those members from search?

@kernc kernc added the bug Something isn't working label Dec 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants