markdown.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #pragma once
  2. // License: zlib
  3. // Copyright (c) 2019 Juliette Foucaut & Doug Binks
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. // 2. Altered source versions must be plainly marked as such, and must not be
  18. // misrepresented as being the original software.
  19. // 3. This notice may not be removed or altered from any source distribution.
  20. /*
  21. imgui_markdown https://github.com/juliettef/imgui_markdown
  22. Markdown for Dear ImGui
  23. A permissively licensed markdown single-header library for https://github.com/ocornut/imgui
  24. imgui_markdown currently supports the following markdown functionality:
  25. - Wrapped text
  26. - Headers H1, H2, H3
  27. - Indented text, multi levels
  28. - Unordered lists and sub-lists
  29. - Links
  30. Syntax
  31. Wrapping:
  32. Text wraps automatically. To add a new line, use 'Return'.
  33. Headers:
  34. # H1
  35. ## H2
  36. ### H3
  37. Indents:
  38. On a new line, at the start of the line, add two spaces per indent.
  39. Indent level 1
  40. Indent level 2
  41. Unordered lists:
  42. On a new line, at the start of the line, add two spaces, an asterisks and a space.
  43. For nested lists, add two additional spaces in front of the asterisk per list level increment.
  44. * Unordered List level 1
  45. * Unordered List level 2
  46. Links:
  47. [link description](https://...)
  48. ===============================================================================
  49. // Example use on Windows with links opening in a browser
  50. #include "ImGui.h" // https://github.com/ocornut/imgui
  51. #include "imgui_markdown.h" // https://github.com/juliettef/imgui_markdown
  52. #include "IconsFontAwesome5.h" // https://github.com/juliettef/IconFontCppHeaders
  53. // Following includes for Windows LinkCallback
  54. #define WIN32_LEAN_AND_MEAN
  55. #include <Windows.h>
  56. #include "Shellapi.h"
  57. #include <string>
  58. // You can make your own Markdown function with your prefered string container and markdown config.
  59. static ImGui::MarkdownConfig mdConfig{ LinkCallback, ICON_FA_LINK, { NULL, true, NULL, true, NULL, false } };
  60. void LinkCallback( const char* link_, uint32_t linkLength_ )
  61. {
  62. std::string url( link_, linkLength_ );
  63. ShellExecuteA( NULL, "open", url.c_str(), NULL, NULL, SW_SHOWNORMAL );
  64. }
  65. void LoadFonts( float fontSize_ = 12.0f )
  66. {
  67. ImGuiIO& io = ImGui::GetIO();
  68. io.Fonts->Clear();
  69. // Base font
  70. io.Fonts->AddFontFromFileTTF( "myfont.ttf", fontSize_ );
  71. // Bold headings H2 and H3
  72. mdConfig.headingFormats[ 1 ].font = io.Fonts->AddFontFromFileTTF( "myfont-bold.ttf", fontSize_ );
  73. mdConfig.headingFormats[ 2 ].font = mdConfig.headingFormats[ 1 ].font;
  74. // bold heading H1
  75. float fontSizeH1 = fontSize_ * 1.1f;
  76. mdConfig.headingFormats[ 0 ].font = io.Fonts->AddFontFromFileTTF( "myfont-bold.ttf", fontSizeH1 );
  77. }
  78. void Markdown( const std::string& markdown_ )
  79. {
  80. // fonts for, respectively, headings H1, H2, H3 and beyond
  81. ImGui::Markdown( markdown_.c_str(), markdown_.length(), mdConfig );
  82. }
  83. void MarkdownExample()
  84. {
  85. const std::string markdownText = u8R"(
  86. # H1 Header: Text and Links
  87. You can add [links like this one to enkisoftware](https://www.enkisoftware.com/) and lines will wrap well.
  88. ## H2 Header: indented text.
  89. This text has an indent (two leading spaces).
  90. This one has two.
  91. ### H3 Header: Lists
  92. * Unordered lists
  93. * Lists can be indented with two extra spaces.
  94. * Lists can have [links like this one to Avoyd](https://www.avoyd.com/)
  95. )";
  96. Markdown( markdownText );
  97. }
  98. ===============================================================================
  99. */
  100. #include <stdint.h>
  101. namespace ImGui
  102. {
  103. // Configuration struct for Markdown
  104. // * linkCallback is called when a link is clicked on
  105. // * linkIcon is a string which encode a "Link" icon, if available in the current font (e.g. linkIcon = ICON_FA_LINK with FontAwesome + IconFontCppHeaders https://github.com/juliettef/IconFontCppHeaders)
  106. // * HeadingFormat controls the format of heading H1 to H3, those above H3 use H3 format
  107. // * font is the index into the ImGui font array
  108. // * separator controls whether an underlined separator is drawn after the header
  109. struct MarkdownConfig
  110. {
  111. typedef void MarkdownLinkCallback( const char* link_, uint32_t linkLength_ );
  112. struct HeadingFormat{ ImFont* font; bool separator; };
  113. static const int NUMHEADINGS = 3;
  114. MarkdownLinkCallback* linkCallback = 0;
  115. const char* linkIcon = "";
  116. HeadingFormat headingFormats[ NUMHEADINGS ] = { { NULL, true }, { NULL, true }, { NULL, true } };
  117. };
  118. // External interface
  119. void Markdown( const char* markdown_, int32_t markdownLength_, const MarkdownConfig& mdConfig_ );
  120. }