Skip to content
Pavel edited this page Oct 23, 2026 · 15 revisions

English | Русский

MarkdownEditor from the second package edits the source of its document right where it is drawn. It is the viewer with everything the viewer can do, plus the source in AvalonEdit: the blocks under the caret show their Markdown, and the rest of the document stays drawn.

The block under the caret shows its source

Install

Terminal, in the folder of your application project:

dotnet add package Beresta.Markdown.Wpf.Editor

The package brings the viewer of the same version, AvalonEdit, and ReverseMarkdown for pasting HTML as Markdown. Merge the theme of the editor into the application resources after the theme of the viewer: it holds the keys of the source look.

App.xaml, the application resources:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Beresta.Markdown.Wpf;component/Themes/Generic.xaml" />
            <ResourceDictionary Source="pack://application:,,,/Beresta.Markdown.Wpf.Editor;component/Themes/Generic.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Edit a document

The editor lives in your scrolling like the viewer. The XAML namespace is the same: xmlns:md="https://github.com/porohkun/Beresta.Markdown.Wpf".

MainWindow.xaml, the window content; Text is a string property of your view model:

<ScrollViewer>
    <md:MarkdownEditor x:Name="Editor"
                       Markdown="{Binding Text, Mode=TwoWay}"
                       EditMode="Blocks"
                       Margin="24,16" />
</ScrollViewer>

The source lives in the document of AvalonEdit. The edited text goes to Markdown after every edit, and a two-way binding hands it to your application at once: read it whenever you like, before saving too. The drawing catches up with the text a second after the last edit, at least every five seconds while typing goes on, and at once when the editor loses the keyboard focus; CommitText() draws it at once.

A new Markdown from your application goes into AvalonEdit the way AvalonEdit takes a new text: the caret goes to the start, the undo history is cleared, the edited blocks are drawn again — right for another document. With MergeExternalText="True" a text that differs lands as one edit of the part between the common start and end: the caret and the selection stay on their text, the edit is one step of undo, the edited blocks stay open, and the page does not follow the caret — right for the same document changed from outside.

MainWindow.xaml, a page that the application also changes:

<md:MarkdownEditor Markdown="{Binding Text, Mode=TwoWay}" EditMode="Full" MergeExternalText="True" />

Modes

EditMode chooses how the editor edits:

Mode What is seen What is edited
View — by default the drawing nothing, like the viewer
Controls the drawing task boxes and folding write into the source — see Task Lists
Blocks the drawing; the blocks under the caret and the selection — their source the edited blocks; outside them task boxes and folding, as in Controls
Full the whole source in place of the drawing the whole source

The whole source in place of the drawing

In Blocks, the edited blocks are the blocks under the caret and the selection, always whole. In a list it is the item, and the rest of the list stays drawn; in a quote, a callout or a collapsible block — the block inside, and the container stays drawn around it. The line of a marker belongs to its item and the title line to its callout: the caret there makes the whole item or callout edited, and so does a collapsed callout. A selection across containers makes their common parent edited. Lines the viewer does not draw — link definitions, comments, hidden front matter — are edited together with the neighbouring block.

A list item as its source

While you type the edited blocks only grow: a blank line that splits a paragraph does not cut them; when the caret leaves, the blocks it left are drawn again. An unclosed code fence turns everything below into code, and the edited blocks grow to cover it.

Switching the mode keeps the caret and the scrolling. In Blocks nothing is edited until a click or a key; leaving to View draws the typed text at once.

Keys and mouse

