Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ShridharGoel committed Jun 24, 2024
1 parent a3e0e88 commit dfe69f9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
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
35 changes: 27 additions & 8 deletions lib/ExpensiMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,14 +546,33 @@ export default class ExpensiMark {
},
{
name: 'bold',
regex: /<(b|strong)(?:"[^"]*"|'[^']*'|[^'">])*>([\s\S]*?)<\/\1>(?![^<]*(<\/pre>|<\/code>))/gi,
replacement: (match, tagContent, innerContent) => {
const fontWeightMatch = innerContent.match(/style="(.*?)"/);
const isFontWeightBold = fontWeightMatch
? fontWeightMatch[1].replaceAll(/\s/g, '').includes('font-weight:bold;') || fontWeightMatch[1].replaceAll(/\s/g, '').includes('font-weight:700;')
: false;
const isBold = fontWeightMatch ? isFontWeightBold : true;
return isBold ? `*${innerContent}*` : innerContent;
regex: /<(b|strong|span)(?:"[^"]*"|'[^']*'|[^'">])*>([\s\S]*?)<\/\1>(?![^<]*(<\/pre>|<\/code>))/gi,
replacement: (extras, match, tagName, innerContent) => {
// To check if style attribute contains bold font-weight
const isBoldFromStyle = (style) => {

Check failure on line 552 in lib/ExpensiMark.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter 'style' implicitly has an 'any' type.
return style ? style.replace(/\s/g, '').includes('font-weight:bold;') || style.replace(/\s/g, '').includes('font-weight:700;') : false;
};

const updateSpacesAndWrapWithAsterisksIfBold = (content, isBold) => {

Check failure on line 556 in lib/ExpensiMark.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter 'content' implicitly has an 'any' type.

Check failure on line 556 in lib/ExpensiMark.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter 'isBold' implicitly has an 'any' type.
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);
},
},
{
Expand Down

0 comments on commit dfe69f9

Please sign in to comment.