-
Notifications
You must be signed in to change notification settings - Fork 137
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
🔧 ExpensiMark: Markdown image shorthand syntax #676
🔧 ExpensiMark: Markdown image shorthand syntax #676
Conversation
lib/ExpensiMark.js
Outdated
|
||
/** | ||
* Replace MD characters with their HTML entity equivalent | ||
* @param {String} text | ||
* @return {String} | ||
*/ | ||
escapeMarkdownEntities(text) { | ||
// A regex pattern matching special MD characters we'd like to escape | ||
const pattern = /([*_{}[\]~])/g; | ||
|
||
// A map of MD characters to their HTML entity equivalent | ||
const entities = { | ||
'*': '*', | ||
_: '_', | ||
'{': '{', | ||
'}': '}', | ||
'[': '[', | ||
']': ']', | ||
'~': '~', | ||
}; | ||
|
||
return text.replace(pattern, char => entities[char] || char); | ||
} |
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.
With the introduction of attributeCleanup
rule - this logic is no longer needed
lib/ExpensiMark.js
Outdated
const MARKDOWN_LINK = `\\[([^\\][]*(?:\\[[^\\][]*][^\\][]*)*)]\\(${MARKDOWN_URL_REGEX}\\)(?![^<]*(<\\/pre>|<\\/code>))`; | ||
const MARKDOWN_LINK_REGEX = new RegExp(MARKDOWN_LINK, 'gi'); | ||
const MARKDOWN_IMAGE_REGEX = new RegExp(`\\!${MARKDOWN_LINK}`, 'gi'); | ||
const MARKDOWN_LINK_REGEX = new RegExp(`\\[([^\\][]*(?:\\[[^\\][]*][^\\][]*)*)]\\(${MARKDOWN_URL_REGEX}\\)(?![^<]*(<\\/pre>|<\\/code>))`, 'gi'); | ||
const MARKDOWN_IMAGE_REGEX = new RegExp(`\\!(?:\\[([^\\][]*(?:\\[[^\\][]*][^\\][]*)*)])?\\(${MARKDOWN_URL_REGEX}\\)(?![^<]*(<\\/pre>|<\\/code>))`, 'gi'); |
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.
MARKDOWN_LINK's string was directly moved into the MARDOWN_LINK_REGEX constructor as it's no longer reusable
MARKDOWN_IMAGE_REGEX
was updated and although is similar to MARKDOWN_LINK
the [alt text]
part was made optional
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.
Code LGTM, i just have 2 questions for you. If nothing needs to be modified I'll merge once answered.
Hi! @kidroca
|
Support the `!(url)` to create images without alt text 🧪 Add test for image md shorthand conversion to html tag without alt text ℹ️ Added tests for single image with alt text, single image with empty alt text, and single image short syntax without alt text in ExpensiMark-HTML-test.js.
…matching The update aims to avoid matching and converting these rules if the match happens to be within an HTML attribute - in that case the content should not be converted to HTML
c3ac1a6
to
7a7c075
Compare
@robertKozik, @chiragsalian, I have updated the pull request with a revised approach to handling HTML attributes. The original method aimed to clean up HTML within attributes but proved unreliable because distinguishing embedded HTML from the attribute content was challenging. Consider the example: <img src="https://example.com/image.png" alt="<pre data-code-raw="code">code</pre>" data-raw-href="https://example.com/image.png" data-link-variant="labeled" /> /> In this case, the As the scope and focus of the PR have shifted, I kindly request a re-review. I have made an effort to limit the extent of changes for easier review. |
@robertKozik, regarding:
While using a patch package might seem convenient, I've updated the PR, which necessitates creating the patch again. Initially, my suggestion to use the commit hash from my fork was intended as a temporary measure for testing purposes in your PR. This approach would enable us to validate the integration, merge the changes into expensify-common, and then revert to the original package source. Alternatively, if it’s more feasible, I can open a new PR in NewDot using a previous version of |
lib/ExpensiMark.js
Outdated
replacement: (match, g1, g2) => `<img src="${Str.sanitizeURL(g2)}" alt="${this.escapeMarkdownEntities(g1)}" />`, | ||
rawInputReplacement: (match, g1, g2) => `<img src="${Str.sanitizeURL(g2)}" alt="${this.escapeMarkdownEntities(g1)}" data-raw-href="${g2}" data-link-variant="labeled" />` | ||
replacement: (match, g1, g2) => `<img src="${Str.sanitizeURL(g2)}"${g1 ? ` alt="${this.escapeAttributeContent(g1)}"` : ''} />`, | ||
rawInputReplacement: (match, g1, g2) => `<img src="${Str.sanitizeURL(g2)}"${g1 ? ` alt="${this.escapeAttributeContent(g1)}"` : ''} data-raw-href="${g2}" data-link-variant="labeled" />` |
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.
One minor thing, sorry for bother @kidroca .
If user types !(link)
data-link-variant should be set to "auto" rather than "labeled" indicating that it's typed using shorthand syntax
This is needed to distinguish between ![](link)
and !(link)
Actually I'm still prefering patching the repo with your changes, rather than pointing to the forked repository. I've added one more comment - do you think it's solvable? @kidroca |
Thanks, I'll post an update in a minute |
I've came up with something like that: |
22e8a4d
to
046b6ec
Compare
Updated |
@kidroca |
Pls merge main, bold -> |
# Conflicts: # __tests__/ExpensiMark-HTML-test.js # __tests__/ExpensiMark-Markdown-test.js # lib/ExpensiMark.js
ℹ️ Skip replacing bold text within code blocks during parsing for ExpensiMark-HTML-test
test('Test bold within code blocks is skipped', () => { | ||
const testString = 'bold\n```*not a bold*```\nThis is *bold*'; | ||
const replacedString = 'bold<br /><pre>*not a bold*</pre>This is <strong>bold</strong>'; | ||
expect(parser.replace(testString)).toBe(replacedString); | ||
}); |
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.
Added this test to ensure the merge conflict was resolved correctly
The test is related to code added in: #681
Merged |
This PR introduces refinements in the
ExpensiMark
library to enhance the handling of markdown syntax for images, including the introduction of shorthand syntax for images without alt text, and refines the cleanup of HTML attributesFixed Issues
$ Expensify/App#38952
Tests
__tests__/ExpensiMark-HTML-test.js
and__tests__/ExpensiMark-Markdown-test.js
to cover the new markdown shorthand for images without alt text, the handling of images with empty and special characters in alt text, and the refinement in HTML attribute cleanup. These tests validate the parsing of markdown to HTML and vice versa, ensuring that the changes behave as expected.QA