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

Weird bug with pruneLength and Chinese text #22390

Closed
lucasholder opened this issue Mar 19, 2020 · 4 comments
Closed

Weird bug with pruneLength and Chinese text #22390

lucasholder opened this issue Mar 19, 2020 · 4 comments
Assignees
Labels
type: bug An issue or pull request relating to a bug in Gatsby

Comments

@lucasholder
Copy link

Description

When the Chinese text in a MDX file is too long, excerpt(pruneLength: 250) returns an empty string. When the text is a bit shorter, it returns the expected string of length 250.

Steps to reproduce

I'm a gatsby noob, not sure how to make a smaller reproduction of the bug... Basically, I did gatsby new gatsby-prunelength-bug https://github.com/vim-labs/gatsby-theme-sky-lite-starter and changed last 2 blog posts, one with long Chinese text and other with shorter Chinese text. Here's the guilty line: https://github.com/vim-labs/gatsby-theme-sky-lite/blob/master/src/templates/home.js#L139

git clone https://github.com/lucasholder/gatsby-prunelength-bug.git;
cd gatsby-prunelength-bug;
npm install;
gatsby develop;

Screenshot 2020-03-18 21 03 36

Environment

gatsby 2.19.48
@lucasholder lucasholder added the type: bug An issue or pull request relating to a bug in Gatsby label Mar 19, 2020
@jlkiri
Copy link
Contributor

jlkiri commented Mar 29, 2020

Apparently this is a bug in the prune function that gatsby-plugin-mdx uses. Here is the full code:

module.exports = function prune(str, length, pruneStr) {
  str = makeString(str);
  length = ~~length;
  pruneStr = pruneStr != null ? String(pruneStr) : '...';

  if (str.length <= length) return str;

  var tmpl = function(c) {
      return c.toUpperCase() !== c.toLowerCase() ? 'A' : ' ';
    },
    template = str.slice(0, length + 1).replace(/.(?=\W*\w*$)/g, tmpl); // 'Hello, world' -> 'HellAA AAAAA'

  if (template.slice(template.length - 2).match(/\w\w/))
    template = template.replace(/\s*\S+$/, '');
  else
    template = rtrim(template.slice(0, template.length - 1));

  return (template + pruneStr).length > str.length ? str : str.slice(0, template.length) + pruneStr;
};

The particular line c.toUpperCase() !== c.toLowerCase() is the culprit. It returns false for every character in the Chinese string, so the whole result is just whitespace. I think we need to switch it to some new dependency.

@lucasholder
Copy link
Author

@jlkiri thanks.. hopefully your fix will get merged soon!

@danabrit
Copy link
Contributor

@lucasholder Can you confirm whether this PR fixed your issue?

@pvdz
Copy link
Contributor

pvdz commented Sep 22, 2020

Going to close this due to lack of response. If this is still the case please reopen or open a new issue pointing to this issue. Thanks!

@pvdz pvdz closed this as completed Sep 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

No branches or pull requests

4 participants