diff --git a/src/woofmark.js b/src/woofmark.js index bd44f98..be88f46 100644 --- a/src/woofmark.js +++ b/src/woofmark.js @@ -219,6 +219,34 @@ function woofmark (textarea, options) { textarea.value = parse('parseHTML', textarea.value).trim(); } else { textarea.value = parse('parseHTML', editable).trim(); + // if textarea contains wrongly formatted bold or italic text i.e texts that have space before the closing tag + // E.g **text **, remove the space before the tag and place it after the tag. + const matchWrongBold = /\*\*[A-Z][^*]+ \*\*/gi; + const matchWrongItalic = /_[A-Z][^_]+ _/gi; + + if (textarea.value.match(matchWrongBold)) { + const wrongBoldCount = textarea.value.match(matchWrongBold); + const matchWrongBold2 = /\*\*[A-Z][^*]+ \*\*/i; + + for (let i = 0; i <= wrongBoldCount.length - 1; i++) { + if (textarea.value.match(matchWrongBold2)) { + wrongBoldCount[i] = wrongBoldCount[i].replace(' **', '** ') + textarea.value = textarea.value.replace(matchWrongBold2, wrongBoldCount[i]); + } + } + } + + if (textarea.value.match(matchWrongItalic)) { + const wrongItalicCount = textarea.value.match(matchWrongItalic); + const matchWrongItalic2 = /_[A-Z][^_]+ _/i; + + for (let i = 0; i <= wrongItalicCount.length - 1; i++) { + if (textarea.value.match(matchWrongItalic2)) { + wrongItalicCount[i] = wrongItalicCount[i].replace(' _', '_ ') + textarea.value = textarea.value.replace(matchWrongItalic2, wrongItalicCount[i]); + } + } + } } } else if (nextMode === 'html') { if (currentMode === 'markdown') { diff --git a/test/ui-tests/bold-module.test.js b/test/ui-tests/bold-module.test.js new file mode 100644 index 0000000..cd3b4cd --- /dev/null +++ b/test/ui-tests/bold-module.test.js @@ -0,0 +1,37 @@ +const timeout = process.env.SLOWMO ? 130000 : 40000; +const fs = require('fs'); +beforeAll(async () => { + path = fs.realpathSync('file://../index.html'); + await page.goto('file://' + path, {waitUntil: 'domcontentloaded'}); +}); + +describe('bold module work as expected', () => { + + test('bold text does not loose formatting after converting to markdown and back to rich', async () => { + await page.evaluate(() => document.querySelector('.wk-wysiwyg').innerHTML = ''); + + await page.waitForSelector('.wk-wysiwyg'); + await page.keyboard.type(' not using bold ', {delay: 160}); + await page.keyboard.down('Control'); + await page.keyboard.press('KeyB'); + await page.keyboard.up('Control'); + await page.keyboard.type('using bold ', {delay: 160}); + await page.keyboard.down('Control'); + await page.keyboard.press('KeyB'); + await page.keyboard.up('Control'); + await page.keyboard.type('using new line', {delay: 160}); + + await page.keyboard.down('Control'); + await page.keyboard.press('KeyM'); + await page.keyboard.up('Control'); + await page.keyboard.type('writing in markdown mode', {delay: 160}); + await page.keyboard.down('Control'); + await page.keyboard.press('KeyP'); + await page.keyboard.up('Control'); + await page.keyboard.type('back writing in wysiwyg mode', {delay: 160}); + + const stringIsIncluded = await page.evaluate(() => document.querySelector('.wk-wysiwyg').innerHTML.includes('**using bold **')); + expect(stringIsIncluded).toBe(false); + + }, timeout); +}); \ No newline at end of file