whistyun / MdXaml

Markdown for WPF - alternate version of Markdown.Xaml
MIT License
245 stars 37 forks source link

Different code styles #65

Open GilbertoTheMighty opened 1 year ago

GilbertoTheMighty commented 1 year ago

How to configure syntax style for different codeBlock languages?

<mdxam:MarkdownScrollViewer x:Name="Markdownview"
                                                            HorizontalAlignment="Center" Foreground="White" VerticalAlignment="Stretch"
                                                            VerticalScrollBarVisibility="Disabled" Markdown="{Binding content}" Grid.Column="1" Margin="20,20,20,20" PreviewMouseWheel="Markdownview_MouseWheel">
                                    <mdxam:MarkdownScrollViewer.MarkdownStyle>
                                        <Style TargetType="FlowDocument" BasedOn="{x:Static mdxam:MarkdownStyle.Sasabune}">
                                            <Style.Resources>
                                                <Style TargetType="Paragraph">
                                                    <Style.Triggers>
                                                        <Trigger Property="Tag" Value="Heading1">
                                                            <Setter Property="FontSize" Value="42" />
                                                        </Trigger>

                                                        <Trigger Property="Tag" Value="Heading2">
                                                            <Setter Property="FontSize" Value="20" />
                                                        </Trigger>

                                                        <Trigger Property="Tag" Value="CodeBlock">
                                                            <Setter Property="Margin" Value="15"/>
                                                            <Setter Property="Padding" Value="15"/>
                                                        </Trigger>
                                                    </Style.Triggers>
                                                </Style>
                                                <Style TargetType="Hyperlink">
                                                    <Setter Property="TextDecorations" Value="None" />
                                                </Style>
                                            </Style.Resources>
                                        </Style>
                                    </mdxam:MarkdownScrollViewer.MarkdownStyle>
                                </mdxam:MarkdownScrollViewer>
GilbertoTheMighty commented 1 year ago

image c# code is not highlighted,

whistyun commented 1 year ago

What lang-code do you use for codeblock? For now, MdXaml recognizes only cs to display C# code.

The bellow code is highlighted.
```cs
using System;
class Main{}

The bellow code is not highlighted.

using System;
class Main{}
GilbertoTheMighty commented 1 year ago

Thank you! Using replace on incoming string did the trick. How can I configure styles for different langs in codeblock?

whistyun commented 1 year ago

MdXaml v1.20.1 has been released. This version enables the addition of syntax highlighting rules.

To add syntax highlighting, use MdXamlPlugins.Highlights.

sample: App.xaml

<!-- xmlns:mdplugins="clr-namespace:MdXaml.Plugins;assembly=MdXaml.Plugins" -->

<Application ...>
  <Application.Resources>
    <mdplugins:MdXamlPlugins x:Key="MdXamlPlugins">
      <mdplugins:MdXamlPlugins.Highlights>

        <!-- 
          Create a new alias from the xshd file.
          Multiple alias names can be indicated by separating them with ",".
          In this case pegasus and peg.
        -->
        <mdplugins:Definition Alias="pegasus,peg"   Resource="pack://application:,,,/Asset/Pegasus-Mode.xshd" />

        <!--
          Alias an already defined langcode (in this case javascript).
        -->
        <mdplugins:Definition Alias="typescript,ts" RealName="javascript" />

      </mdplugins:MdXamlPlugins.Highlights>
    </mdplugins:MdXamlPlugins>
  </Application.Resources>
</Application>

markdown sample

```peg
@namespace PegExamples
@classname SignificantWhitespaceParser
@using System.Linq

program <object>
  = s:"Hello" {s}

===

const message="Hello world!"
console.log(message)