Skip to content

Commit

Permalink
fix bold/italic issue
Browse files Browse the repository at this point in the history
  • Loading branch information
CamJN committed Feb 13, 2022
1 parent f2c0489 commit d6514b3
Showing 1 changed file with 28 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) {
if (nextMode === 'markdown') {
if (currentMode === 'html') {
textarea.value = parse('parseHTML', textarea.value).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 {
textarea.value = parse('parseHTML', editable).trim();
}
Expand Down

0 comments on commit d6514b3

Please sign in to comment.