Action What happens
a click on the drawing in Blocks the caret goes to the character of the source under the mouse — where the drawing differs from the source, to the start of that piece; off the text, to the start of the source line — and its block becomes edited; dragging selects the source and makes the blocks under the mouse edited
a double click on the drawing in Blocks the block becomes edited, and the word under the mouse is selected in the source
Ctrl+click on a link, an image, a card, a copy button as in the viewer
a click on a task box or a folding header writes into the source; the caret does not move
arrows, Ctrl+Home, Ctrl+End across the whole document; the block the caret comes to becomes edited
PageUp, PageDown by the height of the screen, not of the edited blocks
Ctrl+A selects the whole source, and it all becomes edited
Esc, the editor loses focus the edited blocks are drawn again; the typed text stays and is drawn at once
Enter in a list item a new item with the same marker: the next number, - [ ] after a task; in an item with only a marker — leaves the list. Inside a quote or a callout the new item keeps the > of the line
Enter in a quote the new line starts with the same >; on a line with only > — leaves the quote, one level at a time
Tab, Shift+Tab in a list item moves the item with all its lines under the text of the item above, or back to its parent; a selection across several items moves each of them
Enter, Tab, Shift+Tab, and Backspace or Delete that remove a line of an item the items of the numbered list below get numbers in a row after the item above; the delimiter . or ) and the column of the text stay; a list where every item has the same number, 1., 1., 1., is left as it is
Ctrl+B, Ctrl+I ** or * around the selection, or around the word under the caret; outside a word — a pair with the caret between. A selection already framed by them, outside or inside, loses them. A selection across lines is framed line by line, without the markers of items, quotes and headings and without blank lines. The caret and the selection stay on the same text; EditingCommands.ToggleBold and ToggleItalic do the same from a button
typing ``` or ~~~ at the start of a line the fence closes on the next line if nothing below closes it; a longer opening fence lengthens the closing one too
Ctrl+V pastes plain text only
Ctrl+Shift+V pastes the HTML from the clipboard as Markdown in place of the selection — see Paste with formatting
Ctrl+Z, Ctrl+Y undo and redo across the whole document; the caret goes to the undone place; a key and the renumbering it caused are one step

When the edited blocks change, the page does not move: the line nearest to the caret that is shown the same way before and after — drawn both times or edited both times — keeps its place on the screen.

Paste with formatting

Ctrl+Shift+V and «Paste with formatting» in the menu of the source turn the HTML a browser or an office program puts into the clipboard into Markdown: headings, bold and italic, links, lists, tables, code with its language, images, quotes; an unknown tag gives its text. HTML that does not convert is pasted as text. Without HTML in the clipboard the text is pasted as by Ctrl+V. The paste is one step of undo; from a button of yours it is MarkdownEditor.PasteFormattedCommand.

A right click on the source opens its menu — Cut, Copy, Paste, Paste with formatting, Select all; the drawing keeps the menu of the viewer. The menu is the resource Md.Source.ContextMenu of the editor theme, its captions are the keys Md.Source.ContextMenu.Cut, .Copy, .Paste, .PasteFormatted and .SelectAll, English by default — see Theming.

Undo and the source from code

Members What for
Undo(), Redo(), CanUndo, CanRedo undo of AvalonEdit: typing, task boxes, folding and fields of the properties card are in it; the caret goes to the undone place
SourceCaretOffset, SourceSelectionStart, SourceSelectionLength, SelectSource(start, length) the caret and the selection in the source; in Blocks their blocks become edited. SourceSelectionStart and SourceSelectionLength are dependency properties that bind both ways: a toolbar of a view model reads and sets the selection through them. SelectionStart and SelectionLength of the viewer are the selection in the drawing
SourceSelectionChanged — event the caret or the selection in the source moved
InsertText(text) text in place of the selection or at the caret
ReplaceSource(start, length, text) text in place of a piece of the source in one step of undo; the caret and the selection stay on their text, and the page does not follow the caret
CommitText() the edited text is drawn at once, without the pause after the last edit
SourceChanged — event every edit of the source: the place, how much went away, what came in
TextEditor AvalonEdit itself: its options and everything else

MainWindow.xaml.cs, a button that inserts a link:

private void OnInsertLink(object sender, RoutedEventArgs e) => Editor.InsertText("[text](https://example.com)");

MainWindow.xaml, the selection of the source in the view model:

<md:MarkdownEditor Markdown="{Binding Text, Mode=TwoWay}"
                   SourceSelectionStart="{Binding SelectionStart}"
                   SourceSelectionLength="{Binding SelectionLength}"
                   EditMode="Full" />

A click on a task box asks for the edit with the routed event SourceEditRequested, as in the viewer. The editor puts the edit into AvalonEdit with its undo, unless your handler of the event sets Handled.

Front matter

With PropertiesCard front matter is drawn as a card. FrontMatterEditing chooses how it is edited:

Value Where it is edited
Text — by default in Blocks and Full — as text, like any other block; in Controls — in the fields of the card
Fields in the fields of the card in every mode that edits; in Full the source starts after front matter
None nowhere: the card only shows; in Full the source starts after front matter

Fields of the properties card

The card edits a value by its type: text in a field, a flag with a check box, a date with DatePicker, a list with chips; a cross removes a property, and the last row adds one. A value is written the same way it was written: plain stays plain while it is safe, quotes stay quotes, a list stays a list — see Media and Data.

MainWindow.xaml:

<md:MarkdownEditor Markdown="{Binding Text, Mode=TwoWay}" EditMode="Blocks" FrontMatterEditing="Fields">
    <md:MarkdownEditor.Extensions>
        <md:PropertiesCard />
    </md:MarkdownEditor.Extensions>
</md:MarkdownEditor>

The look of the source

The source is drawn with the keys of the viewer theme: headings take their size and weight, code — its font and colors, links — their color, quotes — their bar, callouts — the bar and the kind in the title in the color of their kind. Code is highlighted as in the drawing: the editor takes MdAvalonCodeHighlighter unless CodeHighlighter is set — see Code Blocks. A property of a look without its own key comes from the setter of its style Md.<look>.Style: styled text of extensions looks in the source as in the drawing. Selected text takes the selection colors of AvalonEdit, TextArea.SelectionBrush and SelectionForeground, without the background of its look. The editor theme adds two keys of its own:

Key What it is
Md.Source.Markup.Foreground the color of markup: # of headings, **, backticks, list markers, link brackets and addresses, | of tables
Md.Source.Background the background of the edited blocks

App.xaml, after the editor theme:

<SolidColorBrush x:Key="Md.Source.Markup.Foreground" Color="#999999" />

An edited block keeps the height of its drawing, so the text below does not move when the block turns into its source: a heading line takes the margins of the heading, Md.Heading{N}.Margin; the lines of a look with the key Md.<look>.Margin take its top margin on the first line and the bottom one on the last. A callout takes its margin, border and padding the same way, its title line is at least as tall as the icon, Md.Icon.Size with Md.Icon.Margin, and the first line of its body takes Md.Callout.ContentMargin; a folded callout grows by its body, which the source shows. A code block takes its border and padding, Md.CodeBlock.BorderThickness and Md.CodeBlock.Padding, and a fenced one also the header with its language, Md.CodeBlock.HeaderFontSize and Md.CodeBlock.HeaderMargin; a table takes its border and the padding of its cells, Md.Table.BorderThickness and Md.Table.CellPadding. The drawing has no fences and no delimiter row: fences take the room of these margins, so the lines of code stand a little higher than in the drawing, and the rows of a table share their margins with the delimiter row evenly. A blank line between blocks, empty, of spaces or of the > of a quote, takes the height of its space in the drawing, Md.BlankLine.Height, and an empty line of code — the height of a line of code. A wrapped line of a list item or a quote starts after its marker, under the text.

A table stands in columns without spaces in its text: before each | the editor leaves room up to the edge of the column — by the widest cell with its formatting. A column ---: is aligned to the right, :---: — to the center. The room is not text: the caret skips it, and it does not get into the selection or a copy; a click in it puts the caret at the end of the cell. A table wider than the editor stays as typed, and a row the parse does not take for a row of the table yet is not aligned. Tables from CSV stand in columns by their separators.

A table in its source

The source of a widget of your extension looks by what the widget writes into the text map, and the extension can describe it over that — see Extensions.

Good to know

  • The editor sets AreControlsEditable, IsSelectionEnabled and IsFrontMatterEditable of the viewer by its mode; set the mode, not them.
  • A long text may be parsed in the background, as in the viewer — see Live Preview: until the new parse lands, the drawing shows the previous text.
  • Search and replace in the source is MdSourceSearch — see Marks and Search.

Beresta.Markdown.Wpf

Basics

Working with Text

Appearance

Integration

Editing

Your Own Syntax

Ready-made Extensions


По-русски

Основы

Работа с текстом

Оформление

Встраивание в приложение

Правка

Свой синтаксис

Готовые расширения

Clone this wiki locally