Skip to content

Commit

Permalink
fix bold texts loosing formatting after converting to markdown and ba…
Browse files Browse the repository at this point in the history
…ck to wysiwyg (#15)

* fix bold texts loosing formatting

* add test for bold formatting

* remove unrelated test

* fix emphasized texts loosing formatting

* remove test.html file
  • Loading branch information
Shulammite-Aso committed Jul 21, 2020
1 parent e9e7bb4 commit e6f56ea
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/woofmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
37 changes: 37 additions & 0 deletions test/ui-tests/bold-module.test.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit e6f56ea

Please sign in to comment.