From a834e8dd2b11f5de6e9d4932274f01a3f785a1e9 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Thu, 1 Aug 2024 14:35:37 +0700 Subject: [PATCH] fix markdown in alt of video markdown --- __tests__/ExpensiMark-HTML-test.js | 19 +++++++++ lib/ExpensiMark.ts | 64 +++++++++++++++--------------- 2 files changed, 51 insertions(+), 32 deletions(-) diff --git a/__tests__/ExpensiMark-HTML-test.js b/__tests__/ExpensiMark-HTML-test.js index 4a986716..ab3cd854 100644 --- a/__tests__/ExpensiMark-HTML-test.js +++ b/__tests__/ExpensiMark-HTML-test.js @@ -2078,6 +2078,25 @@ describe('Video markdown conversion to html tag', () => { expect(parser.replace(testString)).toBe(resultString); }); + test('with alt text containing markdown', () => { + const testString = '![# fake-heading *bold* _italic_ ~strike~ [:-)]](https://example.com/video.mp4)'; + const resultString = ''; + expect(parser.replace(testString)).toBe(resultString); + }); + + test('inline code in alt', () => { + const testString = '![`code`](https://example.com/video.mp4)'; + const resultString = ''; + expect(parser.replace(testString)).toBe(resultString); + }); + + test('blockquote in alt', () => { + const testString = '![```test```](https://example.com/video.mp4)'; + const resultString = ''; + expect(parser.replace(testString)).toBe(resultString); + }); + + }) describe('Image markdown conversion to html tag', () => { diff --git a/lib/ExpensiMark.ts b/lib/ExpensiMark.ts index 0dbbcd2d..34b437ed 100644 --- a/lib/ExpensiMark.ts +++ b/lib/ExpensiMark.ts @@ -156,6 +156,30 @@ export default class ExpensiMark { }, }, + /** + * Converts markdown style video to video tags e.g. ![Expensify](https://www.expensify.com/attachment.mp4) + * We need to convert before image rules since they will not try to create a image tag from an existing video URL + * Extras arg could contain the attribute cache for the video tag which is cached during the html-to-markdown conversion + */ + { + name: 'video', + regex: MARKDOWN_VIDEO_REGEX, + /** + * @param extras - The extras object + * @param videoName - The first capture group - video name + * @param videoSource - The second capture group - video URL + * @return Returns the HTML video tag + */ + replacement: (extras, _match, videoName, videoSource) => { + const extraAttrs = extras && extras.videoAttributeCache && extras.videoAttributeCache[videoSource]; + return ``; + }, + rawInputReplacement: (extras, _match, videoName, videoSource) => { + const extraAttrs = extras && extras.videoAttributeCache && extras.videoAttributeCache[videoSource]; + return ``; + }, + }, + /** * Apply inline code-block to avoid applying any other formatting rules inside of it, * like we do for the multi-line code-blocks @@ -166,7 +190,7 @@ export default class ExpensiMark { // Use the url escaped version of a backtick (`) symbol. Mobile platforms do not support lookbehinds, // so capture the first and third group and place them in the replacement. // but we should not replace backtick symbols if they include
 tags between them.
-                regex: /(\B|_|)`(.*?(?![`])\S.*?)`(\B|_|)(?!`|[^<]*<\/pre>)/gm,
+                regex: /(\B|_|)`(.*?(?![`])\S.*?)`(\B|_|)(?!`|[^<]*<\/pre>|[^<]*<\/video>)/gm,
                 replacement: '$1$2$3',
             },
 
@@ -204,36 +228,12 @@ export default class ExpensiMark {
             {
                 name: 'heading1',
                 process: (textToProcess, replacement, shouldKeepRawInput = false) => {
-                    const regexp = shouldKeepRawInput ? /^# ( *(?! )(?:(?!
|\n|\r\n).)+)/gm : /^# +(?! )((?:(?!
|\n|\r\n).)+)/gm;
+                    const regexp = shouldKeepRawInput ? /^# ( *(?! )(?:(?!
|