Skip to content

About

Markdown viewers for .NET: one Markdig-based parsing layer with Avalonia and Blazor viewers that render the same text the same way

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Polhem.Markdown

English | 繁體中文

Build CI Quality Gate Status Bugs Vulnerabilities Code Smells Coverage

Markdown viewers for .NET that show the same text the same way on different front ends, and a Markdown source editor for Avalonia. One package parses the text and applies the safety policy; a viewer package for each UI framework renders the result.

Package For
Polhem.Markdown Parsing (with Markdig) and the safety policy shared by the viewers
Polhem.Markdown.Avalonia A viewer control and a source editor control for Avalonia
Polhem.Markdown.Blazor A viewer component for Blazor, rendering semantic HTML styled with CSS variables

The packages are published on nuget.org; the changes of each release are in the CHANGELOG. They are at 0.x: a minor version may still change the API, and the CHANGELOG says when it does.

What the viewers show

The same reply in the Blazor viewer and the Avalonia viewer

Both viewers render the same model, so a reply looks alike in a browser and in a desktop or mobile app. The picture shows the start of the sample text in each.

  • GitHub Flavored Markdown: headings, emphasis, strikethrough, lists and task lists, quotes, tables with column alignment, footnotes, emoji shortcodes such as :wave:, and GitHub alerts (> [!NOTE], > [!TIP], > [!IMPORTANT], > [!WARNING], > [!CAUTION]). YAML front matter is hidden, and a link to a heading of the same text scrolls to it.

  • Code blocks show their language and a copy button, and code in common languages such as C#, JSON, SQL, XML, JavaScript and Python is highlighted. Light and dark themes are both supported:

    A table and highlighted code on a dark theme

  • Mermaid diagrams: flowcharts and sequence diagrams are drawn by the viewer itself, with no script. A diagram of another type shows as code.

    A flowchart with Chinese labels and a sequence diagram

  • Math written as on GitHub, $…$ in a line and $$…$$ or a ```math block on its own, also without a script. Dollar signs in prose, such as prices, stay text.

    Inline math, fractions, sums, cases and a matrix

  • The HTML README files use, such as <div align="center">, <details>, <sup>, <kbd> and <img>, becomes part of the text; any other HTML is dropped, and scripts with their content.

    Centered text, a details section, a dropped script and a footnote

  • Safety: only links with an allowed scheme (by default http, https and mailto) can be clicked, and only images from an allowed scheme (by default https) load. A host can add a scheme of its own and decide what such a link does.

  • Streaming: the text can be set again as a reply arrives; the gallery's replay shows it.

Each viewer's README lists what it supports in detail, and how to set its colors: Avalonia, Blazor.

The editor

The sample text in the editor, beside the viewer showing it

MarkdownEditor in Polhem.Markdown.Avalonia edits Markdown as source, with a live preview in MarkdownViewer beside it, not as rich text.

  • Colours the source the way the viewer reads it: markers, headings, emphasis, inline code, links and their addresses, raw HTML and formulas, and fenced code with the viewer's highlighting.
  • Formats and list keys: ApplyFormat toggles bold, italic, strikethrough, inline code, links, headings, lists, task lists, quotes and code blocks, with Ctrl or Cmd and B, I, K or E for the inline ones. Enter continues a list, and Tab and Shift+Tab move an item in and out.
  • Keeps in step with the viewer: the editor reports the source at the top of its view and scrolls to a source offset; the viewer maps source offsets to positions and back, and a command-click asks for the source of what was clicked. The gallery wires the two together.
  • Works under any theme with no styles from the host, and its links cannot be opened from the source: the viewer checks their scheme.

The Avalonia README has the details. There is no Blazor editor yet.

Samples

samples/MarkdownGallery shows every kind of node on a Semi theme, light or dark, with a replay that types the text in chunks as a streamed reply arrives. Its Edit switch puts the editor beside the viewer, scrolling together.

dotnet run --project samples/MarkdownGallery.Desktop
dotnet run --project samples/MarkdownGallery.Blazor

The Blazor gallery shows the same text, so the two viewers can be compared side by side.

The iOS head is not in the solution, because it needs the iOS workload. On a Mac, build and start it on a simulator:

dotnet build samples/MarkdownGallery.iOS -c Release -p:RuntimeIdentifier=iossimulator-arm64
dotnet build samples/MarkdownGallery.iOS -c Release -t:Run -p:RuntimeIdentifier=iossimulator-arm64 -p:_DeviceName=":v2:udid=<simulator UDID>"

If the build says the iOS workload needs another Xcode version, point DEVELOPER_DIR at that version for the build.

Building

dotnet build Polhem.Markdown.slnx --configuration Release
dotnet test Polhem.Markdown.slnx --configuration Release --no-build

License

MIT

About

Markdown viewers for .NET: one Markdig-based parsing layer with Avalonia and Blazor viewers that render the same text the same way

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages