Repository: naturalcrit/homebrewery (js)

Issue:
Markdown variables consume too many characters if parentheses are nested with whitespace before final )
Not sure what is causing this issue, but it is a bug of some sort.

Some combination of links and parentheses inside tables merges cells together, sometimes splitting words.

the following code will work as a demonstration:
```
|  title 1  | title 2 | title 3 | title 4|
|-----------|---------|---------|--------|
|[foo](bar) |  Ipsum  |    )    |   )    |
```
results in this:
![image](https://github.com/naturalcrit/homebrewery/assets/91662974/29122643-a802-4f1d-8bf5-ab4d49917801)




Pull request description:
Implement suggested fix for 3448
## Description

Per issue, remove trim sequence and remove unneeded if wrapper.

```
|  title 1  | title 2 | title 3 | title 4|
|-----------|---------|---------|--------|
|[foo](bar) |  Ipsum  |    )    |   )    |
```

![image](https://github.com/user-attachments/assets/e176e838-48de-4509-9cb8-8c8953712141)

## Related Issues or Discussions

- Closes #3448

### Reviewer Checklist

_Replace the list below with specific features you want reviewers to look at._

*Reviewers, refer to this list when testing features, or suggest new items *
- [ ] Verify new features are functional
  - [ ] Test as above


Patch:
diff --git a/shared/naturalcrit/markdown.js b/shared/naturalcrit/markdown.js
index 852243d8..ffd2395f 100644
--- a/shared/naturalcrit/markdown.js
+++ b/shared/naturalcrit/markdown.js
@@ -1,3 +1,4 @@
+/* eslint-disable max-depth */
 /* eslint-disable max-lines */
 import _                        from 'lodash';
 import { Parser as MathParser } from 'expr-eval';
@@ -680,7 +681,7 @@ function MarkedVariables() {
 					}
 					if(match[8]) { // Inline Definition
 						const label = match[10] ? match[10].trim().replace(/\s+/g, ' ') : null; // Trim edge spaces and shorten blocks of whitespace to 1 space
-						let content = match[11] ? match[11].trim().replace(/\s+/g, ' ') : null; // Trim edge spaces and shorten blocks of whitespace to 1 space
+						let content = match[11] || null;
 
 						// In case of nested (), find the correct matching end )
 						let level = 0;
@@ -696,10 +697,8 @@ function MarkedVariables() {
 									break;
 							}
 						}
-						if(i > -1) {
-							combinedRegex.lastIndex = combinedRegex.lastIndex - (content.length - i);
-							content = content.slice(0, i).trim().replace(/\s+/g, ' ');
-						}
+						combinedRegex.lastIndex = combinedRegex.lastIndex - (content.length - i);
+						content = content.slice(0, i).trim().replace(/\s+/g, ' ');
 
 						varsQueue.push(
 							{ type    : 'varDefBlock',


Test patch:
diff --git a/tests/markdown/variables.test.js b/tests/markdown/variables.test.js
index be16e8a2..41259da7 100644
--- a/tests/markdown/variables.test.js
+++ b/tests/markdown/variables.test.js
@@ -402,4 +402,12 @@ describe('Variable names that are subsets of other names', ()=>{
 		const rendered = Markdown.render(source).trimReturns();
 		expect(rendered).toBe('<p>14</p>');
 	});
+});
+
+describe('Regression Tests', ()=>{
+	it('Don\'t Eat all the parentheticals!', function() {
+		const source='\n|  title 1  | title 2 | title 3 | title 4|\n|-----------|---------|---------|--------|\n|[foo](bar) |  Ipsum  |    )    |   )    |\n';
+		const rendered = Markdown.render(source).trimReturns();
+		expect(rendered).toBe('<table><thead><tr><th>title 1</th><th>title 2</th><th>title 3</th><th>title 4</th></tr></thead><tbody><tr><td><a href=\"bar\">foo</a></td><td>Ipsum</td><td>)</td><td>)</td></tr></tbody></table>');
+	});
 });
\ No newline at end of file