demo_imgui_md.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // Part of ImGui Bundle - MIT License - Copyright (c) 2022-2026 Pascal Thomet - https://github.com/pthom/imgui_bundle
  2. #include "hello_imgui/hello_imgui.h"
  3. #include "imgui_md_wrapper/imgui_md_wrapper.h"
  4. #include "immapp/immapp.h"
  5. std::string exampleMarkdownString()
  6. {
  7. std::string md = R"(
  8. # Dear ImGui Bundle — Markdown tour
  9. `imgui_md` renders markdown directly inside an ImGui window — no browser,
  10. no HTML, no external renderer.
  11. > [!TIP]
  12. > Use this demo as a reference when writing markdown in your own application.
  13. > Expand the *"Show source"* sections to get copyable snippets.
  14. ---
  15. # Basics
  16. <details open>
  17. <summary>Text and typography</summary>
  18. All the usual inline styling works:
  19. - *emphasis*, **bold**, ***both***, ~~strikethrough~~, <u>underlined</u>
  20. - <mark>highlighted passages</mark> for things that need to stand out
  21. - Inline `code` — handy for tokens, flags, and short snippets
  22. HTML-like spans render natively too, no callbacks needed:
  23. - Keyboard shortcuts read naturally: press <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd>, or <kbd>Cmd</kbd>+<kbd>K</kbd> on a Mac
  24. - Chemistry: H<sub>2</sub>O, CO<sub>2</sub>, C<sub>8</sub>H<sub>10</sub>N<sub>4</sub>O<sub>2</sub>
  25. - Exponents: x<sup>2</sup> + y<sup>2</sup> = r<sup>2</sup>
  26. For any HTML span not in the default set, wire `MarkdownCallbacks.on_html_span`.
  27. <details>
  28. <summary>Show source</summary>
  29. ```
  30. *emphasis*, **bold**, ***both***, ~~strikethrough~~, <u>underlined</u>
  31. <mark>highlighted</mark>, inline `code`
  32. <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd>
  33. H<sub>2</sub>O, x<sup>2</sup> + y<sup>2</sup> = r<sup>2</sup>
  34. ```
  35. </details>
  36. </details>
  37. <details>
  38. <summary>Structure: headers, lists, quotes, rules</summary>
  39. Markdown handles the bones of a document out of the box.
  40. **Headers** go from `#` (H1) to `###` (H3); all render as ImGui-styled headings.
  41. <details>
  42. <summary>Test Headers</summary>
  43. # Title 1
  44. A quick intro in normal text.
  45. ## Title 2
  46. ### Title 3
  47. #### Title 4
  48. ##### Title 5
  49. </details>
  50. **Ordered and unordered lists**, including nesting:
  51. 1. First
  52. 2. Second
  53. - Nested bullet
  54. - Another one
  55. 1. Deeper still
  56. 3. Third
  57. **Blockquotes** for callouts that aren't admonitions:
  58. > Markdown inside an ImGui window — the best of both worlds.
  59. **Horizontal rules** to separate sections (three dashes on a line):
  60. ---
  61. <details>
  62. <summary>Show source</summary>
  63. ```
  64. # H1
  65. ## H2
  66. ### H3
  67. 1. First
  68. 2. Second
  69. - Nested bullet
  70. 1. Deeper still
  71. > A blockquote.
  72. ---
  73. ```
  74. </details>
  75. </details>
  76. <details>
  77. <summary>Links and autolinks</summary>
  78. Explicit markdown link syntax: [Dear ImGui Bundle](https://github.com/pthom/imgui_bundle).
  79. Autolinks turn bare URLs, `www.` hosts, and email addresses into clickable
  80. links automatically:
  81. - https://github.com/pthom/imgui_bundle
  82. - www.dearimgui.org
  83. - Contact: pthomet@gmail.com
  84. Disable with `MarkdownOptions.autolinks = False` for strict CommonMark
  85. behavior.
  86. <details>
  87. <summary>Show source</summary>
  88. ```
  89. [Dear ImGui Bundle](https://github.com/pthom/imgui_bundle)
  90. https://github.com/pthom/imgui_bundle
  91. www.dearimgui.org
  92. Contact: pthomet@gmail.com
  93. ```
  94. </details>
  95. </details>
  96. <details>
  97. <summary>Images</summary>
  98. Images load from local assets with a relative path:
  99. ![World](images/world.png)
  100. Remote URLs load asynchronously — a spinner is shown while downloading:
  101. ![Photo](https://picsum.photos/id/1018/300/200)
  102. Use `<img>` when you need to control the size:
  103. <img src="https://picsum.photos/id/237/300/200" width="100">
  104. > *Remote image work by default when using python and C++/emscripten.
  105. > On desktop/C++ an async download callback needs to be provided.*
  106. <details>
  107. <summary>Show source</summary>
  108. ```
  109. ![World](images/world.png)
  110. ![Photo](https://picsum.photos/id/1018/300/200)
  111. <img src="https://picsum.photos/id/237/300/200" width="100">
  112. ```
  113. </details>
  114. </details>
  115. # Tables and code blocks
  116. <details>
  117. <summary>Code blocks</summary>
  118. **Inline snippets**
  119. Inline snippets are rendered like code inside a regular paragraph, like this: `result = 37`.
  120. They are written like this:
  121. <pre>
  122. `result = 37`
  123. </pre>
  124. **Code blocks**
  125. Code blocks preserve layout and use a monospaced font.
  126. A small Python example:
  127. ```python
  128. from imgui_bundle import imgui, immapp
  129. def gui():
  130. imgui.text("Hello, World!")
  131. immapp.run(gui, window_title="My App")
  132. ```
  133. And its C++ equivalent:
  134. ```cpp
  135. #include "imgui.h"
  136. #include "immapp/immapp.h"
  137. void gui() {
  138. ImGui::Text("Hello, World!");
  139. }
  140. int main() {
  141. ImmApp::Run(gui);
  142. }
  143. ```
  144. Code blocks are delimited by three backticks, plus an optional language. See example below:
  145. <pre>
  146. ```python
  147. int main()
  148. {
  149. return 0;
  150. }
  151. ```
  152. </pre>
  153. </details>
  154. <details>
  155. <summary>Tables</summary>
  156. Columns are resizable at runtime — grab a column border and drag. First-row
  157. widths drive column layout, so use `&nbsp;` in that row to enforce a
  158. minimum width where needed.
  159. Column alignment is controlled by colons in the separator row:
  160. ```
  161. :--- left
  162. ---: right
  163. :---: centre
  164. ```
  165. | Continent | Population | Countries |
  166. |----------------|--------------:|:---------:|
  167. | Africa | 1300 million | 54 |
  168. | Asia | 4500 million | 48 |
  169. | Europe | 743 million | 44 |
  170. | North America | 579 million | 23 |
  171. | Oceania | 41 million | 14 |
  172. | South America | 422 million | 12 |
  173. | Antarctica | 0 | 0 |
  174. <details>
  175. <summary>Show source</summary>
  176. ```
  177. | Continent | Population | Countries |
  178. |----------------|--------------:|:---------:|
  179. | Africa | 1300 million | 54 |
  180. | Asia | 4500 million | 48 |
  181. ```
  182. </details>
  183. </details>
  184. # Useful extensions
  185. <details>
  186. <summary>GitHub-style admonitions</summary>
  187. Blockquotes that start with `[!NOTE]`, `[!TIP]`, `[!IMPORTANT]`, `[!WARNING]`
  188. or `[!CAUTION]` render as coloured callouts — great for in-app help and
  189. onboarding:
  190. > [!NOTE]
  191. > A note provides useful context that a reader should know.
  192. > [!TIP]
  193. > A small hint that saves the reader time.
  194. > [!IMPORTANT]
  195. > Required information to complete a task.
  196. > [!WARNING]
  197. > Heads up: something can subtly go wrong here.
  198. > [!CAUTION]
  199. > A negative outcome is likely without care.
  200. <details>
  201. <summary>Show source</summary>
  202. ```
  203. > [!NOTE]
  204. > A note provides useful context that a reader should know.
  205. > [!WARNING]
  206. > Heads up: something can subtly go wrong here.
  207. ```
  208. </details>
  209. </details>
  210. <details>
  211. <summary>Task lists</summary>
  212. GitHub-style task lists render as checkbox glyphs. Handy for changelogs
  213. and roadmap-style content embedded inside your app:
  214. - [x] Design the UI
  215. - [x] Wire up the data layer
  216. - [x] Write the onboarding flow
  217. - [ ] Polish documentation
  218. - [ ] Record a demo video
  219. - [ ] Ship it
  220. <details>
  221. <summary>Show source</summary>
  222. ```
  223. - [x] Done item
  224. - [ ] Pending item
  225. ```
  226. </details>
  227. </details>
  228. <details>
  229. <summary>Math with LaTeX</summary>
  230. Requires `immapp.run(..., with_latex=True)`. Rendering is powered by
  231. [MicroTeX](https://github.com/NanoMichael/MicroTeX).
  232. **Inline math** uses single dollars: Euler's identity $e^{i\pi} + 1 = 0$
  233. is a consequence of the more general $e^{i\theta} = \cos\theta + i\sin\theta$.
  234. **Display math** uses double dollars on their own line. The quadratic
  235. formula:
  236. $$
  237. x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
  238. $$
  239. Sums, integrals and matrices all work:
  240. $$
  241. \int_{-\infty}^{\infty} e^{-x^2}\, dx = \sqrt{\pi}
  242. \qquad
  243. A = \begin{pmatrix} a & b \\ c & d \end{pmatrix}
  244. \qquad
  245. \sum_{i=0}^{n} i = \frac{n(n+1)}{2}
  246. $$
  247. <details>
  248. <summary>Show source</summary>
  249. ```
  250. Inline: $e^{i\pi} + 1 = 0$
  251. Display:
  252. $$
  253. x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
  254. $$
  255. ```
  256. </details>
  257. </details>
  258. <details>
  259. <summary>Preformatted text with the pre tag</summary>
  260. `<pre>` renders a block of monospaced text **without** the styling of a
  261. fenced code block — no background frame, no syntax coloring. Use it
  262. for ASCII layouts, aligned data, and anything where "monospace" is
  263. what you want but "this is source code" is not the right message:
  264. <pre>
  265. Metric Aligned Value
  266. -------- --------- -----
  267. Ping right 12ms
  268. Throughput right 42MB/s
  269. Latency right 87us
  270. </pre>
  271. Same content in a fenced code block, for comparison:
  272. ```
  273. Metric Aligned Value
  274. -------- --------- -----
  275. Ping right 12ms
  276. ```
  277. <details>
  278. <summary>Show source</summary>
  279. ```
  280. <pre>
  281. First line
  282. Indented line
  283. Last line
  284. </pre>
  285. ```
  286. </details>
  287. </details>
  288. ---
  289. # Under the hood: how this page is built
  290. <details>
  291. <summary>It's collapsibles all the way down</summary>
  292. Every section above — and every "Show source" inside them — is a
  293. `<details>` block. The tags render as `CollapsingHeader` widgets,
  294. and blank lines around the opening / closing tags let the inner
  295. content be parsed as regular markdown:
  296. <details>
  297. <summary>A nested collapsible</summary>
  298. Hidden until you click. The content is regular markdown.
  299. - One
  300. - Two
  301. <details>
  302. <summary>Going deeper</summary>
  303. Another level. Indentation doesn't matter; what matters is the blank
  304. lines around the tags.
  305. </details>
  306. </details>
  307. <details>
  308. <summary>Show source</summary>
  309. ```
  310. <details>
  311. <summary>Click me</summary>
  312. Hidden content (regular markdown here).
  313. </details>
  314. ```
  315. </details>
  316. </details>
  317. )";
  318. return md;
  319. }
  320. void demo_imgui_md()
  321. {
  322. ImGuiMd::Render(exampleMarkdownString());
  323. // Note: you may also use:
  324. // ImGuiMd::RenderUnindented(exampleMarkdownString());
  325. // (it will remove the main indentation of the Markdown string before rendering it,
  326. // which is useful when the string is defined inside a function with indentation)
  327. }
  328. // Standalone main(): bypasses the auto-generated main from ibd_add_auto_demo,
  329. // which would only enable withMarkdown. We need withLatex=true (which implies
  330. // withMarkdown=true) so the LaTeX section actually renders.
  331. // When this file is built as part of the demo_imgui_bundle aggregator
  332. // (IMGUI_BUNDLE_BUILD_DEMO_AS_LIBRARY), this main() is excluded.
  333. #ifndef IMGUI_BUNDLE_BUILD_DEMO_AS_LIBRARY
  334. int main(int, char**)
  335. {
  336. HelloImGui::SimpleRunnerParams runnerParams;
  337. runnerParams.guiFunction = demo_imgui_md;
  338. runnerParams.windowTitle = "Dear ImGui Bundle - Markdown demo";
  339. runnerParams.windowSize = {800, 800};
  340. ImmApp::AddOnsParams addons;
  341. addons.withLatex = true; // implies withMarkdown=true
  342. ImmApp::Run(runnerParams, addons);
  343. return 0;
  344. }
  345. #endif