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

capture newline as the code fence content #730

Merged
merged 4 commits into from
Jul 2, 2024

Conversation

bernhardoj
Copy link
Contributor

@bernhardoj bernhardoj commented Jun 25, 2024

Fixed Issues

$ Expensify/App#43750

Tests

  1. What unit/integration tests cover your change? What autoQA tests cover your change?
    Updated existing unit test for codefence.

  2. What tests did you perform that validates your changed worked?
    To test this in App,

  • open live markdown repo, apply the expensi-mark and live-markdown fix
  • run cd parser && npm run build
  • copy the parser/react-native-live-markdown-parser to App node_modules/@expensify/react-native-live-markdown/lib/parser/react-native-live-markdown-parser.js.
  1. Submit an expense with code block as description
  2. Open the transaction thread
  3. Open the description
  4. Verify there is no console error of react-native-live-markdown
Web
Screen.Recording.2024-06-25.at.13.32.56.mov
Desktop
Screen.Recording.2024-06-25.at.19.59.10.mov
iOS mWeb
Screen.Recording.2024-06-25.at.20.02.21.mov
iOS Native
Screen.Recording.2024-06-25.at.20.06.46.mov
Android mWeb
Screen.Recording.2024-06-25.at.20.08.08.mov
Android Native
Screen.Recording.2024-06-25.at.20.12.54.mov

QA

Same as Tests above

@bernhardoj bernhardoj requested a review from a team as a code owner June 25, 2024 05:42
@melvin-bot melvin-bot bot requested review from carlosmiceli and removed request for a team June 25, 2024 05:43
@bernhardoj
Copy link
Contributor Author

bernhardoj commented Jun 25, 2024

@BartoszGrajdek here is the PR for Expensify/App#43750

@BartoszGrajdek
Copy link
Contributor

Testing today!

Copy link
Contributor

@BartoszGrajdek BartoszGrajdek left a comment

Choose a reason for hiding this comment

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

Looks good to me - please attach some test videos for other platforms, because these changes affect all of them 🙏🏻

@bernhardoj
Copy link
Contributor Author

Done 👍

@@ -2220,7 +2220,7 @@ describe('room mentions', () => {

test('room mention inside code block should not be rendered', () => {
const testString = '```\n#room\n```';
const resultString = '<pre>#room<br /></pre>';
const resultString = '<pre><br />#room<br /></pre>';
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this is unwanted. HTML BR collapsing works for <br /></pre> but not for <pre><br /> https://codesandbox.io/s/silly-brattain-dz3rrw

Copy link
Contributor Author

@bernhardoj bernhardoj Jun 25, 2024

Choose a reason for hiding this comment

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

Interesting. The App PreRenderer works fine though, but I think we may want to avoid this. Maybe we can put the new line info on the <pre> tag like:

<pre data-newline="\r\n">

and live markdown can get the new line from data-newline.

Copy link
Contributor

Choose a reason for hiding this comment

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

That would work but it won't work if we have mixed newlines. Technically the live markdown works correctly and the console error is a false positive. Maybe we can ignore the error if the difference between the original text and the reconstructed one is only the new lines? Though ultimately we want the reconstructed message to be the same

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would work but it won't work if we have mixed newlines

Hmm, you're right.

Maybe we can ignore the error if the difference between the original text and the reconstructed one is only the new lines?

I think we can replace all the new lines to \n only when doing the comparison. This way, we don't alter the user input at all, but we need @BartoszGrajdek thoughts on this one.

Copy link
Contributor

Choose a reason for hiding this comment

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

What if we added the line break just in rawInputReplacement? The returned value of it is coming only into react-native-live-markdown input, so it won't affect how we treat code blocks in the chat itself, just the input.

We could just treat the linebreak before code block content as a separate group and use it only in rawInputReplacement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that works.

@s77rt do you agree with the above suggestion?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good to me but can you please test this against react-native-live-markdown and verify that we don't get extra newlines

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I don't see any extra newlines
image

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's go with that then 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated!

@marcochavezf marcochavezf self-requested a review June 26, 2024 16:47
@@ -129,21 +129,21 @@ export default class ExpensiMark {
name: 'codeFence',

// &#x60; is a backtick symbol we are matching on three of them before then after a new line character
regex: /(&#x60;&#x60;&#x60;(?:\r\n|\n))((?:\s*?(?!(?:\r\n|\n)?&#x60;&#x60;&#x60;(?!&#x60;))[\S])+\s*?(?:\r\n|\n))(&#x60;&#x60;&#x60;)/g,
regex: /(&#x60;&#x60;&#x60;(\r\n|\n))((?:\s*?(?!(?:\r\n|\n)?&#x60;&#x60;&#x60;(?!&#x60;))[\S])+\s*?(?:\r\n|\n))(&#x60;&#x60;&#x60;)/g,
Copy link
Contributor

Choose a reason for hiding this comment

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

NAB. This has 4 capturing groups but we only need 2. Can you make the other two non-capturing groups?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think there is 6 groups in total including the negative lookahead and we use 3, but I will leave it as it is.

const group = textWithinFences.replace(/(?:(?![\n\r])\s)/g, '&#32;');
return `<pre>${group}</pre>`;
},
rawInputReplacement: (_extras, _match, _g1, textWithinFences) => {
rawInputReplacement: (_extras, _match, _g1, g2, textWithinFences) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename the group variable that has the newline to something more fitting / explaining what the groups holds e.g. newLineCharacter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, updated!

@s77rt
Copy link
Contributor

s77rt commented Jul 1, 2024

@BartoszGrajdek @marcochavezf Can you please review/approve if the changes looks good to you

@BartoszGrajdek
Copy link
Contributor

Hi! Code changes look good to me, I'll just try testing it inside react-native-live-markdown lib tomorrow morning before approving to ensure no regressions are introduced.

I'm currently working on an issue with higher priority, hence the delay 🙏🏻

Copy link
Contributor

@BartoszGrajdek BartoszGrajdek left a comment

Choose a reason for hiding this comment

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

LGTM - Works well in react-native-live-markdown

@carlosmiceli carlosmiceli merged commit a6bc4c1 into Expensify:main Jul 2, 2024
6 checks passed
Copy link

github-actions bot commented Jul 2, 2024

🚀Published to npm in v2.0.35

@bernhardoj
Copy link
Contributor Author

Will prepare the App PR tomorrow!

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

Successfully merging this pull request may close these issues.

4 participants