Skip to content

Commit

Permalink
Merge pull request #710 from ShridharGoel/patch-1
Browse files Browse the repository at this point in the history
Use new logic to check bold text
  • Loading branch information
jasperhuangg authored Jul 8, 2024
2 parents ec876f2 + 05a015d commit 002c5e1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
12 changes: 12 additions & 0 deletions __tests__/ExpensiMark-Markdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ test('Test multi-line bold HTML replacement', () => {
expect(parser.htmlToMarkdown(testString)).toBe(replacedString);
});

test('Converts <b> tags with font-weight 700 inline style in between to markdown bold', () => {
const input = '<b><span style="font-weight:400;">This is a text with </span><span style="font-weight:700;">nested bold</span><span style="font-weight:400;"> content</span></b>';
const expected = 'This is a text with *nested bold* content';
expect(parser.htmlToMarkdown(input)).toBe(expected);
});

test('Does not convert <b> tags with font-weight normal inline style to markdown bold', () => {
const input = '<b><span style="font-weight:400;">This is a text with </span><span style="font-weight:normal;">no bold</span><span style="font-weight:400;"> content</span></b>';
const expected = 'This is a text with no bold content';
expect(parser.htmlToMarkdown(input)).toBe(expected);
});

test('Test italic HTML replacement', () => {
const italicTestStartString = 'This is a <em>sentence,</em> and it has some <em>punctuation, words, and spaces</em>. <em>test</em> _ testing_ test_test_test. _ test _ _test _ '
+ 'This is a <i>sentence,</i> and it has some <i>punctuation, words, and spaces</i>. <i>test</i> _ testing_ test_test_test. _ test _ _test _';
Expand Down
28 changes: 27 additions & 1 deletion lib/ExpensiMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,33 @@ export default class ExpensiMark {
{
name: 'bold',
regex: /<(b|strong)(?:"[^"]*"|'[^']*'|[^'">])*>([\s\S]*?)<\/\1>(?![^<]*(<\/pre>|<\/code>))/gi,
replacement: '*$2*',
replacement: (extras, match, tagName, innerContent) => {
// To check if style attribute contains bold font-weight
const isBoldFromStyle = (style: string | null) => {
return style ? style.replace(/\s/g, '').includes('font-weight:bold;') || style.replace(/\s/g, '').includes('font-weight:700;') : false;
};

const updateSpacesAndWrapWithAsterisksIfBold = (content: string, isBold: boolean) => {
const trimmedContent = content.trim();
const leadingSpace = content.startsWith(' ') ? ' ' : '';
const trailingSpace = content.endsWith(' ') ? ' ' : '';
return isBold ? `${leadingSpace}*${trimmedContent}*${trailingSpace}` : content;
};

// Determine if the outer tag is bold
const styleAttributeMatch = match.match(/style="(.*?)"/);
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 isNestedBold = isBoldFromStyle(nestedStyleMatch ? nestedStyleMatch[1] : null);
return updateSpacesAndWrapWithAsterisksIfBold(nestedContent, isNestedBold);
});

return updateSpacesAndWrapWithAsterisksIfBold(processedInnerContent, isBold);
},
},
{
name: 'strikethrough',
Expand Down

0 comments on commit 002c5e1

Please sign in to comment.