Certainly! Let's break this into steps. We'll start with removing the markdown markers (stage 1) and progress to handling multiple styles (stage 2) and different styles in different words (stage 3).
Step 1: Removing Markers
We'll create a utility function to remove markdown markers. Let's focus on the main markers for bold (** or __), italic (* or _), underline (__), code (`), and strikethrough (~~).
Here’s how we can implement the markdown marker removal function:
wxString RemoveMarkdownMarkers(const wxString& text) {
wxString result = text;
// Removing bold markers (both `**` and `__`)
if ((result.StartsWith(BOLD_MARKER) && result.EndsWith(BOLD_MARKER)) ||
(result.StartsWith("__") && result.EndsWith("__"))) {
result = result.Mid(2, result.Length() - 4);
}
// Removing italic markers (`*` and `_`)
if ((result.StartsWith("*") && result.EndsWith("*")) ||
(result.StartsWith("_") && result.EndsWith("_"))) {
result = result.Mid(1, result.Length() - 2);
}
// Removing underline markers
if (result.StartsWith("__") && result.EndsWith("__")) {
result = result.Mid(2, result.Length() - 4);
}
// Removing code markers
if (result.StartsWith("`") && result.EndsWith("`")) {
result = result.Mid(1, result.Length() - 2);
}
// Removing strikethrough markers
if (result.StartsWith("~~") && result.EndsWith("~~")) {
result = result.Mid(2, result.Length() - 4);
}
return result;
}
Now, let’s integrate this function into the FormatTableMarkdownStyle method to remove the markers before applying styles.
Step 2: Update FormatTableMarkdownStyle
Update the method to remove markers before applying styles:
wxRichTextAttr Markdown::FormatTableMarkdownStyle(const wxString& styledText, const wxTextAttr& baseStyle, bool header) {
wxRichTextAttr textAttr = baseStyle;
bool isBold = false, isItalic = false, isCode = false, isUnderline = false, isStrikethrough = false;
wxString text = styledText;
// Check and remove bold markers (`**` or `__`)
if ((text.StartsWith(BOLD_MARKER) && text.EndsWith(BOLD_MARKER)) ||
(text.StartsWith("__") && text.EndsWith("__"))) {
isBold = true;
text = RemoveMarkdownMarkers(text);
}
// Check and remove italic markers (`*` or `_`)
if ((text.StartsWith("*") && text.EndsWith("*")) ||
(text.StartsWith("_") && text.EndsWith("_"))) {
isItalic = true;
text = RemoveMarkdownMarkers(text);
}
// Check and remove underline markers (`__`)
if (text.StartsWith("__") && text.EndsWith("__")) {
isUnderline = true;
text = RemoveMarkdownMarkers(text);
}
// Check and remove code markers (`` ` ``)
if (text.StartsWith("`") && text.EndsWith("`")) {
isCode = true;
text = RemoveMarkdownMarkers(text);
}
// Check and remove strikethrough markers (`~~`)
if (text.StartsWith("~~") && text.EndsWith("~~")) {
isStrikethrough = true;
text = RemoveMarkdownMarkers(text);
}
// Apply styles based on detected markers
if (header || isBold) {
textAttr.SetFontWeight(wxFONTWEIGHT_BOLD);
}
if (isItalic) {
textAttr.SetFontStyle(wxFONTSTYLE_ITALIC);
}
if (isUnderline) {
textAttr.SetFontUnderlined(true);
}
if (isCode) {
textAttr.SetFontWeight(wxFONTWEIGHT_MEDIUM);
textAttr.SetBackgroundColour(baseStyle.GetBackgroundColour().ChangeLightness(60));
textAttr.SetTextColour(baseStyle.GetTextColour().ChangeLightness(140));
}
if (isStrikethrough) {
wxFont font = textAttr.GetFont();
font.MakeStrikethrough();
textAttr.SetFont(font);
}
return textAttr;
}
Step 3: Update CreateAndInsertTable to Use the Updated FormatTableMarkdownStyle
In CreateAndInsertTable, use FormatTableMarkdownStyle to ensure markers are removed:
void Markdown::CreateAndInsertTable(wxRichTextCtrl* richTextChatBox, const wxString& tableData, const wxTextAttr& style) {
wxArrayString rows = wxSplit(tableData, '\n');
if (rows.empty()) return;
wxArrayString headers = wxSplit(rows[0], TABLE_COLUMN_MARKER[0]);
headers = FilterEmptyElements(headers);
wxString::size_type colsCount = headers.GetCount();
wxString::size_type rowsCount = rows.GetCount() - 2; // Exclude header underline
if (colsCount < 2) {
return;
}
wxRichTextTable* table = richTextChatBox->WriteTable(rowsCount, colsCount);
// Headers
for (wxString::size_type col = 0; col < colsCount; ++col) {
wxString cellText = headers[col].Trim(false).Trim();
wxRichTextAttr cellAttr = FormatTableMarkdownStyle(cellText, style, true);
cellAttr.SetAlignment(wxTEXT_ALIGNMENT_CENTRE);
table->GetCell(0, col)->AddParagraph(RemoveMarkdownMarkers(cellText), &cellAttr);
}
for (wxString::size_type row = 1; row < rowsCount; ++row) {
wxArrayString cells = wxSplit(rows[row + 1], TABLE_COLUMN_MARKER[0]); // Skip header row
cells = FilterEmptyElements(cells);
if (cells.GetCount() != colsCount) {
continue;
}
for (wxString::size_type col = 0; col < colsCount; ++col) {
wxString cellText = cells[col].Trim(false).Trim();
wxRichTextAttr cellAttr = FormatTableMarkdownStyle(cellText, style);
cellAttr.SetAlignment(wxTEXT_ALIGNMENT_CENTRE);
table->GetCell(row, col)->AddParagraph(RemoveMarkdownMarkers(cellText), &cellAttr);
}
}
}
Next Steps
This updated code will correctly remove the markdown markers while applying styles. In the next stage, we can focus on handling the cases where multiple markdown styles are applied simultaneously.
Would you like to proceed to adding support for multiple styles applied simultaneously?