Skip to content

Commit a7e3eed

Browse files
committed
Implement suggested changes
1 parent ffdd54a commit a7e3eed

File tree

1 file changed

+40
-60
lines changed

1 file changed

+40
-60
lines changed

imgui_markdown.h

Lines changed: 40 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ ___
227227

228228
#include <stdint.h>
229229

230+
typedef int ImGuiMarkdownFormatFlags;
231+
232+
enum ImGuiMarkdownFormatFlags_
233+
{
234+
ImGuiMarkdownFormatFlags_None = 0,
235+
ImGuiMarkdownFormatFlags_DiscardExtraNewLines = 1 << 0, // (Accurate parsing) Provided markdown will discard all redundant newlines
236+
ImGuiMarkdownFormatFlags_NoNewLineIfHeadingFirstLine = 1 << 1, // (Accurate parsing) Provided markdown will not format a newline after the first line if it is a heading
237+
ImGuiMarkdownFormatFlags_SeperatorDoesNotAdvance = 1 << 2, // (Accurate parsing) Provided markdown will not advance to the next line after formatting a seperator
238+
ImGuiMarkdownFormatFlags_GithubStyle = ImGuiMarkdownFormatFlags_DiscardExtraNewLines | ImGuiMarkdownFormatFlags_NoNewLineIfHeadingFirstLine | ImGuiMarkdownFormatFlags_SeperatorDoesNotAdvance,
239+
ImGuiMarkdownFormatFlags_CommonMarkAll = ImGuiMarkdownFormatFlags_DiscardExtraNewLines | ImGuiMarkdownFormatFlags_NoNewLineIfHeadingFirstLine | ImGuiMarkdownFormatFlags_SeperatorDoesNotAdvance,
240+
};
241+
230242
namespace ImGui
231243
{
232244
//-----------------------------------------------------------------------------
@@ -320,16 +332,16 @@ namespace ImGui
320332
// - headingFormats controls the format of heading H1 to H3, those above H3 use H3 format
321333
struct MarkdownConfig
322334
{
323-
static const int NUMHEADINGS = 3;
335+
static const int NUMHEADINGS = 3;
324336

325-
MarkdownLinkCallback* linkCallback = NULL;
337+
MarkdownLinkCallback* linkCallback = NULL;
326338
MarkdownTooltipCallback* tooltipCallback = NULL;
327-
MarkdownImageCallback* imageCallback = NULL;
328-
const char* linkIcon = ""; // icon displayd in link tooltip
329-
MarkdownHeadingFormat headingFormats[ NUMHEADINGS ] = { { NULL, true }, { NULL, true }, { NULL, true } };
330-
void* userData = NULL;
331-
MarkdownFormalCallback* formatCallback = defaultMarkdownFormatCallback;
332-
bool useComplexFormatting = false; // Set this to true to enable complex Markdown formatting (accurate to GitHub etc.). By default imgui_markdown uses psuedo-Markdown for backwards compatibility.
339+
MarkdownImageCallback* imageCallback = NULL;
340+
const char* linkIcon = ""; // icon displayd in link tooltip
341+
MarkdownHeadingFormat headingFormats[ NUMHEADINGS ] = { { NULL, true }, { NULL, true }, { NULL, true } };
342+
void* userData = NULL;
343+
MarkdownFormalCallback* formatCallback = defaultMarkdownFormatCallback;
344+
ImGuiMarkdownFormatFlags formatFlags = ImGuiMarkdownFormatFlags_None; // Configure this to change how Markdown gets formatted. By default imgui_markdown uses psuedo-Markdown for backwards compatibility.
333345
};
334346

335347
//-----------------------------------------------------------------------------
@@ -542,7 +554,7 @@ namespace ImGui
542554
else if ( c == '\n' )
543555
{
544556
concurrentEmptyNewlines++;
545-
if ( mdConfig_.useComplexFormatting ) // Discard LF newlines by markdown spec
557+
if ( (mdConfig_.formatFlags & ImGuiMarkdownFormatFlags_DiscardExtraNewLines) ) // Discard LF newlines by markdown spec
546558
{
547559
line.lineStart += 1;
548560
continue;
@@ -551,7 +563,7 @@ namespace ImGui
551563
else if ( ( c == '\r' ) && ( (int)markdownLength_ > i + 1 ) && ( markdown_[i + 1] == '\n' ) )
552564
{
553565
concurrentEmptyNewlines++;
554-
if (mdConfig_.useComplexFormatting) // Discard CRLF newlines by markdown spec
566+
if ( (mdConfig_.formatFlags & ImGuiMarkdownFormatFlags_DiscardExtraNewLines) ) // Discard CRLF newlines by markdown spec
555567
{
556568
line.lineStart += 2;
557569
i += 1;
@@ -607,7 +619,7 @@ namespace ImGui
607619
}
608620
}
609621

610-
if ( mdConfig_.useComplexFormatting )
622+
if ( (mdConfig_.formatFlags & ImGuiMarkdownFormatFlags_DiscardExtraNewLines) )
611623
{
612624
// In markdown spec, 2 or more consecutive newlines gets converted to a single blank
613625
// line. The first newline is always digested by this parser so we check for 1 or more here.
@@ -1113,69 +1125,37 @@ namespace ImGui
11131125
{
11141126
fmt = markdownFormatInfo_.config->headingFormats[ markdownFormatInfo_.level - 1 ];
11151127
}
1116-
if ( markdownFormatInfo_.config->useComplexFormatting )
1128+
if (start_)
11171129
{
1118-
if (start_)
1130+
if ( 0 == ( markdownFormatInfo_.config->formatFlags & ImGuiMarkdownFormatFlags_NoNewLineIfHeadingFirstLine ) || !markdownFormatInfo_.firstLine )
1131+
{
1132+
ImGui::NewLine();
1133+
}
1134+
if (fmt.font)
11191135
{
1120-
if (!markdownFormatInfo_.firstLine)
1121-
{
1122-
ImGui::NewLine();
1123-
}
1124-
if (fmt.font)
1125-
{
11261136
#ifdef IMGUI_HAS_TEXTURES // used to detect dynamic font capability: https://github.com/ocornut/imgui/issues/8465#issuecomment-2701570771
1127-
ImGui::PushFont(fmt.font, fmt.fontSize > 0.0f ? fmt.fontSize : fmt.font->LegacySize);
1137+
ImGui::PushFont(fmt.font, fmt.fontSize > 0.0f ? fmt.fontSize : fmt.font->LegacySize);
11281138
#else
1129-
ImGui::PushFont(fmt.font);
1139+
ImGui::PushFont(fmt.font);
11301140
#endif
1131-
}
1132-
}
1133-
else
1134-
{
1135-
if (fmt.separator)
1136-
{
1137-
// In markdown the separator does not advance the cursor
1138-
ImVec2 cursor = ImGui::GetCursorPos();
1139-
ImGui::Separator();
1140-
ImGui::SetCursorPos(cursor);
1141-
}
1142-
if (fmt.font)
1143-
{
1144-
ImGui::PopFont();
1145-
}
1146-
ImGui::NewLine();
11471141
}
11481142
}
11491143
else
11501144
{
1151-
if (start_)
1145+
if (fmt.separator)
11521146
{
1153-
if (fmt.font)
1154-
{
1155-
#ifdef IMGUI_HAS_TEXTURES // used to detect dynamic font capability: https://github.com/ocornut/imgui/issues/8465#issuecomment-2701570771
1156-
ImGui::PushFont(fmt.font, fmt.fontSize > 0.0f ? fmt.fontSize : fmt.font->LegacySize);
1157-
#else
1158-
ImGui::PushFont(fmt.font);
1159-
#endif
1147+
// In markdown the separator does not advance the cursor
1148+
ImVec2 cursor = ImGui::GetCursorPos();
1149+
ImGui::Separator();
1150+
if ( (markdownFormatInfo_.config->formatFlags & ImGuiMarkdownFormatFlags_SeperatorDoesNotAdvance) ) {
1151+
ImGui::SetCursorPos(cursor);
11601152
}
1161-
ImGui::NewLine();
11621153
}
1163-
else
1154+
if (fmt.font)
11641155
{
1165-
if (fmt.separator)
1166-
{
1167-
ImGui::Separator();
1168-
ImGui::NewLine();
1169-
}
1170-
else
1171-
{
1172-
ImGui::NewLine();
1173-
}
1174-
if (fmt.font)
1175-
{
1176-
ImGui::PopFont();
1177-
}
1156+
ImGui::PopFont();
11781157
}
1158+
ImGui::NewLine();
11791159
}
11801160
break;
11811161
}

0 commit comments

Comments
 (0)