Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bold tag isn't rendered as markdown when it has a style #783

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions __tests__/ExpensiMark-Markdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ test('Test bold HTML replacement', () => {
const boldTestStartString = 'This is a <strong>sentence,</strong> and it has some <strong>punctuation, words, and spaces</strong>. '
+ '<strong>test</strong> * testing* test*test*test. * testing * *testing * '
+ 'This is a <b>sentence,</b> and it has some <b>punctuation, words, and spaces</b>. '
+ 'This is a <b style="font-size: 20px;">bold sentence with style</b> '
+ '<b>test</b> * testing* test*test*test. * testing * *testing *';
const boldTestReplacedString = 'This is a *sentence,* and it has some *punctuation, words, and spaces*. '
+ '*test* * testing* test*test*test. * testing * *testing * '
+ 'This is a *sentence,* and it has some *punctuation, words, and spaces*. '
+ 'This is a *bold sentence with style* '
+ '*test* * testing* test*test*test. * testing * *testing *';

expect(parser.htmlToMarkdown(boldTestStartString)).toBe(boldTestReplacedString);
Expand Down
5 changes: 3 additions & 2 deletions lib/ExpensiMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,13 +576,14 @@ export default class ExpensiMark {
};

// Determine if the outer tag is bold
const styleAttributeMatch = match.match(/style="(.*?)"/);
const fontWeightRegex = /style="([^"]*?\bfont-weight:\s*(\d+|bold|normal)[^"]*?)"/;
const styleAttributeMatch = match.match(fontWeightRegex);
const isFontWeightBold = isBoldFromStyle(styleAttributeMatch ? styleAttributeMatch[1] : null);
const isBold = styleAttributeMatch ? isFontWeightBold : tagName === 'b' || tagName === 'strong';

// Process nested spans with potential bold style
const processedInnerContent = innerContent.replace(/<span(?:"[^"]*"|'[^']*'|[^'">])*>([\s\S]*?)<\/span>/gi, (nestedMatch, nestedContent) => {
const nestedStyleMatch = nestedMatch.match(/style="(.*?)"/);
const nestedStyleMatch = nestedMatch.match(fontWeightRegex);
const isNestedBold = isBoldFromStyle(nestedStyleMatch ? nestedStyleMatch[1] : null);
return updateSpacesAndWrapWithAsterisksIfBold(nestedContent, isNestedBold);
});
Expand Down
Loading