-
-
Notifications
You must be signed in to change notification settings - Fork 182
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
refactor(TextFormatter): create TextFormatter component TASK-996 #5257
Conversation
const formattedParts: ReactNode[] = []; | ||
|
||
for (const part of parts) { | ||
if (part.startsWith('**')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a style issue, but we generally like to avoid if/else chains in favor of multiple simple if statements:
if (x) {
return a
}
if (y) {
return b
}
return c
Left one minor comment, but other than that lgtm. |
@pauloamorimbr do we have plans to use it in all the places that currently go with The list of components that could use
Also do we have plans to replace our old way of handling links ( |
Also I think that this should be discussed with @JacquelineMorrissette (unless it already was) given the fact that we are kinda setting some new rules for how translation text is written. |
@@ -0,0 +1,56 @@ | |||
import type {ReactNode} from 'react'; | |||
|
|||
const regExpMatchParts = /[^*[\]]+|(\*\*[^*]*(?:\*[^*]+)*\*\*)|(\*[^*]+\*)|(\[.+?\]\(.+?\)({:.+})?)/g; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't some existing library satisfy our needs here? Like https://www.npmjs.com/package/markdown-to-jsx or https://www.npmjs.com/package/react-markdown or https://www.npmjs.com/package/marked ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've used react-markdown
before and suggest it. I like most the ability to override all components with custom implementation, that's useful especially with Link-s.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the need here was a simple subset of what markdown does I opted not to use a full library, not at first at least, so we wouldn't have to worry about unwanted markdown styles being accidentally inserted in string and also implementing it simply would avoid having another library linked to the project, but after talking to @Akuukis about this I ended up agreeing that using a library would simplify things here, mainly because there are libraries where we can opt for which marks we want to implement.
I did talk to @JacquelineMorrissette about this. 👍🏻 |
I didn't think of migrating other stuff. The idea is to apply this to the changes I'm working on right now. Regarding replacing the |
I've done tests with the |
@pauloamorimbr made a task for the refactor proposition: https://www.notion.so/kobotoolbox/Refactor-some-dangerouslySetInnerHTML-code-to-use-react-markdown-13e7e515f65480c5a95ec9b1a7a7477c?pvs=4 |
Thanks. I'm mentioning this in the other PR. |
🗒️ Checklist
<type>(<scope>)<!>: <title> TASK-1234
frontend
orbackend
unless it's global👀 Preview steps
💭 Notes
A
TextFormatter
component is being added to support long strings formatted in a markdown-like fashion.Such component is needed to support long sentences to be properly translated without having to break out words just to add simple styles like bold, italic or links.
TextFormatter
component suports an input of atext
prop containing an string to be processed and generates the output based on the processing of the string.