Repository navigation
How It Renders
English | Русский
The viewer is not a browser: it turns Markdown into WPF controls. This page tells what follows from that, where the document looks the way you are used to from GitHub and where it does not. The full list of Markdown elements and how to turn them on and off is in Supported Markdown.
Markdig parses the text into a tree, the viewer turns the tree into a model of blocks and draws every block with a WPF template: a paragraph is a text block, a table is a grid, a code block is a control with a header and a copy button. There is no browser engine, no HTML, no CSS and no scripts. What follows from it:
- The look is set by resource keys, styles and templates — see Theming and Styles and Templates.
- Only the blocks you see are created; the rest of the document is a light model — see Scrolling and Virtualization.
- The text stays text: one selection runs across blocks, the whole document can be searched and copied — see Selection and Copying.
- The viewer goes to the network only for images, and only with the default image loader — see Images.
Every source line starts a new line on the screen: a line break inside a paragraph is drawn as a break. A browser joins such lines with a space; the viewer by default does not. Every blank line between blocks becomes vertical space. The document looks like its text, and that is what makes line numbers and two panes line up with the source.
JoinSoftLineBreaks="True" joins the lines of a paragraph with a space, as GitHub does; a hard break —
two spaces or a backslash at the end of a line — stays a break. The property is inherited: set it on a
viewer, a window or a list of messages. Copied Markdown joins the lines too, and inside a paragraph
line numbers and empty places lose their exact places — see Line Numbers
and Empty places.
MainWindow.xaml:
<md:MarkdownViewer Markdown="{Binding Text}" JoinSoftLineBreaks="True" />Whatever Markdig recognizes gets on the screen. A construct without its own look is shown as its
content — a :::name container — or as its source text: a formula $x^2$, an HTML tag the viewer does
not know. Only four things are not drawn: link reference definitions [id]: address, abbreviation
definitions *[HTML]: …, HTML comments and front matter.
Nesting has a limit. Quotes, lists, <div> and other containers unfold 32 levels deep, emphasis and
links — 64 levels; anything deeper is shown as its source text. A text Markdig itself refuses to parse
is shown as its source lines, and RenderFailed reports the exception — see
Extensions.
READMEs use a little HTML for what Markdown cannot do: a centered logo, a row of badges, a collapsible
section. The viewer draws such HTML by meaning, the way GitHub does; the tags it knows are listed in
Supported Markdown. An HTML block with any other tag — <table>,
<span> — is shown as its source text.
As on GitHub, Markdown between the tags is parsed only when blank lines separate it from them; text right next to the tags is plain text, and its spaces and line breaks collapse into single spaces.
Any .md file:
<p align="center">
<img src="logo.png" width="200" alt="Logo">
<br>
<b>My Project</b>
</p>
<details>
<summary>Build from source</summary>
Run `dotnet build` in the repository root.
</details>A footnote definition is drawn where it is written, not gathered at the end of the document. A click on
a reference scrolls to the definition, ↩ in the definition scrolls back. A footnote in a tooltip at
its reference comes with the ready-made extension FootnoteTips.
Any .md file:
The viewer is built on Markdig[^parser].
[^parser]: A fast, CommonMark-compliant Markdown parser for .NET.A quote that starts with [!NOTE], [!TIP], [!IMPORTANT], [!WARNING] or [!CAUTION] is a callout,
as on GitHub. A kind GitHub does not know is still a callout here: it is drawn as a note titled with the
name of the kind, [!kitten] gets the title "Kitten". More kinds, own titles and folding come with
ready-made extensions.
The block between --- lines at the very start of the document — usually YAML with a title, tags, a
date — is not drawn: the document starts with what follows it, and source line numbers still count its
lines. The text of the block, unparsed, is in the FrontMatter property; without the block it is
null. The ready-made properties card draws it as a table.
MainWindow.xaml.cs, after the text is set:
Viewer.Markdown = "---\ntitle: Release notes\n---\n\n# 0.6.0";
string? yaml = Viewer.FrontMatter; // "title: Release notes"A web or mail link opens in the system browser or mail client, an anchor #... scrolls inside the
document. Every other link — your own scheme, a file path — is not opened by the viewer: your
application decides, and it can take over any link before that — see
Links and Anchors.
An image takes its size in pixels and shrinks to the width of the text if it is wider. While it loads, and if it fails, its alt text stands in its place — see Images.
Code is not wrapped, and a wide code block scrolls sideways on its own. A table fits the width of the viewer like in a browser: its columns share the width by their content, and the text of cells wraps; it scrolls sideways only when even its longest words do not fit. The mouse wheel over such a block still scrolls the page — see Scrolling and Virtualization.
Beresta.Markdown.Wpf · Issues · MIT License
Basics
- Getting Started
- How It Renders
- Supported Markdown
- Settings and Hooks
- Scrolling and Virtualization
- Comparison
Working with Text
Appearance
Integration
- Application Links
- Images
- Untrusted Text
- Task Lists
- Source Lines and Two Panes
- Coordinates
- Live Preview
Editing
Your Own Syntax
Ready-made Extensions
Основы
- Начало работы
- Как рисует вьюер
- Что поддерживается
- Настройки и перехват
- Прокрутка и виртуализация
- Сравнение
Работа с текстом
Оформление
Встраивание в приложение
- Ссылки приложения
- Картинки
- Чужой текст
- Списки задач
- Строки исходника и две панели
- Координаты
- Живой предпросмотр
Правка
Свой синтаксис
Готовые расширения