Skip to content

fix: genIndex error for search #1933

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

Merged
merged 10 commits into from
Jan 3, 2023
Next Next commit
fix search.js
1. url is wrong if multiple contents are on the same site, example: `https://xxxxx/docs/` and `https://xxxxx/blog/`
2. when the `token.text` is undefined, `handlePostContent` is undefined too at line 216
  • Loading branch information
wangliang181230 authored Jul 23, 2022
commit 306d606b9bd169abb5228bbd50029f33d48e3cec
5 changes: 2 additions & 3 deletions src/plugins/search/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export function genIndex(path, content = '', router, depth) {
} else {
slug = router.toURL(path, { id: slugify(escapeHtml(token.text)) });
}
slug = location.pathname + slug;

Copy link
Member

Choose a reason for hiding this comment

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

What does those changes for ? could you plz give a sample and add verify test cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When my website has multiple paths, it can prevent 404 error.
For example:
http://xxxxxx/A/
http://xxxxxx/B/
When searching on page A and accessing page B, 404 will appear.

Copy link
Member

Choose a reason for hiding this comment

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

When my website has multiple paths, it can prevent 404 error. For example: http://xxxxxx/A/ http://xxxxxx/B/ When searching on page A and accessing page B, 404 will appear.

I see, maybe u can take a look on this namespace configs here.

Copy link
Contributor Author

@wangliang181230 wangliang181230 Dec 19, 2022

Choose a reason for hiding this comment

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

I see, maybe u can take a look on this namespace configs here.

Should I configure it like this?

{
    search: [
        '/A',
        '/B'
    ]
}

or

{
    search: {
        paths: [
            '/A',
            '/B'
        ]
    }
}

Copy link
Member

@Koooooo-7 Koooooo-7 Jan 3, 2023

Choose a reason for hiding this comment

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

I guess you need distinguish those namespace for different paths (sites), may could refer to our site with multi langs nav . if you deploy multi sites in same domain, the namespace config should be in each site's index.html search config.

      // Use different indexes for path prefixes (namespaces).
      // NOTE: Only works in 'auto' mode.
      //
      // When initialiazing an index, we look for the first path from the sidebar.
      // If it matches the prefix from the list, we switch to the corresponding index.
      pathNamespaces: ['/zh-cn', '/ru-ru', '/ru-ru/v1'],

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I will try it.

if (str) {
title = str
Expand Down Expand Up @@ -130,9 +131,7 @@ export function genIndex(path, content = '', router, depth) {
token.text = getTableData(token);
token.text = getListData(token);

index[slug].body = index[slug].body
? index[slug].body + token.text
: token.text;
Copy link
Contributor Author

@wangliang181230 wangliang181230 Dec 6, 2022

Choose a reason for hiding this comment

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

index[slug].body is always undefined, see the line 122

120  if (!index[slug]) {
121    index[slug] = { slug, title: '', body: '' };
122  } else if (index[slug].body) {
       ......
127  } else {
       ...... //index[slug].body is always undefined
     }

Copy link
Contributor Author

@wangliang181230 wangliang181230 Dec 14, 2022

Choose a reason for hiding this comment

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

When the token.text is undefined, it will make the index[slug].body also undefined.
So I change the token.text to token.text || ''.

Copy link
Contributor Author

@wangliang181230 wangliang181230 Dec 14, 2022

Choose a reason for hiding this comment

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

The code index[slug].body ? index[slug].body + token.text : is redundant, and it is not the cause of the bug.

Copy link
Member

Choose a reason for hiding this comment

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

nice point.

index[slug].body = token.text || '';
}
}
});
Expand Down