diff --git a/__tests__/ExpensiMark-test.js b/__tests__/ExpensiMark-test.js index 4de8eb1f..e4bffea3 100644 --- a/__tests__/ExpensiMark-test.js +++ b/__tests__/ExpensiMark-test.js @@ -92,12 +92,30 @@ test('Test period replacements', () => { }); test('Test code fencing', () => { - const codeFenceExampleMarkdown = '```\nconst javaScript = \'javaScript\'\n```'; + let codeFenceExampleMarkdown = '```\nconst javaScript = \'javaScript\'\n```'; + expect(parser.replace(codeFenceExampleMarkdown)).toBe('
const javaScript = 'javaScript'
'); + + codeFenceExampleMarkdown = '```const javaScript = \'javaScript\'\n```'; + expect(parser.replace(codeFenceExampleMarkdown)).toBe('
const javaScript = 'javaScript'
'); + + codeFenceExampleMarkdown = '```\nconst javaScript = \'javaScript\'```'; + expect(parser.replace(codeFenceExampleMarkdown)).toBe('
const javaScript = 'javaScript'
'); + + codeFenceExampleMarkdown = '```const javaScript = \'javaScript\'```'; expect(parser.replace(codeFenceExampleMarkdown)).toBe('
const javaScript = 'javaScript'
'); }); test('Test code fencing with spaces and new lines', () => { - const codeFenceExample = '```\nconst javaScript = \'javaScript\'\n const php = \'php\'\n```'; + let codeFenceExample = '```\nconst javaScript = \'javaScript\'\n const php = \'php\'\n```'; + expect(parser.replace(codeFenceExample)).toBe('
const javaScript = 'javaScript'
const php = 'php'
'); + + codeFenceExample = '```const javaScript = \'javaScript\'\n const php = \'php\'\n```'; + expect(parser.replace(codeFenceExample)).toBe('
const javaScript = 'javaScript'
const php = 'php'
'); + + codeFenceExample = '```\nconst javaScript = \'javaScript\'\n const php = \'php\'```'; + expect(parser.replace(codeFenceExample)).toBe('
const javaScript = 'javaScript'
const php = 'php'
'); + + codeFenceExample = '```const javaScript = \'javaScript\'\n const php = \'php\'```'; expect(parser.replace(codeFenceExample)).toBe('
const javaScript = 'javaScript'
const php = 'php'
'); }); @@ -122,7 +140,16 @@ test('Test inline code blocks inside ExpensiMark', () => { }); test('Test code fencing with ExpensiMark syntax inside', () => { - const codeFenceExample = '```\nThis is how you can write ~strikethrough~, *bold*, _italics_, and [links](https://www.expensify.com)\n```'; + let codeFenceExample = '```\nThis is how you can write ~strikethrough~, *bold*, _italics_, and [links](https://www.expensify.com)\n```'; + expect(parser.replace(codeFenceExample)).toBe('
This is how you can write ~strikethrough~, *bold*, _italics_, and [links](https://www.expensify.com)
'); + + codeFenceExample = '```This is how you can write ~strikethrough~, *bold*, _italics_, and [links](https://www.expensify.com)\n```'; + expect(parser.replace(codeFenceExample)).toBe('
This is how you can write ~strikethrough~, *bold*, _italics_, and [links](https://www.expensify.com)
'); + + codeFenceExample = '```\nThis is how you can write ~strikethrough~, *bold*, _italics_, and [links](https://www.expensify.com)```'; + expect(parser.replace(codeFenceExample)).toBe('
This is how you can write ~strikethrough~, *bold*, _italics_, and [links](https://www.expensify.com)
'); + + codeFenceExample = '```This is how you can write ~strikethrough~, *bold*, _italics_, and [links](https://www.expensify.com)```'; expect(parser.replace(codeFenceExample)).toBe('
This is how you can write ~strikethrough~, *bold*, _italics_, and [links](https://www.expensify.com)
'); }); diff --git a/lib/ExpensiMark.js b/lib/ExpensiMark.js index fc3e36b4..78d1ff7c 100644 --- a/lib/ExpensiMark.js +++ b/lib/ExpensiMark.js @@ -24,7 +24,7 @@ export default class ExpensiMark { name: 'codeFence', // < is a backtick symbol we are matching on three of them before then after a new line character - regex: /```\n((?:(?!```)[\s\S])+)\n```/g, + regex: /(```[\n]?)((?:(?![\n]?```)[\s\S])+)([\n]?```)/g, // We're using a function here to perform an additional replace on the content // inside the backticks because Android is not able to use
 tags and does
@@ -32,8 +32,8 @@ export default class ExpensiMark {
                 // with the new lines here since they need to be converted into 
. And we don't // want to do this anywhere else since that would break HTML. //   will create styling issues so use - replacement: (match, firstCapturedGroup) => { - const group = firstCapturedGroup.replace(/(?:(?![\n\r])\s)/g, ' '); + replacement: (match, _, textWithinFences) => { + const group = textWithinFences.replace(/(?:(?![\n\r])\s)/g, ' '); return `
${group}
`; }, },