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

Use new logic to check bold text #710

Merged
merged 1 commit into from
Jul 8, 2024
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
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
Loading