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

HtmlExctractors returns more characters than expected #66

Open
evans opened this issue Sep 7, 2023 · 2 comments
Open

HtmlExctractors returns more characters than expected #66

evans opened this issue Sep 7, 2023 · 2 comments

Comments

@evans
Copy link

evans commented Sep 7, 2023

First, thanks for creating a wonderful library! I ran into a bug where HtmlExctractors creates a message with excess characters when html is used within a prop. The following script returns more than the expected: Some good text. <a href="example.com">Learn more</a>.

import { GettextExtractor, HtmlExtractors } from 'gettext-extractor';

  const markupExtractor = new GettextExtractor();
  markupExtractor
    .createHtmlParser([HtmlExtractors.elementContent('[translated]', {})])
    .parseFilesGlob('**/*.js', undefined, {});

  markupExtractor.getMessages().forEach((message) => {
    console.log(message.text);
  });

The parsed file

const Text = ({ children }) => <div>{children}</div>;
const Container = ({ children, secondaryText }) => (
  <div>
    {children}
    {secondaryText}
  </div>
);

const Parent = () => {
  return (
    <Container
      secondaryText={
        <Text translated>
          Some good text. <a href="example.com">Learn more</a>.
        </Text>
      }
      maxlength={25}
    />
  );
};
 
@evans
Copy link
Author

evans commented Sep 7, 2023

Looks like the issue lies in parse5, since it returns a node that includes more child nodes for Text than expected. I'm guessing this sort of usage isn't expected, so if you have advice for mixing this sort of html/jsx extraction, I'd love to hear your thoughts!

let document = parse5.parse(source, {sourceCodeLocationInfo: true});

@lukasgeiter
Copy link
Owner

The extractor is made for HTML not JSX. I'm also not really sure what you would expect.
If you only want Some good text extracted, why don't you do <Text translated>Some good text</Text> instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants