mirror of
https://github.com/xoofx/markdig.git
synced 2026-02-04 05:44:50 +00:00
Compare commits
95 Commits
0.28.0
...
example-lu
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
62907f9314 | ||
|
|
5f80d86265 | ||
|
|
b85cc0daf5 | ||
|
|
76073e81c0 | ||
|
|
5b6621d729 | ||
|
|
9723eda455 | ||
|
|
7228ad5072 | ||
|
|
96f55d0aa6 | ||
|
|
2d69ac4499 | ||
|
|
5e91b9b763 | ||
|
|
e6255de62b | ||
|
|
c7b8772669 | ||
|
|
89a10ee76b | ||
|
|
7676079b4e | ||
|
|
495abab743 | ||
|
|
210b39e8fb | ||
|
|
f09d030fd3 | ||
|
|
f2ca6be7a6 | ||
|
|
94e07d11ce | ||
|
|
263041e899 | ||
|
|
e8f9274b64 | ||
|
|
b32e71aaeb | ||
|
|
f991b2123b | ||
|
|
a4a1a177bc | ||
|
|
59d59694f4 | ||
|
|
caf3c722e1 | ||
|
|
47c64d8815 | ||
|
|
2502fab340 | ||
|
|
17b5500b03 | ||
|
|
b754aef6b0 | ||
|
|
04843a08d2 | ||
|
|
fcc73691b6 | ||
|
|
cb8dc99d96 | ||
|
|
6f45ac0885 | ||
|
|
891e2fca78 | ||
|
|
925d4f9227 | ||
|
|
f0c200fc28 | ||
|
|
70184179b7 | ||
|
|
d69b989810 | ||
|
|
da756f4efe | ||
|
|
e192831db0 | ||
|
|
be3c93a9b0 | ||
|
|
6466f01a80 | ||
|
|
cb26f30f7b | ||
|
|
147c3f059a | ||
|
|
3201699053 | ||
|
|
e86d1ffce5 | ||
|
|
48c979dc74 | ||
|
|
3ae0c8b369 | ||
|
|
ee732e5a42 | ||
|
|
76e25833ad | ||
|
|
53dff53260 | ||
|
|
b2eeaf7185 | ||
|
|
47a22bc5e8 | ||
|
|
89e4c29f9f | ||
|
|
a946c6d0b4 | ||
|
|
e2770d8c11 | ||
|
|
6eacf8a170 | ||
|
|
e11a2630b8 | ||
|
|
4169e538af | ||
|
|
ccf455d316 | ||
|
|
8beb096814 | ||
|
|
6a35ec45b9 | ||
|
|
ed83943ba5 | ||
|
|
9adf60116b | ||
|
|
31904f6c53 | ||
|
|
3f3b3c46b6 | ||
|
|
f3d6c2775b | ||
|
|
bb6ace15b7 | ||
|
|
202ac1e4f9 | ||
|
|
2604239764 | ||
|
|
cc04208b95 | ||
|
|
14ab45cf8f | ||
|
|
e36d4564f1 | ||
|
|
358a5f09ef | ||
|
|
25db6cb414 | ||
|
|
315ffd42ab | ||
|
|
2675b4dd1e | ||
|
|
58d7fae12d | ||
|
|
e16ed79dcd | ||
|
|
9ef5171369 | ||
|
|
0cfe6d7da4 | ||
|
|
fe65c1b187 | ||
|
|
92385ee19a | ||
|
|
9f651feac0 | ||
|
|
d42b297128 | ||
|
|
b7d02cadbb | ||
|
|
6f75b5156c | ||
|
|
61452c91e9 | ||
|
|
b697a03c2b | ||
|
|
9f734ba3c9 | ||
|
|
88cdbf3a17 | ||
|
|
fb9561cf89 | ||
|
|
9145f47f89 | ||
|
|
1862b37bbd |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -242,3 +242,6 @@ _Pvt_Extensions
|
||||
|
||||
# Remove artifacts produced by dotnet-releaser
|
||||
artifacts-dotnet-releaser/
|
||||
|
||||
# Remove .lunet temp folder
|
||||
.lunet/build
|
||||
134
doc/parsing-ast.md
Normal file
134
doc/parsing-ast.md
Normal file
@@ -0,0 +1,134 @@
|
||||
# The Abstract Syntax Tree
|
||||
|
||||
If successful, the `Markdown.Parse(...)` method returns the abstract syntax tree (AST) of the source text.
|
||||
|
||||
This will be an object of the `MarkdownDocument` type, which is in turn derived from a more general block container and is part of a larger taxonomy of classes which represent different semantic constructs of a markdown syntax tree.
|
||||
|
||||
This document will discuss the different types of elements within the Markdig representation of the AST.
|
||||
|
||||
## Structure of the AST
|
||||
|
||||
Within Markdig, there are two general types of node in the markdown syntax tree: `Block`, and `Inline`. Block nodes may contain inline nodes, but the reverse is not true. Blocks may contain other blocks, and inlines may contain other inlines.
|
||||
|
||||
The root of the AST is the `MarkdownDocument` which is itself derived from a container block but also contains information on the line count and starting positions within the document. Nodes in the AST have links both to parent and children, allowing the edges in the tree to be traversed efficiently in either direction.
|
||||
|
||||
Different semantic constructs are represented by types derived from the `Block` and `Inline` types, which are both `abstract` themselves. These elements are produced by `BlockParser` and `InlineParser` derived types, respectively, and so new constructs can be added with the implementation of a new block or inline parser and a new block or inline type, as well as an extension to register it in the pipeline. For more information on extending Markdig this way refer to the [Extensions/Parsers](parsing-extensions.md) document.
|
||||
|
||||
The AST is assembled by the static method `Markdown.Parse(...)` using the collections of block and inline parsers contained in the `MarkdownPipeline`. For more detailed information refer to the [Markdig Parsing Overview](parsing-overview.md) document.
|
||||
|
||||
### Quick Examples: Descendants API
|
||||
|
||||
The easiest way to traverse the abstract syntax tree is with a group of extension methods that have the name `Descendants`. Several different overloads exist to allow it to search for both `Block` and `Inline` elements, starting from any node in the tree.
|
||||
|
||||
The `Descendants` methods return `IEnumerable<MarkdownObject>` or `IEnumerable<T>` as their results. Internally they are using `yield return` to perform edge traversals lazily.
|
||||
|
||||
#### Depth-First Like Traversal of All Elements
|
||||
|
||||
```csharp
|
||||
MarkdownDocument result = Markdown.Parse(sourceText, pipeline);
|
||||
|
||||
// Iterate through all MarkdownObjects in a depth-first order
|
||||
foreach (var item in result.Descendants())
|
||||
{
|
||||
Console.WriteLine(item.GetType());
|
||||
|
||||
// You can use pattern matching to isolate elements of certain type,
|
||||
// otherwise you can use the filtering mechanism demonstrated in the
|
||||
// next section
|
||||
if (item is ListItemBlock listItem)
|
||||
{
|
||||
// ...
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### Filtering of Specific Child Types
|
||||
|
||||
Filtering can be performed using the `Descendants<T>()` method, in which T is required to be derived from `MarkdownObject`.
|
||||
|
||||
```csharp
|
||||
MarkdownDocument result = Markdown.Parse(sourceText, pipeline);
|
||||
|
||||
// Iterate through all ListItem blocks
|
||||
foreach (var item in result.Descendants<ListItemBlock>())
|
||||
{
|
||||
// ...
|
||||
}
|
||||
|
||||
// Iterate through all image links
|
||||
foreach (var item in result.Descendants<LinkInline>().Where(x => x.IsImage))
|
||||
{
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
#### Combined Hierarchies
|
||||
|
||||
The `Descendants` method can be used on any `MarkdownObject`, not just the root node, so complex hierarchies can be queried.
|
||||
|
||||
```csharp
|
||||
MarkdownDocument result = Markdown.Parse(sourceText, pipeline);
|
||||
|
||||
// Find all Emphasis inlines which descend from a ListItem block
|
||||
var items = document.Descendants<ListItemBlock>()
|
||||
.Select(block => block.Descendants<EmphasisInline>());
|
||||
|
||||
// Find all Emphasis inlines whose direct parent block is a ListItem
|
||||
var other = document.Descendants<EmphasisInline>()
|
||||
.Where(inline => inline.ParentBlock is ListItemBlock);
|
||||
```
|
||||
|
||||
## Block Elements
|
||||
|
||||
Block elements all derive from `Block` and may be one of two types:
|
||||
|
||||
1. `ContainerBlock`, which is a block which holds other blocks (`MarkdownDocument` is itself derived from this)
|
||||
2. `LeafBlock`, which is a block that has no child blocks, but may contain inlines
|
||||
|
||||
Block elements in markdown refer to things like paragraphs, headings, lists, code, etc. Most blocks may contain inlines, with the exception of things like code blocks.
|
||||
|
||||
### Properties of Blocks
|
||||
|
||||
The following are properties of `Block` objects which warrant elaboration. For a full list of properties see the generated API documentation (coming soon).
|
||||
|
||||
#### Block Parent
|
||||
All blocks have a reference to a parent (`Parent`) of type `ContainerBlock?`, which allows for efficient traversal up the abstract syntax tree. The parent will be `null` in the case of the root node (the `MarkdownDocument`).
|
||||
|
||||
#### Parser
|
||||
|
||||
All blocks have a reference to a parser (`Parser`) of type `BlockParser?` which refers to the instance of the parser which created this block.
|
||||
|
||||
#### IsOpen Flag
|
||||
|
||||
Blocks have an `IsOpen` boolean flag which is set true while they're being parsed and then closed when parsing is complete.
|
||||
|
||||
Blocks are created by `BlockParser` objects which are managed by an instance of a `BlockProcessor` object. During the parsing algorithm the `BlockProcessor` maintains a list of all currently open `Block` objects as it steps through the source line by line. The `IsOpen` flag indicates to the `BlockProcessor` that the block should remain open as the next line begins. If the `IsOpen` flag is not directly set by the `BlockParser` on each line, the `BlockProcessor` will consider the `Block` fully parsed and will no longer call its `BlockParser` on it.
|
||||
|
||||
#### IsBreakable Flag
|
||||
|
||||
Blocks are either breakable or not, specified by the `IsBreakable` flag. If a block is non-breakable it indicates to the parser that the close condition of any parent container do not apply so long as the non-breakable child block is still open.
|
||||
|
||||
The only built-in example of this is the `FencedCodeBlock`, which, if existing as the child of a container block of some sort, will prevent that container from being closed before the `FencedCodeBlock` is closed, since any characters inside the `FencedCodeBlock` are considered to be valid code and not the container's close condition.
|
||||
|
||||
#### RemoveAfterProcessInlines
|
||||
|
||||
|
||||
|
||||
## Inline Elements
|
||||
|
||||
Inlines in markdown refer to things like embellishments (italics, bold, underline, etc), links, urls, inline code, images, etc.
|
||||
|
||||
Inline elements may be one of two types:
|
||||
|
||||
1. `Inline`, whose parent is always a `ContainerInline`
|
||||
2. `ContainerInline`, derived from `Inline`, which contains other inlines. `ContainerInline` also has a `ParentBlock` property of type `LeafBlock?`
|
||||
|
||||
|
||||
**(Is there anything special worth documenting about inlines or types of inlines?)**
|
||||
|
||||
## The SourceSpan Struct
|
||||
|
||||
If the pipeline was configured with `.UsePreciseSourceLocation()`, all elements in the abstract syntax tree will contain a reference to the location in the original source where they occurred. This is done with the `SourceSpan` type, a custom Markdig `struct` which provides a start and end location.
|
||||
|
||||
All objects derived from `MarkdownObject` contain the `Span` property, which is of type `SourceSpan`.
|
||||
|
||||
127
doc/parsing-extensions.md
Normal file
127
doc/parsing-extensions.md
Normal file
@@ -0,0 +1,127 @@
|
||||
# Extensions and Parsers
|
||||
|
||||
Markdig was [implemented in such a way](http://xoofx.com/blog/2016/06/13/implementing-a-markdown-processor-for-dotnet/) as to be extremely pluggable, with even basic behaviors being mutable and extendable.
|
||||
|
||||
The basic mechanism for extension of Markdig is the `IMarkdownExtension` interface, which allows any implementing class to be registered with the pipeline builder and thus to directly modify the collections of `BlockParser` and `InlineParser` objects which end up in the pipeline.
|
||||
|
||||
This document discusses the `IMarkdownExtension` interface, the `BlockParser` abstract base class, and the `InlineParser` abstract base class, which together are the foundation of extending Markdig's parsing machinery.
|
||||
|
||||
## Creating Extensions
|
||||
|
||||
Extensions can vary from very simple to very complicated.
|
||||
|
||||
A simple extension, for example, might simply find a parser already in the pipeline and modify a setting on it. An example of this is the `SoftlineBreakAsHardlineExtension`, which locates the `LineBreakInlineParser` and modifies a single boolean flag on it.
|
||||
|
||||
A complex extension, on the other hand, might add an entire taxonomy of new `Block` and `Inline` types, as well as several related parsers and renderers, and require being added to the the pipeline in a specific order in relation to other extensions which are already configured. The `FootnoteExtension` and `PipeTableExtension` are examples of more complex extensions.
|
||||
|
||||
For extensions that don't require order considerations, the implementation of the extension itself is adequate, and the extension can be added to the pipeline with the generic `Use<TExtension>()` method on the pipeline builder. For extensions which do require order considerations, it is best to create an extension method on the `MarkdownPipelineBuilder` to perform the registration. See the following two sections for further information.
|
||||
|
||||
### Implementation of an Extension
|
||||
|
||||
The [IMarkdownExtension.cs](https://github.com/xoofx/markdig/blob/master/src/Markdig/IMarkdownExtension.cs) interface specifies two methods which must be implemented.
|
||||
|
||||
The first, which takes only the pipeline builder as an argument, is called when the `Build()` method on the pipeline builder is invoked, and should set up any modifications to the parsers or parser collections. These parsers will then be used by the main parsing algorithm to process the source text.
|
||||
|
||||
```csharp
|
||||
void Setup(MarkdownPipelineBuilder pipeline);
|
||||
```
|
||||
|
||||
The second, which takes the pipeline itself and a renderer, is used to set up a rendering component in order to convert any special `MarkdownObject` types associated with the extension into an output. This is not relevant for parsing, but is necessary for rendering.
|
||||
|
||||
```csharp
|
||||
void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer);
|
||||
```
|
||||
|
||||
The extension can then be registered to the pipeline builder using the `Use<TExtension>()` method. A skeleton example is given below:
|
||||
|
||||
```csharp
|
||||
public class MySpecialBlockParser : BlockParser
|
||||
{
|
||||
// ...
|
||||
}
|
||||
|
||||
public class MyExtension : IMarkdownExtension
|
||||
{
|
||||
void Setup(MarkdownPipelineBuilder pipeline)
|
||||
{
|
||||
pipeline.BlockParsers.AddIfNotAlready<MySpecialBlockParser>();
|
||||
}
|
||||
|
||||
void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer) { }
|
||||
}
|
||||
```
|
||||
|
||||
```csharp
|
||||
var builder = new MarkdownPipelineBuilder()
|
||||
.Use<MyExtension>();
|
||||
```
|
||||
|
||||
### Pipeline Builder Extension Methods
|
||||
|
||||
For extensions which require specific ordering and/or need to perform multiple operations to register with the builder, it's recommended to create an extension method.
|
||||
|
||||
```csharp
|
||||
public static class MyExtensionMethods
|
||||
{
|
||||
public static MarkdownPipelineBuilder UseMyExtension(this MarkdownPipelineBuilder pipeline)
|
||||
{
|
||||
// Directly access or modify pipeline.Extensions here, with the ability to
|
||||
// search for other extensions, insert before or after, remove other extensions,
|
||||
// or modify their settings.
|
||||
|
||||
// ...
|
||||
|
||||
return pipeline;
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### Simple Extension Example
|
||||
|
||||
An example of a simple extension which does not add any new parsers, but instead creates a new, horrific emphasis tag, marked by triple percentage signs. This example is based on [CitationExtension.cs](https://github.com/xoofx/markdig/blob/master/src/Markdig/Extensions/Citations/CitationExtension.cs)
|
||||
|
||||
```csharp
|
||||
/// <summary>
|
||||
/// An extension which applies to text of the form %%%text%%%
|
||||
/// </summary>
|
||||
public class BlinkExtension : IMarkdownExtension
|
||||
{
|
||||
// This setup method will be run when the pipeline builder's `Build()` method is invoked. As this
|
||||
// is a simple, self-contained extension we won't be adding anything new, but rather finding an
|
||||
// existing parser already in the pipeline and adding some settings to it.
|
||||
public void Setup(MarkdownPipelineBuilder pipeline)
|
||||
{
|
||||
// We check the pipeline builder's inline parser collection and see if we can find a parser
|
||||
// registered of the type EmphasisInlineParser. This is the parser which nominally handles
|
||||
// bold and italic emphasis, but we know from its documentation that it is a general parser
|
||||
// that can have new characters added to it.
|
||||
var parser = pipeline.InlineParsers.FindExact<EmphasisInlineParser>();
|
||||
|
||||
// If we find the parser and it doesn't already have the % character registered, we add
|
||||
// a descriptor for 3 consecutive % signs. This is specific to the EmphasisInlineParser and
|
||||
// is just used here as an example.
|
||||
if (parser is not null && !parser.HasEmphasisChar('%'))
|
||||
{
|
||||
parser.EmphasisDescriptors.Add(new EmphasisDescriptor('%', 3, 3, false));
|
||||
}
|
||||
}
|
||||
|
||||
// This method is called by the pipeline before rendering, which is a separate operation from
|
||||
// parsing. This implementation is just here for the purpose of the example, in which we
|
||||
// daisy-chain a delegate specific to the EmphasisInlineRenderer to cause an unconscionable tag
|
||||
// to be inserted into the HTML output wherever a %%% annotated span was placed in the source.
|
||||
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
|
||||
{
|
||||
if (renderer is not HtmlRenderer) return;
|
||||
|
||||
var emphasisRenderer = renderer.ObjectRenderers.FindExact<EmphasisInlineRenderer>();
|
||||
if (emphasisRenderer is null) return;
|
||||
|
||||
var previousTag = emphasisRenderer.GetTag;
|
||||
emphasisRenderer.GetTag = inline =>
|
||||
(inline.DelimiterCount == 3 && inline.DelimiterChar == '%' ? "blink" : null)
|
||||
?? previousTag(inline);
|
||||
}
|
||||
}
|
||||
```
|
||||
336
doc/parsing-overview.md
Normal file
336
doc/parsing-overview.md
Normal file
@@ -0,0 +1,336 @@
|
||||
# Markdig Parsing
|
||||
|
||||
Markdig provides efficient, regex-free parsing of markdown documents directly into an abstract syntax tree (AST). The AST is a representation of the markdown document's semantic constructs, which can be manipulated and explored programmatically.
|
||||
|
||||
* This document contains a general overview of the parsing system and components and their use
|
||||
* The [Abstract Syntax Tree](parsing-ast.md) document contains a discussion of how Markdig represents the product of the parsing operation
|
||||
* The [Extensions/Parsers](parsing-extensions.md) document explores extensions and block/inline parsers within the context of extending Markdig's parsing capabilities
|
||||
|
||||
## Introduction
|
||||
|
||||
Markdig's parsing machinery consists of two main components at its surface: the `Markdown.Parse(...)` method and the `MarkdownPipeline` type. The parsed document is represented by a `MarkdownDocument` object, which is a tree of objects derived from `MarkdownObject`, including block and inline elements.
|
||||
|
||||
The `Markdown` static class is the main entrypoint to the Markdig API. It contains the `Parse(...)` method, the main algorithm for parsing a markdown document. The `Parse(...)` method in turn uses a `MarkdownPipeline`, which is a sealed internal class which maintains some configuration information and the collections of parsers and extensions. The `MarkdownPipeline` determines how the parser behaves and what its capabilities are. The `MarkdownPipeline` can be modified with built-in as well as user developed extensions.
|
||||
|
||||
### Glossary of Relevant Types
|
||||
|
||||
The following is a table of some of the types relevant to parsing and mentioned in the related documentation. For an exhaustive list refer to API documentation (coming soon).
|
||||
|
||||
|Type|Description|
|
||||
|-|-|
|
||||
|`Markdown`|Static class with the entry point to the parsing algorithm via the `Parse(...)` method|
|
||||
|`MarkdownPipeline`|Configuration object for the parser, contains collections of block and inline parsers and registered extensions|
|
||||
|`MarkdownPipelineBuilder`|Responsible for constructing the `MarkdownPipeline`, used by client code to configure pipeline options and behaviors|
|
||||
|`IMarkdownExtension`|Interface for [Extensions](#extensions-imarkdownextension) which alter the behavior of the pipeline, this is the standard mechanism for extending Markdig|
|
||||
|`BlockParser`|Base type for an individual parsing component meant to identify `Block` elements in the markdown source|
|
||||
|`InlineParser`|Base type for an individual parsing component meant to identify `Inline` elements within a `Block`|
|
||||
|`Block`|A node in the AST representing a markdown block element, can either be a `ContainerBlock` or a `LeafBlock`|
|
||||
|`Inline`|A node in the AST representing a markdown inline element|
|
||||
|`MarkdownDocument`|The root node of the AST produced by the parser, derived from `ContainerBlock`|
|
||||
|`MarkdownObject`|The base type of all `Block` and `Inline` derived objects (as well as `HtmlAttributes`)|
|
||||
|
||||
### Simple Examples
|
||||
|
||||
*The following are simple examples of parsing to help get you started, see the following sections for an in-depth explanation of the different parts of Markdig's parsing mechanisms*
|
||||
|
||||
The `MarkdownPipeline` dictate how the parser will behave. The `Markdown.Parse(...)` method will construct a default pipeline if none is provided. A default pipeline will be CommonMark compliant but nothing else.
|
||||
|
||||
```csharp
|
||||
var markdownText = File.ReadAllText("sample.md");
|
||||
|
||||
// No pipeline provided means a default pipeline will be used
|
||||
var document = Markdown.Parse(markdownText);
|
||||
```
|
||||
|
||||
Pipelines can be created and configured manually, however this must be done using a `MarkdownPipelineBuilder` object, which then is configured through a fluent interface composed of extension methods.
|
||||
|
||||
```csharp
|
||||
var markdownText = File.ReadAllText("sample.md");
|
||||
|
||||
// Markdig's "UseAdvancedExtensions" option includes many common extensions beyond
|
||||
// CommonMark, such as citations, figures, footnotes, grid tables, mathematics
|
||||
// task lists, diagrams, and more.
|
||||
var pipeline = new MarkdownPipelineBuilder()
|
||||
.UseAdvancedExtensions()
|
||||
.Build();
|
||||
|
||||
var document = Markdown.Parse(markdownText, pipeline);
|
||||
```
|
||||
|
||||
Extensions can also be added individually:
|
||||
|
||||
```csharp
|
||||
var markdownText = File.ReadAllText("sample.md");
|
||||
|
||||
var pipeline = new MarkdownPipelineBuilder()
|
||||
.UseCitations()
|
||||
.UseFootnotes()
|
||||
.UseMyCustomExtension()
|
||||
.Build();
|
||||
|
||||
var document = Markdown.Parse(markdownText, pipeline);
|
||||
```
|
||||
|
||||
## Markdown.Parse and the MarkdownPipeline
|
||||
|
||||
As metioned in the [Introduction](#introduction), Markdig's parsing machinery involves two surface components: the `Markdown.Parse(...)` method, and the `MarkdownPipeline` type. The main parsing algorithm (not to be confused with individual `BlockParser` and `InlineParser` components) lives in the `Markdown.Parse(...)` static method. The `MarkdownPipeline` is responsible for configuring the behavior of the parser.
|
||||
|
||||
These two components are covered in further detail in the following sections.
|
||||
|
||||
### The MarkdownPipeline
|
||||
|
||||
The `MarkdownPipeline` is a sealed internal class which dictates what features the parsing algorithm has. The pipeline must be created by using a `MarkdownPipelineBuilder` as shown in the examples above.
|
||||
|
||||
The `MarkdownPipeline` holds configuration information and collections of extensions and parsers. Parsers fall into one of two categories:
|
||||
|
||||
* Block Parsers (`BlockParser`)
|
||||
* Inline Parsers (`InlineParser`)
|
||||
|
||||
Extensions are classes implementing `IMarkdownExtension` which are allowed to add to the list of parsers, or modify existing parsers and/or renderers. They are invoked to perform their mutations on the pipeline when the pipeline is built by the `MarkdownPipelineBuilder`.
|
||||
|
||||
Lastly, the `MarkdownPipeline` contains a few extra elements:
|
||||
|
||||
* A configuration setting determining whether or not trivial elements, referred to as *trivia*, (whitespace, extra heading characters, unescaped strings, etc) are to be tracked
|
||||
* A configuration setting determining whether or not nodes in the resultant abstract syntax tree will refer to their precise original locations in the source
|
||||
* An optional delegate which will be invoked when the document has been processed.
|
||||
* An optional `TextWriter` which will get debug logging from the parser
|
||||
|
||||
### The Markdown.Parse Method
|
||||
|
||||
`Markdown.Parse` is a static method which contains the overall parsing algorithm but not the actual parsing components, which instead are contained within the pipeline.
|
||||
|
||||
The `Markdown.Parse(...)` method takes a string containing raw markdown and returns a `MarkdownDocument`, which is the root node in the abstract syntax tree. The `Parse(...)` method optionally takes a pre-configured `MarkdownPipeline`, but if none is given will create a default pipeline which has minimal features.
|
||||
|
||||
Within the `Parse(...)` method, the following sequence of operations occur:
|
||||
|
||||
1. The block parsers contained in the pipeline are invoked on the raw markdown text, creating the initial tree of block elements
|
||||
2. If the pipeline is configured to track markdown trivia (trivial/non-contributing elements), the blocks are expanded to absorb neighboring trivia
|
||||
3. The inline parsers contained in the pipeline are now invoked on the blocks, populating the inline elements of the abstract syntax tree
|
||||
4. If a delegate has been configured for when the document has completed processing, it is now invoked
|
||||
5. The abstract syntax tree (`MarkdownDocument` object) is returned
|
||||
|
||||
## The Pipeline Builder and Extensions
|
||||
|
||||
The `MarkdownPipeline` determines the behavior and capabilities of the parser, and *extensions* added via the `MarkdownPipelineBuilder` determine the configuration of the pipeline.
|
||||
|
||||
This section discusses the pipeline builder and the concept of *extensions* in more detail.
|
||||
|
||||
### Extensions (IMarkdownExtension)
|
||||
|
||||
***Note**: This section discusses how to consume extensions by adding them to pipeline. For a discussion on how to implement an extension, refer to the [Extensions/Parsers](parsing-extensions.md) document.*
|
||||
|
||||
Extensions are the primary mechanism for modifying the parsers in the pipeline.
|
||||
|
||||
An extension is any class which implements the `IMarkdownExtension` interface found in [IMarkdownExtension.cs](https://github.com/xoofx/markdig/blob/master/src/Markdig/IMarkdownExtension.cs). This interface consists solely of two `Setup(...)` overloads, which both take a `MarkdownPipelineBuilder` as the first argument.
|
||||
|
||||
When the `MarkdownPipelineBuilder.Build()` method is invoked as the final stage in pipeline construction, the builder runs through the list of registered extensions in order and calls the `Setup(...)` method on each of them. The extension then has full access to modify both the parser collections themselves (by adding new parsers to it), or to find and modify existing parsers.
|
||||
|
||||
Because of this, *some* extensions may need to be ordered in relation to others, for instance if they modify a parser that gets added by a different extension. The `OrderedList<T>` class contains convenience methods to this end, which aid in finding other extensions by type and then being able to added an item before or after them.
|
||||
|
||||
### The MarkdownPipelineBuilder
|
||||
|
||||
Because the `MarkdownPipeline` is a sealed internal class, it cannot (and *should* not be attempted to) be created directly. Rather, the `MarkdownPipelineBuilder` manages the requisite construction of the pipeline after the configuration has been provided by the client code.
|
||||
|
||||
As discussed in the [section above](#the-markdownpipeline), the `MarkdownPipeline` primarily consists of a collection of block parsers and a collection of inline parsers, which are provided to the `Markdown.Parse(...)` method and thus determine its features and behavior. Both the collections and some of the parsers themselves are mutable, and the mechanism of mutation is the `Setup(...)` method of the `IMarkdownExtension` interface. This is covered in more detail in the section on [Extensions](#extensions-imarkdownextension).
|
||||
|
||||
#### The Fluent Interface
|
||||
|
||||
A collection of extension methods in the [MarkdownExtensions.cs](https://github.com/xoofx/markdig/blob/master/src/Markdig/MarkdownExtensions.cs) source file provides a convenient fluent API for the configuration of the pipeline builder. This should be considered the standard way of configuring the builder.
|
||||
|
||||
##### Configuration Options
|
||||
|
||||
There are several extension methods which apply configurations to the builder which change settings in the pipeline outside of the use of typical extensions.
|
||||
|
||||
|Method|Description|
|
||||
|-|-|
|
||||
|`.ConfigureNewLine(...)`|Takes a string which will serve as the newline delimiter during parsing|
|
||||
|`.DisableHeadings()`|Disables the parsing of ATX and Setex headings|
|
||||
|`.DisableHtml()`|Disables the parsing of HTML elements|
|
||||
|`.EnableTrackTrivia()`|Enables the tracking of trivia (trivial elements like whitespace)|
|
||||
|`.UsePreciseSourceLocation()`|Maps syntax objects to their precise location in the original source, such as would be required for syntax highlighting|
|
||||
|
||||
```csharp
|
||||
var builder = new MarkdownPipelineBuilder()
|
||||
.ConfigureNewLine("\r\n")
|
||||
.DisableHeadings()
|
||||
.DisableHtml()
|
||||
.EnableTrackTrivia()
|
||||
.UsePreciseSourceLocation();
|
||||
|
||||
var pipeline = builder.Build();
|
||||
```
|
||||
|
||||
##### Adding Extensions
|
||||
|
||||
All extensions which ship with Markdig can be added through a dedicated fluent method, while user code which implements the `IMarkdownExtension` interface can be added with one of the `Use()` methods, or via a custom extension method implemented in the client code.
|
||||
|
||||
Refer to [MarkdownExtensions.cs](https://github.com/xoofx/markdig/blob/master/src/Markdig/MarkdownExtensions.cs) for a full list of extension methods:
|
||||
|
||||
```csharp
|
||||
var builder = new MarkdownPipelineBuilder()
|
||||
.UseFootnotes()
|
||||
.UseFigures();
|
||||
```
|
||||
|
||||
For custom/user-provided extensions, the `Use<TExtension>(...)` methods allow either a type to be directly added or an already constructed instance to be put into the extension container. Internally they will prevent two of the same type of extension from being added to the container.
|
||||
|
||||
```csharp
|
||||
public class MyExtension : IMarkdownExtension
|
||||
{
|
||||
// ...
|
||||
}
|
||||
|
||||
// Only works if MyExtension has an empty constructor (aka new())
|
||||
var builder = new MarkdownPipelineBuilder()
|
||||
.Use<MyExtension>();
|
||||
```
|
||||
|
||||
Alternatively:
|
||||
|
||||
```csharp
|
||||
public class MyExtension : IMarkdownExtension
|
||||
{
|
||||
public MyExtension(object someConfigurationObject) { /* ... */ }
|
||||
// ...
|
||||
}
|
||||
|
||||
var instance = new MyExtension(configData);
|
||||
|
||||
var builder = new MarkdownPipelineBuilder()
|
||||
.Use(instance);
|
||||
```
|
||||
|
||||
##### Adding Extensions with the Configure Method
|
||||
|
||||
The `MarkdownPipelineBuilder` has one additional method for the configuration of extensions worth mentioning: the `Configure(...)` method, which takes a `string?` of `+` delimited tokens specifying which extensions should be dynamically configured. This is a convenience method for the configuration of pipelines whose extensions are only known at runtime.
|
||||
|
||||
Refer to [MarkdownExtensions.cs's `Configure(...)`](https://github.com/xoofx/markdig/blob/983187eace6ba02ee16d1443c387267ad6e78f58/src/Markdig/MarkdownExtensions.cs#L538) code for the full list of extensions.
|
||||
|
||||
|
||||
```csharp
|
||||
var builder = new MarkdownPipelineBuilder()
|
||||
.Configure("common+footnotes+figures");
|
||||
|
||||
var pipeline = builder.Build();
|
||||
```
|
||||
|
||||
#### Manual Configuration
|
||||
|
||||
Internally, the fluent interface wraps manual operations on the three primary collections:
|
||||
|
||||
* `MarkdownPipelineBuilder.BlockParsers` - this is an `OrderedList<BlockParser>` of the block parsers
|
||||
* `MarkdownPipelineBuilder.InlineParsers` - this is an `OrderedList<InlineParser>` of the inline element parsers
|
||||
* `MarkdownPipelineBuilder.Extensions` - this is an `OrderedList<IMarkdownExtension>` of the extensions
|
||||
|
||||
All three collections are `OrderedList<T>`, which is a collection type custom to Markdig which contains special methods for finding and inserting derived types. With the builder created, manual configuration can be performed by accessing these collections and their elements and modifying them as necessary.
|
||||
|
||||
***Warning**: be aware that it should not be necessary to directly modify either the `BlockParsers` or the `InlineParsers` collections directly during the pipeline configuration. Rather, these can and should be modified whenever possible through the `Setup(...)` method of extensions, which will be deferred until the pipeline is actually built and will allow for ordering such that operations dependent on other operations can be accounted for.*
|
||||
|
||||
## Block and Inline Parsers
|
||||
|
||||
Let's dive deeper into the parsing system. With a configured pipeline, the `Markdown.Parse` method will run through two two conceptual passes to produce the abstract syntax tree.
|
||||
|
||||
1. First, `BlockProcessor.ProcessLine` is called on the file's lines, one by one, trying to identify block elements in the source
|
||||
2. Next, an `InlineProcessor` is created or borrowed and run on each block to identify inline elements.
|
||||
|
||||
These two conceptual operations dictate Markdig's two types of parsers, both of which derive from `ParserBase<TProcessor>`.
|
||||
|
||||
Block parsers, derived from `BlockParser`, identify block elements from lines in the source text and push them onto the abstract syntax tree. Inline parsers, derived from `InlineParser`, identify inline elements from `LeafBlock` elements and push them into an attached container: the `ContainerInline? LeafBlock.Inline` property.
|
||||
|
||||
Both inline and block parsers are regex-free, and instead work on finding opening characters and then making fast read-only views into the source text.
|
||||
|
||||
### Block Parser
|
||||
|
||||
**(The contents of this section I am very unsure of, this is from my reading of the code but I could use some guidance here)**
|
||||
|
||||
**(Does `CanInterrupt` specifically refer to interrupting a paragraph block?)**
|
||||
|
||||
In order to be added to the parsing pipeline, all block parsers must be derived from `BlockParser`.
|
||||
|
||||
Internally, the main parsing algorithm will be stepping through the source text, using the `HasOpeningCharacter(char c)` method of the block parser collection to pre-identify parsers which *could* be opening a block at a given position in the text based on the active character. Thus any derived implementation needs to set the value of the `char[]? OpeningCharacter` property with the initial characters that might begin the block.
|
||||
|
||||
If a parser can potentially open a block at a place in the source text it should expect to have the `TryOpen(BlockProcessor processor)` method called. This is a virtual method that must be implemented on any derived class. The `BlockProcessor` argument is a reference to an object which stores the current state of parsing and the position in the source.
|
||||
|
||||
**(What are the rules concerning how the `BlockState` return type should work for `TryOpen`? I see examples returning `None`, `Continue`, `BreakDiscard`, `ContinueDiscard`. How does the return value change the algorithm behavior?)**
|
||||
|
||||
**(Should a new block always be pushed into `processor.NewBlocks` in the `TryOpen` method?)**
|
||||
|
||||
As the main parsing algorithm moves forward, it will then call `TryContinue(...)` on blocks that were opened in `TryOpen(..)`.
|
||||
|
||||
**(Is this where/how you close a block? Is there anything that needs to be done to perform that beyond `block.UpdateSpanEnd` and returning `BlockState.Break`?)**
|
||||
|
||||
|
||||
### Inline Parsers
|
||||
|
||||
Inline parsers extract inline markdown elements from the source, but their starting point is the text of each individual `LeafBlock` produced by the block parsing process. To understand the role of each inline parser it is necessary to first understand the inline parsing process as a whole.
|
||||
|
||||
#### The Inline Parsing Process
|
||||
|
||||
After the block parsing process has occurred, the abstract syntax tree of the document has been populated only with block elements, starting from the root `MarkdownDocument` node and ending with the individual `LeafBlock` derived block elements, most of which will be `ParagraphBlocks`, but also include things like `CodeBlocks`, `HeadingBlocks`, `FigureCaptions`, and so on.
|
||||
|
||||
At this point, the parsing machinery will iterate through each `LeafBlock` one by one, creating and assigning its `LeafBlock.Inline` property with an empty `ContainerInline`, and then sweeping through the `LeafBlock`'s text running the inline parsers. This occurs by the following process:
|
||||
|
||||
Starting at the first character of the text it will run through all of its `InlineParser` objects which have that character as a possible opening character for the type of inline they extract. The parsers will run in order (as such ordering is the *only* way which conflicts between parsers are resolved, and thus is important to the overall behavior of the parsing system) and the `Match(...)` method will be called on each candidate parser, in order, until one of them returns `true`.
|
||||
|
||||
The `Match(...)` method will be passed a slice of the text beginning at the *specific character* being processed and running until the end of the `LeafBlock`'s complete text. If the parser can create an `Inline` element it will do so and return `true`, otherwise it will return `false`. The parser will store the created `Inline` object in the processor's `InlineProcessor.Inline` property, which as passed into the `Match(...)` method as an argument. The parser will also advance the start of the working `StringSlice` by the characters consumed in the match.
|
||||
|
||||
* If the parser has created an inline element and returned `true`, that element is pushed into the deepest open `ContainerInline`
|
||||
* If `false` was returned, a default `LiteralInlineParser` will run instead:
|
||||
* If the `InlineProcessor.Inline` property already has an existing `LiteralInline` in it, these characters will be added to the existing `LiteralInline`, effectively growing it
|
||||
* If no `LiteralInline` exists in the `InlineProcessor.Inline` property, a new one will be created containing the consumed characters and pushed into the deepest open `ContainerInline`
|
||||
|
||||
After that, the working text of the `LeafBlock` has been conceptually shortened by the advancing start of the working `StringSlice`, moving the starting character forward. If there is still text remaining, the process repeats from the new starting character until all of the text is consumed.
|
||||
|
||||
At this point, when all of the source text from the `LeafBlock` has been consumed, a post-processing step occurs. `InlineParser` objects in the pipeline which also implement `IPostInlineProcessor` are invoked on the `LeafBlock`'s root `ContainerInline`. This, for example, is the mechanism by which the unstructured output of the `EmphasisInlineParser` is then restructured into cleanly nested `EmphasisInline` and `LiteralInline` elements.
|
||||
|
||||
|
||||
#### Responsibilities of an Inline Parser
|
||||
|
||||
Like the block parsers, an inline parser must provide an array of opening characters with the `char[]? OpeningCharacter` property.
|
||||
|
||||
However, inline parsers only require one other method, the `Match(InlineProcessor processor, ref StringSlice slice)` method, which is expected to determine if a match for the related inline is located at the starting character of the slice.
|
||||
|
||||
Within the `Match` method a parser should:
|
||||
|
||||
1. Determine if a match begins at the starting character of the `slice` argument
|
||||
2. If no match exists, the method should return `false` and not advance the `Start` property of the `slice` argument
|
||||
3. If a match does exist, perform the following actions:
|
||||
* Instantiate the appropriate `Inline` derived class and assign it to the processor argument with `processor.Inline = myInlineObject`
|
||||
* Advance the `Start` property of the `slice` argument by the number of characters contained in the match, for example by using the `NextChar()`, `SkipChar()`, or other helper methods of the `StringSlice` class
|
||||
* Return `true`
|
||||
|
||||
While parsing, the `InlineProcessor` performing the processing, which is available to the `Match` function through the `processor` argument, contains a number of properties which can be used to access the current state of parsing. For example, the `processor.Inline` property is the mechanism for returning a new inline element, but before assignment it contains the last created inline, which in turn can be accessed for its parents.
|
||||
|
||||
Additionally, in the case of inlines which can be expected to contain other inlines, a possible strategy is to inject an inline element derived from `DelimiterInline` when the opening delimiter is detected, then to replace the opening delimiter with the final desired element when the closing delimiter is found. This is the strategy used by the `LinkInlineParser`, for example. In such cases the tools described in the next section, such as the `ReplaceBy` method, can be used. Note that if this method is used the post-processing should be invoked on the `InlineProcessor` in order to finalize any emphasis elements. For example, in the following code adapted from the `LinkInlineParser`:
|
||||
|
||||
```csharp
|
||||
var parent = processor.Inline?.FirstParentOfType<MyDelimiterInline>();
|
||||
if (parent is null) return;
|
||||
|
||||
var myInline = new MySpecialInline { /* set span and other parameters here */ };
|
||||
|
||||
// Replace the delimiter inline with the final inline type, adopting all of its children
|
||||
parent.ReplaceBy(myInline);
|
||||
|
||||
// Notifies processor as we are creating an inline locally
|
||||
processor.Inline = myInline;
|
||||
|
||||
// Process emphasis delimiters
|
||||
processor.PostProcessInlines(0, myInline, null, false);
|
||||
```
|
||||
|
||||
#### Inline Post-Processing
|
||||
|
||||
The purpose of post-processing inlines is typically to re-structure inline elements after the initial parsing is complete and the entire structure of the inline elements within a parent container is now available in a way it was not during the parsing process. Generally this consists of removing, replacing, and re-ordering `Inline` elements.
|
||||
|
||||
To this end, the `Inline` abstract base class contains several helper methods intended to allow manipulation of inline elements during the post-processing phase.
|
||||
|
||||
|Method|Purpose|
|
||||
|-|-|
|
||||
|`InsertAfter(...)`|Takes a new inline as an argument and inserts it into the same parent container after this instance|
|
||||
|`InsertBefore(...)`|Takes a new inline as an argument and inserts it into the same parent container before this instance|
|
||||
|`Remove()`|Removes this inline from its parent container|
|
||||
|`ReplaceBy(...)`|Removes this instance and replaces it with a new inline specified in the argument. Has an option to move all of the original inline's children into the new inline.|
|
||||
|
||||
Additionally, the `PreviousSibling` and `NextSibling` properties can be used to determine the siblings of an inline element within its parent container. The `FirstParentOfType<T>()` method can be used to search for a parent element, which is often useful when searching for `DelimiterInline` derived elements, which are implemented as containers.
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 9.1 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 8.6 KiB |
90
readme.md
90
readme.md
@@ -14,7 +14,7 @@ You can **try Markdig online** and compare it to other implementations on [babel
|
||||
- **Abstract Syntax Tree** with precise source code location for syntax tree, useful when building a Markdown editor.
|
||||
- Checkout [MarkdownEditor for Visual Studio](https://visualstudiogallery.msdn.microsoft.com/eaab33c3-437b-4918-8354-872dfe5d1bfe) powered by Markdig!
|
||||
- Converter to **HTML**
|
||||
- Passing more than **600+ tests** from the latest [CommonMark specs (0.29)](http://spec.commonmark.org/)
|
||||
- Passing more than **600+ tests** from the latest [CommonMark specs (0.30)](http://spec.commonmark.org/)
|
||||
- Includes all the core elements of CommonMark:
|
||||
- including **GFM fenced code blocks**.
|
||||
- **Extensible** architecture
|
||||
@@ -112,82 +112,34 @@ This software is released under the [BSD-Clause 2 license](https://github.com/lu
|
||||
|
||||
## Benchmarking
|
||||
|
||||
This is an early preview of the benchmarking against various implementations:
|
||||
The latest benchmark was collected on April 23 2022, against the following implementations:
|
||||
|
||||
**C implementations**:
|
||||
|
||||
- [cmark](https://github.com/jgm/cmark) (version: 0.25.0): Reference C implementation of CommonMark, no support for extensions
|
||||
- [Moonshine](https://github.com/brandonc/moonshine) (version: : popular C Markdown processor
|
||||
|
||||
**.NET implementations**:
|
||||
|
||||
- [Markdig](https://github.com/lunet-io/markdig) (version: 0.5.x): itself
|
||||
- [CommonMark.NET(master)](https://github.com/Knagis/CommonMark.NET) (version: 0.11.0): CommonMark implementation for .NET, no support for extensions, port of cmark
|
||||
- [CommonMark.NET(pipe_tables)](https://github.com/AMDL/CommonMark.NET/tree/pipe-tables): An evolution of CommonMark.NET, supports extensions, not released yet
|
||||
- [MarkdownDeep](https://github.com/toptensoftware/markdowndeep) (version: 1.5.0): another .NET implementation
|
||||
- [MarkdownSharp](https://github.com/Kiri-rin/markdownsharp) (version: 1.13.0): Open source C# implementation of Markdown processor, as featured on Stack Overflow, regexp based.
|
||||
- [Marked.NET](https://github.com/T-Alex/MarkedNet) (version: 1.0.5) port of original [marked.js](https://github.com/chjj/marked) project
|
||||
- [Microsoft.DocAsCode.MarkdownLite](https://github.com/dotnet/docfx/tree/dev/src/Microsoft.DocAsCode.MarkdownLite) (version: 2.0.1) used by the [docfx](https://github.com/dotnet/docfx) project
|
||||
|
||||
### Analysis of the results:
|
||||
|
||||
- Markdig is roughly **x100 times faster than MarkdownSharp**, **30x times faster than docfx**
|
||||
- **Among the best in CPU**, Extremely competitive and often faster than other implementations (not feature wise equivalent)
|
||||
- **15% to 30% less allocations** and GC pressure
|
||||
|
||||
Because Marked.NET, MarkdownSharp and DocAsCode.MarkdownLite are way too slow, they are not included in the following charts:
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
|
||||
### Performance for x86:
|
||||
- [Markdig](https://github.com/lunet-io/markdig) (version: 0.30.2): itself
|
||||
- [cmark](https://github.com/commonmark/cmark) (version: 0.30.2): Reference C implementation of CommonMark, no support for extensions
|
||||
- [CommonMark.NET(master)](https://github.com/Knagis/CommonMark.NET) (version: 0.15.1): CommonMark implementation for .NET, no support for extensions, port of cmark, deprecated.
|
||||
- [MarkdownSharp](https://github.com/Kiri-rin/markdownsharp) (version: 2.0.5): Open source C# implementation of Markdown processor, as featured previously on Stack Overflow, regexp based.
|
||||
|
||||
```
|
||||
BenchmarkDotNet-Dev=v0.9.7.0+
|
||||
OS=Microsoft Windows NT 6.2.9200.0
|
||||
Processor=Intel(R) Core(TM) i7-4770 CPU 3.40GHz, ProcessorCount=8
|
||||
Frequency=3319351 ticks, Resolution=301.2637 ns, Timer=TSC
|
||||
HostCLR=MS.NET 4.0.30319.42000, Arch=32-bit RELEASE
|
||||
JitModules=clrjit-v4.6.1080.0
|
||||
// * Summary *
|
||||
|
||||
Type=Program Mode=SingleRun LaunchCount=2
|
||||
WarmupCount=2 TargetCount=10
|
||||
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.22000
|
||||
AMD Ryzen 9 5950X, 1 CPU, 32 logical and 16 physical cores
|
||||
.NET SDK=6.0.202
|
||||
[Host] : .NET 6.0.4 (6.0.422.16404), X64 RyuJIT
|
||||
DefaultJob : .NET 6.0.4 (6.0.422.16404), X64 RyuJIT
|
||||
|
||||
Method | Median | StdDev |Scaled | Gen 0 | Gen 1| Gen 2|Bytes Allocated/Op |
|
||||
--------------------------- |------------ |---------- |------ | ------ |------|---------|------------------ |
|
||||
Markdig | 5.5316 ms | 0.0372 ms | 0.71 | 56.00| 21.00| 49.00| 1,285,917.31 |
|
||||
CommonMark.NET(master) | 4.7035 ms | 0.0422 ms | 0.60 | 113.00| 7.00| 49.00| 1,502,404.60 |
|
||||
CommonMark.NET(pipe_tables) | 5.6164 ms | 0.0298 ms | 0.72 | 111.00| 56.00| 49.00| 1,863,128.13 |
|
||||
MarkdownDeep | 7.8193 ms | 0.0334 ms | 1.00 | 120.00| 56.00| 49.00| 1,884,854.85 |
|
||||
cmark | 4.2698 ms | 0.1526 ms | 0.55 | -| -| -| NA |
|
||||
Moonshine | 6.0929 ms | 0.1053 ms | 1.28 | -| -| -| NA |
|
||||
Marked.NET | 207.3169 ms | 5.2628 ms | 26.51 | 0.00| 0.00| 0.00| 303,125,228.65 |
|
||||
MarkdownSharp | 675.0185 ms | 2.8447 ms | 86.32 | 40.00| 27.00| 41.00| 2,413,394.17 |
|
||||
Microsoft DocfxMarkdownLite | 166.3357 ms | 0.4529 ms | 21.27 |4,452.00|948.00|11,167.00| 180,218,359.60 |
|
||||
|
||||
| Method | Mean | Error | StdDev |
|
||||
|------------------ |-----------:|----------:|----------:|
|
||||
| markdig | 1.979 ms | 0.0221 ms | 0.0185 ms |
|
||||
| cmark | 2.571 ms | 0.0081 ms | 0.0076 ms |
|
||||
| CommonMark.NET | 2.016 ms | 0.0169 ms | 0.0158 ms |
|
||||
| MarkdownSharp | 221.455 ms | 1.4442 ms | 1.3509 ms |
|
||||
```
|
||||
|
||||
### Performance for x64:
|
||||
- Markdig is roughly **x100 times faster than MarkdownSharp**
|
||||
- **20% faster than the reference cmark C implementation**
|
||||
|
||||
```
|
||||
BenchmarkDotNet-Dev=v0.9.6.0+
|
||||
OS=Microsoft Windows NT 6.2.9200.0
|
||||
Processor=Intel(R) Core(TM) i7-4770 CPU @ 3.40GHz, ProcessorCount=8
|
||||
Frequency=3319351 ticks, Resolution=301.2637 ns, Timer=TSC
|
||||
HostCLR=MS.NET 4.0.30319.42000, Arch=64-bit RELEASE [RyuJIT]
|
||||
JitModules=clrjit-v4.6.1080.0
|
||||
|
||||
Type=Program Mode=SingleRun LaunchCount=2
|
||||
WarmupCount=2 TargetCount=10
|
||||
|
||||
Method | Median | StdDev | Gen 0 | Gen 1 | Gen 2 | Bytes Allocated/Op |
|
||||
--------------------- |---------- |---------- |------- |------- |------ |------------------- |
|
||||
TestMarkdig | 5.5276 ms | 0.0402 ms | 109.00 | 96.00 | 84.00 | 1,537,027.66 |
|
||||
TestCommonMarkNet | 4.4661 ms | 0.1190 ms | 157.00 | 96.00 | 84.00 | 1,747,432.06 |
|
||||
TestCommonMarkNetNew | 5.3151 ms | 0.0815 ms | 229.00 | 168.00 | 84.00 | 2,323,922.97 |
|
||||
TestMarkdownDeep | 7.4076 ms | 0.0617 ms | 318.00 | 186.00 | 84.00 | 2,576,728.69 |
|
||||
```
|
||||
|
||||
## Donate
|
||||
|
||||
|
||||
162
site/.lunet/css/main.css
Normal file
162
site/.lunet/css/main.css
Normal file
@@ -0,0 +1,162 @@
|
||||
/* main */
|
||||
:root {
|
||||
--base-html-font-size: 62.5%;
|
||||
--base-font-size: 1.65rem;
|
||||
--container-xl-max-width: 160rem;
|
||||
--site-logo-size: 45px;
|
||||
--site-logo-url: url("/img/markdig.png");
|
||||
}
|
||||
|
||||
/* make tocbot working with halfmoon */
|
||||
html, body, .page-wrapper, .content-wrapper {
|
||||
position: static;
|
||||
}
|
||||
|
||||
.page-wrapper {
|
||||
overflow: visible;
|
||||
}
|
||||
|
||||
/* disable any box around active headers with tocbot */
|
||||
h1:focus, h1:active,
|
||||
h2:focus, h2:active,
|
||||
h3:focus, h3:active,
|
||||
h4:focus, h4:active,
|
||||
h5:focus, h5:active,
|
||||
h6:focus, h6:active
|
||||
{
|
||||
outline: none;
|
||||
}
|
||||
|
||||
.nav-item.active {
|
||||
border-bottom-width: 2px;
|
||||
border-bottom-style: solid;
|
||||
border-bottom-color: var(--lm-navbar-link-active-text-color);
|
||||
}
|
||||
|
||||
.main-row {
|
||||
min-height: 92vh;
|
||||
}
|
||||
|
||||
.nav-item.active .nav-link {
|
||||
color: var(--lm-navbar-link-active-text-color);
|
||||
background-color: var(--lm-navbar-link-active-bg-color);
|
||||
}
|
||||
|
||||
.site-logo {
|
||||
background: transparent var(--site-logo-url) no-repeat scroll 0px 0px / var(--site-logo-size) var(--site-logo-size);
|
||||
display: block;
|
||||
width: var(--site-logo-size);
|
||||
height: var(--site-logo-size);
|
||||
}
|
||||
|
||||
.in-this-article-nav {
|
||||
position: fixed;
|
||||
margin-right: 2rem;
|
||||
z-index: 20;
|
||||
padding: 0.5rem;
|
||||
border-radius: 0.4rem;
|
||||
}
|
||||
|
||||
.in-this-article-nav .title {
|
||||
font-weight: bold;
|
||||
font-size: 1em;
|
||||
padding-bottom: 1em;
|
||||
}
|
||||
|
||||
.content .is-active-link::before {
|
||||
background-color: var(--primary-color);
|
||||
}
|
||||
|
||||
.heading-anchor-hidden {
|
||||
display: none !important;
|
||||
}
|
||||
|
||||
@media (min-width: 1600px) {
|
||||
html {
|
||||
font-size: var(--base-html-font-size);
|
||||
}
|
||||
}
|
||||
@media (min-width: 1920px) {
|
||||
html {
|
||||
font-size: var(--base-html-font-size);
|
||||
}
|
||||
}
|
||||
|
||||
table.api-dotnet-inherit, table.api-dotnet-implements, table.api-dotnet-derived {
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
.api-dotnet-inherit td, .api-dotnet-implements td, .api-dotnet-derived td {
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
.api-dotnet-inherit div:not(:first-child):before {
|
||||
content: " ᐅ ";
|
||||
white-space: pre;
|
||||
}
|
||||
|
||||
/*.api-dotnet-inherit td, .api-dotnet-inherit tr, .api-dotnet-implements td, .api-dotnet-implements tr, .api-dotnet-derived tr {
|
||||
display: flex;
|
||||
}
|
||||
*/
|
||||
|
||||
.api-dotnet-inherit div, .api-dotnet-implements div
|
||||
{
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
.api-dotnet-inherit td:not(:first-child), .api-dotnet-implements td:not(:first-child), .api-dotnet-derived td:not(:first-child) {
|
||||
padding-left: 1em;
|
||||
}
|
||||
|
||||
.api-dotnet-implements div:not(:first-child):before {
|
||||
content: ", ";
|
||||
white-space: pre;
|
||||
}
|
||||
|
||||
.api-dotnet-parameter-list {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.api-dotnet-parameter-list dd {
|
||||
margin-left: 1em;
|
||||
}
|
||||
|
||||
dl.api-dotnet-parameter-list {
|
||||
margin-bottom: 0px;
|
||||
}
|
||||
|
||||
table.api-dotnet-members-table {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
table.api-dotnet-members-table p {
|
||||
margin-top: 0px;
|
||||
margin-bottom: 0px;
|
||||
}
|
||||
|
||||
.api-dotnet-members-table tr td:first-child {
|
||||
width: 33%;
|
||||
}
|
||||
|
||||
div.api-dotnet {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.api-dotnet-members-table tr:not(:first-child) {
|
||||
border-top: 1px solid #ddd;
|
||||
}
|
||||
|
||||
.api-dotnet-members-table td {
|
||||
padding-top: 0.5rem;
|
||||
padding-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
.api-dotnet-members-table tr:not(:first-child) {
|
||||
border-top: 1px solid #777;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
701
site/.lunet/css/prism.css
Normal file
701
site/.lunet/css/prism.css
Normal file
@@ -0,0 +1,701 @@
|
||||
/* PrismJS 1.15.0
|
||||
https://prismjs.com/download.html#themes=prism-coy&languages=markup+clike+c+csharp+cpp+fsharp+markdown&plugins=line-highlight+line-numbers+toolbar+show-language */
|
||||
/**
|
||||
* prism.js Coy theme for JavaScript, CoffeeScript, CSS and HTML
|
||||
* Based on https://github.com/tshedor/workshop-wp-theme (Example: http://workshop.kansan.com/category/sessions/basics or http://workshop.timshedor.com/category/sessions/basics);
|
||||
* @author Tim Shedor
|
||||
*/
|
||||
|
||||
@media (prefers-color-scheme: light) {
|
||||
code[class*="language-"],
|
||||
pre[class*="language-"] {
|
||||
color: #393A34;
|
||||
background: none;
|
||||
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
|
||||
text-align: left;
|
||||
white-space: pre-wrap;
|
||||
word-spacing: normal;
|
||||
word-break: break-word;
|
||||
word-wrap: normal;
|
||||
line-height: 1.5;
|
||||
|
||||
-moz-tab-size: 4;
|
||||
-o-tab-size: 4;
|
||||
tab-size: 4;
|
||||
|
||||
-webkit-hyphens: none;
|
||||
-moz-hyphens: none;
|
||||
-ms-hyphens: none;
|
||||
hyphens: none;
|
||||
}
|
||||
|
||||
/* Code blocks */
|
||||
pre[class*="language-"] {
|
||||
position: relative;
|
||||
margin: .5em 0;
|
||||
overflow: visible;
|
||||
padding: 0;
|
||||
}
|
||||
pre[class*="language-"]>code {
|
||||
position: relative;
|
||||
border-left: 4px solid #358ccb;
|
||||
box-shadow: -1px 0px 0px 0px #358ccb, 0px 0px 0px 1px #dfdfdf;
|
||||
background-color: #fdfdfd;
|
||||
background-image: linear-gradient(transparent 50%, rgba(69, 142, 209, 0.04) 50%);
|
||||
background-size: 3em 3em;
|
||||
background-origin: content-box;
|
||||
background-attachment: local;
|
||||
}
|
||||
|
||||
code[class*="language"] {
|
||||
max-height: inherit;
|
||||
height: inherit;
|
||||
padding: 0 1em;
|
||||
display: block;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
/* Margin bottom to accommodate shadow */
|
||||
:not(pre) > code[class*="language-"],
|
||||
pre[class*="language-"] {
|
||||
background-color: #fdfdfd;
|
||||
-webkit-box-sizing: border-box;
|
||||
-moz-box-sizing: border-box;
|
||||
box-sizing: border-box;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
/* Inline code */
|
||||
:not(pre) > code[class*="language-"] {
|
||||
position: relative;
|
||||
padding: .2em;
|
||||
border-radius: 0.3em;
|
||||
color: #c92c2c;
|
||||
border: 1px solid #dddddd;
|
||||
display: inline;
|
||||
white-space: normal;
|
||||
}
|
||||
|
||||
pre[class*="language-"]:before,
|
||||
pre[class*="language-"]:after {
|
||||
content: '';
|
||||
z-index: -2;
|
||||
display: block;
|
||||
position: absolute;
|
||||
bottom: 0.75em;
|
||||
left: 0.18em;
|
||||
width: 40%;
|
||||
height: 20%;
|
||||
max-height: 13em;
|
||||
}
|
||||
|
||||
:not(pre) > code[class*="language-"]:after,
|
||||
pre[class*="language-"]:after {
|
||||
right: 0.75em;
|
||||
left: auto;
|
||||
-webkit-transform: rotate(2deg);
|
||||
-moz-transform: rotate(2deg);
|
||||
-ms-transform: rotate(2deg);
|
||||
-o-transform: rotate(2deg);
|
||||
transform: rotate(2deg);
|
||||
}
|
||||
|
||||
.token.comment,
|
||||
.token.block-comment,
|
||||
.token.prolog,
|
||||
.token.doctype,
|
||||
.token.cdata {
|
||||
color: #008000; font-style: italic;
|
||||
}
|
||||
|
||||
.token.char,
|
||||
.token.string {
|
||||
color: #A31515;
|
||||
}
|
||||
|
||||
.token.punctuation {
|
||||
color: #393A34; /* no highlight */
|
||||
}
|
||||
|
||||
.token.deleted {
|
||||
color: #c92c2c;
|
||||
}
|
||||
|
||||
.token.function-name,
|
||||
.token.function {
|
||||
color: #393A34;
|
||||
}
|
||||
|
||||
|
||||
.token.tag,
|
||||
.token.selector {
|
||||
color: #800000;
|
||||
}
|
||||
|
||||
.token.attr-name,
|
||||
.token.regex,
|
||||
.token.entity {
|
||||
color: #ff0000;
|
||||
}
|
||||
|
||||
.token.builtin,
|
||||
.token.url,
|
||||
.token.symbol,
|
||||
.token.number,
|
||||
.token.boolean,
|
||||
.token.variable,
|
||||
.token.constant,
|
||||
.token.inserted {
|
||||
color: #36acaa;
|
||||
}
|
||||
|
||||
.token.operator,
|
||||
.token.variable {
|
||||
color: #a67f59;
|
||||
background: rgba(255, 255, 255, 0.5);
|
||||
}
|
||||
|
||||
.token.atrule,
|
||||
.token.attr-value,
|
||||
.token.keyword,
|
||||
.token.class-name {
|
||||
color: #0000ff;
|
||||
}
|
||||
|
||||
.token.directive.tag .tag {
|
||||
background: #ffff00;
|
||||
color: #393A34;
|
||||
}
|
||||
|
||||
.token.class-name,
|
||||
.token.property {
|
||||
color: #2B91AF;
|
||||
}
|
||||
|
||||
.token.important {
|
||||
color: #e90;
|
||||
}
|
||||
|
||||
.language-css .token.string,
|
||||
.style .token.string {
|
||||
color: #a67f59;
|
||||
background: rgba(255, 255, 255, 0.5);
|
||||
}
|
||||
|
||||
.token.important,
|
||||
.token.bold {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.token.bold {
|
||||
font-weight: bold;
|
||||
}
|
||||
.token.italic {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.token.entity {
|
||||
cursor: help;
|
||||
}
|
||||
|
||||
.namespace {
|
||||
opacity: .7;
|
||||
}
|
||||
|
||||
@media screen and (max-width: 767px) {
|
||||
pre[class*="language-"]:before,
|
||||
pre[class*="language-"]:after {
|
||||
bottom: 14px;
|
||||
box-shadow: none;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Plugin styles */
|
||||
.token.tab:not(:empty):before,
|
||||
.token.cr:before,
|
||||
.token.lf:before {
|
||||
color: #e0d7d1;
|
||||
}
|
||||
|
||||
/* Plugin styles: Line Numbers */
|
||||
pre[class*="language-"].line-numbers.line-numbers {
|
||||
padding-left: 0;
|
||||
}
|
||||
|
||||
pre[class*="language-"].line-numbers.line-numbers code {
|
||||
padding-left: 3.8em;
|
||||
}
|
||||
|
||||
pre[class*="language-"].line-numbers.line-numbers .line-numbers-rows {
|
||||
left: 0;
|
||||
}
|
||||
|
||||
/* Plugin styles: Line Highlight */
|
||||
pre[class*="language-"][data-line] {
|
||||
padding-top: 0;
|
||||
padding-bottom: 0;
|
||||
padding-left: 0;
|
||||
}
|
||||
pre[data-line] code {
|
||||
position: relative;
|
||||
padding-left: 4em;
|
||||
}
|
||||
pre .line-highlight {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
pre[data-line] {
|
||||
position: relative;
|
||||
padding: 1em 0 1em 3em;
|
||||
}
|
||||
|
||||
.line-highlight {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
right: 0;
|
||||
padding: inherit 0;
|
||||
margin-top: 1em; /* Same as .prism’s padding-top */
|
||||
|
||||
background: hsla(24, 20%, 50%,.08);
|
||||
background: linear-gradient(to right, hsla(24, 20%, 50%,.1) 70%, hsla(24, 20%, 50%,0));
|
||||
|
||||
pointer-events: none;
|
||||
|
||||
line-height: inherit;
|
||||
white-space: pre;
|
||||
}
|
||||
|
||||
.line-highlight:before,
|
||||
.line-highlight[data-end]:after {
|
||||
content: attr(data-start);
|
||||
position: absolute;
|
||||
top: .4em;
|
||||
left: .6em;
|
||||
min-width: 1em;
|
||||
padding: 0 .5em;
|
||||
background-color: hsla(24, 20%, 50%,.4);
|
||||
color: hsl(24, 20%, 95%);
|
||||
font: bold 65%/1.5 sans-serif;
|
||||
text-align: center;
|
||||
vertical-align: .3em;
|
||||
border-radius: 999px;
|
||||
text-shadow: none;
|
||||
box-shadow: 0 1px white;
|
||||
}
|
||||
|
||||
.line-highlight[data-end]:after {
|
||||
content: attr(data-end);
|
||||
top: auto;
|
||||
bottom: .4em;
|
||||
}
|
||||
|
||||
.line-numbers .line-highlight:before,
|
||||
.line-numbers .line-highlight:after {
|
||||
content: none;
|
||||
}
|
||||
|
||||
pre[class*="language-"].line-numbers {
|
||||
position: relative;
|
||||
padding-left: 3.8em;
|
||||
counter-reset: linenumber;
|
||||
}
|
||||
|
||||
pre[class*="language-"].line-numbers > code {
|
||||
position: relative;
|
||||
white-space: inherit;
|
||||
}
|
||||
|
||||
.line-numbers .line-numbers-rows {
|
||||
position: absolute;
|
||||
pointer-events: none;
|
||||
top: 0;
|
||||
font-size: 100%;
|
||||
left: -3.8em;
|
||||
width: 3em; /* works for line-numbers below 1000 lines */
|
||||
letter-spacing: -1px;
|
||||
border-right: 1px solid #a5a5a5;
|
||||
|
||||
-webkit-user-select: none;
|
||||
-moz-user-select: none;
|
||||
-ms-user-select: none;
|
||||
user-select: none;
|
||||
|
||||
}
|
||||
|
||||
.line-numbers-rows > span {
|
||||
pointer-events: none;
|
||||
display: block;
|
||||
counter-increment: linenumber;
|
||||
}
|
||||
|
||||
.line-numbers-rows > span:before {
|
||||
content: counter(linenumber);
|
||||
color: #2B91AF;
|
||||
display: block;
|
||||
padding-right: 0.8em;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
div.code-toolbar {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
div.code-toolbar > .toolbar {
|
||||
position: absolute;
|
||||
top: .3em;
|
||||
right: .2em;
|
||||
transition: opacity 0.3s ease-in-out;
|
||||
opacity: 0;
|
||||
}
|
||||
|
||||
div.code-toolbar:hover > .toolbar {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
div.code-toolbar > .toolbar .toolbar-item {
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
div.code-toolbar > .toolbar a {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
div.code-toolbar > .toolbar button {
|
||||
background: none;
|
||||
border: 0;
|
||||
color: inherit;
|
||||
font: inherit;
|
||||
line-height: normal;
|
||||
overflow: visible;
|
||||
padding: 0;
|
||||
-webkit-user-select: none; /* for button */
|
||||
-moz-user-select: none;
|
||||
-ms-user-select: none;
|
||||
}
|
||||
|
||||
div.code-toolbar > .toolbar a,
|
||||
div.code-toolbar > .toolbar button,
|
||||
div.code-toolbar > .toolbar span {
|
||||
color: #bbb;
|
||||
font-size: .8em;
|
||||
padding: 0 .5em;
|
||||
background: #f5f2f0;
|
||||
background: rgba(224, 224, 224, 0.2);
|
||||
box-shadow: 0 2px 0 0 rgba(0,0,0,0.2);
|
||||
border-radius: .5em;
|
||||
}
|
||||
|
||||
div.code-toolbar > .toolbar a:hover,
|
||||
div.code-toolbar > .toolbar a:focus,
|
||||
div.code-toolbar > .toolbar button:hover,
|
||||
div.code-toolbar > .toolbar button:focus,
|
||||
div.code-toolbar > .toolbar span:hover,
|
||||
div.code-toolbar > .toolbar span:focus {
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
/* https://github.com/PrismJS/prism-themes/blob/master/themes/prism-vsc-dark-plus.css */
|
||||
pre[class*="language-"],
|
||||
code[class*="language-"] {
|
||||
color: #d4d4d4;
|
||||
font-size: 13px;
|
||||
text-shadow: none;
|
||||
font-family: Menlo, Monaco, Consolas, "Andale Mono", "Ubuntu Mono", "Courier New", monospace;
|
||||
direction: ltr;
|
||||
text-align: left;
|
||||
white-space: pre-wrap;
|
||||
word-spacing: normal;
|
||||
word-break: break-word;
|
||||
line-height: 1.5;
|
||||
-moz-tab-size: 4;
|
||||
-o-tab-size: 4;
|
||||
tab-size: 4;
|
||||
-webkit-hyphens: none;
|
||||
-moz-hyphens: none;
|
||||
-ms-hyphens: none;
|
||||
hyphens: none;
|
||||
}
|
||||
|
||||
pre[class*="language-"] {
|
||||
position: relative;
|
||||
margin: .5em 0;
|
||||
overflow: visible;
|
||||
background: #1e1e1e;
|
||||
}
|
||||
|
||||
pre[class*="language-"]>code {
|
||||
border-left: 4px solid #358ccb;
|
||||
}
|
||||
|
||||
code[class*="language"] {
|
||||
max-height: inherit;
|
||||
height: inherit;
|
||||
padding: 0.5em 1em;
|
||||
display: block;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
pre[class*="language-"]::selection,
|
||||
code[class*="language-"]::selection,
|
||||
pre[class*="language-"] *::selection,
|
||||
code[class*="language-"] *::selection {
|
||||
text-shadow: none;
|
||||
background: #75a7ca;
|
||||
}
|
||||
|
||||
@media print {
|
||||
pre[class*="language-"],
|
||||
code[class*="language-"] {
|
||||
text-shadow: none;
|
||||
}
|
||||
}
|
||||
|
||||
:not(pre) > code[class*="language-"] {
|
||||
padding: .1em .3em;
|
||||
border-radius: .3em;
|
||||
color: #db4c69;
|
||||
background: #f9f2f4;
|
||||
}
|
||||
/*********************************************************
|
||||
* Tokens
|
||||
*/
|
||||
.namespace {
|
||||
opacity: .7;
|
||||
}
|
||||
|
||||
.token.doctype .token.doctype-tag {
|
||||
color: #569CD6;
|
||||
}
|
||||
|
||||
.token.doctype .token.name {
|
||||
color: #9cdcfe;
|
||||
}
|
||||
|
||||
.token.comment,
|
||||
.token.prolog {
|
||||
color: #6a9955;
|
||||
}
|
||||
|
||||
.token.punctuation,
|
||||
.language-html .language-css .token.punctuation,
|
||||
.language-html .language-javascript .token.punctuation {
|
||||
color: #d4d4d4;
|
||||
}
|
||||
|
||||
.token.property,
|
||||
.token.tag,
|
||||
.token.boolean,
|
||||
.token.number,
|
||||
.token.constant,
|
||||
.token.symbol,
|
||||
.token.inserted,
|
||||
.token.unit {
|
||||
color: #b5cea8;
|
||||
}
|
||||
|
||||
.token.selector,
|
||||
.token.attr-name,
|
||||
.token.string,
|
||||
.token.char,
|
||||
.token.builtin,
|
||||
.token.deleted {
|
||||
color: #ce9178;
|
||||
}
|
||||
|
||||
.language-css .token.string.url {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.token.operator,
|
||||
.token.entity {
|
||||
color: #d4d4d4;
|
||||
}
|
||||
|
||||
.token.operator.arrow {
|
||||
color: #569CD6;
|
||||
}
|
||||
|
||||
.token.atrule {
|
||||
color: #ce9178;
|
||||
}
|
||||
|
||||
.token.atrule .token.rule {
|
||||
color: #c586c0;
|
||||
}
|
||||
|
||||
.token.atrule .token.url {
|
||||
color: #9cdcfe;
|
||||
}
|
||||
|
||||
.token.atrule .token.url .token.function {
|
||||
color: #dcdcaa;
|
||||
}
|
||||
|
||||
.token.atrule .token.url .token.punctuation {
|
||||
color: #d4d4d4;
|
||||
}
|
||||
|
||||
.token.keyword {
|
||||
color: #569CD6;
|
||||
}
|
||||
|
||||
.token.keyword.module,
|
||||
.token.keyword.control-flow {
|
||||
color: #c586c0;
|
||||
}
|
||||
|
||||
.token.function,
|
||||
.token.function .token.maybe-class-name {
|
||||
color: #dcdcaa;
|
||||
}
|
||||
|
||||
.token.regex {
|
||||
color: #d16969;
|
||||
}
|
||||
|
||||
.token.important {
|
||||
color: #569cd6;
|
||||
}
|
||||
|
||||
.token.italic {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.token.constant {
|
||||
color: #9cdcfe;
|
||||
}
|
||||
|
||||
.token.class-name,
|
||||
.token.maybe-class-name {
|
||||
color: #4ec9b0;
|
||||
}
|
||||
|
||||
.token.console {
|
||||
color: #9cdcfe;
|
||||
}
|
||||
|
||||
.token.parameter {
|
||||
color: #9cdcfe;
|
||||
}
|
||||
|
||||
.token.interpolation {
|
||||
color: #9cdcfe;
|
||||
}
|
||||
|
||||
.token.punctuation.interpolation-punctuation {
|
||||
color: #569cd6;
|
||||
}
|
||||
|
||||
.token.boolean {
|
||||
color: #569cd6;
|
||||
}
|
||||
|
||||
.token.property,
|
||||
.token.variable,
|
||||
.token.imports .token.maybe-class-name,
|
||||
.token.exports .token.maybe-class-name {
|
||||
color: #9cdcfe;
|
||||
}
|
||||
|
||||
.token.selector {
|
||||
color: #d7ba7d;
|
||||
}
|
||||
|
||||
.token.escape {
|
||||
color: #d7ba7d;
|
||||
}
|
||||
|
||||
.token.tag {
|
||||
color: #569cd6;
|
||||
}
|
||||
|
||||
.token.tag .token.punctuation {
|
||||
color: #808080;
|
||||
}
|
||||
|
||||
.token.cdata {
|
||||
color: #808080;
|
||||
}
|
||||
|
||||
.token.attr-name {
|
||||
color: #9cdcfe;
|
||||
}
|
||||
|
||||
.token.attr-value,
|
||||
.token.attr-value .token.punctuation {
|
||||
color: #ce9178;
|
||||
}
|
||||
|
||||
.token.attr-value .token.punctuation.attr-equals {
|
||||
color: #d4d4d4;
|
||||
}
|
||||
|
||||
.token.entity {
|
||||
color: #569cd6;
|
||||
}
|
||||
|
||||
.token.namespace {
|
||||
color: #4ec9b0;
|
||||
}
|
||||
/*********************************************************
|
||||
* Language Specific
|
||||
*/
|
||||
|
||||
pre[class*="language-javascript"],
|
||||
code[class*="language-javascript"],
|
||||
pre[class*="language-jsx"],
|
||||
code[class*="language-jsx"],
|
||||
pre[class*="language-typescript"],
|
||||
code[class*="language-typescript"],
|
||||
pre[class*="language-tsx"],
|
||||
code[class*="language-tsx"] {
|
||||
color: #9cdcfe;
|
||||
}
|
||||
|
||||
pre[class*="language-css"],
|
||||
code[class*="language-css"] {
|
||||
color: #ce9178;
|
||||
}
|
||||
|
||||
pre[class*="language-html"],
|
||||
code[class*="language-html"] {
|
||||
color: #d4d4d4;
|
||||
}
|
||||
|
||||
.language-regex .token.anchor {
|
||||
color: #dcdcaa;
|
||||
}
|
||||
|
||||
.language-html .token.punctuation {
|
||||
color: #808080;
|
||||
}
|
||||
/*********************************************************
|
||||
* Line highlighting
|
||||
*/
|
||||
pre[data-line] {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
pre[class*="language-"] > code[class*="language-"] {
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
}
|
||||
|
||||
.line-highlight {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
right: 0;
|
||||
padding: inherit 0;
|
||||
margin-top: 1em;
|
||||
background: #f7ebc6;
|
||||
box-shadow: inset 5px 0 0 #f7d87c;
|
||||
z-index: 0;
|
||||
pointer-events: none;
|
||||
line-height: inherit;
|
||||
white-space: pre;
|
||||
}
|
||||
}
|
||||
24
site/.lunet/js/xoofx.js
Normal file
24
site/.lunet/js/xoofx.js
Normal file
@@ -0,0 +1,24 @@
|
||||
anchors.add();
|
||||
var jstoc = document.getElementsByClassName("js-toc");
|
||||
if (jstoc.length > 0)
|
||||
{
|
||||
tocbot.init({
|
||||
// Which headings to grab inside of the contentSelector element.
|
||||
headingSelector: 'h2, h3, h4, h5',
|
||||
collapseDepth: 3,
|
||||
orderedList: true,
|
||||
hasInnerContainers: true,
|
||||
});
|
||||
}
|
||||
|
||||
(function () {
|
||||
const InitLunetTheme = function (e) {
|
||||
if (halfmoon.darkModeOn != e.matches) {
|
||||
halfmoon.toggleDarkMode();
|
||||
}
|
||||
}
|
||||
|
||||
let colorSchemeQueryList = window.matchMedia('(prefers-color-scheme: dark)')
|
||||
InitLunetTheme(colorSchemeQueryList);
|
||||
colorSchemeQueryList.addListener(InitLunetTheme);
|
||||
})();
|
||||
65
site/.lunet/layouts/_default.sbn-html
Normal file
65
site/.lunet/layouts/_default.sbn-html
Normal file
@@ -0,0 +1,65 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" itemscope itemtype="http://schema.org/WebPage" class="my-html-setup">
|
||||
<head>
|
||||
{{~ Head ~}}
|
||||
</head>
|
||||
<body class="with-custom-webkit-scrollbars with-custom-css-scrollbars">
|
||||
<div class="page-wrapper with-transitions">
|
||||
<div class="sticky-alerts"></div>
|
||||
<div class="content-wrapper bg-light-lm bg-dark-dm">
|
||||
<div class="container-xl bg-white-lm bg-dark-light-dm">
|
||||
<div class="row">
|
||||
<div class="col-xl-12">
|
||||
<nav class="navbar">
|
||||
<a class="site-logo navbar-brand" href="/"></a>
|
||||
<div id="navbarSupportedContent" class="collapse navbar-collapse justify-content-between">
|
||||
{{~ site.menu.home.render {
|
||||
depth: 1,
|
||||
kind: "nav",
|
||||
list_class: "w-100"
|
||||
}
|
||||
~}}
|
||||
</div>
|
||||
</nav>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row main-row">
|
||||
{{~ $main_content_col_size = 8 ~}}
|
||||
{{~ if page.nomenu; $main_content_col_size = $main_content_col_size + 2; end }}
|
||||
{{~ if page.notoc; $main_content_col_size = $main_content_col_size + 2; end }}
|
||||
{{~ if !page.nomenu ~}}
|
||||
<div class="col-xl-2 ">
|
||||
<div class="content">
|
||||
Menu
|
||||
</div>
|
||||
</div>
|
||||
{{~ end ~}}
|
||||
<div class="col-xl-{{ $main_content_col_size }} ">
|
||||
<div class="content js-toc-content">
|
||||
{{ content }}
|
||||
</div>
|
||||
</div>
|
||||
{{~ if !page.notoc ~}}
|
||||
<div class="col-xl-2 ">
|
||||
<div class="content">
|
||||
<div class="in-this-article-nav">
|
||||
<div class="title">In this article</div>
|
||||
<nav class="js-toc toc"></nav>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{~ end ~}}
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-xl-12">
|
||||
<nav class="navbar navbar-fixed-bottom justify-content-center">
|
||||
<footer>Copyright © 2009 - {{ date.now.year }}, Alexandre Mutel - Blog content licensed under the Creative Commons <a href="http://creativecommons.org/licenses/by/2.5/">CC BY 2.5</a> | Site powered by <a href="https://github.com/lunet-io/lunet">lunet</a></footer>
|
||||
</nav>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
5
site/.lunet/layouts/simple.sbn-html
Normal file
5
site/.lunet/layouts/simple.sbn-html
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
layout: _default
|
||||
---
|
||||
<h1 class="title">{{ page.title_html ?? page.title }}</h1>
|
||||
{{ content }}
|
||||
70
site/config.scriban
Normal file
70
site/config.scriban
Normal file
@@ -0,0 +1,70 @@
|
||||
# Site Settings
|
||||
author = "Alexandre Mutel"
|
||||
title = "markdig"
|
||||
description = "markdig website"
|
||||
|
||||
html.head.title = do; ret page.title + " | " + site.title; end
|
||||
|
||||
basepath = ""
|
||||
baseurl = baseurl ?? "https://markdig.net"
|
||||
|
||||
# Github repository
|
||||
github_user = "xoofx"
|
||||
github_repo_url = "https://github.com/lunet-io/markdig/"
|
||||
|
||||
with cards.twitter
|
||||
enable = true
|
||||
card = "summary_large_image"
|
||||
user = "markdig"
|
||||
image = "/images/twitter-banner.png"
|
||||
end
|
||||
|
||||
# Resources bundle
|
||||
with bundle
|
||||
fontawesome = resource "npm:font-awesome" "4.7.0"
|
||||
halfmoon = resource "npm:halfmoon" "1.1.0"
|
||||
tocbot = resource "npm:tocbot" "4.12.1"
|
||||
anchorjs = resource "npm:anchor-js" "4.2.2"
|
||||
prismjs = resource "npm:prismjs" "1.20.0"
|
||||
|
||||
# scss.includes.add fontawesome.path + "/scss"
|
||||
|
||||
# css files
|
||||
css halfmoon "/css/halfmoon-variables.min.css"
|
||||
css tocbot "/dist/tocbot.css"
|
||||
css fontawesome "/css/font-awesome.min.css"
|
||||
css "/css/prism.css"
|
||||
css "/css/main.css"
|
||||
|
||||
# js files
|
||||
js anchorjs "/anchor.min.js"
|
||||
js halfmoon "/js/halfmoon.min.js"
|
||||
js tocbot "/dist/tocbot.min.js"
|
||||
js prismjs "/prism.js"
|
||||
js prismjs "/components/prism-shell-session.min.js"
|
||||
js prismjs "/components/prism-clike.min.js"
|
||||
js prismjs "/components/prism-c.min.js"
|
||||
js prismjs "/components/prism-cpp.min.js"
|
||||
js prismjs "/components/prism-csharp.min.js"
|
||||
js "/js/prism-stark.js"
|
||||
js "/js/xoofx.js"
|
||||
|
||||
# copy font files
|
||||
content fontawesome "/fonts/fontawesome-webfont.*" "/fonts/"
|
||||
|
||||
# concatenate css/js files
|
||||
concat = true
|
||||
minify = true
|
||||
end
|
||||
|
||||
with attributes
|
||||
# match "/blog/**/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]*.md" {
|
||||
# url: "/:section/:year/:month/:day/:title:output_ext"
|
||||
#}
|
||||
end
|
||||
|
||||
with api.dotnet
|
||||
title = "Markdig .NET API Reference"
|
||||
projects = ["../src/Markdig/Markdig.csproj"]
|
||||
properties = {TargetFramework: "netstandard2.0"}
|
||||
end
|
||||
BIN
site/img/markdig.png
Normal file
BIN
site/img/markdig.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 16 KiB |
3
site/menu.yml
Normal file
3
site/menu.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
home:
|
||||
- {path: readme.md}
|
||||
- {path: api/readme.md, title: "Markdig API"}
|
||||
10
site/readme.md
Normal file
10
site/readme.md
Normal file
@@ -0,0 +1,10 @@
|
||||
---
|
||||
layout: simple
|
||||
title: Home
|
||||
title_html: Hi and Welcome!
|
||||
---
|
||||
|
||||
Hello World!
|
||||
|
||||
Test: <xref:system.object>
|
||||
|
||||
8
site/system.md
Normal file
8
site/system.md
Normal file
@@ -0,0 +1,8 @@
|
||||
---
|
||||
uid: system.object
|
||||
layout: simple
|
||||
title: Home
|
||||
title_html: Hi and Welcome!
|
||||
---
|
||||
|
||||
Hello World from system.object!
|
||||
@@ -8,29 +8,11 @@
|
||||
<ItemGroup>
|
||||
<None Remove="spec.md" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="CommonMarkNew, Version=0.1.0.0, Culture=neutral, PublicKeyToken=001ef8810438905d, processorArchitecture=MSIL">
|
||||
<HintPath>lib\CommonMarkNew.dll</HintPath>
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<Aliases>newcmark</Aliases>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="MoonShine">
|
||||
<HintPath>lib\MoonShine.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MarkdownDeep">
|
||||
<HintPath>lib\MarkdownDeep.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="cmark.dll">
|
||||
<HintPath>cmark.dll</HintPath>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="libsundown.dll">
|
||||
<HintPath>libsundown.dll</HintPath>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="spec.md">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
@@ -39,9 +21,9 @@
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
|
||||
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.1" />
|
||||
<PackageReference Include="CommonMark.NET" Version="0.15.1" />
|
||||
<PackageReference Include="Markdown" Version="2.2.1" />
|
||||
<PackageReference Include="MarkdownSharp" Version="2.0.5" />
|
||||
<PackageReference Include="Microsoft.Diagnostics.Runtime" Version="2.0.226801" />
|
||||
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="2.0.74" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Markdig\Markdig.csproj" />
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
extern alias newcmark;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
using System.IO;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Configs;
|
||||
@@ -25,7 +23,7 @@ namespace Testamina.Markdig.Benchmarks
|
||||
}
|
||||
|
||||
//[Benchmark(Description = "TestMarkdig", OperationsPerInvoke = 4096)]
|
||||
[Benchmark]
|
||||
[Benchmark(Description = "markdig")]
|
||||
public void TestMarkdig()
|
||||
{
|
||||
//var reader = new StreamReader(File.Open("spec.md", FileMode.Open));
|
||||
@@ -33,7 +31,7 @@ namespace Testamina.Markdig.Benchmarks
|
||||
//File.WriteAllText("spec.html", writer.ToString());
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
[Benchmark(Description = "cmark")]
|
||||
public void TestCommonMarkCpp()
|
||||
{
|
||||
//var reader = new StreamReader(File.Open("spec.md", FileMode.Open));
|
||||
@@ -41,7 +39,7 @@ namespace Testamina.Markdig.Benchmarks
|
||||
//File.WriteAllText("spec.html", writer.ToString());
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
[Benchmark(Description = "CommonMark.NET")]
|
||||
public void TestCommonMarkNet()
|
||||
{
|
||||
////var reader = new StreamReader(File.Open("spec.md", FileMode.Open));
|
||||
@@ -55,93 +53,25 @@ namespace Testamina.Markdig.Benchmarks
|
||||
//writer.ToString();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void TestCommonMarkNetNew()
|
||||
{
|
||||
////var reader = new StreamReader(File.Open("spec.md", FileMode.Open));
|
||||
// var reader = new StringReader(text);
|
||||
//CommonMark.CommonMarkConverter.Parse(reader);
|
||||
//CommonMark.CommonMarkConverter.Parse(reader);
|
||||
//reader.Dispose();
|
||||
//var writer = new StringWriter();
|
||||
newcmark::CommonMark.CommonMarkConverter.Convert(text);
|
||||
//writer.Flush();
|
||||
//writer.ToString();
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void TestMarkdownDeep()
|
||||
{
|
||||
new MarkdownDeep.Markdown().Transform(text);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
[Benchmark(Description = "MarkdownSharp")]
|
||||
public void TestMarkdownSharp()
|
||||
{
|
||||
new MarkdownSharp.Markdown().Transform(text);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public void TestMoonshine()
|
||||
{
|
||||
Sundown.MoonShine.Markdownify(text);
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
bool markdig = args.Length == 0;
|
||||
bool simpleBench = false;
|
||||
var config = ManualConfig.Create(DefaultConfig.Instance);
|
||||
//var gcDiagnoser = new MemoryDiagnoser();
|
||||
//config.Add(new Job { Mode = Mode.SingleRun, LaunchCount = 2, WarmupCount = 2, IterationTime = 1024, TargetCount = 10 });
|
||||
//config.Add(new Job { Mode = Mode.Throughput, LaunchCount = 2, WarmupCount = 2, TargetCount = 10 });
|
||||
//config.Add(gcDiagnoser);
|
||||
|
||||
if (simpleBench)
|
||||
{
|
||||
var clock = Stopwatch.StartNew();
|
||||
var program = new Program();
|
||||
|
||||
GC.Collect(2, GCCollectionMode.Forced, true);
|
||||
|
||||
var gc0 = GC.CollectionCount(0);
|
||||
var gc1 = GC.CollectionCount(1);
|
||||
var gc2 = GC.CollectionCount(2);
|
||||
|
||||
const int count = 12*64;
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (markdig)
|
||||
{
|
||||
program.TestMarkdig();
|
||||
}
|
||||
else
|
||||
{
|
||||
program.TestCommonMarkNetNew();
|
||||
}
|
||||
}
|
||||
clock.Stop();
|
||||
Console.WriteLine((markdig ? "MarkDig" : "CommonMark") + $" => time: {(double)clock.ElapsedMilliseconds/count}ms (total {clock.ElapsedMilliseconds}ms)");
|
||||
DumpGC(gc0, gc1, gc2);
|
||||
}
|
||||
else
|
||||
{
|
||||
//new TestMatchPerf().TestMatch();
|
||||
|
||||
var config = ManualConfig.Create(DefaultConfig.Instance);
|
||||
//var gcDiagnoser = new MemoryDiagnoser();
|
||||
//config.Add(new Job { Mode = Mode.SingleRun, LaunchCount = 2, WarmupCount = 2, IterationTime = 1024, TargetCount = 10 });
|
||||
//config.Add(new Job { Mode = Mode.Throughput, LaunchCount = 2, WarmupCount = 2, TargetCount = 10 });
|
||||
//config.Add(gcDiagnoser);
|
||||
|
||||
//var config = DefaultConfig.Instance;
|
||||
BenchmarkRunner.Run<Program>(config);
|
||||
//BenchmarkRunner.Run<TestDictionary>(config);
|
||||
//BenchmarkRunner.Run<TestMatchPerf>();
|
||||
//BenchmarkRunner.Run<TestStringPerf>();
|
||||
}
|
||||
}
|
||||
|
||||
private static void DumpGC(int gc0, int gc1, int gc2)
|
||||
{
|
||||
Console.WriteLine($"gc0: {GC.CollectionCount(0)-gc0}");
|
||||
Console.WriteLine($"gc1: {GC.CollectionCount(1)-gc1}");
|
||||
Console.WriteLine($"gc2: {GC.CollectionCount(2)-gc2}");
|
||||
//var config = DefaultConfig.Instance;
|
||||
BenchmarkRunner.Run<Program>(config);
|
||||
//BenchmarkRunner.Run<TestDictionary>(config);
|
||||
//BenchmarkRunner.Run<TestMatchPerf>();
|
||||
//BenchmarkRunner.Run<TestStringPerf>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
@@ -10,13 +10,9 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.msbuild" Version="3.1.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
|
||||
<PackageReference Include="NUnit" Version="3.13.2" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.2.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
|
||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -32,7 +28,7 @@
|
||||
<InputSpecFiles Include="RoundtripSpecs\*.md" />
|
||||
<InputSpecFiles Remove="Specs\readme.md" />
|
||||
<!-- Allow Visual Studio up-to-date check to verify that nothing has changed - https://github.com/dotnet/project-system/blob/main/docs/up-to-date-check.md -->
|
||||
<UpToDateCheckInput Include="@(InputSpecFiles)"/>
|
||||
<UpToDateCheckInput Include="@(InputSpecFiles)" />
|
||||
<OutputSpecFiles Include="@(InputSpecFiles->'%(RelativeDir)%(Filename).generated.cs')" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
25
src/Markdig.Tests/RoundtripSpecs/TestYamlFrontMatterBlock.cs
Normal file
25
src/Markdig.Tests/RoundtripSpecs/TestYamlFrontMatterBlock.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Markdig.Renderers.Roundtrip;
|
||||
using Markdig.Syntax;
|
||||
using NUnit.Framework;
|
||||
using static Markdig.Tests.TestRoundtrip;
|
||||
|
||||
namespace Markdig.Tests.RoundtripSpecs
|
||||
{
|
||||
[TestFixture]
|
||||
public class TestYamlFrontMatterBlock
|
||||
{
|
||||
[TestCase("---\nkey1: value1\nkey2: value2\n---\n\nContent\n")]
|
||||
[TestCase("No front matter")]
|
||||
[TestCase("Looks like front matter but actually is not\n---\nkey1: value1\nkey2: value2\n---")]
|
||||
public void FrontMatterBlockIsPreserved(string value)
|
||||
{
|
||||
RoundTrip(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -226,5 +226,27 @@ namespace Markdig.Tests.Specs.Globalization
|
||||
|
||||
TestParser.TestSpec("Nutrition |Apple | Oranges\n--|-- | --\nCalories|52|47\nSugar|10g|9g\n\n پێکهاتە |سێو | پڕتەقاڵ\n--|-- | --\nکالۆری|٥٢|٤٧\nشەکر| ١٠گ|٩گ", "<table>\n<thead>\n<tr>\n<th>Nutrition</th>\n<th>Apple</th>\n<th>Oranges</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Calories</td>\n<td>52</td>\n<td>47</td>\n</tr>\n<tr>\n<td>Sugar</td>\n<td>10g</td>\n<td>9g</td>\n</tr>\n</tbody>\n</table>\n<table dir=\"rtl\" align=\"right\">\n<thead>\n<tr>\n<th>پێکهاتە</th>\n<th>سێو</th>\n<th>پڕتەقاڵ</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>کالۆری</td>\n<td>٥٢</td>\n<td>٤٧</td>\n</tr>\n<tr>\n<td>شەکر</td>\n<td>١٠گ</td>\n<td>٩گ</td>\n</tr>\n</tbody>\n</table>", "globalization+advanced+emojis", context: "Example 3\nSection Extensions / Globalization\n");
|
||||
}
|
||||
|
||||
// But if text starts with LTR characters, no attributes are added.
|
||||
[Test]
|
||||
public void ExtensionsGlobalization_Example004()
|
||||
{
|
||||
// Example 4
|
||||
// Section: Extensions / Globalization
|
||||
//
|
||||
// The following Markdown:
|
||||
// Foo میوە
|
||||
//
|
||||
// میوە bar
|
||||
//
|
||||
// Baz میوە
|
||||
//
|
||||
// Should be rendered as:
|
||||
// <p>Foo میوە</p>
|
||||
// <p dir="rtl">میوە bar</p>
|
||||
// <p>Baz میوە</p>
|
||||
|
||||
TestParser.TestSpec("Foo میوە\n\nمیوە bar\n\nBaz میوە", "<p>Foo میوە</p>\n<p dir=\"rtl\">میوە bar</p>\n<p>Baz میوە</p>", "globalization+advanced+emojis", context: "Example 4\nSection Extensions / Globalization\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,4 +186,18 @@ Sugar|10g|9g
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
````````````````````````````````
|
||||
|
||||
But if text starts with LTR characters, no attributes are added.
|
||||
|
||||
```````````````````````````````` example
|
||||
Foo میوە
|
||||
|
||||
میوە bar
|
||||
|
||||
Baz میوە
|
||||
.
|
||||
<p>Foo میوە</p>
|
||||
<p dir="rtl">میوە bar</p>
|
||||
<p>Baz میوە</p>
|
||||
````````````````````````````````
|
||||
@@ -88,5 +88,102 @@ namespace Markdig.Tests
|
||||
|
||||
Assert.Throws<ArgumentException>(() => container[0] = two); // two already has a parent
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Contains()
|
||||
{
|
||||
var container = new MockContainerBlock();
|
||||
var block = new ParagraphBlock();
|
||||
|
||||
Assert.False(container.Contains(block));
|
||||
|
||||
container.Add(block);
|
||||
Assert.True(container.Contains(block));
|
||||
|
||||
container.Add(new ParagraphBlock());
|
||||
Assert.True(container.Contains(block));
|
||||
|
||||
container.Insert(0, new ParagraphBlock());
|
||||
Assert.True(container.Contains(block));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Remove()
|
||||
{
|
||||
var container = new MockContainerBlock();
|
||||
var block = new ParagraphBlock();
|
||||
|
||||
Assert.False(container.Remove(block));
|
||||
Assert.AreEqual(0, container.Count);
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => container.RemoveAt(0));
|
||||
Assert.AreEqual(0, container.Count);
|
||||
|
||||
container.Add(block);
|
||||
Assert.AreEqual(1, container.Count);
|
||||
Assert.True(container.Remove(block));
|
||||
Assert.AreEqual(0, container.Count);
|
||||
Assert.False(container.Remove(block));
|
||||
Assert.AreEqual(0, container.Count);
|
||||
|
||||
container.Add(block);
|
||||
Assert.AreEqual(1, container.Count);
|
||||
container.RemoveAt(0);
|
||||
Assert.AreEqual(0, container.Count);
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => container.RemoveAt(0));
|
||||
Assert.AreEqual(0, container.Count);
|
||||
|
||||
container.Add(new ParagraphBlock { Column = 1 });
|
||||
container.Add(new ParagraphBlock { Column = 2 });
|
||||
container.Add(new ParagraphBlock { Column = 3 });
|
||||
container.Add(new ParagraphBlock { Column = 4 });
|
||||
Assert.AreEqual(4, container.Count);
|
||||
|
||||
container.RemoveAt(2);
|
||||
Assert.AreEqual(3, container.Count);
|
||||
Assert.AreEqual(4, container[2].Column);
|
||||
|
||||
Assert.True(container.Remove(container[1]));
|
||||
Assert.AreEqual(2, container.Count);
|
||||
Assert.AreEqual(1, container[0].Column);
|
||||
Assert.AreEqual(4, container[1].Column);
|
||||
Assert.Throws<IndexOutOfRangeException>(() => _ = container[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CopyTo()
|
||||
{
|
||||
var container = new MockContainerBlock();
|
||||
|
||||
var destination = new Block[4];
|
||||
container.CopyTo(destination, 0);
|
||||
container.CopyTo(destination, 1);
|
||||
container.CopyTo(destination, -1);
|
||||
container.CopyTo(destination, 5);
|
||||
Assert.Null(destination[0]);
|
||||
|
||||
container.Add(new ParagraphBlock());
|
||||
container.CopyTo(destination, 0);
|
||||
Assert.NotNull(destination[0]);
|
||||
Assert.Null(destination[1]);
|
||||
Assert.Null(destination[2]);
|
||||
Assert.Null(destination[3]);
|
||||
|
||||
container.CopyTo(destination, 2);
|
||||
Assert.NotNull(destination[0]);
|
||||
Assert.Null(destination[1]);
|
||||
Assert.NotNull(destination[2]);
|
||||
Assert.Null(destination[3]);
|
||||
|
||||
Array.Clear(destination);
|
||||
|
||||
container.Add(new ParagraphBlock());
|
||||
container.CopyTo(destination, 1);
|
||||
Assert.Null(destination[0]);
|
||||
Assert.NotNull(destination[1]);
|
||||
Assert.NotNull(destination[2]);
|
||||
Assert.Null(destination[3]);
|
||||
|
||||
Assert.Throws<IndexOutOfRangeException>(() => container.CopyTo(destination, 3));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
189
src/Markdig.Tests/TestFastStringWriter.cs
Normal file
189
src/Markdig.Tests/TestFastStringWriter.cs
Normal file
@@ -0,0 +1,189 @@
|
||||
using Markdig.Helpers;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Markdig.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class TestFastStringWriter
|
||||
{
|
||||
private const string NewLineReplacement = "~~NEW_LINE~~";
|
||||
|
||||
private FastStringWriter _writer = new();
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_writer = new FastStringWriter
|
||||
{
|
||||
NewLine = NewLineReplacement
|
||||
};
|
||||
}
|
||||
|
||||
public void AssertToString(string value)
|
||||
{
|
||||
value = value.Replace("\n", NewLineReplacement);
|
||||
Assert.AreEqual(value, _writer.ToString());
|
||||
Assert.AreEqual(value, _writer.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task NewLine()
|
||||
{
|
||||
Assert.AreEqual("\n", new FastStringWriter().NewLine);
|
||||
|
||||
_writer.NewLine = "\r";
|
||||
Assert.AreEqual("\r", _writer.NewLine);
|
||||
|
||||
_writer.NewLine = "foo";
|
||||
Assert.AreEqual("foo", _writer.NewLine);
|
||||
|
||||
_writer.WriteLine();
|
||||
await _writer.WriteLineAsync();
|
||||
_writer.WriteLine("bar");
|
||||
Assert.AreEqual("foofoobarfoo", _writer.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task FlushCloseDispose()
|
||||
{
|
||||
_writer.Write('a');
|
||||
|
||||
// Nops
|
||||
_writer.Close();
|
||||
_writer.Dispose();
|
||||
await _writer.DisposeAsync();
|
||||
_writer.Flush();
|
||||
await _writer.FlushAsync();
|
||||
|
||||
_writer.Write('b');
|
||||
AssertToString("ab");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Write_Char()
|
||||
{
|
||||
_writer.Write('a');
|
||||
AssertToString("a");
|
||||
|
||||
_writer.Write('b');
|
||||
AssertToString("ab");
|
||||
|
||||
_writer.Write('\0');
|
||||
_writer.Write('\r');
|
||||
_writer.Write('\u1234');
|
||||
AssertToString("ab\0\r\u1234");
|
||||
|
||||
_writer.Reset();
|
||||
AssertToString("");
|
||||
|
||||
_writer.Write('a');
|
||||
_writer.WriteLine('b');
|
||||
_writer.Write('c');
|
||||
_writer.Write('d');
|
||||
_writer.WriteLine('e');
|
||||
AssertToString("ab\ncde\n");
|
||||
|
||||
await _writer.WriteAsync('f');
|
||||
await _writer.WriteLineAsync('g');
|
||||
AssertToString("ab\ncde\nfg\n");
|
||||
|
||||
_writer.Reset();
|
||||
|
||||
for (int i = 0; i < 2050; i++)
|
||||
{
|
||||
_writer.Write('a');
|
||||
AssertToString(new string('a', i + 1));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Write_String()
|
||||
{
|
||||
_writer.Write("foo");
|
||||
AssertToString("foo");
|
||||
|
||||
_writer.WriteLine("bar");
|
||||
AssertToString("foobar\n");
|
||||
|
||||
await _writer.WriteAsync("baz");
|
||||
await _writer.WriteLineAsync("foo");
|
||||
AssertToString("foobar\nbazfoo\n");
|
||||
|
||||
_writer.Write(new string('a', 1050));
|
||||
AssertToString("foobar\nbazfoo\n" + new string('a', 1050));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Write_Span()
|
||||
{
|
||||
_writer.Write("foo".AsSpan());
|
||||
AssertToString("foo");
|
||||
|
||||
_writer.WriteLine("bar".AsSpan());
|
||||
AssertToString("foobar\n");
|
||||
|
||||
await _writer.WriteAsync("baz".AsMemory());
|
||||
await _writer.WriteLineAsync("foo".AsMemory());
|
||||
AssertToString("foobar\nbazfoo\n");
|
||||
|
||||
_writer.Write(new string('a', 1050).AsSpan());
|
||||
AssertToString("foobar\nbazfoo\n" + new string('a', 1050));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Write_CharArray()
|
||||
{
|
||||
_writer.Write("foo".ToCharArray());
|
||||
AssertToString("foo");
|
||||
|
||||
_writer.WriteLine("bar".ToCharArray());
|
||||
AssertToString("foobar\n");
|
||||
|
||||
await _writer.WriteAsync("baz".ToCharArray());
|
||||
await _writer.WriteLineAsync("foo".ToCharArray());
|
||||
AssertToString("foobar\nbazfoo\n");
|
||||
|
||||
_writer.Write(new string('a', 1050).ToCharArray());
|
||||
AssertToString("foobar\nbazfoo\n" + new string('a', 1050));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Write_CharArrayWithIndexes()
|
||||
{
|
||||
_writer.Write("foo".ToCharArray(), 1, 1);
|
||||
AssertToString("o");
|
||||
|
||||
_writer.WriteLine("bar".ToCharArray(), 0, 2);
|
||||
AssertToString("oba\n");
|
||||
|
||||
await _writer.WriteAsync("baz".ToCharArray(), 0, 1);
|
||||
await _writer.WriteLineAsync("foo".ToCharArray(), 0, 3);
|
||||
AssertToString("oba\nbfoo\n");
|
||||
|
||||
_writer.Write(new string('a', 1050).ToCharArray(), 10, 1035);
|
||||
AssertToString("oba\nbfoo\n" + new string('a', 1035));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task Write_StringBuilder()
|
||||
{
|
||||
_writer.Write(new StringBuilder("foo"));
|
||||
AssertToString("foo");
|
||||
|
||||
_writer.WriteLine(new StringBuilder("bar"));
|
||||
AssertToString("foobar\n");
|
||||
|
||||
await _writer.WriteAsync(new StringBuilder("baz"));
|
||||
await _writer.WriteLineAsync(new StringBuilder("foo"));
|
||||
AssertToString("foobar\nbazfoo\n");
|
||||
|
||||
var sb = new StringBuilder("foo");
|
||||
sb.Append('a', 1050);
|
||||
_writer.Write(sb);
|
||||
AssertToString("foobar\nbazfoo\nfoo" + new string('a', 1050));
|
||||
}
|
||||
}
|
||||
}
|
||||
46
src/Markdig.Tests/TestFencedCodeBlocks.cs
Normal file
46
src/Markdig.Tests/TestFencedCodeBlocks.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Linq;
|
||||
using Markdig.Syntax;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Markdig.Tests
|
||||
{
|
||||
public class TestFencedCodeBlocks
|
||||
{
|
||||
[Test]
|
||||
[TestCase("c#", "c#", "")]
|
||||
[TestCase("C#", "C#", "")]
|
||||
[TestCase(" c#", "c#", "")]
|
||||
[TestCase(" c# ", "c#", "")]
|
||||
[TestCase(" \tc# ", "c#", "")]
|
||||
[TestCase("\t c# \t", "c#", "")]
|
||||
[TestCase(" c# ", "c#", "")]
|
||||
[TestCase(" c# foo", "c#", "foo")]
|
||||
[TestCase(" c# \t fOo \t", "c#", "fOo")]
|
||||
[TestCase("in\\%fo arg\\%ument", "in%fo", "arg%ument")]
|
||||
[TestCase("info	 arg´ument", "info\t", "arg\u00B4ument")]
|
||||
public void TestInfoAndArguments(string infoString, string expectedInfo, string expectedArguments)
|
||||
{
|
||||
Test('`');
|
||||
Test('~');
|
||||
|
||||
void Test(char fencedChar)
|
||||
{
|
||||
const string Contents = "Foo\nBar\n";
|
||||
|
||||
string fence = new string(fencedChar, 3);
|
||||
string markdownText = $"{fence}{infoString}\n{Contents}\n{fence}\n";
|
||||
|
||||
MarkdownDocument document = Markdown.Parse(markdownText);
|
||||
|
||||
FencedCodeBlock codeBlock = document.Descendants<FencedCodeBlock>().Single();
|
||||
|
||||
Assert.AreEqual(fencedChar, codeBlock.FencedChar);
|
||||
Assert.AreEqual(3, codeBlock.OpeningFencedCharCount);
|
||||
Assert.AreEqual(3, codeBlock.ClosingFencedCharCount);
|
||||
Assert.AreEqual(expectedInfo, codeBlock.Info);
|
||||
Assert.AreEqual(expectedArguments, codeBlock.Arguments);
|
||||
Assert.AreEqual(Contents, codeBlock.Lines.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
67
src/Markdig.Tests/TestLazySubstring.cs
Normal file
67
src/Markdig.Tests/TestLazySubstring.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using Markdig.Helpers;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Markdig.Tests
|
||||
{
|
||||
public class TestLazySubstring
|
||||
{
|
||||
[Theory]
|
||||
[TestCase("")]
|
||||
[TestCase("a")]
|
||||
[TestCase("foo")]
|
||||
public void LazySubstring_ReturnsCorrectSubstring(string text)
|
||||
{
|
||||
var substring = new LazySubstring(text);
|
||||
Assert.AreEqual(0, substring.Offset);
|
||||
Assert.AreEqual(text.Length, substring.Length);
|
||||
|
||||
Assert.AreEqual(text, substring.AsSpan().ToString());
|
||||
Assert.AreEqual(text, substring.AsSpan().ToString());
|
||||
Assert.AreEqual(0, substring.Offset);
|
||||
Assert.AreEqual(text.Length, substring.Length);
|
||||
|
||||
Assert.AreSame(substring.ToString(), substring.ToString());
|
||||
Assert.AreEqual(text, substring.ToString());
|
||||
Assert.AreEqual(0, substring.Offset);
|
||||
Assert.AreEqual(text.Length, substring.Length);
|
||||
|
||||
Assert.AreEqual(text, substring.AsSpan().ToString());
|
||||
Assert.AreEqual(text, substring.AsSpan().ToString());
|
||||
Assert.AreEqual(0, substring.Offset);
|
||||
Assert.AreEqual(text.Length, substring.Length);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[TestCase("", 0, 0)]
|
||||
[TestCase("a", 0, 0)]
|
||||
[TestCase("a", 1, 0)]
|
||||
[TestCase("a", 0, 1)]
|
||||
[TestCase("foo", 1, 0)]
|
||||
[TestCase("foo", 1, 1)]
|
||||
[TestCase("foo", 1, 2)]
|
||||
[TestCase("foo", 0, 3)]
|
||||
public void LazySubstring_ReturnsCorrectSubstring(string text, int start, int length)
|
||||
{
|
||||
var substring = new LazySubstring(text, start, length);
|
||||
Assert.AreEqual(start, substring.Offset);
|
||||
Assert.AreEqual(length, substring.Length);
|
||||
|
||||
string expectedSubstring = text.Substring(start, length);
|
||||
|
||||
Assert.AreEqual(expectedSubstring, substring.AsSpan().ToString());
|
||||
Assert.AreEqual(expectedSubstring, substring.AsSpan().ToString());
|
||||
Assert.AreEqual(start, substring.Offset);
|
||||
Assert.AreEqual(length, substring.Length);
|
||||
|
||||
Assert.AreSame(substring.ToString(), substring.ToString());
|
||||
Assert.AreEqual(expectedSubstring, substring.ToString());
|
||||
Assert.AreEqual(0, substring.Offset);
|
||||
Assert.AreEqual(length, substring.Length);
|
||||
|
||||
Assert.AreEqual(expectedSubstring, substring.AsSpan().ToString());
|
||||
Assert.AreEqual(expectedSubstring, substring.AsSpan().ToString());
|
||||
Assert.AreEqual(0, substring.Offset);
|
||||
Assert.AreEqual(length, substring.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -74,6 +74,35 @@ namespace Markdig.Tests
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestDocumentToHtmlWithWriter()
|
||||
{
|
||||
var writer = new StringWriter();
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
MarkdownDocument document = Markdown.Parse("This is a text with some *emphasis*");
|
||||
document.ToHtml(writer);
|
||||
string html = writer.ToString();
|
||||
Assert.AreEqual("<p>This is a text with some <em>emphasis</em></p>\n", html);
|
||||
writer.GetStringBuilder().Length = 0;
|
||||
}
|
||||
|
||||
writer = new StringWriter();
|
||||
var pipeline = new MarkdownPipelineBuilder()
|
||||
.UseAdvancedExtensions()
|
||||
.Build();
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
MarkdownDocument document = Markdown.Parse("This is a text with a https://link.tld/", pipeline);
|
||||
document.ToHtml(writer, pipeline);
|
||||
string html = writer.ToString();
|
||||
Assert.AreEqual("<p>This is a text with a <a href=\"https://link.tld/\">https://link.tld/</a></p>\n", html);
|
||||
writer.GetStringBuilder().Length = 0;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestConvert()
|
||||
{
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
using System.Linq;
|
||||
using Markdig.Syntax;
|
||||
using Markdig.Syntax.Inlines;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Markdig.Tests
|
||||
@@ -8,10 +11,21 @@ namespace Markdig.Tests
|
||||
[TestCase("a \nb", "<p>a<br />\nb</p>\n")]
|
||||
[TestCase("a\\\nb", "<p>a<br />\nb</p>\n")]
|
||||
[TestCase("a `b\nc`", "<p>a <code>b c</code></p>\n")]
|
||||
[TestCase("# Text A\nText B\n\n## Text C", "<h1>Text A</h1>\n<p>Text B</p>\n<h2>Text C</h2>\n")]
|
||||
public void Test(string value, string expectedHtml)
|
||||
{
|
||||
Assert.AreEqual(expectedHtml, Markdown.ToHtml(value));
|
||||
Assert.AreEqual(expectedHtml, Markdown.ToHtml(value.Replace("\n", "\r\n")));
|
||||
}
|
||||
|
||||
[Test()]
|
||||
public void TestEscapeLineBreak()
|
||||
{
|
||||
var input = "test\\\r\ntest1\r\n";
|
||||
var doc = Markdown.Parse(input);
|
||||
var inlines = doc.Descendants<LineBreakInline>().ToList();
|
||||
Assert.AreEqual(1, inlines.Count, "Invalid number of LineBreakInline");
|
||||
Assert.True(inlines[0].IsBackslash);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace Markdig.Tests
|
||||
[TestCase(/* markdownText: */ "# foo\nbar", /* expected: */ "foo\nbar\n")]
|
||||
[TestCase(/* markdownText: */ "> foo", /* expected: */ "foo\n")]
|
||||
[TestCase(/* markdownText: */ "> foo\nbar\n> baz", /* expected: */ "foo\nbar\nbaz\n")]
|
||||
[TestCase(/* markdownText: */ "`foo`", /* expected: */ "foo\n")]
|
||||
[TestCase(/* markdownText: */ "`foo`", /* expected: */ "foo\n")]
|
||||
[TestCase(/* markdownText: */ "`foo\nbar`", /* expected: */ "foo bar\n")] // new line within codespan is treated as whitespace (Example317)
|
||||
[TestCase(/* markdownText: */ "```\nfoo bar\n```", /* expected: */ "foo bar\n")]
|
||||
[TestCase(/* markdownText: */ "- foo\n- bar\n- baz", /* expected: */ "foo\nbar\nbaz\n")]
|
||||
@@ -23,7 +23,7 @@ namespace Markdig.Tests
|
||||
[TestCase(/* markdownText: */ "- foo<baz", /* expected: */ "foo<baz\n")]
|
||||
[TestCase(/* markdownText: */ "## foo `bar::baz >`", /* expected: */ "foo bar::baz >\n")]
|
||||
public void TestPlainEnsureNewLine(string markdownText, string expected)
|
||||
{
|
||||
{
|
||||
var actual = Markdown.ToPlainText(markdownText);
|
||||
Assert.AreEqual(expected, actual);
|
||||
}
|
||||
@@ -31,6 +31,7 @@ namespace Markdig.Tests
|
||||
[Test]
|
||||
[TestCase(/* markdownText: */ ":::\nfoo\n:::", /* expected: */ "foo\n", /*extensions*/ "customcontainers|advanced")]
|
||||
[TestCase(/* markdownText: */ ":::bar\nfoo\n:::", /* expected: */ "foo\n", /*extensions*/ "customcontainers+attributes|advanced")]
|
||||
[TestCase(/* markdownText: */ "| Header1 | Header2 | Header3 |\n|--|--|--|\nt**es**t|value2|value3", /* expected: */ "Header1 Header2 Header3 test value2 value3","pipetables")]
|
||||
public void TestPlainWithExtensions(string markdownText, string expected, string extensions)
|
||||
{
|
||||
TestParser.TestSpec(markdownText, expected, extensions, plainText: true);
|
||||
|
||||
@@ -12,6 +12,12 @@ namespace Markdig.Tests
|
||||
[TestFixture]
|
||||
public class TestPlayParser
|
||||
{
|
||||
[Test]
|
||||
public void TestBugWithEmphasisAndTable()
|
||||
{
|
||||
TestParser.TestSpec("**basics | 8:00**", "<p><strong>basics | 8:00</strong></p>", "advanced");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestLinksWithCarriageReturn()
|
||||
{
|
||||
|
||||
@@ -16,10 +16,12 @@ namespace Markdig.Tests
|
||||
{
|
||||
var pipelineBuilder = new MarkdownPipelineBuilder();
|
||||
pipelineBuilder.EnableTrackTrivia();
|
||||
pipelineBuilder.UseYamlFrontMatter();
|
||||
MarkdownPipeline pipeline = pipelineBuilder.Build();
|
||||
MarkdownDocument markdownDocument = Markdown.Parse(markdown, pipeline);
|
||||
var sw = new StringWriter();
|
||||
var nr = new RoundtripRenderer(sw);
|
||||
pipeline.Setup(nr);
|
||||
|
||||
nr.Write(markdownDocument);
|
||||
|
||||
|
||||
102
src/Markdig.Tests/TestTransformedStringCache.cs
Normal file
102
src/Markdig.Tests/TestTransformedStringCache.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Markdig.Helpers;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Markdig.Tests
|
||||
{
|
||||
public class TestTransformedStringCache
|
||||
{
|
||||
[Test]
|
||||
public void GetRunsTransformationCallback()
|
||||
{
|
||||
var cache = new TransformedStringCache(static s => "callback-" + s);
|
||||
|
||||
Assert.AreEqual("callback-foo", cache.Get("foo"));
|
||||
Assert.AreEqual("callback-bar", cache.Get("bar"));
|
||||
Assert.AreEqual("callback-baz", cache.Get("baz"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CachesTransformedInstance()
|
||||
{
|
||||
var cache = new TransformedStringCache(static s => "callback-" + s);
|
||||
|
||||
string transformedBar = cache.Get("bar");
|
||||
Assert.AreSame(transformedBar, cache.Get("bar"));
|
||||
|
||||
string transformedFoo = cache.Get("foo".AsSpan());
|
||||
Assert.AreSame(transformedFoo, cache.Get("foo"));
|
||||
|
||||
Assert.AreSame(cache.Get("baz"), cache.Get("baz".AsSpan()));
|
||||
|
||||
Assert.AreSame(transformedBar, cache.Get("bar"));
|
||||
Assert.AreSame(transformedFoo, cache.Get("foo"));
|
||||
Assert.AreSame(transformedBar, cache.Get("bar".AsSpan()));
|
||||
Assert.AreSame(transformedFoo, cache.Get("foo".AsSpan()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DoesNotCacheEmptyInputs()
|
||||
{
|
||||
var cache = new TransformedStringCache(static s => new string('a', 4));
|
||||
|
||||
string cached = cache.Get("");
|
||||
string cached2 = cache.Get("");
|
||||
string cached3 = cache.Get(ReadOnlySpan<char>.Empty);
|
||||
|
||||
Assert.AreEqual("aaaa", cached);
|
||||
Assert.AreEqual(cached, cached2);
|
||||
Assert.AreEqual(cached, cached3);
|
||||
|
||||
Assert.AreNotSame(cached, cached2);
|
||||
Assert.AreNotSame(cached, cached3);
|
||||
Assert.AreNotSame(cached2, cached3);
|
||||
}
|
||||
|
||||
[Test]
|
||||
[TestCase(TransformedStringCache.InputLengthLimit, true)]
|
||||
[TestCase(TransformedStringCache.InputLengthLimit + 1, false)]
|
||||
public void DoesNotCacheLongInputs(int length, bool shouldBeCached)
|
||||
{
|
||||
var cache = new TransformedStringCache(static s => "callback-" + s);
|
||||
|
||||
string input = new string('a', length);
|
||||
|
||||
string cached = cache.Get(input);
|
||||
string cached2 = cache.Get(input);
|
||||
|
||||
Assert.AreEqual("callback-" + input, cached);
|
||||
Assert.AreEqual(cached, cached2);
|
||||
|
||||
if (shouldBeCached)
|
||||
{
|
||||
Assert.AreSame(cached, cached2);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.AreNotSame(cached, cached2);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CachesAtMostNEntriesPerCharacter()
|
||||
{
|
||||
var cache = new TransformedStringCache(static s => "callback-" + s);
|
||||
|
||||
int limit = TransformedStringCache.MaxEntriesPerCharacter;
|
||||
|
||||
string[] a = Enumerable.Range(1, limit + 1).Select(i => $"a{i}").ToArray();
|
||||
string[] cachedAs = a.Select(a => cache.Get(a)).ToArray();
|
||||
|
||||
for (int i = 0; i < limit; i++)
|
||||
{
|
||||
Assert.AreSame(cachedAs[i], cache.Get(a[i]));
|
||||
}
|
||||
|
||||
Assert.AreNotSame(cachedAs[limit], cache.Get(a[limit]));
|
||||
|
||||
Assert.AreSame(cache.Get("b1"), cache.Get("b1"));
|
||||
}
|
||||
}
|
||||
}
|
||||
78
src/Markdig.Tests/TestYamlFrontMatterExtension.cs
Normal file
78
src/Markdig.Tests/TestYamlFrontMatterExtension.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Markdig.Extensions.Yaml;
|
||||
using Markdig.Renderers;
|
||||
using Markdig.Syntax;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Markdig.Tests
|
||||
{
|
||||
public class TestYamlFrontMatterExtension
|
||||
{
|
||||
[TestCaseSource(nameof(TestCases))]
|
||||
public void ProperYamlFrontMatterRenderersAdded(IMarkdownObjectRenderer[] objectRenderers, bool hasYamlFrontMatterHtmlRenderer, bool hasYamlFrontMatterRoundtripRenderer)
|
||||
{
|
||||
var builder = new MarkdownPipelineBuilder();
|
||||
builder.Extensions.Add(new YamlFrontMatterExtension());
|
||||
var markdownRenderer = new DummyRenderer();
|
||||
markdownRenderer.ObjectRenderers.AddRange(objectRenderers);
|
||||
builder.Build().Setup(markdownRenderer);
|
||||
Assert.That(markdownRenderer.ObjectRenderers.Contains<YamlFrontMatterHtmlRenderer>(), Is.EqualTo(hasYamlFrontMatterHtmlRenderer));
|
||||
Assert.That(markdownRenderer.ObjectRenderers.Contains<YamlFrontMatterRoundtripRenderer>(), Is.EqualTo(hasYamlFrontMatterRoundtripRenderer));
|
||||
}
|
||||
|
||||
private static IEnumerable<TestCaseData> TestCases()
|
||||
{
|
||||
yield return new TestCaseData(new IMarkdownObjectRenderer[]
|
||||
{
|
||||
}, false, false) {TestName = "No ObjectRenderers"};
|
||||
|
||||
yield return new TestCaseData(new IMarkdownObjectRenderer[]
|
||||
{
|
||||
new Markdig.Renderers.Html.CodeBlockRenderer()
|
||||
}, true, false) {TestName = "Html CodeBlock"};
|
||||
|
||||
yield return new TestCaseData(new IMarkdownObjectRenderer[]
|
||||
{
|
||||
new Markdig.Renderers.Roundtrip.CodeBlockRenderer()
|
||||
}, false, true) {TestName = "Roundtrip CodeBlock"};
|
||||
|
||||
yield return new TestCaseData(new IMarkdownObjectRenderer[]
|
||||
{
|
||||
new Markdig.Renderers.Html.CodeBlockRenderer(),
|
||||
new Markdig.Renderers.Roundtrip.CodeBlockRenderer()
|
||||
}, true, true) {TestName = "Html/Roundtrip CodeBlock"};
|
||||
|
||||
yield return new TestCaseData(new IMarkdownObjectRenderer[]
|
||||
{
|
||||
new Markdig.Renderers.Html.CodeBlockRenderer(),
|
||||
new Markdig.Renderers.Roundtrip.CodeBlockRenderer(),
|
||||
new YamlFrontMatterHtmlRenderer()
|
||||
}, true, true) {TestName = "Html/Roundtrip CodeBlock, Yaml Html"};
|
||||
|
||||
yield return new TestCaseData(new IMarkdownObjectRenderer[]
|
||||
{
|
||||
new Markdig.Renderers.Html.CodeBlockRenderer(),
|
||||
new Markdig.Renderers.Roundtrip.CodeBlockRenderer(),
|
||||
new YamlFrontMatterRoundtripRenderer()
|
||||
}, true, true) { TestName = "Html/Roundtrip CodeBlock, Yaml Roundtrip" };
|
||||
}
|
||||
|
||||
private class DummyRenderer : IMarkdownRenderer
|
||||
{
|
||||
public DummyRenderer()
|
||||
{
|
||||
ObjectRenderers = new ObjectRendererCollection();
|
||||
}
|
||||
|
||||
public event Action<IMarkdownRenderer, MarkdownObject> ObjectWriteBefore;
|
||||
public event Action<IMarkdownRenderer, MarkdownObject> ObjectWriteAfter;
|
||||
public ObjectRendererCollection ObjectRenderers { get; }
|
||||
public object Render(MarkdownObject markdownObject)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -172,17 +172,22 @@ namespace Markdig.Extensions.AutoIdentifiers
|
||||
var baseHeadingId = string.IsNullOrEmpty(headingText) ? "section" : headingText;
|
||||
|
||||
// Add a trailing -1, -2, -3...etc. in case of collision
|
||||
int index = 0;
|
||||
var headingId = baseHeadingId;
|
||||
var headingBuffer = StringBuilderCache.Local();
|
||||
while (!identifiers.Add(headingId))
|
||||
if (!identifiers.Add(headingId))
|
||||
{
|
||||
index++;
|
||||
var headingBuffer = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
headingBuffer.Append(baseHeadingId);
|
||||
headingBuffer.Append('-');
|
||||
headingBuffer.Append(index);
|
||||
headingId = headingBuffer.ToString();
|
||||
headingBuffer.Length = 0;
|
||||
uint index = 0;
|
||||
do
|
||||
{
|
||||
index++;
|
||||
headingBuffer.Append(index);
|
||||
headingId = headingBuffer.AsSpan().ToString();
|
||||
headingBuffer.Length = baseHeadingId.Length + 1;
|
||||
}
|
||||
while (!identifiers.Add(headingId));
|
||||
headingBuffer.Dispose();
|
||||
}
|
||||
|
||||
attributes.Id = headingId;
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using Markdig.Renderers;
|
||||
using Markdig.Renderers.Normalize;
|
||||
using Markdig.Renderers.Normalize.Inlines;
|
||||
using Markdig.Syntax.Inlines;
|
||||
|
||||
namespace Markdig.Extensions.AutoLinks
|
||||
@@ -33,10 +31,6 @@ namespace Markdig.Extensions.AutoLinks
|
||||
|
||||
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
|
||||
{
|
||||
if (renderer is NormalizeRenderer normalizeRenderer && !normalizeRenderer.ObjectRenderers.Contains<NormalizeAutoLinkRenderer>())
|
||||
{
|
||||
normalizeRenderer.ObjectRenderers.InsertBefore<LinkInlineRenderer>(new NormalizeAutoLinkRenderer());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -49,6 +49,53 @@ namespace Markdig.Extensions.AutoLinks
|
||||
return false;
|
||||
}
|
||||
|
||||
var startPosition = slice.Start;
|
||||
int domainOffset = 0;
|
||||
|
||||
var c = slice.CurrentChar;
|
||||
// Precheck URL
|
||||
switch (c)
|
||||
{
|
||||
case 'h':
|
||||
if (slice.MatchLowercase("ttp://", 1))
|
||||
{
|
||||
domainOffset = 7; // http://
|
||||
}
|
||||
else if (slice.MatchLowercase("ttps://", 1))
|
||||
{
|
||||
domainOffset = 8; // https://
|
||||
}
|
||||
else return false;
|
||||
break;
|
||||
case 'f':
|
||||
if (!slice.MatchLowercase("tp://", 1))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
domainOffset = 6; // ftp://
|
||||
break;
|
||||
case 'm':
|
||||
if (!slice.MatchLowercase("ailto:", 1))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 't':
|
||||
if (!slice.MatchLowercase("el:", 1))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
domainOffset = 4;
|
||||
break;
|
||||
case 'w':
|
||||
if (!slice.MatchLowercase("ww.", 1)) // We won't match http:/www. or /www.xxx
|
||||
{
|
||||
return false;
|
||||
}
|
||||
domainOffset = 4; // www.
|
||||
break;
|
||||
}
|
||||
|
||||
List<char> pendingEmphasis = _listOfCharCache.Get();
|
||||
try
|
||||
{
|
||||
@@ -58,53 +105,6 @@ namespace Markdig.Extensions.AutoLinks
|
||||
return false;
|
||||
}
|
||||
|
||||
var startPosition = slice.Start;
|
||||
int domainOffset = 0;
|
||||
|
||||
var c = slice.CurrentChar;
|
||||
// Precheck URL
|
||||
switch (c)
|
||||
{
|
||||
case 'h':
|
||||
if (slice.MatchLowercase("ttp://", 1))
|
||||
{
|
||||
domainOffset = 7; // http://
|
||||
}
|
||||
else if (slice.MatchLowercase("ttps://", 1))
|
||||
{
|
||||
domainOffset = 8; // https://
|
||||
}
|
||||
else return false;
|
||||
break;
|
||||
case 'f':
|
||||
if (!slice.MatchLowercase("tp://", 1))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
domainOffset = 6; // ftp://
|
||||
break;
|
||||
case 'm':
|
||||
if (!slice.MatchLowercase("ailto:", 1))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
case 't':
|
||||
if (!slice.MatchLowercase("el:", 1))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
domainOffset = 4;
|
||||
break;
|
||||
case 'w':
|
||||
if (!slice.MatchLowercase("ww.", 1)) // We won't match http:/www. or /www.xxx
|
||||
{
|
||||
return false;
|
||||
}
|
||||
domainOffset = 4; // www.
|
||||
break;
|
||||
}
|
||||
|
||||
// Parse URL
|
||||
if (!LinkHelper.TryParseUrl(ref slice, out string? link, out _, true))
|
||||
{
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using Markdig.Renderers;
|
||||
using Markdig.Renderers.Normalize;
|
||||
using Markdig.Syntax;
|
||||
using Markdig.Syntax.Inlines;
|
||||
|
||||
namespace Markdig.Extensions.AutoLinks
|
||||
{
|
||||
public class NormalizeAutoLinkRenderer : NormalizeObjectRenderer<LinkInline>
|
||||
{
|
||||
public override bool Accept(RendererBase renderer, MarkdownObject obj)
|
||||
{
|
||||
if (base.Accept(renderer, obj))
|
||||
{
|
||||
return renderer is NormalizeRenderer normalizeRenderer
|
||||
&& obj is LinkInline link
|
||||
&& !normalizeRenderer.Options.ExpandAutoLinks
|
||||
&& link.IsAutoLink;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
protected override void Write(NormalizeRenderer renderer, LinkInline obj)
|
||||
{
|
||||
renderer.Write(obj.Url);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,9 +28,16 @@ namespace Markdig.Extensions.Bootstrap
|
||||
|
||||
private static void PipelineOnDocumentProcessed(MarkdownDocument document)
|
||||
{
|
||||
foreach(var node in document.Descendants())
|
||||
foreach (var node in document.Descendants())
|
||||
{
|
||||
if (node is Block)
|
||||
if (node.IsInline)
|
||||
{
|
||||
if (node.IsContainerInline && node is LinkInline link && link.IsImage)
|
||||
{
|
||||
link.GetAttributes().AddClass("img-fluid");
|
||||
}
|
||||
}
|
||||
else if (node.IsContainerBlock)
|
||||
{
|
||||
if (node is Tables.Table)
|
||||
{
|
||||
@@ -44,18 +51,14 @@ namespace Markdig.Extensions.Bootstrap
|
||||
{
|
||||
node.GetAttributes().AddClass("figure");
|
||||
}
|
||||
else if (node is Figures.FigureCaption)
|
||||
}
|
||||
else
|
||||
{
|
||||
if (node is Figures.FigureCaption)
|
||||
{
|
||||
node.GetAttributes().AddClass("figure-caption");
|
||||
}
|
||||
}
|
||||
else if (node is Inline)
|
||||
{
|
||||
if (node is LinkInline link && link.IsImage)
|
||||
{
|
||||
link.GetAttributes().AddClass("img-fluid");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,20 +93,23 @@ namespace Markdig.Extensions.Globalization
|
||||
{
|
||||
for (int i = slice.Start; i <= slice.End; i++)
|
||||
{
|
||||
if (slice[i] < 128)
|
||||
char c = slice[i];
|
||||
if (c < 128)
|
||||
{
|
||||
if (CharHelper.IsAlpha(c))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
int rune;
|
||||
if (CharHelper.IsHighSurrogate(slice[i]) && i < slice.End && CharHelper.IsLowSurrogate(slice[i + 1]))
|
||||
int rune = c;
|
||||
if (CharHelper.IsHighSurrogate(c) && i < slice.End && CharHelper.IsLowSurrogate(slice[i + 1]))
|
||||
{
|
||||
Debug.Assert(char.IsSurrogatePair(slice[i], slice[i + 1]));
|
||||
rune = char.ConvertToUtf32(slice[i], slice[i + 1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
rune = slice[i];
|
||||
Debug.Assert(char.IsSurrogatePair(c, slice[i + 1]));
|
||||
rune = char.ConvertToUtf32(c, slice[i + 1]);
|
||||
i++;
|
||||
}
|
||||
|
||||
if (CharHelper.IsRightToLeft(rune))
|
||||
|
||||
@@ -95,13 +95,19 @@ namespace Markdig.Extensions.JiraLinks
|
||||
jiraLink.Span.End = jiraLink.Span.Start + (endIssue - startKey);
|
||||
|
||||
// Builds the Url
|
||||
var builder = StringBuilderCache.Local();
|
||||
builder.Append(_baseUrl).Append('/').Append(jiraLink.ProjectKey).Append('-').Append(jiraLink.Issue);
|
||||
jiraLink.Url = builder.ToString();
|
||||
var builder = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
builder.Append(_baseUrl);
|
||||
builder.Append('/');
|
||||
builder.Append(jiraLink.ProjectKey.AsSpan());
|
||||
builder.Append('-');
|
||||
builder.Append(jiraLink.Issue.AsSpan());
|
||||
jiraLink.Url = builder.AsSpan().ToString();
|
||||
|
||||
// Builds the Label
|
||||
builder.Length = 0;
|
||||
builder.Append(jiraLink.ProjectKey).Append('-').Append(jiraLink.Issue);
|
||||
builder.Append(jiraLink.ProjectKey.AsSpan());
|
||||
builder.Append('-');
|
||||
builder.Append(jiraLink.Issue.AsSpan());
|
||||
jiraLink.AppendChild(new LiteralInline(builder.ToString()));
|
||||
|
||||
if (_options.OpenInNewWindow)
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using System.Text;
|
||||
using Markdig.Helpers;
|
||||
using System;
|
||||
|
||||
namespace Markdig.Extensions.JiraLinks
|
||||
{
|
||||
@@ -38,20 +39,10 @@ namespace Markdig.Extensions.JiraLinks
|
||||
/// </summary>
|
||||
public virtual string GetUrl()
|
||||
{
|
||||
var url = new StringBuilder();
|
||||
var baseUrl = BaseUrl;
|
||||
if (baseUrl != null)
|
||||
{
|
||||
url.Append(baseUrl.TrimEnd('/'));
|
||||
}
|
||||
|
||||
url.Append("/");
|
||||
|
||||
if (BasePath != null)
|
||||
{
|
||||
url.Append(BasePath.Trim('/'));
|
||||
}
|
||||
|
||||
var url = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
url.Append(BaseUrl.AsSpan().TrimEnd('/'));
|
||||
url.Append('/');
|
||||
url.Append(BasePath.AsSpan().Trim('/'));
|
||||
return url.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,122 +17,154 @@ namespace Markdig.Extensions.Tables
|
||||
{
|
||||
protected override void Write(HtmlRenderer renderer, Table table)
|
||||
{
|
||||
renderer.EnsureLine();
|
||||
renderer.Write("<table").WriteAttributes(table).WriteLine('>');
|
||||
|
||||
bool hasBody = false;
|
||||
bool hasAlreadyHeader = false;
|
||||
bool isHeaderOpen = false;
|
||||
|
||||
|
||||
bool hasColumnWidth = false;
|
||||
foreach (var tableColumnDefinition in table.ColumnDefinitions)
|
||||
if (renderer.EnableHtmlForBlock)
|
||||
{
|
||||
if (tableColumnDefinition.Width != 0.0f && tableColumnDefinition.Width != 1.0f)
|
||||
{
|
||||
hasColumnWidth = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
renderer.EnsureLine();
|
||||
renderer.Write("<table").WriteAttributes(table).WriteLine('>');
|
||||
|
||||
if (hasColumnWidth)
|
||||
{
|
||||
bool hasBody = false;
|
||||
bool hasAlreadyHeader = false;
|
||||
bool isHeaderOpen = false;
|
||||
|
||||
|
||||
bool hasColumnWidth = false;
|
||||
foreach (var tableColumnDefinition in table.ColumnDefinitions)
|
||||
{
|
||||
var width = Math.Round(tableColumnDefinition.Width*100)/100;
|
||||
var widthValue = string.Format(CultureInfo.InvariantCulture, "{0:0.##}", width);
|
||||
renderer.WriteLine($"<col style=\"width:{widthValue}%\" />");
|
||||
if (tableColumnDefinition.Width != 0.0f && tableColumnDefinition.Width != 1.0f)
|
||||
{
|
||||
hasColumnWidth = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var rowObj in table)
|
||||
{
|
||||
var row = (TableRow)rowObj;
|
||||
if (row.IsHeader)
|
||||
if (hasColumnWidth)
|
||||
{
|
||||
// Allow a single thead
|
||||
if (!hasAlreadyHeader)
|
||||
foreach (var tableColumnDefinition in table.ColumnDefinitions)
|
||||
{
|
||||
renderer.WriteLine("<thead>");
|
||||
isHeaderOpen = true;
|
||||
var width = Math.Round(tableColumnDefinition.Width * 100) / 100;
|
||||
var widthValue = string.Format(CultureInfo.InvariantCulture, "{0:0.##}", width);
|
||||
renderer.WriteLine($"<col style=\"width:{widthValue}%\" />");
|
||||
}
|
||||
hasAlreadyHeader = true;
|
||||
}
|
||||
else if (!hasBody)
|
||||
{
|
||||
if (isHeaderOpen)
|
||||
{
|
||||
renderer.WriteLine("</thead>");
|
||||
isHeaderOpen = false;
|
||||
}
|
||||
|
||||
renderer.WriteLine("<tbody>");
|
||||
hasBody = true;
|
||||
}
|
||||
renderer.Write("<tr").WriteAttributes(row).WriteLine('>');
|
||||
for (int i = 0; i < row.Count; i++)
|
||||
foreach (var rowObj in table)
|
||||
{
|
||||
var cellObj = row[i];
|
||||
var cell = (TableCell)cellObj;
|
||||
|
||||
renderer.EnsureLine();
|
||||
renderer.Write(row.IsHeader ? "<th" : "<td");
|
||||
if (cell.ColumnSpan != 1)
|
||||
var row = (TableRow)rowObj;
|
||||
if (row.IsHeader)
|
||||
{
|
||||
renderer.Write($" colspan=\"{cell.ColumnSpan}\"");
|
||||
}
|
||||
if (cell.RowSpan != 1)
|
||||
{
|
||||
renderer.Write($" rowspan=\"{cell.RowSpan}\"");
|
||||
}
|
||||
if (table.ColumnDefinitions.Count > 0)
|
||||
{
|
||||
var columnIndex = cell.ColumnIndex < 0 || cell.ColumnIndex >= table.ColumnDefinitions.Count
|
||||
? i
|
||||
: cell.ColumnIndex;
|
||||
columnIndex = columnIndex >= table.ColumnDefinitions.Count ? table.ColumnDefinitions.Count - 1 : columnIndex;
|
||||
var alignment = table.ColumnDefinitions[columnIndex].Alignment;
|
||||
if (alignment.HasValue)
|
||||
// Allow a single thead
|
||||
if (!hasAlreadyHeader)
|
||||
{
|
||||
switch (alignment)
|
||||
renderer.WriteLine("<thead>");
|
||||
isHeaderOpen = true;
|
||||
}
|
||||
|
||||
hasAlreadyHeader = true;
|
||||
}
|
||||
else if (!hasBody)
|
||||
{
|
||||
if (isHeaderOpen)
|
||||
{
|
||||
renderer.WriteLine("</thead>");
|
||||
isHeaderOpen = false;
|
||||
}
|
||||
|
||||
renderer.WriteLine("<tbody>");
|
||||
hasBody = true;
|
||||
}
|
||||
|
||||
renderer.Write("<tr").WriteAttributes(row).WriteLine('>');
|
||||
for (int i = 0; i < row.Count; i++)
|
||||
{
|
||||
var cellObj = row[i];
|
||||
var cell = (TableCell)cellObj;
|
||||
|
||||
renderer.EnsureLine();
|
||||
renderer.Write(row.IsHeader ? "<th" : "<td");
|
||||
if (cell.ColumnSpan != 1)
|
||||
{
|
||||
renderer.Write($" colspan=\"{cell.ColumnSpan}\"");
|
||||
}
|
||||
|
||||
if (cell.RowSpan != 1)
|
||||
{
|
||||
renderer.Write($" rowspan=\"{cell.RowSpan}\"");
|
||||
}
|
||||
|
||||
if (table.ColumnDefinitions.Count > 0)
|
||||
{
|
||||
var columnIndex = cell.ColumnIndex < 0 || cell.ColumnIndex >= table.ColumnDefinitions.Count
|
||||
? i
|
||||
: cell.ColumnIndex;
|
||||
columnIndex = columnIndex >= table.ColumnDefinitions.Count ? table.ColumnDefinitions.Count - 1 : columnIndex;
|
||||
var alignment = table.ColumnDefinitions[columnIndex].Alignment;
|
||||
if (alignment.HasValue)
|
||||
{
|
||||
case TableColumnAlign.Center:
|
||||
renderer.Write(" style=\"text-align: center;\"");
|
||||
break;
|
||||
case TableColumnAlign.Right:
|
||||
renderer.Write(" style=\"text-align: right;\"");
|
||||
break;
|
||||
case TableColumnAlign.Left:
|
||||
renderer.Write(" style=\"text-align: left;\"");
|
||||
break;
|
||||
switch (alignment)
|
||||
{
|
||||
case TableColumnAlign.Center:
|
||||
renderer.Write(" style=\"text-align: center;\"");
|
||||
break;
|
||||
case TableColumnAlign.Right:
|
||||
renderer.Write(" style=\"text-align: right;\"");
|
||||
break;
|
||||
case TableColumnAlign.Left:
|
||||
renderer.Write(" style=\"text-align: left;\"");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
renderer.WriteAttributes(cell);
|
||||
renderer.Write('>');
|
||||
|
||||
var previousImplicitParagraph = renderer.ImplicitParagraph;
|
||||
if (cell.Count == 1)
|
||||
{
|
||||
renderer.ImplicitParagraph = true;
|
||||
}
|
||||
renderer.Write(cell);
|
||||
renderer.ImplicitParagraph = previousImplicitParagraph;
|
||||
renderer.WriteAttributes(cell);
|
||||
renderer.Write('>');
|
||||
|
||||
renderer.WriteLine(row.IsHeader ? "</th>" : "</td>");
|
||||
var previousImplicitParagraph = renderer.ImplicitParagraph;
|
||||
if (cell.Count == 1)
|
||||
{
|
||||
renderer.ImplicitParagraph = true;
|
||||
}
|
||||
|
||||
renderer.Write(cell);
|
||||
renderer.ImplicitParagraph = previousImplicitParagraph;
|
||||
|
||||
renderer.WriteLine(row.IsHeader ? "</th>" : "</td>");
|
||||
}
|
||||
|
||||
renderer.WriteLine("</tr>");
|
||||
}
|
||||
renderer.WriteLine("</tr>");
|
||||
}
|
||||
|
||||
if (hasBody)
|
||||
{
|
||||
renderer.WriteLine("</tbody>");
|
||||
if (hasBody)
|
||||
{
|
||||
renderer.WriteLine("</tbody>");
|
||||
}
|
||||
else if (isHeaderOpen)
|
||||
{
|
||||
renderer.WriteLine("</thead>");
|
||||
}
|
||||
|
||||
renderer.WriteLine("</table>");
|
||||
}
|
||||
else if (isHeaderOpen)
|
||||
else
|
||||
{
|
||||
renderer.WriteLine("</thead>");
|
||||
//no html, just write the table contents
|
||||
var impliciParagraph = renderer.ImplicitParagraph;
|
||||
|
||||
//enable implicit paragraphs to avoid newlines after each cell
|
||||
renderer.ImplicitParagraph = true;
|
||||
foreach (var rowObj in table)
|
||||
{
|
||||
var row = (TableRow)rowObj;
|
||||
for (int i = 0; i < row.Count; i++)
|
||||
{
|
||||
var cellObj = row[i];
|
||||
var cell = (TableCell)cellObj;
|
||||
renderer.Write(cell);
|
||||
//write a space after each cell to avoid text being merged with the next cell
|
||||
renderer.Write(' ');
|
||||
}
|
||||
}
|
||||
renderer.ImplicitParagraph = impliciParagraph;
|
||||
}
|
||||
renderer.WriteLine("</table>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace Markdig.Extensions.Tables
|
||||
public override bool Match(InlineProcessor processor, ref StringSlice slice)
|
||||
{
|
||||
// Only working on Paragraph block
|
||||
if (!(processor.Block is ParagraphBlock))
|
||||
if (!processor.Block!.IsParagraphBlock)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using Markdig.Parsers;
|
||||
using Markdig.Renderers;
|
||||
using Markdig.Renderers.Html;
|
||||
|
||||
namespace Markdig.Extensions.Yaml
|
||||
{
|
||||
@@ -24,9 +23,14 @@ namespace Markdig.Extensions.Yaml
|
||||
|
||||
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
|
||||
{
|
||||
if (!renderer.ObjectRenderers.Contains<YamlFrontMatterRenderer>())
|
||||
if (!renderer.ObjectRenderers.Contains<YamlFrontMatterHtmlRenderer>())
|
||||
{
|
||||
renderer.ObjectRenderers.InsertBefore<CodeBlockRenderer>(new YamlFrontMatterRenderer());
|
||||
renderer.ObjectRenderers.InsertBefore<Renderers.Html.CodeBlockRenderer>(new YamlFrontMatterHtmlRenderer());
|
||||
}
|
||||
|
||||
if (!renderer.ObjectRenderers.Contains<YamlFrontMatterRoundtripRenderer>())
|
||||
{
|
||||
renderer.ObjectRenderers.InsertBefore<Renderers.Roundtrip.CodeBlockRenderer>(new YamlFrontMatterRoundtripRenderer());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Markdig.Extensions.Yaml
|
||||
/// Empty renderer for a <see cref="YamlFrontMatterBlock"/>
|
||||
/// </summary>
|
||||
/// <seealso cref="HtmlObjectRenderer{YamlFrontMatterBlock}" />
|
||||
public class YamlFrontMatterRenderer : HtmlObjectRenderer<YamlFrontMatterBlock>
|
||||
public class YamlFrontMatterHtmlRenderer : HtmlObjectRenderer<YamlFrontMatterBlock>
|
||||
{
|
||||
protected override void Write(HtmlRenderer renderer, YamlFrontMatterBlock obj)
|
||||
{
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using Markdig.Renderers;
|
||||
using Markdig.Renderers.Roundtrip;
|
||||
|
||||
namespace Markdig.Extensions.Yaml
|
||||
{
|
||||
public class YamlFrontMatterRoundtripRenderer : MarkdownObjectRenderer<RoundtripRenderer, YamlFrontMatterBlock>
|
||||
{
|
||||
private readonly CodeBlockRenderer _codeBlockRenderer;
|
||||
|
||||
public YamlFrontMatterRoundtripRenderer()
|
||||
{
|
||||
_codeBlockRenderer = new CodeBlockRenderer();
|
||||
}
|
||||
|
||||
protected override void Write(RoundtripRenderer renderer, YamlFrontMatterBlock obj)
|
||||
{
|
||||
renderer.Writer.WriteLine("---");
|
||||
_codeBlockRenderer.Write(renderer, obj);
|
||||
renderer.Writer.WriteLine("---");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
namespace Markdig.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
30
src/Markdig/Helpers/BlockWrapper.cs
Normal file
30
src/Markdig/Helpers/BlockWrapper.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using Markdig.Syntax;
|
||||
using System;
|
||||
|
||||
namespace Markdig.Helpers
|
||||
{
|
||||
// Used to avoid the overhead of type covariance checks
|
||||
internal readonly struct BlockWrapper : IEquatable<BlockWrapper>
|
||||
{
|
||||
public readonly Block Block;
|
||||
|
||||
public BlockWrapper(Block block)
|
||||
{
|
||||
Block = block;
|
||||
}
|
||||
|
||||
public static implicit operator Block(BlockWrapper wrapper) => wrapper.Block;
|
||||
|
||||
public static implicit operator BlockWrapper(Block block) => new BlockWrapper(block);
|
||||
|
||||
public bool Equals(BlockWrapper other) => ReferenceEquals(Block, other.Block);
|
||||
|
||||
public override bool Equals(object obj) => Block.Equals(obj);
|
||||
|
||||
public override int GetHashCode() => Block.GetHashCode();
|
||||
}
|
||||
}
|
||||
@@ -744,7 +744,7 @@ namespace Markdig.Helpers
|
||||
return cache[number];
|
||||
}
|
||||
|
||||
return number.ToString();
|
||||
return number.ToString(CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,11 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
#if NETCOREAPP3_1_OR_GREATER
|
||||
using System.Numerics;
|
||||
using System.Runtime.Intrinsics;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
#endif
|
||||
|
||||
namespace Markdig.Helpers
|
||||
{
|
||||
@@ -16,6 +21,10 @@ namespace Markdig.Helpers
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public sealed class CharacterMap<T> where T : class
|
||||
{
|
||||
#if NETCOREAPP3_1_OR_GREATER
|
||||
private readonly Vector128<byte> _asciiBitmap;
|
||||
#endif
|
||||
|
||||
private readonly T[] asciiMap;
|
||||
private readonly Dictionary<uint, T>? nonAsciiMap;
|
||||
private readonly BoolVector128 isOpeningCharacter;
|
||||
@@ -39,6 +48,11 @@ namespace Markdig.Helpers
|
||||
if (openingChar < 128)
|
||||
{
|
||||
maxChar = Math.Max(maxChar, openingChar);
|
||||
|
||||
if (openingChar == 0)
|
||||
{
|
||||
ThrowHelper.ArgumentOutOfRangeException("Null is not a valid opening character.", nameof(maps));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -64,6 +78,23 @@ namespace Markdig.Helpers
|
||||
nonAsciiMap[openingChar] = state.Value;
|
||||
}
|
||||
}
|
||||
|
||||
#if NETCOREAPP3_1_OR_GREATER
|
||||
if (nonAsciiMap is null)
|
||||
{
|
||||
long bitmap_0_3 = 0;
|
||||
long bitmap_4_7 = 0;
|
||||
|
||||
foreach (char openingChar in OpeningCharacters)
|
||||
{
|
||||
int position = (openingChar >> 4) | ((openingChar & 0x0F) << 3);
|
||||
if (position < 64) bitmap_0_3 |= 1L << position;
|
||||
else bitmap_4_7 |= 1L << (position - 64);
|
||||
}
|
||||
|
||||
_asciiBitmap = Vector128.Create(bitmap_0_3, bitmap_4_7).AsByte();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -105,9 +136,63 @@ namespace Markdig.Helpers
|
||||
/// <returns>Index position within the string of the first opening character found in the specified text; if not found, returns -1</returns>
|
||||
public int IndexOfOpeningCharacter(string text, int start, int end)
|
||||
{
|
||||
Debug.Assert(text is not null);
|
||||
Debug.Assert(start >= 0 && end >= 0);
|
||||
Debug.Assert(end - start + 1 >= 0);
|
||||
Debug.Assert(end - start + 1 <= text.Length);
|
||||
|
||||
if (nonAsciiMap is null)
|
||||
{
|
||||
#if NETCOREAPP3_1_OR_GREATER
|
||||
if (Ssse3.IsSupported && BitConverter.IsLittleEndian)
|
||||
{
|
||||
// Based on http://0x80.pl/articles/simd-byte-lookup.html#universal-algorithm
|
||||
// Optimized for sets in the [1, 127] range
|
||||
|
||||
int lengthMinusOne = end - start;
|
||||
int charsToProcessVectorized = lengthMinusOne & ~(2 * Vector128<short>.Count - 1);
|
||||
int finalStart = start + charsToProcessVectorized;
|
||||
|
||||
if (start < finalStart)
|
||||
{
|
||||
ref char textStartRef = ref Unsafe.Add(ref Unsafe.AsRef(in text.GetPinnableReference()), start);
|
||||
Vector128<byte> bitmap = _asciiBitmap;
|
||||
do
|
||||
{
|
||||
// Load 32 bytes (16 chars) into two Vector128<short>s (chars)
|
||||
// Drop the high byte of each char
|
||||
// Pack the remaining bytes into a single Vector128<byte>
|
||||
Vector128<byte> input = Sse2.PackUnsignedSaturate(
|
||||
Unsafe.ReadUnaligned<Vector128<short>>(ref Unsafe.As<char, byte>(ref textStartRef)),
|
||||
Unsafe.ReadUnaligned<Vector128<short>>(ref Unsafe.As<char, byte>(ref Unsafe.Add(ref textStartRef, Vector128<short>.Count))));
|
||||
|
||||
// Extract the higher nibble of each character ((input >> 4) & 0xF)
|
||||
Vector128<byte> higherNibbles = Sse2.And(Sse2.ShiftRightLogical(input.AsUInt16(), 4).AsByte(), Vector128.Create((byte)0xF));
|
||||
|
||||
// Lookup the matching higher nibble for each character based on the lower nibble
|
||||
// PSHUFB will set the result to 0 for any non-ASCII (> 127) character
|
||||
Vector128<byte> bitsets = Ssse3.Shuffle(bitmap, input);
|
||||
|
||||
// Calculate a bitmask (1 << (higherNibble % 8)) for each character
|
||||
Vector128<byte> bitmask = Ssse3.Shuffle(Vector128.Create(0x8040201008040201).AsByte(), higherNibbles);
|
||||
|
||||
// Check which characters are present in the set
|
||||
// We are relying on bitsets being zero for non-ASCII characters
|
||||
Vector128<byte> result = Sse2.And(bitsets, bitmask);
|
||||
|
||||
if (!result.Equals(Vector128<byte>.Zero))
|
||||
{
|
||||
int resultMask = ~Sse2.MoveMask(Sse2.CompareEqual(result, Vector128<byte>.Zero));
|
||||
return start + BitOperations.TrailingZeroCount((uint)resultMask);
|
||||
}
|
||||
|
||||
start += 2 * Vector128<short>.Count;
|
||||
textStartRef = ref Unsafe.Add(ref textStartRef, 2 * Vector128<short>.Count);
|
||||
}
|
||||
while (start != finalStart);
|
||||
}
|
||||
}
|
||||
|
||||
ref char textRef = ref Unsafe.AsRef(in text.GetPinnableReference());
|
||||
for (; start <= end; start++)
|
||||
{
|
||||
|
||||
@@ -33,7 +33,6 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Markdig.Helpers
|
||||
{
|
||||
@@ -81,7 +80,7 @@ namespace Markdig.Helpers
|
||||
});
|
||||
}
|
||||
|
||||
public static void DecodeEntity(int utf32, StringBuilder sb)
|
||||
internal static void DecodeEntity(int utf32, ref ValueStringBuilder sb)
|
||||
{
|
||||
if (!CharHelper.IsInInclusiveRange(utf32, 1, 1114111) || CharHelper.IsInInclusiveRange(utf32, 55296, 57343))
|
||||
{
|
||||
@@ -99,7 +98,7 @@ namespace Markdig.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
#region [ EntityMap ]
|
||||
#region [ EntityMap ]
|
||||
/// <summary>
|
||||
/// Source: http://www.w3.org/html/wg/drafts/html/master/syntax.html#named-character-references
|
||||
/// </summary>
|
||||
|
||||
293
src/Markdig/Helpers/FastStringWriter.cs
Normal file
293
src/Markdig/Helpers/FastStringWriter.cs
Normal file
@@ -0,0 +1,293 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Markdig.Helpers
|
||||
{
|
||||
internal sealed class FastStringWriter : TextWriter
|
||||
{
|
||||
#if NET452
|
||||
private static Task CompletedTask => Task.FromResult(0);
|
||||
#else
|
||||
private static Task CompletedTask => Task.CompletedTask;
|
||||
#endif
|
||||
|
||||
public override Encoding Encoding => Encoding.Unicode;
|
||||
|
||||
private char[] _chars;
|
||||
private int _pos;
|
||||
private string _newLine;
|
||||
|
||||
public FastStringWriter()
|
||||
{
|
||||
_chars = new char[1024];
|
||||
_newLine = "\n";
|
||||
}
|
||||
|
||||
[AllowNull]
|
||||
public override string NewLine
|
||||
{
|
||||
get => _newLine;
|
||||
set => _newLine = value ?? Environment.NewLine;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void Write(char value)
|
||||
{
|
||||
char[] chars = _chars;
|
||||
int pos = _pos;
|
||||
if ((uint)pos < (uint)chars.Length)
|
||||
{
|
||||
chars[pos] = value;
|
||||
_pos = pos + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
GrowAndAppend(value);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void WriteLine(char value)
|
||||
{
|
||||
Write(value);
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
public override Task WriteAsync(char value)
|
||||
{
|
||||
Write(value);
|
||||
return CompletedTask;
|
||||
}
|
||||
|
||||
public override Task WriteLineAsync(char value)
|
||||
{
|
||||
WriteLine(value);
|
||||
return CompletedTask;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void Write(string? value)
|
||||
{
|
||||
if (value is not null)
|
||||
{
|
||||
if (_pos > _chars.Length - value.Length)
|
||||
{
|
||||
Grow(value.Length);
|
||||
}
|
||||
|
||||
value.AsSpan().CopyTo(_chars.AsSpan(_pos));
|
||||
_pos += value.Length;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void WriteLine(string? value)
|
||||
{
|
||||
Write(value);
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
public override Task WriteAsync(string? value)
|
||||
{
|
||||
Write(value);
|
||||
return CompletedTask;
|
||||
}
|
||||
|
||||
public override Task WriteLineAsync(string? value)
|
||||
{
|
||||
WriteLine(value);
|
||||
return CompletedTask;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void Write(char[]? buffer)
|
||||
{
|
||||
if (buffer is not null)
|
||||
{
|
||||
if (_pos > _chars.Length - buffer.Length)
|
||||
{
|
||||
Grow(buffer.Length);
|
||||
}
|
||||
|
||||
buffer.CopyTo(_chars.AsSpan(_pos));
|
||||
_pos += buffer.Length;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void WriteLine(char[]? buffer)
|
||||
{
|
||||
Write(buffer);
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void Write(char[] buffer, int index, int count)
|
||||
{
|
||||
if (buffer is not null)
|
||||
{
|
||||
if (_pos > _chars.Length - count)
|
||||
{
|
||||
Grow(buffer.Length);
|
||||
}
|
||||
|
||||
buffer.AsSpan(index, count).CopyTo(_chars.AsSpan(_pos));
|
||||
_pos += count;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void WriteLine(char[] buffer, int index, int count)
|
||||
{
|
||||
Write(buffer, index, count);
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
public override Task WriteAsync(char[] buffer, int index, int count)
|
||||
{
|
||||
Write(buffer, index, count);
|
||||
return CompletedTask;
|
||||
}
|
||||
|
||||
public override Task WriteLineAsync(char[] buffer, int index, int count)
|
||||
{
|
||||
WriteLine(buffer, index, count);
|
||||
return CompletedTask;
|
||||
}
|
||||
|
||||
#if !(NET452 || NETSTANDARD2_0)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void Write(ReadOnlySpan<char> value)
|
||||
{
|
||||
if (_pos > _chars.Length - value.Length)
|
||||
{
|
||||
Grow(value.Length);
|
||||
}
|
||||
|
||||
value.CopyTo(_chars.AsSpan(_pos));
|
||||
_pos += value.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void WriteLine(ReadOnlySpan<char> buffer)
|
||||
{
|
||||
Write(buffer);
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
public override Task WriteAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Write(buffer.Span);
|
||||
return CompletedTask;
|
||||
}
|
||||
|
||||
public override Task WriteLineAsync(ReadOnlyMemory<char> buffer, CancellationToken cancellationToken = default)
|
||||
{
|
||||
WriteLine(buffer.Span);
|
||||
return CompletedTask;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !(NET452 || NETSTANDARD2_0 || NETSTANDARD2_1)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void Write(StringBuilder? value)
|
||||
{
|
||||
if (value is not null)
|
||||
{
|
||||
int length = value.Length;
|
||||
if (_pos > _chars.Length - length)
|
||||
{
|
||||
Grow(length);
|
||||
}
|
||||
|
||||
value.CopyTo(0, _chars.AsSpan(_pos), length);
|
||||
_pos += length;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void WriteLine(StringBuilder? value)
|
||||
{
|
||||
Write(value);
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
public override Task WriteAsync(StringBuilder? value, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Write(value);
|
||||
return CompletedTask;
|
||||
}
|
||||
|
||||
public override Task WriteLineAsync(StringBuilder? value, CancellationToken cancellationToken = default)
|
||||
{
|
||||
WriteLine(value);
|
||||
return CompletedTask;
|
||||
}
|
||||
#endif
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void WriteLine()
|
||||
{
|
||||
foreach (char c in _newLine)
|
||||
{
|
||||
Write(c);
|
||||
}
|
||||
}
|
||||
|
||||
public override Task WriteLineAsync()
|
||||
{
|
||||
WriteLine();
|
||||
return CompletedTask;
|
||||
}
|
||||
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private void GrowAndAppend(char value)
|
||||
{
|
||||
Grow(1);
|
||||
Write(value);
|
||||
}
|
||||
|
||||
private void Grow(int additionalCapacityBeyondPos)
|
||||
{
|
||||
Debug.Assert(additionalCapacityBeyondPos > 0);
|
||||
Debug.Assert(_pos > _chars.Length - additionalCapacityBeyondPos, "No resize is needed.");
|
||||
|
||||
char[] newArray = new char[(int)Math.Max((uint)(_pos + additionalCapacityBeyondPos), (uint)_chars.Length * 2)];
|
||||
_chars.AsSpan(0, _pos).CopyTo(newArray);
|
||||
_chars = newArray;
|
||||
}
|
||||
|
||||
|
||||
public override void Flush() { }
|
||||
|
||||
public override void Close() { }
|
||||
|
||||
public override Task FlushAsync() => CompletedTask;
|
||||
|
||||
#if !(NET452 || NETSTANDARD2_0)
|
||||
public override ValueTask DisposeAsync() => default;
|
||||
#endif
|
||||
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_pos = 0;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return _chars.AsSpan(0, _pos).ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text;
|
||||
|
||||
namespace Markdig.Helpers
|
||||
{
|
||||
@@ -39,22 +38,22 @@ namespace Markdig.Helpers
|
||||
|
||||
public static bool TryParseHtmlTag(ref StringSlice text, [NotNullWhen(true)] out string? htmlTag)
|
||||
{
|
||||
var builder = StringBuilderCache.Local();
|
||||
if (TryParseHtmlTag(ref text, builder))
|
||||
var builder = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
if (TryParseHtmlTag(ref text, ref builder))
|
||||
{
|
||||
htmlTag = builder.GetStringAndReset();
|
||||
htmlTag = builder.ToString();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Dispose();
|
||||
htmlTag = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryParseHtmlTag(ref StringSlice text, StringBuilder builder)
|
||||
private static bool TryParseHtmlTag(ref StringSlice text, ref ValueStringBuilder builder)
|
||||
{
|
||||
if (builder is null) ThrowHelper.ArgumentNullException(nameof(builder));
|
||||
var c = text.CurrentChar;
|
||||
if (c != '<')
|
||||
{
|
||||
@@ -67,29 +66,29 @@ namespace Markdig.Helpers
|
||||
switch (c)
|
||||
{
|
||||
case '/':
|
||||
return TryParseHtmlCloseTag(ref text, builder);
|
||||
return TryParseHtmlCloseTag(ref text, ref builder);
|
||||
case '?':
|
||||
return TryParseHtmlTagProcessingInstruction(ref text, builder);
|
||||
return TryParseHtmlTagProcessingInstruction(ref text, ref builder);
|
||||
case '!':
|
||||
builder.Append(c);
|
||||
c = text.NextChar();
|
||||
if (c == '-')
|
||||
{
|
||||
return TryParseHtmlTagHtmlComment(ref text, builder);
|
||||
return TryParseHtmlTagHtmlComment(ref text, ref builder);
|
||||
}
|
||||
|
||||
if (c == '[')
|
||||
{
|
||||
return TryParseHtmlTagCData(ref text, builder);
|
||||
return TryParseHtmlTagCData(ref text, ref builder);
|
||||
}
|
||||
|
||||
return TryParseHtmlTagDeclaration(ref text, builder);
|
||||
return TryParseHtmlTagDeclaration(ref text, ref builder);
|
||||
}
|
||||
|
||||
return TryParseHtmlTagOpenTag(ref text, builder);
|
||||
return TryParseHtmlTagOpenTag(ref text, ref builder);
|
||||
}
|
||||
|
||||
internal static bool TryParseHtmlTagOpenTag(ref StringSlice text, StringBuilder builder)
|
||||
internal static bool TryParseHtmlTagOpenTag(ref StringSlice text, ref ValueStringBuilder builder)
|
||||
{
|
||||
var c = text.CurrentChar;
|
||||
|
||||
@@ -244,7 +243,7 @@ namespace Markdig.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryParseHtmlTagDeclaration(ref StringSlice text, StringBuilder builder)
|
||||
private static bool TryParseHtmlTagDeclaration(ref StringSlice text, ref ValueStringBuilder builder)
|
||||
{
|
||||
var c = text.CurrentChar;
|
||||
bool hasAlpha = false;
|
||||
@@ -279,7 +278,7 @@ namespace Markdig.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryParseHtmlTagCData(ref StringSlice text, StringBuilder builder)
|
||||
private static bool TryParseHtmlTagCData(ref StringSlice text, ref ValueStringBuilder builder)
|
||||
{
|
||||
if (text.Match("[CDATA["))
|
||||
{
|
||||
@@ -310,7 +309,7 @@ namespace Markdig.Helpers
|
||||
return false;
|
||||
}
|
||||
|
||||
internal static bool TryParseHtmlCloseTag(ref StringSlice text, StringBuilder builder)
|
||||
internal static bool TryParseHtmlCloseTag(ref StringSlice text, ref ValueStringBuilder builder)
|
||||
{
|
||||
// </[A-Za-z][A-Za-z0-9]+\s*>
|
||||
builder.Append('/');
|
||||
@@ -355,7 +354,7 @@ namespace Markdig.Helpers
|
||||
}
|
||||
|
||||
|
||||
private static bool TryParseHtmlTagHtmlComment(ref StringSlice text, StringBuilder builder)
|
||||
private static bool TryParseHtmlTagHtmlComment(ref StringSlice text, ref ValueStringBuilder builder)
|
||||
{
|
||||
var c = text.NextChar();
|
||||
if (c != '-')
|
||||
@@ -393,7 +392,7 @@ namespace Markdig.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
private static bool TryParseHtmlTagProcessingInstruction(ref StringSlice text, StringBuilder builder)
|
||||
private static bool TryParseHtmlTagProcessingInstruction(ref StringSlice text, ref ValueStringBuilder builder)
|
||||
{
|
||||
builder.Append('?');
|
||||
var prevChar = '\0';
|
||||
@@ -435,13 +434,12 @@ namespace Markdig.Helpers
|
||||
// remove backslashes before punctuation chars:
|
||||
int searchPos = 0;
|
||||
int lastPos = 0;
|
||||
char c;
|
||||
char c = '\0';
|
||||
char[] search = removeBackSlash ? SearchBackAndAmp : SearchAmp;
|
||||
StringBuilder? sb = null;
|
||||
var sb = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
|
||||
while ((searchPos = text!.IndexOfAny(search, searchPos)) != -1)
|
||||
{
|
||||
sb ??= StringBuilderCache.Local();
|
||||
c = text[searchPos];
|
||||
if (removeBackSlash && c == '\\')
|
||||
{
|
||||
@@ -453,7 +451,7 @@ namespace Markdig.Helpers
|
||||
c = text[searchPos];
|
||||
if (c.IsEscapableSymbol())
|
||||
{
|
||||
sb.Append(text, lastPos, searchPos - lastPos - 1);
|
||||
sb.Append(text.AsSpan(lastPos, searchPos - lastPos - 1));
|
||||
lastPos = searchPos;
|
||||
}
|
||||
}
|
||||
@@ -473,26 +471,29 @@ namespace Markdig.Helpers
|
||||
var decoded = EntityHelper.DecodeEntity(text.AsSpan(entityNameStart, entityNameLength));
|
||||
if (decoded != null)
|
||||
{
|
||||
sb.Append(text, lastPos, searchPos - match - lastPos);
|
||||
sb.Append(text.AsSpan(lastPos, searchPos - match - lastPos));
|
||||
sb.Append(decoded);
|
||||
lastPos = searchPos;
|
||||
}
|
||||
}
|
||||
else if (numericEntity >= 0)
|
||||
{
|
||||
sb.Append(text, lastPos, searchPos - match - lastPos);
|
||||
EntityHelper.DecodeEntity(numericEntity, sb);
|
||||
sb.Append(text.AsSpan(lastPos, searchPos - match - lastPos));
|
||||
EntityHelper.DecodeEntity(numericEntity, ref sb);
|
||||
lastPos = searchPos;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sb is null || lastPos == 0)
|
||||
if (c == 0)
|
||||
{
|
||||
sb.Dispose();
|
||||
return text;
|
||||
}
|
||||
|
||||
sb.Append(text, lastPos, text.Length - lastPos);
|
||||
return sb.GetStringAndReset();
|
||||
sb.Append(text.AsSpan(lastPos, text.Length - lastPos));
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
44
src/Markdig/Helpers/LazySubstring.cs
Normal file
44
src/Markdig/Helpers/LazySubstring.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Markdig.Helpers
|
||||
{
|
||||
internal struct LazySubstring
|
||||
{
|
||||
private string _text;
|
||||
public int Offset;
|
||||
public int Length;
|
||||
|
||||
public LazySubstring(string text)
|
||||
{
|
||||
_text = text;
|
||||
Offset = 0;
|
||||
Length = text.Length;
|
||||
}
|
||||
|
||||
public LazySubstring(string text, int offset, int length)
|
||||
{
|
||||
Debug.Assert((ulong)offset + (ulong)length <= (ulong)text.Length, $"{offset}-{length} in {text}");
|
||||
_text = text;
|
||||
Offset = offset;
|
||||
Length = length;
|
||||
}
|
||||
|
||||
public ReadOnlySpan<char> AsSpan() => _text.AsSpan(Offset, Length);
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (Offset != 0 || Length != _text.Length)
|
||||
{
|
||||
_text = _text.Substring(Offset, Length);
|
||||
Offset = 0;
|
||||
}
|
||||
|
||||
return _text;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Markdig.Helpers
|
||||
{
|
||||
@@ -39,41 +41,55 @@ namespace Markdig.Helpers
|
||||
/// <returns>A new line or null if the end of <see cref="TextReader"/> has been reached</returns>
|
||||
public StringSlice ReadLine()
|
||||
{
|
||||
string text = _text;
|
||||
string? text = _text;
|
||||
int end = text.Length;
|
||||
int sourcePosition = SourcePosition;
|
||||
int newSourcePosition = int.MaxValue;
|
||||
NewLine newLine = NewLine.None;
|
||||
|
||||
for (int i = sourcePosition; i < text.Length; i++)
|
||||
if ((uint)sourcePosition >= (uint)end)
|
||||
{
|
||||
char c = text[i];
|
||||
if (c == '\r')
|
||||
text = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
#if NETCOREAPP3_1_OR_GREATER
|
||||
ReadOnlySpan<char> span = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.Add(ref Unsafe.AsRef(text.GetPinnableReference()), sourcePosition), end - sourcePosition);
|
||||
#else
|
||||
ReadOnlySpan<char> span = text.AsSpan(sourcePosition);
|
||||
#endif
|
||||
|
||||
int crlf = span.IndexOfAny('\r', '\n');
|
||||
if (crlf >= 0)
|
||||
{
|
||||
int length = 1;
|
||||
var newLine = NewLine.CarriageReturn;
|
||||
if (c == '\r' && (uint)(i + 1) < (uint)text.Length && text[i + 1] == '\n')
|
||||
end = sourcePosition + crlf;
|
||||
newSourcePosition = end + 1;
|
||||
|
||||
#if NETCOREAPP3_1_OR_GREATER
|
||||
if (Unsafe.Add(ref Unsafe.AsRef(text.GetPinnableReference()), end) == '\r')
|
||||
#else
|
||||
if ((uint)end < (uint)text.Length && text[end] == '\r')
|
||||
#endif
|
||||
{
|
||||
i++;
|
||||
length = 2;
|
||||
newLine = NewLine.CarriageReturnLineFeed;
|
||||
if ((uint)(newSourcePosition) < (uint)text.Length && text[newSourcePosition] == '\n')
|
||||
{
|
||||
newLine = NewLine.CarriageReturnLineFeed;
|
||||
newSourcePosition++;
|
||||
}
|
||||
else
|
||||
{
|
||||
newLine = NewLine.CarriageReturn;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
newLine = NewLine.LineFeed;
|
||||
}
|
||||
|
||||
var slice = new StringSlice(text, sourcePosition, i - length, newLine);
|
||||
SourcePosition = i + 1;
|
||||
return slice;
|
||||
}
|
||||
|
||||
if (c == '\n')
|
||||
{
|
||||
var slice = new StringSlice(text, sourcePosition, i - 1, NewLine.LineFeed);
|
||||
SourcePosition = i + 1;
|
||||
return slice;
|
||||
}
|
||||
}
|
||||
|
||||
if (sourcePosition >= text.Length)
|
||||
return default;
|
||||
|
||||
SourcePosition = int.MaxValue;
|
||||
return new StringSlice(text, sourcePosition, text.Length - 1);
|
||||
SourcePosition = newSourcePosition;
|
||||
return new StringSlice(text, sourcePosition, end - 1, newLine, dummy: false);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using Markdig.Syntax;
|
||||
|
||||
namespace Markdig.Helpers
|
||||
@@ -21,7 +20,7 @@ namespace Markdig.Helpers
|
||||
|
||||
public static string Urilize(string headingText, bool allowOnlyAscii, bool keepOpeningDigits = false)
|
||||
{
|
||||
var headingBuffer = StringBuilderCache.Local();
|
||||
var headingBuffer = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
bool hasLetter = keepOpeningDigits && headingText.Length > 0 && char.IsLetterOrDigit(headingText[0]);
|
||||
bool previousIsSpace = false;
|
||||
for (int i = 0; i < headingText.Length; i++)
|
||||
@@ -92,15 +91,13 @@ namespace Markdig.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
var text = headingBuffer.ToString();
|
||||
headingBuffer.Length = 0;
|
||||
return text;
|
||||
return headingBuffer.ToString();
|
||||
}
|
||||
|
||||
public static string UrilizeAsGfm(string headingText)
|
||||
{
|
||||
// Following https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb
|
||||
var headingBuffer = StringBuilderCache.Local();
|
||||
var headingBuffer = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
for (int i = 0; i < headingText.Length; i++)
|
||||
{
|
||||
var c = headingText[i];
|
||||
@@ -109,7 +106,7 @@ namespace Markdig.Helpers
|
||||
headingBuffer.Append(c == ' ' ? '-' : char.ToLowerInvariant(c));
|
||||
}
|
||||
}
|
||||
return headingBuffer.GetStringAndReset();
|
||||
return headingBuffer.ToString();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
@@ -165,7 +162,7 @@ namespace Markdig.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
var builder = StringBuilderCache.Local();
|
||||
var builder = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
|
||||
// ****************************
|
||||
// 1. Scan scheme or user email
|
||||
@@ -193,8 +190,7 @@ namespace Markdig.Helpers
|
||||
// a scheme is any sequence of 2–32 characters
|
||||
if (state > 0 && builder.Length >= 32)
|
||||
{
|
||||
builder.Length = 0;
|
||||
return false;
|
||||
goto ReturnFalse;
|
||||
}
|
||||
builder.Append(c);
|
||||
}
|
||||
@@ -202,8 +198,7 @@ namespace Markdig.Helpers
|
||||
{
|
||||
if (state < 0 || builder.Length <= 2)
|
||||
{
|
||||
builder.Length = 0;
|
||||
return false;
|
||||
goto ReturnFalse;
|
||||
}
|
||||
state = 1;
|
||||
break;
|
||||
@@ -211,16 +206,14 @@ namespace Markdig.Helpers
|
||||
{
|
||||
if (state > 0)
|
||||
{
|
||||
builder.Length = 0;
|
||||
return false;
|
||||
goto ReturnFalse;
|
||||
}
|
||||
state = -1;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Length = 0;
|
||||
return false;
|
||||
goto ReturnFalse;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -249,7 +242,6 @@ namespace Markdig.Helpers
|
||||
|
||||
text.SkipChar();
|
||||
link = builder.ToString();
|
||||
builder.Length = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -297,7 +289,6 @@ namespace Markdig.Helpers
|
||||
{
|
||||
text.SkipChar();
|
||||
link = builder.ToString();
|
||||
builder.Length = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -318,7 +309,8 @@ namespace Markdig.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
builder.Length = 0;
|
||||
ReturnFalse:
|
||||
builder.Dispose();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -528,8 +520,7 @@ namespace Markdig.Helpers
|
||||
|
||||
public static bool TryParseTitle<T>(ref T text, out string? title, out char enclosingCharacter) where T : ICharIterator
|
||||
{
|
||||
bool isValid = false;
|
||||
var buffer = StringBuilderCache.Local();
|
||||
var buffer = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
enclosingCharacter = '\0';
|
||||
|
||||
// a sequence of zero or more characters between straight double-quote characters ("), including a " character only if it is backslash-escaped, or
|
||||
@@ -582,8 +573,7 @@ namespace Markdig.Helpers
|
||||
|
||||
// Skip last quote
|
||||
text.SkipChar();
|
||||
isValid = true;
|
||||
break;
|
||||
goto ReturnValid;
|
||||
}
|
||||
|
||||
if (hasEscape && !c.IsAsciiPunctuation())
|
||||
@@ -615,15 +605,18 @@ namespace Markdig.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
title = isValid ? buffer.ToString() : null;
|
||||
buffer.Length = 0;
|
||||
return isValid;
|
||||
buffer.Dispose();
|
||||
title = null;
|
||||
return false;
|
||||
|
||||
ReturnValid:
|
||||
title = buffer.ToString();
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool TryParseTitleTrivia<T>(ref T text, out string? title, out char enclosingCharacter) where T : ICharIterator
|
||||
{
|
||||
bool isValid = false;
|
||||
var buffer = StringBuilderCache.Local();
|
||||
var buffer = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
enclosingCharacter = '\0';
|
||||
|
||||
// a sequence of zero or more characters between straight double-quote characters ("), including a " character only if it is backslash-escaped, or
|
||||
@@ -676,8 +669,7 @@ namespace Markdig.Helpers
|
||||
|
||||
// Skip last quote
|
||||
text.SkipChar();
|
||||
isValid = true;
|
||||
break;
|
||||
goto ReturnValid;
|
||||
}
|
||||
|
||||
if (hasEscape && !c.IsAsciiPunctuation())
|
||||
@@ -709,9 +701,13 @@ namespace Markdig.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
title = isValid ? buffer.ToString() : null;
|
||||
buffer.Length = 0;
|
||||
return isValid;
|
||||
buffer.Dispose();
|
||||
title = null;
|
||||
return false;
|
||||
|
||||
ReturnValid:
|
||||
title = buffer.ToString();
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool TryParseUrl<T>(T text, [NotNullWhen(true)] out string? link) where T : ICharIterator
|
||||
@@ -723,7 +719,7 @@ namespace Markdig.Helpers
|
||||
{
|
||||
bool isValid = false;
|
||||
hasPointyBrackets = false;
|
||||
var buffer = StringBuilderCache.Local();
|
||||
var buffer = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
|
||||
var c = text.CurrentChar;
|
||||
|
||||
@@ -854,8 +850,15 @@ namespace Markdig.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
link = isValid ? buffer.ToString() : null;
|
||||
buffer.Length = 0;
|
||||
if (isValid)
|
||||
{
|
||||
link = buffer.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.Dispose();
|
||||
link = null;
|
||||
}
|
||||
return isValid;
|
||||
}
|
||||
|
||||
@@ -863,8 +866,7 @@ namespace Markdig.Helpers
|
||||
{
|
||||
bool isValid = false;
|
||||
hasPointyBrackets = false;
|
||||
var buffer = StringBuilderCache.Local();
|
||||
var unescaped = new StringBuilder();
|
||||
var buffer = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
|
||||
var c = text.CurrentChar;
|
||||
|
||||
@@ -892,13 +894,11 @@ namespace Markdig.Helpers
|
||||
if (hasEscape && !c.IsAsciiPunctuation())
|
||||
{
|
||||
buffer.Append('\\');
|
||||
unescaped.Append('\\');
|
||||
}
|
||||
|
||||
if (c == '\\')
|
||||
{
|
||||
hasEscape = true;
|
||||
unescaped.Append('\\');
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -910,7 +910,6 @@ namespace Markdig.Helpers
|
||||
hasEscape = false;
|
||||
|
||||
buffer.Append(c);
|
||||
unescaped.Append(c);
|
||||
|
||||
} while (c != '\0');
|
||||
}
|
||||
@@ -958,7 +957,6 @@ namespace Markdig.Helpers
|
||||
{
|
||||
hasEscape = true;
|
||||
c = text.NextChar();
|
||||
unescaped.Append('\\');
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -989,7 +987,6 @@ namespace Markdig.Helpers
|
||||
}
|
||||
|
||||
buffer.Append(c);
|
||||
unescaped.Append(c);
|
||||
|
||||
c = text.NextChar();
|
||||
}
|
||||
@@ -1000,8 +997,15 @@ namespace Markdig.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
link = isValid ? buffer.ToString() : null;
|
||||
buffer.Length = 0;
|
||||
if (isValid)
|
||||
{
|
||||
link = buffer.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.Dispose();
|
||||
link = null;
|
||||
}
|
||||
return isValid;
|
||||
}
|
||||
|
||||
@@ -1357,7 +1361,7 @@ namespace Markdig.Helpers
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var buffer = StringBuilderCache.Local();
|
||||
var buffer = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
|
||||
var startLabel = -1;
|
||||
var endLabel = -1;
|
||||
@@ -1365,7 +1369,6 @@ namespace Markdig.Helpers
|
||||
bool hasEscape = false;
|
||||
bool previousWhitespace = true;
|
||||
bool hasNonWhiteSpace = false;
|
||||
bool isValid = false;
|
||||
while (true)
|
||||
{
|
||||
c = lines.NextChar();
|
||||
@@ -1413,9 +1416,7 @@ namespace Markdig.Helpers
|
||||
{
|
||||
labelSpan = SourceSpan.Empty;
|
||||
}
|
||||
|
||||
label = buffer.ToString();
|
||||
isValid = true;
|
||||
goto ReturnValid;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -1458,9 +1459,12 @@ namespace Markdig.Helpers
|
||||
previousWhitespace = isWhitespace;
|
||||
}
|
||||
|
||||
buffer.Length = 0;
|
||||
buffer.Dispose();
|
||||
return false;
|
||||
|
||||
return isValid;
|
||||
ReturnValid:
|
||||
label = buffer.ToString();
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool TryParseLabelTrivia<T>(ref T lines, bool allowEmpty, out string? label, out SourceSpan labelSpan) where T : ICharIterator
|
||||
@@ -1472,7 +1476,7 @@ namespace Markdig.Helpers
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var buffer = StringBuilderCache.Local();
|
||||
var buffer = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
|
||||
var startLabel = -1;
|
||||
var endLabel = -1;
|
||||
@@ -1480,7 +1484,6 @@ namespace Markdig.Helpers
|
||||
bool hasEscape = false;
|
||||
bool previousWhitespace = true;
|
||||
bool hasNonWhiteSpace = false;
|
||||
bool isValid = false;
|
||||
while (true)
|
||||
{
|
||||
c = lines.NextChar();
|
||||
@@ -1528,9 +1531,7 @@ namespace Markdig.Helpers
|
||||
{
|
||||
labelSpan = SourceSpan.Empty;
|
||||
}
|
||||
|
||||
label = buffer.ToString();
|
||||
isValid = true;
|
||||
goto ReturnValid;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -1577,10 +1578,12 @@ namespace Markdig.Helpers
|
||||
previousWhitespace = isWhitespace;
|
||||
}
|
||||
|
||||
buffer.Length = 0;
|
||||
buffer.Dispose();
|
||||
return false;
|
||||
|
||||
return isValid;
|
||||
ReturnValid:
|
||||
label = buffer.ToString();
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace Markdig.Helpers
|
||||
{
|
||||
@@ -13,14 +13,14 @@ namespace Markdig.Helpers
|
||||
/// <typeparam name="T">Type of the object to cache</typeparam>
|
||||
public abstract class ObjectCache<T> where T : class
|
||||
{
|
||||
private readonly Stack<T> builders;
|
||||
private readonly ConcurrentQueue<T> _builders;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ObjectCache{T}"/> class.
|
||||
/// </summary>
|
||||
protected ObjectCache()
|
||||
{
|
||||
builders = new Stack<T>(4);
|
||||
_builders = new ConcurrentQueue<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -28,10 +28,7 @@ namespace Markdig.Helpers
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
lock (builders)
|
||||
{
|
||||
builders.Clear();
|
||||
}
|
||||
_builders.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -40,12 +37,9 @@ namespace Markdig.Helpers
|
||||
/// <returns></returns>
|
||||
public T Get()
|
||||
{
|
||||
lock (builders)
|
||||
if (_builders.TryDequeue(out T instance))
|
||||
{
|
||||
if (builders.Count > 0)
|
||||
{
|
||||
return builders.Pop();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
return NewInstance();
|
||||
@@ -60,10 +54,7 @@ namespace Markdig.Helpers
|
||||
{
|
||||
if (instance is null) ThrowHelper.ArgumentNullException(nameof(instance));
|
||||
Reset(instance);
|
||||
lock (builders)
|
||||
{
|
||||
builders.Push(instance);
|
||||
}
|
||||
_builders.Enqueue(instance);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -20,12 +20,5 @@ namespace Markdig.Helpers
|
||||
{
|
||||
return builder.Append(slice.Text, slice.Start, slice.Length);
|
||||
}
|
||||
|
||||
internal static string GetStringAndReset(this StringBuilder builder)
|
||||
{
|
||||
string text = builder.ToString();
|
||||
builder.Length = 0;
|
||||
return text;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -85,6 +85,14 @@ namespace Markdig.Helpers
|
||||
Count--;
|
||||
}
|
||||
|
||||
internal void RemoveStartRange(int toRemove)
|
||||
{
|
||||
int remaining = Count - toRemove;
|
||||
Count = remaining;
|
||||
Array.Copy(Lines, toRemove, Lines, 0, remaining);
|
||||
Array.Clear(Lines, remaining, toRemove);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the specified line to this instance.
|
||||
/// </summary>
|
||||
@@ -139,7 +147,7 @@ namespace Markdig.Helpers
|
||||
}
|
||||
|
||||
// Else use a builder
|
||||
var builder = StringBuilderCache.Local();
|
||||
var builder = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
int previousStartOfLine = 0;
|
||||
var newLine = NewLine.None;
|
||||
for (int i = 0; i < Count; i++)
|
||||
@@ -152,13 +160,13 @@ namespace Markdig.Helpers
|
||||
ref StringLine line = ref Lines[i];
|
||||
if (!line.Slice.IsEmpty)
|
||||
{
|
||||
builder.Append(line.Slice.Text, line.Slice.Start, line.Slice.Length);
|
||||
builder.Append(line.Slice.AsSpan());
|
||||
}
|
||||
newLine = line.NewLine;
|
||||
|
||||
lineOffsets?.Add(new LineOffset(line.Position, line.Column, line.Slice.Start - line.Position, previousStartOfLine, builder.Length));
|
||||
}
|
||||
return new StringSlice(builder.GetStringAndReset());
|
||||
return new StringSlice(builder.ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -213,21 +221,24 @@ namespace Markdig.Helpers
|
||||
public struct Iterator : ICharIterator
|
||||
{
|
||||
private readonly StringLineGroup _lines;
|
||||
private StringSlice _currentSlice;
|
||||
private int _offset;
|
||||
|
||||
public Iterator(StringLineGroup lines)
|
||||
public Iterator(StringLineGroup stringLineGroup)
|
||||
{
|
||||
this._lines = lines;
|
||||
_lines = stringLineGroup;
|
||||
Start = -1;
|
||||
_offset = -1;
|
||||
SliceIndex = 0;
|
||||
CurrentChar = '\0';
|
||||
End = -1;
|
||||
for (int i = 0; i < lines.Count; i++)
|
||||
StringLine[] lines = stringLineGroup.Lines;
|
||||
for (int i = 0; i < stringLineGroup.Count && i < lines.Length; i++)
|
||||
{
|
||||
ref StringLine line = ref lines.Lines[i];
|
||||
End += line.Slice.Length + line.NewLine.Length(); // Add chars
|
||||
ref StringSlice slice = ref lines[i].Slice;
|
||||
End += slice.Length + slice.NewLine.Length(); // Add chars
|
||||
}
|
||||
_currentSlice = _lines.Lines[0].Slice;
|
||||
SkipChar();
|
||||
}
|
||||
|
||||
@@ -243,17 +254,14 @@ namespace Markdig.Helpers
|
||||
|
||||
public StringLineGroup Remaining()
|
||||
{
|
||||
var lines = _lines;
|
||||
StringLineGroup lines = _lines;
|
||||
if (IsEmpty)
|
||||
{
|
||||
lines.Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = SliceIndex - 1; i >= 0; i--)
|
||||
{
|
||||
lines.RemoveAt(i);
|
||||
}
|
||||
lines.RemoveStartRange(SliceIndex);
|
||||
|
||||
if (lines.Count > 0 && _offset > 0)
|
||||
{
|
||||
@@ -266,59 +274,85 @@ namespace Markdig.Helpers
|
||||
return lines;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public char NextChar()
|
||||
{
|
||||
Start++;
|
||||
if (Start <= End)
|
||||
{
|
||||
var slice = _lines.Lines[SliceIndex].Slice;
|
||||
ref StringSlice slice = ref _currentSlice;
|
||||
_offset++;
|
||||
if (_offset < slice.Length)
|
||||
|
||||
int index = slice.Start + _offset;
|
||||
string text = slice.Text;
|
||||
if (index <= slice.End && (uint)index < (uint)text.Length)
|
||||
{
|
||||
CurrentChar = slice[slice.Start + _offset];
|
||||
char c = text[index];
|
||||
CurrentChar = c;
|
||||
return c;
|
||||
}
|
||||
else
|
||||
{
|
||||
var newLine = slice.NewLine;
|
||||
if (_offset == slice.Length)
|
||||
{
|
||||
if (newLine == NewLine.LineFeed)
|
||||
{
|
||||
CurrentChar = '\n';
|
||||
SliceIndex++;
|
||||
_offset = -1;
|
||||
}
|
||||
else if (newLine == NewLine.CarriageReturn)
|
||||
{
|
||||
CurrentChar = '\r';
|
||||
SliceIndex++;
|
||||
_offset = -1;
|
||||
}
|
||||
else if (newLine == NewLine.CarriageReturnLineFeed)
|
||||
{
|
||||
CurrentChar = '\r';
|
||||
}
|
||||
}
|
||||
else if (_offset - 1 == slice.Length)
|
||||
{
|
||||
if (newLine == NewLine.CarriageReturnLineFeed)
|
||||
{
|
||||
CurrentChar = '\n';
|
||||
SliceIndex++;
|
||||
_offset = -1;
|
||||
}
|
||||
}
|
||||
return NextCharNewLine();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CurrentChar = '\0';
|
||||
Start = End + 1;
|
||||
SliceIndex = _lines.Count;
|
||||
return NextCharEndOfEnumerator();
|
||||
}
|
||||
}
|
||||
|
||||
private char NextCharNewLine()
|
||||
{
|
||||
int sliceLength = _currentSlice.Length;
|
||||
NewLine newLine = _currentSlice.NewLine;
|
||||
|
||||
if (_offset == sliceLength)
|
||||
{
|
||||
if (newLine == NewLine.LineFeed)
|
||||
{
|
||||
CurrentChar = '\n';
|
||||
goto MoveToNewLine;
|
||||
}
|
||||
else if (newLine == NewLine.CarriageReturn)
|
||||
{
|
||||
CurrentChar = '\r';
|
||||
goto MoveToNewLine;
|
||||
}
|
||||
else if (newLine == NewLine.CarriageReturnLineFeed)
|
||||
{
|
||||
CurrentChar = '\r';
|
||||
}
|
||||
}
|
||||
else if (_offset - 1 == sliceLength)
|
||||
{
|
||||
if (newLine == NewLine.CarriageReturnLineFeed)
|
||||
{
|
||||
CurrentChar = '\n';
|
||||
goto MoveToNewLine;
|
||||
}
|
||||
}
|
||||
|
||||
goto Return;
|
||||
|
||||
MoveToNewLine:
|
||||
SliceIndex++;
|
||||
_offset = -1;
|
||||
_currentSlice = _lines.Lines[SliceIndex];
|
||||
|
||||
Return:
|
||||
return CurrentChar;
|
||||
}
|
||||
|
||||
private char NextCharEndOfEnumerator()
|
||||
{
|
||||
CurrentChar = '\0';
|
||||
Start = End + 1;
|
||||
SliceIndex = _lines.Count;
|
||||
return '\0';
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void SkipChar() => NextChar();
|
||||
|
||||
public readonly char PeekChar() => PeekChar(1);
|
||||
@@ -335,16 +369,17 @@ namespace Markdig.Helpers
|
||||
offset += _offset;
|
||||
|
||||
int sliceIndex = SliceIndex;
|
||||
ref StringLine line = ref _lines.Lines[sliceIndex];
|
||||
ref StringSlice slice = ref line.Slice;
|
||||
if (!(line.NewLine == NewLine.CarriageReturnLineFeed && offset == slice.Length + 1))
|
||||
ref StringSlice slice = ref _lines.Lines[sliceIndex].Slice;
|
||||
NewLine newLine = slice.NewLine;
|
||||
|
||||
if (!(newLine == NewLine.CarriageReturnLineFeed && offset == slice.Length + 1))
|
||||
{
|
||||
while (offset > slice.Length)
|
||||
{
|
||||
// We are not peeking at the same line
|
||||
offset -= slice.Length + 1; // + 1 for new line
|
||||
|
||||
Debug.Assert(sliceIndex + 1 < _lines.Lines.Length, "'Start + offset > End' check above should prevent us from indexing out of range");
|
||||
Debug.Assert(sliceIndex + 1 < _lines.Count, "'Start + offset > End' check above should prevent us from indexing out of range");
|
||||
slice = ref _lines.Lines[++sliceIndex].Slice;
|
||||
}
|
||||
}
|
||||
@@ -358,15 +393,15 @@ namespace Markdig.Helpers
|
||||
|
||||
if (offset == slice.Length)
|
||||
{
|
||||
if (line.NewLine == NewLine.LineFeed)
|
||||
if (newLine == NewLine.LineFeed)
|
||||
{
|
||||
return '\n';
|
||||
}
|
||||
if (line.NewLine == NewLine.CarriageReturn)
|
||||
if (newLine == NewLine.CarriageReturn)
|
||||
{
|
||||
return '\r';
|
||||
}
|
||||
if (line.NewLine == NewLine.CarriageReturnLineFeed)
|
||||
if (newLine == NewLine.CarriageReturnLineFeed)
|
||||
{
|
||||
return '\r'; // /r of /r/n (first character)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Markdig.Helpers
|
||||
{
|
||||
@@ -82,6 +83,15 @@ namespace Markdig.Helpers
|
||||
NewLine = newLine;
|
||||
}
|
||||
|
||||
// Internal ctor to skip the null check
|
||||
internal StringSlice(string text, int start, int end, NewLine newLine, bool dummy)
|
||||
{
|
||||
Text = text;
|
||||
Start = start;
|
||||
End = end;
|
||||
NewLine = newLine;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The text of this slice.
|
||||
/// </summary>
|
||||
@@ -453,6 +463,25 @@ namespace Markdig.Helpers
|
||||
return text.Substring(start, length);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly ReadOnlySpan<char> AsSpan()
|
||||
{
|
||||
string text = Text;
|
||||
int start = Start;
|
||||
int length = End - start + 1;
|
||||
|
||||
if (text is null || (ulong)(uint)start + (ulong)(uint)length > (ulong)(uint)text.Length)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
#if NETCOREAPP3_1_OR_GREATER
|
||||
return MemoryMarshal.CreateReadOnlySpan(ref Unsafe.Add(ref Unsafe.AsRef(text.GetPinnableReference()), start), length);
|
||||
#else
|
||||
return text.AsSpan(start, length);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether this slice is empty or made only of whitespaces.
|
||||
/// </summary>
|
||||
|
||||
130
src/Markdig/Helpers/TransformedStringCache.cs
Normal file
130
src/Markdig/Helpers/TransformedStringCache.cs
Normal file
@@ -0,0 +1,130 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
||||
namespace Markdig.Helpers
|
||||
{
|
||||
internal sealed class TransformedStringCache
|
||||
{
|
||||
internal const int InputLengthLimit = 20; // Avoid caching unreasonably long strings
|
||||
internal const int MaxEntriesPerCharacter = 8; // Avoid growing too much
|
||||
|
||||
private readonly EntryGroup[] _groups; // One per ASCII character
|
||||
private readonly Func<string, string> _transformation;
|
||||
|
||||
public TransformedStringCache(Func<string, string> transformation)
|
||||
{
|
||||
_transformation = transformation ?? throw new ArgumentNullException(nameof(transformation));
|
||||
_groups = new EntryGroup[128];
|
||||
}
|
||||
|
||||
public string Get(ReadOnlySpan<char> inputSpan)
|
||||
{
|
||||
if ((uint)(inputSpan.Length - 1) < InputLengthLimit) // Length: [1, LengthLimit]
|
||||
{
|
||||
int firstCharacter = inputSpan[0];
|
||||
EntryGroup[] groups = _groups;
|
||||
if ((uint)firstCharacter < (uint)groups.Length)
|
||||
{
|
||||
ref EntryGroup group = ref groups[firstCharacter];
|
||||
string? transformed = group.TryGet(inputSpan);
|
||||
if (transformed is null)
|
||||
{
|
||||
string input = inputSpan.ToString();
|
||||
transformed = _transformation(input);
|
||||
group.TryAdd(input, transformed);
|
||||
}
|
||||
return transformed;
|
||||
}
|
||||
}
|
||||
|
||||
return _transformation(inputSpan.ToString());
|
||||
}
|
||||
|
||||
public string Get(string input)
|
||||
{
|
||||
if ((uint)(input.Length - 1) < InputLengthLimit) // Length: [1, LengthLimit]
|
||||
{
|
||||
int firstCharacter = input[0];
|
||||
EntryGroup[] groups = _groups;
|
||||
if ((uint)firstCharacter < (uint)groups.Length)
|
||||
{
|
||||
ref EntryGroup group = ref groups[firstCharacter];
|
||||
string? transformed = group.TryGet(input.AsSpan());
|
||||
if (transformed is null)
|
||||
{
|
||||
transformed = _transformation(input);
|
||||
group.TryAdd(input, transformed);
|
||||
}
|
||||
return transformed;
|
||||
}
|
||||
}
|
||||
|
||||
return _transformation(input);
|
||||
}
|
||||
|
||||
private struct EntryGroup
|
||||
{
|
||||
private struct Entry
|
||||
{
|
||||
public string Input;
|
||||
public string Transformed;
|
||||
}
|
||||
|
||||
private Entry[]? _entries;
|
||||
|
||||
public string? TryGet(ReadOnlySpan<char> inputSpan)
|
||||
{
|
||||
Entry[]? entries = _entries;
|
||||
if (entries is not null)
|
||||
{
|
||||
for (int i = 0; i < entries.Length; i++)
|
||||
{
|
||||
if (inputSpan.SequenceEqual(entries[i].Input.AsSpan()))
|
||||
{
|
||||
return entries[i].Transformed;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void TryAdd(string input, string transformed)
|
||||
{
|
||||
if (_entries is null)
|
||||
{
|
||||
Interlocked.CompareExchange(ref _entries, new Entry[MaxEntriesPerCharacter], null);
|
||||
}
|
||||
|
||||
if (_entries[MaxEntriesPerCharacter - 1].Input is null) // There is still space
|
||||
{
|
||||
lock (_entries)
|
||||
{
|
||||
for (int i = 0; i < _entries.Length; i++)
|
||||
{
|
||||
string? existingInput = _entries[i].Input;
|
||||
|
||||
if (existingInput is null)
|
||||
{
|
||||
ref Entry entry = ref _entries[i];
|
||||
Volatile.Write(ref entry.Transformed, transformed);
|
||||
Volatile.Write(ref entry.Input, input);
|
||||
break;
|
||||
}
|
||||
|
||||
if (input == existingInput)
|
||||
{
|
||||
// We lost a race and a different thread already added the same value
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
197
src/Markdig/Helpers/ValueStringBuilder.cs
Normal file
197
src/Markdig/Helpers/ValueStringBuilder.cs
Normal file
@@ -0,0 +1,197 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
// Inspired by https://github.com/dotnet/runtime/blob/main/src/libraries/Common/src/System/Text/ValueStringBuilder.cs
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Markdig.Helpers
|
||||
{
|
||||
internal ref partial struct ValueStringBuilder
|
||||
{
|
||||
#if DEBUG
|
||||
public const int StackallocThreshold = 7;
|
||||
#else
|
||||
#if NET5_0_OR_GREATER
|
||||
// NET5+ has SkipLocalsInit, so allocating more is "free"
|
||||
public const int StackallocThreshold = 256;
|
||||
#else
|
||||
public const int StackallocThreshold = 64;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
private char[]? _arrayToReturnToPool;
|
||||
private Span<char> _chars;
|
||||
private int _pos;
|
||||
|
||||
public ValueStringBuilder(Span<char> initialBuffer)
|
||||
{
|
||||
_arrayToReturnToPool = null;
|
||||
_chars = initialBuffer;
|
||||
_pos = 0;
|
||||
}
|
||||
|
||||
public int Length
|
||||
{
|
||||
get => _pos;
|
||||
set
|
||||
{
|
||||
Debug.Assert(value >= 0);
|
||||
Debug.Assert(value <= _chars.Length);
|
||||
_pos = value;
|
||||
}
|
||||
}
|
||||
|
||||
public ref char this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
Debug.Assert(index < _pos);
|
||||
return ref _chars[index];
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
string s = _chars.Slice(0, _pos).ToString();
|
||||
Dispose();
|
||||
return s;
|
||||
}
|
||||
|
||||
public ReadOnlySpan<char> AsSpan() => _chars.Slice(0, _pos);
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Append(char c)
|
||||
{
|
||||
int pos = _pos;
|
||||
Span<char> chars = _chars;
|
||||
if ((uint)pos < (uint)chars.Length)
|
||||
{
|
||||
chars[pos] = c;
|
||||
_pos = pos + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
GrowAndAppend(c);
|
||||
}
|
||||
}
|
||||
|
||||
public void Append(char c, int count)
|
||||
{
|
||||
if (_pos > _chars.Length - count)
|
||||
{
|
||||
Grow(count);
|
||||
}
|
||||
|
||||
Span<char> dst = _chars.Slice(_pos, count);
|
||||
for (int i = 0; i < dst.Length; i++)
|
||||
{
|
||||
dst[i] = c;
|
||||
}
|
||||
_pos += count;
|
||||
}
|
||||
|
||||
public void Append(uint i)
|
||||
{
|
||||
if (i < 10)
|
||||
{
|
||||
Append((char)('0' + i));
|
||||
}
|
||||
else
|
||||
{
|
||||
Append(i.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Append(string s)
|
||||
{
|
||||
int pos = _pos;
|
||||
if (pos > _chars.Length - s.Length)
|
||||
{
|
||||
Grow(s.Length);
|
||||
}
|
||||
|
||||
s
|
||||
#if !NET5_0_OR_GREATER
|
||||
.AsSpan()
|
||||
#endif
|
||||
.CopyTo(_chars.Slice(pos));
|
||||
|
||||
_pos += s.Length;
|
||||
}
|
||||
|
||||
public void Append(ReadOnlySpan<char> value)
|
||||
{
|
||||
if (_pos > _chars.Length - value.Length)
|
||||
{
|
||||
Grow(value.Length);
|
||||
}
|
||||
|
||||
value.CopyTo(_chars.Slice(_pos));
|
||||
_pos += value.Length;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Span<char> AppendSpan(int length)
|
||||
{
|
||||
int origPos = _pos;
|
||||
if (origPos > _chars.Length - length)
|
||||
{
|
||||
Grow(length);
|
||||
}
|
||||
|
||||
_pos = origPos + length;
|
||||
return _chars.Slice(origPos, length);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private void GrowAndAppend(char c)
|
||||
{
|
||||
Grow(1);
|
||||
Append(c);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resize the internal buffer either by doubling current buffer size or
|
||||
/// by adding <paramref name="additionalCapacityBeyondPos"/> to
|
||||
/// <see cref="_pos"/> whichever is greater.
|
||||
/// </summary>
|
||||
/// <param name="additionalCapacityBeyondPos">
|
||||
/// Number of chars requested beyond current position.
|
||||
/// </param>
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private void Grow(int additionalCapacityBeyondPos)
|
||||
{
|
||||
Debug.Assert(additionalCapacityBeyondPos > 0);
|
||||
Debug.Assert(_pos > _chars.Length - additionalCapacityBeyondPos, "Grow called incorrectly, no resize is needed.");
|
||||
|
||||
// Make sure to let Rent throw an exception if the caller has a bug and the desired capacity is negative
|
||||
char[] poolArray = ArrayPool<char>.Shared.Rent((int)Math.Max((uint)(_pos + additionalCapacityBeyondPos), (uint)_chars.Length * 2));
|
||||
|
||||
_chars.Slice(0, _pos).CopyTo(poolArray);
|
||||
|
||||
char[]? toReturn = _arrayToReturnToPool;
|
||||
_chars = _arrayToReturnToPool = poolArray;
|
||||
if (toReturn != null)
|
||||
{
|
||||
ArrayPool<char>.Shared.Return(toReturn);
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Dispose()
|
||||
{
|
||||
char[]? toReturn = _arrayToReturnToPool;
|
||||
this = default; // for safety, to avoid using pooled array if this instance is erroneously appended to again
|
||||
if (toReturn != null)
|
||||
{
|
||||
ArrayPool<char>.Shared.Return(toReturn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,11 @@
|
||||
<None Remove="readme.md" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<AdditionalFiles Include="readme.md" LunetApiDotNet="true"/>
|
||||
<AdditionalFiles Include="readme.md" LunetApiDotNet="true" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="Markdig.Tests" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -37,11 +37,19 @@
|
||||
<ItemGroup>
|
||||
<None Include="../../img/markdig.png" Pack="true" PackagePath="" />
|
||||
<None Include="../../readme.md" Pack="true" PackagePath="/"/>
|
||||
<PackageReference Include="MinVer" Version="2.5.0">
|
||||
<PackageReference Include="MinVer" Version="3.1.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.*" PrivateAssets="All"/>
|
||||
</ItemGroup>
|
||||
|
||||
<Target Name="PatchVersion" AfterTargets="MinVer">
|
||||
<PropertyGroup>
|
||||
<!--In Markdig, the minor version is like a major version because Major is 0
|
||||
Need to remove this when Markdig will be >= 1.0-->
|
||||
<AssemblyVersion>$(MinVerMajor).$(MinVerMinor).0.0</AssemblyVersion>
|
||||
</PropertyGroup>
|
||||
</Target>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -6,7 +6,6 @@ using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Markdig.Extensions.SelfPipeline;
|
||||
using Markdig.Helpers;
|
||||
using Markdig.Parsers;
|
||||
using Markdig.Renderers;
|
||||
@@ -41,11 +40,11 @@ namespace Markdig
|
||||
return DefaultPipeline;
|
||||
}
|
||||
|
||||
var selfPipeline = pipeline.Extensions.Find<SelfPipelineExtension>();
|
||||
if (selfPipeline is not null)
|
||||
if (pipeline.SelfPipeline is not null)
|
||||
{
|
||||
return selfPipeline.CreatePipelineFromInput(markdown);
|
||||
return pipeline.SelfPipeline.CreatePipelineFromInput(markdown);
|
||||
}
|
||||
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
@@ -132,6 +131,28 @@ namespace Markdig
|
||||
return renderer.Writer.ToString() ?? string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a Markdown document to HTML.
|
||||
/// </summary>
|
||||
/// <param name="document">A Markdown document.</param>
|
||||
/// <param name="writer">The destination <see cref="TextWriter"/> that will receive the result of the conversion.</param>
|
||||
/// <param name="pipeline">The pipeline used for the conversion.</param>
|
||||
/// <returns>The result of the conversion</returns>
|
||||
/// <exception cref="ArgumentNullException">if markdown document variable is null</exception>
|
||||
public static void ToHtml(this MarkdownDocument document, TextWriter writer, MarkdownPipeline? pipeline = null)
|
||||
{
|
||||
if (document is null) ThrowHelper.ArgumentNullException(nameof(document));
|
||||
if (writer is null) ThrowHelper.ArgumentNullException_writer();
|
||||
|
||||
pipeline ??= DefaultPipeline;
|
||||
|
||||
using var rentedRenderer = pipeline.RentHtmlRenderer(writer);
|
||||
HtmlRenderer renderer = rentedRenderer.Instance;
|
||||
|
||||
renderer.Render(document);
|
||||
renderer.Writer.Flush();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a Markdown string to HTML and output to the specified writer.
|
||||
/// </summary>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Markdig.Extensions.SelfPipeline;
|
||||
using Markdig.Helpers;
|
||||
using Markdig.Parsers;
|
||||
using Markdig.Renderers;
|
||||
@@ -13,11 +13,10 @@ namespace Markdig
|
||||
{
|
||||
/// <summary>
|
||||
/// This class is the Markdown pipeline build from a <see cref="MarkdownPipelineBuilder"/>.
|
||||
/// <para>An instance of <see cref="MarkdownPipeline"/> is immutable, thread-safe, and should be reused when parsing multiple inputs.</para>
|
||||
/// </summary>
|
||||
public sealed class MarkdownPipeline
|
||||
{
|
||||
// This class is immutable
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MarkdownPipeline" /> class.
|
||||
/// </summary>
|
||||
@@ -36,6 +35,8 @@ namespace Markdig
|
||||
InlineParsers = inlineParsers;
|
||||
DebugLog = debugLog;
|
||||
DocumentProcessed = documentProcessed;
|
||||
|
||||
SelfPipeline = Extensions.Find<SelfPipelineExtension>();
|
||||
}
|
||||
|
||||
internal bool PreciseSourceLocation { get; set; }
|
||||
@@ -54,6 +55,8 @@ namespace Markdig
|
||||
|
||||
internal ProcessDocumentDelegate? DocumentProcessed;
|
||||
|
||||
internal SelfPipelineExtension? SelfPipeline;
|
||||
|
||||
/// <summary>
|
||||
/// True to parse trivia such as whitespace, extra heading characters and unescaped
|
||||
/// string values.
|
||||
@@ -94,9 +97,7 @@ namespace Markdig
|
||||
|
||||
internal sealed class HtmlRendererCache : ObjectCache<HtmlRenderer>
|
||||
{
|
||||
private const int InitialCapacity = 1024;
|
||||
|
||||
private static readonly StringWriter _dummyWriter = new();
|
||||
private static readonly TextWriter s_dummyWriter = new FastStringWriter();
|
||||
|
||||
private readonly MarkdownPipeline _pipeline;
|
||||
private readonly bool _customWriter;
|
||||
@@ -109,7 +110,7 @@ namespace Markdig
|
||||
|
||||
protected override HtmlRenderer NewInstance()
|
||||
{
|
||||
var writer = _customWriter ? _dummyWriter : new StringWriter(new StringBuilder(InitialCapacity));
|
||||
TextWriter writer = _customWriter ? s_dummyWriter : new FastStringWriter();
|
||||
var renderer = new HtmlRenderer(writer);
|
||||
_pipeline.Setup(renderer);
|
||||
return renderer;
|
||||
@@ -121,11 +122,11 @@ namespace Markdig
|
||||
|
||||
if (_customWriter)
|
||||
{
|
||||
instance.Writer = _dummyWriter;
|
||||
instance.Writer = s_dummyWriter;
|
||||
}
|
||||
else
|
||||
{
|
||||
((StringWriter)instance.Writer).GetStringBuilder().Length = 0;
|
||||
((FastStringWriter)instance.Writer).Reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Markdig.Helpers;
|
||||
using Markdig.Syntax;
|
||||
|
||||
@@ -74,7 +75,15 @@ namespace Markdig.Parsers
|
||||
/// <summary>
|
||||
/// Gets the next block in a <see cref="BlockParser.TryContinue"/>.
|
||||
/// </summary>
|
||||
public Block? NextContinue => currentStackIndex + 1 < OpenedBlocks.Count ? OpenedBlocks[currentStackIndex + 1] : null;
|
||||
public Block? NextContinue
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
int index = currentStackIndex + 1;
|
||||
return index < OpenedBlocks.Count ? OpenedBlocks[index].Block : null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the root document.
|
||||
@@ -144,7 +153,7 @@ namespace Markdig.Parsers
|
||||
/// <summary>
|
||||
/// Gets the current stack of <see cref="Block"/> being processed.
|
||||
/// </summary>
|
||||
private List<Block> OpenedBlocks { get; } = new();
|
||||
private List<BlockWrapper> OpenedBlocks { get; } = new();
|
||||
|
||||
private bool ContinueProcessingLine { get; set; }
|
||||
|
||||
@@ -445,7 +454,7 @@ namespace Markdig.Parsers
|
||||
// If we close a block, we close all blocks above
|
||||
for (int i = OpenedBlocks.Count - 1; i >= 0; i--)
|
||||
{
|
||||
if (OpenedBlocks[i] == block)
|
||||
if (ReferenceEquals(OpenedBlocks[i].Block, block))
|
||||
{
|
||||
for (int j = OpenedBlocks.Count - 1; j >= i; j--)
|
||||
{
|
||||
@@ -464,7 +473,7 @@ namespace Markdig.Parsers
|
||||
{
|
||||
for (int i = OpenedBlocks.Count - 1; i >= 1; i--)
|
||||
{
|
||||
if (OpenedBlocks[i] == block)
|
||||
if (ReferenceEquals(OpenedBlocks[i].Block, block))
|
||||
{
|
||||
block.Parent!.Remove(block);
|
||||
OpenedBlocks.RemoveAt(i);
|
||||
@@ -509,7 +518,7 @@ namespace Markdig.Parsers
|
||||
/// <param name="index">The index.</param>
|
||||
private void Close(int index)
|
||||
{
|
||||
var block = OpenedBlocks[index];
|
||||
var block = OpenedBlocks[index].Block;
|
||||
// If the pending object is removed, we need to remove it from the parent container
|
||||
if (block.Parser != null)
|
||||
{
|
||||
@@ -517,9 +526,9 @@ namespace Markdig.Parsers
|
||||
{
|
||||
block.Parent?.Remove(block);
|
||||
|
||||
if (block is LeafBlock leaf)
|
||||
if (block.IsLeafBlock)
|
||||
{
|
||||
leaf.Lines.Release();
|
||||
Unsafe.As<LeafBlock>(block).Lines.Release();
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -541,7 +550,7 @@ namespace Markdig.Parsers
|
||||
// Close any previous blocks not opened
|
||||
for (int i = OpenedBlocks.Count - 1; i >= 1; i--)
|
||||
{
|
||||
var block = OpenedBlocks[i];
|
||||
var block = OpenedBlocks[i].Block;
|
||||
|
||||
// Stop on the first open block
|
||||
if (!force && block.IsOpen)
|
||||
@@ -582,7 +591,7 @@ namespace Markdig.Parsers
|
||||
{
|
||||
for (int i = 1; i < OpenedBlocks.Count; i++)
|
||||
{
|
||||
OpenedBlocks[i].IsOpen = true;
|
||||
OpenedBlocks[i].Block.IsOpen = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -592,24 +601,27 @@ namespace Markdig.Parsers
|
||||
/// <param name="stackIndex">Index of a block in a stack considered as the last block to update from.</param>
|
||||
private void UpdateLastBlockAndContainer(int stackIndex = -1)
|
||||
{
|
||||
currentStackIndex = stackIndex < 0 ? OpenedBlocks.Count - 1 : stackIndex;
|
||||
CurrentBlock = null;
|
||||
LastBlock = null;
|
||||
for (int i = OpenedBlocks.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var block = OpenedBlocks[i];
|
||||
if (CurrentBlock is null)
|
||||
{
|
||||
CurrentBlock = block;
|
||||
}
|
||||
List<BlockWrapper> openedBlocks = OpenedBlocks;
|
||||
currentStackIndex = stackIndex < 0 ? openedBlocks.Count - 1 : stackIndex;
|
||||
|
||||
if (block is ContainerBlock container)
|
||||
Block? currentBlock = null;
|
||||
for (int i = openedBlocks.Count - 1; i >= 0; i--)
|
||||
{
|
||||
var block = openedBlocks[i].Block;
|
||||
currentBlock ??= block;
|
||||
|
||||
if (block.IsContainerBlock)
|
||||
{
|
||||
CurrentContainer = container;
|
||||
LastBlock = CurrentContainer.LastChild;
|
||||
break;
|
||||
var currentContainer = Unsafe.As<ContainerBlock>(block);
|
||||
CurrentContainer = currentContainer;
|
||||
LastBlock = currentContainer.LastChild;
|
||||
CurrentBlock = currentBlock;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
CurrentBlock = currentBlock;
|
||||
LastBlock = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -628,18 +640,18 @@ namespace Markdig.Parsers
|
||||
// They will be marked as open in the following loop
|
||||
for (int i = 1; i < OpenedBlocks.Count; i++)
|
||||
{
|
||||
OpenedBlocks[i].IsOpen = false;
|
||||
OpenedBlocks[i].Block.IsOpen = false;
|
||||
}
|
||||
|
||||
// Process any current block potentially opened
|
||||
for (int i = 1; i < OpenedBlocks.Count; i++)
|
||||
{
|
||||
var block = OpenedBlocks[i];
|
||||
var block = OpenedBlocks[i].Block;
|
||||
|
||||
ParseIndent();
|
||||
|
||||
// If we have a paragraph block, we want to try to match other blocks before trying the Paragraph
|
||||
if (block is ParagraphBlock)
|
||||
if (block.IsParagraphBlock)
|
||||
{
|
||||
break;
|
||||
}
|
||||
@@ -675,7 +687,7 @@ namespace Markdig.Parsers
|
||||
}
|
||||
|
||||
// If we have a leaf block
|
||||
if (block is LeafBlock leaf && NewBlocks.Count == 0)
|
||||
if (block.IsLeafBlock && NewBlocks.Count == 0)
|
||||
{
|
||||
ContinueProcessingLine = false;
|
||||
if (!result.IsDiscard())
|
||||
@@ -689,7 +701,8 @@ namespace Markdig.Parsers
|
||||
UnwindAllIndents();
|
||||
}
|
||||
}
|
||||
leaf.AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia);
|
||||
|
||||
Unsafe.As<LeafBlock>(block).AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -804,7 +817,7 @@ namespace Markdig.Parsers
|
||||
continue;
|
||||
}
|
||||
|
||||
IsLazy = blockParser is ParagraphBlockParser && lastBlock is ParagraphBlock;
|
||||
IsLazy = lastBlock.IsParagraphBlock && blockParser is ParagraphBlockParser;
|
||||
|
||||
var result = IsLazy
|
||||
? blockParser.TryContinue(this, lastBlock)
|
||||
@@ -825,7 +838,7 @@ namespace Markdig.Parsers
|
||||
// Special case for paragraph
|
||||
UpdateLastBlockAndContainer();
|
||||
|
||||
if (IsLazy && CurrentBlock is ParagraphBlock paragraph)
|
||||
if (IsLazy && CurrentBlock is { } currentBlock && currentBlock.IsParagraphBlock)
|
||||
{
|
||||
Debug.Assert(NewBlocks.Count == 0);
|
||||
|
||||
@@ -835,12 +848,13 @@ namespace Markdig.Parsers
|
||||
{
|
||||
UnwindAllIndents();
|
||||
}
|
||||
paragraph.AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia);
|
||||
|
||||
Unsafe.As<ParagraphBlock>(currentBlock).AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia);
|
||||
}
|
||||
if (TrackTrivia)
|
||||
{
|
||||
// special case: take care when refactoring this
|
||||
if (paragraph.Parent is QuoteBlock qb)
|
||||
if (currentBlock.Parent is QuoteBlock qb)
|
||||
{
|
||||
var triviaAfter = UseTrivia(Start - 1);
|
||||
qb.QuoteLines.Last().TriviaAfter = triviaAfter;
|
||||
@@ -893,20 +907,19 @@ namespace Markdig.Parsers
|
||||
block.Line = LineIndex;
|
||||
|
||||
// If we have a leaf block
|
||||
var leaf = block as LeafBlock;
|
||||
if (leaf != null)
|
||||
if (block.IsLeafBlock)
|
||||
{
|
||||
if (!result.IsDiscard())
|
||||
{
|
||||
if (TrackTrivia)
|
||||
{
|
||||
if (block is ParagraphBlock ||
|
||||
block is HtmlBlock)
|
||||
if (block.IsParagraphBlock || block is HtmlBlock)
|
||||
{
|
||||
UnwindAllIndents();
|
||||
}
|
||||
}
|
||||
leaf.AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia);
|
||||
|
||||
Unsafe.As<LeafBlock>(block).AppendLine(ref Line, Column, LineIndex, CurrentLineStartPosition, TrackTrivia);
|
||||
}
|
||||
|
||||
if (newBlocks.Count > 0)
|
||||
@@ -934,7 +947,7 @@ namespace Markdig.Parsers
|
||||
// Add a block BlockProcessor to the stack (and leave it opened)
|
||||
OpenedBlocks.Add(block);
|
||||
|
||||
if (leaf != null)
|
||||
if (block.IsLeafBlock)
|
||||
{
|
||||
ContinueProcessingLine = false;
|
||||
return;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using System;
|
||||
|
||||
using System.Diagnostics;
|
||||
using Markdig.Helpers;
|
||||
using Markdig.Renderers.Html;
|
||||
using Markdig.Syntax;
|
||||
@@ -40,6 +40,9 @@ namespace Markdig.Parsers
|
||||
/// <seealso cref="BlockParser" />
|
||||
public abstract class FencedBlockParserBase<T> : FencedBlockParserBase where T : Block, IFencedBlock
|
||||
{
|
||||
private static readonly TransformedStringCache _infoStringCache = new(static infoString => HtmlHelper.Unescape(infoString));
|
||||
private TransformedStringCache? _infoPrefixCache;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="FencedBlockParserBase{T}"/> class.
|
||||
/// </summary>
|
||||
@@ -50,10 +53,22 @@ namespace Markdig.Parsers
|
||||
MaximumMatchCount = int.MaxValue;
|
||||
}
|
||||
|
||||
private string? _infoPrefix;
|
||||
/// <summary>
|
||||
/// Gets or sets the language prefix (default is "language-")
|
||||
/// </summary>
|
||||
public string? InfoPrefix { get; set; }
|
||||
public string? InfoPrefix
|
||||
{
|
||||
get => _infoPrefix;
|
||||
set
|
||||
{
|
||||
if (_infoPrefix != value)
|
||||
{
|
||||
_infoPrefixCache = new TransformedStringCache(infoString => value + infoString);
|
||||
_infoPrefix = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int MinimumMatchCount { get; set; }
|
||||
|
||||
@@ -161,7 +176,7 @@ namespace Markdig.Parsers
|
||||
|
||||
end:
|
||||
fenced.TriviaAfterFencedChar = afterFence;
|
||||
fenced.Info = HtmlHelper.Unescape(info.ToString());
|
||||
fenced.Info = _infoStringCache.Get(info.AsSpan());
|
||||
fenced.UnescapedInfo = info;
|
||||
fenced.TriviaAfterInfo = afterInfo;
|
||||
fenced.Arguments = HtmlHelper.Unescape(arg.ToString());
|
||||
@@ -182,9 +197,6 @@ namespace Markdig.Parsers
|
||||
/// <returns><c>true</c> if parsing of the line is successfull; <c>false</c> otherwise</returns>
|
||||
public static bool DefaultInfoParser(BlockProcessor state, ref StringSlice line, IFencedBlock fenced, char openingCharacter)
|
||||
{
|
||||
string infoString;
|
||||
string? argString = null;
|
||||
|
||||
// An info string cannot contain any backticks (unless it is a tilde block)
|
||||
int firstSpace = -1;
|
||||
if (openingCharacter == '`')
|
||||
@@ -215,9 +227,12 @@ namespace Markdig.Parsers
|
||||
}
|
||||
}
|
||||
|
||||
StringSlice infoStringSlice;
|
||||
string? argString = null;
|
||||
|
||||
if (firstSpace > 0)
|
||||
{
|
||||
infoString = line.Text.AsSpan(line.Start, firstSpace - line.Start).Trim().ToString();
|
||||
infoStringSlice = new StringSlice(line.Text, line.Start, firstSpace - 1);
|
||||
|
||||
// Skip any spaces after info string
|
||||
firstSpace++;
|
||||
@@ -234,16 +249,18 @@ namespace Markdig.Parsers
|
||||
}
|
||||
}
|
||||
|
||||
argString = line.Text.Substring(firstSpace, line.End - firstSpace + 1).Trim();
|
||||
var argStringSlice = new StringSlice(line.Text, firstSpace, line.End);
|
||||
argStringSlice.Trim();
|
||||
argString = argStringSlice.ToString();
|
||||
}
|
||||
else
|
||||
{
|
||||
var lineCopy = line;
|
||||
lineCopy.Trim();
|
||||
infoString = lineCopy.ToString();
|
||||
infoStringSlice = line;
|
||||
}
|
||||
|
||||
fenced.Info = HtmlHelper.Unescape(infoString);
|
||||
infoStringSlice.Trim();
|
||||
|
||||
fenced.Info = _infoStringCache.Get(infoStringSlice.AsSpan());
|
||||
fenced.Arguments = HtmlHelper.Unescape(argString);
|
||||
|
||||
return true;
|
||||
@@ -295,7 +312,9 @@ namespace Markdig.Parsers
|
||||
// Add the language as an attribute by default
|
||||
if (!string.IsNullOrEmpty(fenced.Info))
|
||||
{
|
||||
fenced.GetAttributes().AddClass(InfoPrefix + fenced.Info);
|
||||
Debug.Assert(_infoPrefixCache is not null || InfoPrefix is null);
|
||||
string infoWithPrefix = _infoPrefixCache?.Get(fenced.Info!) ?? fenced.Info!;
|
||||
fenced.GetAttributes().AddClass(infoWithPrefix);
|
||||
}
|
||||
|
||||
// Store the number of matched string into the context
|
||||
@@ -332,9 +351,13 @@ namespace Markdig.Parsers
|
||||
|
||||
var fencedBlock = (IFencedBlock)block;
|
||||
fencedBlock.ClosingFencedCharCount = closingCount;
|
||||
fencedBlock.NewLine = processor.Line.NewLine;
|
||||
fencedBlock.TriviaBeforeClosingFence = processor.UseTrivia(sourcePosition - 1);
|
||||
fencedBlock.TriviaAfter = new StringSlice(processor.Line.Text, lastFenceCharPosition, endBeforeTrim);
|
||||
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
fencedBlock.NewLine = processor.Line.NewLine;
|
||||
fencedBlock.TriviaBeforeClosingFence = processor.UseTrivia(sourcePosition - 1);
|
||||
fencedBlock.TriviaAfter = new StringSlice(processor.Line.Text, lastFenceCharPosition, endBeforeTrim);
|
||||
}
|
||||
|
||||
// Don't keep the last line
|
||||
return BlockState.BreakDiscard;
|
||||
|
||||
@@ -26,13 +26,19 @@ namespace Markdig.Parsers
|
||||
|
||||
protected override FencedCodeBlock CreateFencedBlock(BlockProcessor processor)
|
||||
{
|
||||
return new FencedCodeBlock(this)
|
||||
var codeBlock = new FencedCodeBlock(this)
|
||||
{
|
||||
IndentCount = processor.Indent,
|
||||
LinesBefore = processor.UseLinesBefore(),
|
||||
TriviaBefore = processor.UseTrivia(processor.Start - 1),
|
||||
NewLine = processor.Line.NewLine,
|
||||
};
|
||||
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
codeBlock.LinesBefore = processor.UseLinesBefore();
|
||||
codeBlock.TriviaBefore = processor.UseTrivia(processor.Start - 1);
|
||||
codeBlock.NewLine = processor.Line.NewLine;
|
||||
}
|
||||
|
||||
return codeBlock;
|
||||
}
|
||||
|
||||
public override BlockState TryContinue(BlockProcessor processor, Block block)
|
||||
|
||||
@@ -82,20 +82,25 @@ namespace Markdig.Parsers
|
||||
var headingBlock = new HeadingBlock(this)
|
||||
{
|
||||
HeaderChar = matchingChar,
|
||||
TriviaAfterAtxHeaderChar = trivia,
|
||||
Level = leadingCount,
|
||||
Column = column,
|
||||
Span = { Start = sourcePosition },
|
||||
TriviaBefore = processor.UseTrivia(sourcePosition - 1),
|
||||
LinesBefore = processor.UseLinesBefore(),
|
||||
NewLine = processor.Line.NewLine,
|
||||
};
|
||||
processor.NewBlocks.Push(headingBlock);
|
||||
if (!processor.TrackTrivia)
|
||||
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
headingBlock.TriviaAfterAtxHeaderChar = trivia;
|
||||
headingBlock.TriviaBefore = processor.UseTrivia(sourcePosition - 1);
|
||||
headingBlock.LinesBefore = processor.UseLinesBefore();
|
||||
headingBlock.NewLine = processor.Line.NewLine;
|
||||
}
|
||||
else
|
||||
{
|
||||
processor.GoToColumn(column + leadingCount + 1);
|
||||
}
|
||||
|
||||
processor.NewBlocks.Push(headingBlock);
|
||||
|
||||
// Gives a chance to parse attributes
|
||||
TryParseAttributes?.Invoke(processor, ref processor.Line, headingBlock);
|
||||
|
||||
|
||||
@@ -62,10 +62,10 @@ namespace Markdig.Parsers
|
||||
|
||||
private BlockState TryParseTagType7(BlockProcessor state, StringSlice line, int startColumn, int startPosition)
|
||||
{
|
||||
var builder = StringBuilderCache.Local();
|
||||
var builder = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
var c = line.CurrentChar;
|
||||
var result = BlockState.None;
|
||||
if ((c == '/' && HtmlHelper.TryParseHtmlCloseTag(ref line, builder)) || HtmlHelper.TryParseHtmlTagOpenTag(ref line, builder))
|
||||
if ((c == '/' && HtmlHelper.TryParseHtmlCloseTag(ref line, ref builder)) || HtmlHelper.TryParseHtmlTagOpenTag(ref line, ref builder))
|
||||
{
|
||||
// Must be followed by whitespace only
|
||||
bool hasOnlySpaces = true;
|
||||
@@ -90,7 +90,7 @@ namespace Markdig.Parsers
|
||||
}
|
||||
}
|
||||
|
||||
builder.Length = 0;
|
||||
builder.Dispose();
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -159,8 +159,8 @@ namespace Markdig.Parsers
|
||||
|
||||
int tagIndex = match.Value;
|
||||
|
||||
// Cannot start with </script </pre or </style
|
||||
if ((tagIndex == 49 || tagIndex == 50 || tagIndex == 53))
|
||||
// Cannot start with </script </pre or </style or </textArea
|
||||
if ((tagIndex == 49 || tagIndex == 50 || tagIndex == 53 || tagIndex == 56))
|
||||
{
|
||||
if (c == '/' || hasLeadingClose)
|
||||
{
|
||||
@@ -241,6 +241,15 @@ namespace Markdig.Parsers
|
||||
htmlBlock.UpdateSpanEnd(index + "</style>".Length);
|
||||
result = BlockState.Break;
|
||||
}
|
||||
else
|
||||
{
|
||||
index = line.IndexOf("</textarea>", 0, true);
|
||||
if (index >= 0)
|
||||
{
|
||||
htmlBlock.UpdateSpanEnd(index + "</textarea>".Length);
|
||||
result = BlockState.Break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -270,20 +279,26 @@ namespace Markdig.Parsers
|
||||
|
||||
private BlockState CreateHtmlBlock(BlockProcessor state, HtmlBlockType type, int startColumn, int startPosition)
|
||||
{
|
||||
state.NewBlocks.Push(new HtmlBlock(this)
|
||||
var htmlBlock = new HtmlBlock(this)
|
||||
{
|
||||
Column = startColumn,
|
||||
Type = type,
|
||||
// By default, setup to the end of line
|
||||
Span = new SourceSpan(startPosition, startPosition + state.Line.End),
|
||||
//BeforeWhitespace = state.PopBeforeWhitespace(startPosition - 1),
|
||||
LinesBefore = state.UseLinesBefore(),
|
||||
NewLine = state.Line.NewLine,
|
||||
});
|
||||
};
|
||||
|
||||
if (state.TrackTrivia)
|
||||
{
|
||||
htmlBlock.LinesBefore = state.UseLinesBefore();
|
||||
htmlBlock.NewLine = state.Line.NewLine;
|
||||
}
|
||||
|
||||
state.NewBlocks.Push(htmlBlock);
|
||||
return BlockState.Continue;
|
||||
}
|
||||
|
||||
private static readonly CompactPrefixTree<int> HtmlTags = new(65, 93, 82)
|
||||
private static readonly CompactPrefixTree<int> HtmlTags = new(66, 94, 83)
|
||||
{
|
||||
{ "address", 0 },
|
||||
{ "article", 1 },
|
||||
@@ -341,15 +356,16 @@ namespace Markdig.Parsers
|
||||
{ "style", 53 }, // <=== special group 1
|
||||
{ "summary", 54 },
|
||||
{ "table", 55 },
|
||||
{ "tbody", 56 },
|
||||
{ "td", 57 },
|
||||
{ "tfoot", 58 },
|
||||
{ "th", 59 },
|
||||
{ "thead", 60 },
|
||||
{ "title", 61 },
|
||||
{ "tr", 62 },
|
||||
{ "track", 63 },
|
||||
{ "ul", 64 }
|
||||
{ "textarea", 56 }, // <=== special group 1
|
||||
{ "tbody", 57 },
|
||||
{ "td", 58 },
|
||||
{ "tfoot", 59 },
|
||||
{ "th", 60 },
|
||||
{ "thead", 61 },
|
||||
{ "title", 62 },
|
||||
{ "tr", 63 },
|
||||
{ "track", 64 },
|
||||
{ "ul", 65 }
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ namespace Markdig.Parsers
|
||||
{
|
||||
public override bool CanInterrupt(BlockProcessor processor, Block block)
|
||||
{
|
||||
return !(block is ParagraphBlock);
|
||||
return !block.IsParagraphBlock;
|
||||
}
|
||||
|
||||
public override BlockState TryOpen(BlockProcessor processor)
|
||||
@@ -36,9 +36,14 @@ namespace Markdig.Parsers
|
||||
{
|
||||
Column = processor.Column,
|
||||
Span = new SourceSpan(processor.Start, processor.Line.End),
|
||||
LinesBefore = processor.UseLinesBefore(),
|
||||
NewLine = processor.Line.NewLine,
|
||||
};
|
||||
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
codeBlock.LinesBefore = processor.UseLinesBefore();
|
||||
codeBlock.NewLine = processor.Line.NewLine;
|
||||
}
|
||||
|
||||
var codeBlockLine = new CodeBlockLine
|
||||
{
|
||||
TriviaBefore = processor.UseTrivia(sourceStartPosition - 1)
|
||||
@@ -68,8 +73,12 @@ namespace Markdig.Parsers
|
||||
if (line.Slice.IsEmpty)
|
||||
{
|
||||
codeBlock.Lines.RemoveAt(i);
|
||||
processor.LinesBefore ??= new List<StringSlice>();
|
||||
processor.LinesBefore.Add(line.Slice);
|
||||
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
processor.LinesBefore ??= new List<StringSlice>();
|
||||
processor.LinesBefore.Add(line.Slice);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -92,12 +101,15 @@ namespace Markdig.Parsers
|
||||
|
||||
// lines
|
||||
var cb = (CodeBlock)block;
|
||||
var codeBlockLine = new CodeBlockLine
|
||||
{
|
||||
TriviaBefore = processor.UseTrivia(processor.Start - 1)
|
||||
};
|
||||
var codeBlockLine = new CodeBlockLine();
|
||||
|
||||
cb.CodeBlockLines.Add(codeBlockLine);
|
||||
cb.NewLine = processor.Line.NewLine; // ensure block newline is last newline
|
||||
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
codeBlockLine.TriviaBefore = processor.UseTrivia(processor.Start - 1);
|
||||
cb.NewLine = processor.Line.NewLine; // ensure block newline is last newline
|
||||
}
|
||||
}
|
||||
|
||||
return BlockState.Continue;
|
||||
|
||||
@@ -6,6 +6,8 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using Markdig.Helpers;
|
||||
using Markdig.Parsers.Inlines;
|
||||
using Markdig.Syntax;
|
||||
@@ -112,11 +114,6 @@ namespace Markdig.Parsers
|
||||
/// </summary>
|
||||
public LiteralInlineParser LiteralInlineParser { get; } = new();
|
||||
|
||||
public int GetSourcePosition(int sliceOffset)
|
||||
{
|
||||
return GetSourcePosition(sliceOffset, out _, out _);
|
||||
}
|
||||
|
||||
public SourceSpan GetSourcePositionFromLocalSpan(SourceSpan span)
|
||||
{
|
||||
if (span.IsEmpty)
|
||||
@@ -124,7 +121,7 @@ namespace Markdig.Parsers
|
||||
return SourceSpan.Empty;
|
||||
}
|
||||
|
||||
return new SourceSpan(GetSourcePosition(span.Start, out _, out _), GetSourcePosition(span.End, out _, out _));
|
||||
return new SourceSpan(GetSourcePosition(span.Start), GetSourcePosition(span.End));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -141,17 +138,26 @@ namespace Markdig.Parsers
|
||||
int position = 0;
|
||||
if (PreciseSourceLocation)
|
||||
{
|
||||
#if NET
|
||||
var offsets = CollectionsMarshal.AsSpan(lineOffsets);
|
||||
|
||||
for (; (uint)lineIndex < (uint)offsets.Length; lineIndex++)
|
||||
{
|
||||
ref var lineOffset = ref offsets[lineIndex];
|
||||
#else
|
||||
for (; lineIndex < lineOffsets.Count; lineIndex++)
|
||||
{
|
||||
var lineOffset = lineOffsets[lineIndex];
|
||||
#endif
|
||||
|
||||
if (sliceOffset <= lineOffset.End)
|
||||
{
|
||||
// Use the beginning of the line as a previous slice offset
|
||||
// (since it is on the same line)
|
||||
previousSliceOffset = lineOffsets[lineIndex].Start;
|
||||
previousSliceOffset = lineOffset.Start;
|
||||
var delta = sliceOffset - previousSliceOffset;
|
||||
column = lineOffsets[lineIndex].Column + delta;
|
||||
position = lineOffset.LinePosition + delta + lineOffsets[lineIndex].Offset;
|
||||
column = lineOffset.Column + delta;
|
||||
position = lineOffset.LinePosition + delta + lineOffset.Offset;
|
||||
previousLineIndexForSliceOffset = lineIndex;
|
||||
|
||||
// Return an absolute line index
|
||||
@@ -163,6 +169,41 @@ namespace Markdig.Parsers
|
||||
return position;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the source position for the specified offset within the current slice.
|
||||
/// </summary>
|
||||
/// <param name="sliceOffset">The slice offset.</param>
|
||||
/// <returns>The source position</returns>
|
||||
public int GetSourcePosition(int sliceOffset)
|
||||
{
|
||||
if (PreciseSourceLocation)
|
||||
{
|
||||
int lineIndex = sliceOffset >= previousSliceOffset ? previousLineIndexForSliceOffset : 0;
|
||||
|
||||
#if NET
|
||||
var offsets = CollectionsMarshal.AsSpan(lineOffsets);
|
||||
|
||||
for (; (uint)lineIndex < (uint)offsets.Length; lineIndex++)
|
||||
{
|
||||
ref var lineOffset = ref offsets[lineIndex];
|
||||
#else
|
||||
for (; lineIndex < lineOffsets.Count; lineIndex++)
|
||||
{
|
||||
var lineOffset = lineOffsets[lineIndex];
|
||||
#endif
|
||||
|
||||
if (sliceOffset <= lineOffset.End)
|
||||
{
|
||||
previousLineIndexForSliceOffset = lineIndex;
|
||||
previousSliceOffset = lineOffset.Start;
|
||||
|
||||
return sliceOffset - lineOffset.Start + lineOffset.LinePosition + lineOffset.Offset;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Processes the inline of the specified <see cref="LeafBlock"/>.
|
||||
/// </summary>
|
||||
@@ -321,9 +362,10 @@ namespace Markdig.Parsers
|
||||
var container = Block!.Inline!;
|
||||
for (int depth = 0; ; depth++)
|
||||
{
|
||||
if (container.LastChild is ContainerInline nextContainer && !nextContainer.IsClosed)
|
||||
Inline? lastChild = container.LastChild;
|
||||
if (lastChild is not null && lastChild.IsContainerInline && !lastChild.IsClosed)
|
||||
{
|
||||
container = nextContainer;
|
||||
container = Unsafe.As<ContainerInline>(lastChild);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -6,6 +6,7 @@ using Markdig.Helpers;
|
||||
using Markdig.Syntax;
|
||||
using Markdig.Syntax.Inlines;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Markdig.Parsers.Inlines
|
||||
{
|
||||
@@ -40,7 +41,7 @@ namespace Markdig.Parsers.Inlines
|
||||
|
||||
char c = slice.CurrentChar;
|
||||
|
||||
var builder = StringBuilderCache.Local();
|
||||
var builder = new ValueStringBuilder(stackalloc char[ValueStringBuilder.StackallocThreshold]);
|
||||
|
||||
// A backtick string is a string of one or more backtick characters (`) that is neither preceded nor followed by a backtick.
|
||||
// A code span begins with a backtick string and ends with a backtick string of equal length.
|
||||
@@ -54,6 +55,7 @@ namespace Markdig.Parsers.Inlines
|
||||
// whitespace from the opening or closing backtick strings.
|
||||
|
||||
bool allSpace = true;
|
||||
bool containsNewLine = false;
|
||||
var contentEnd = -1;
|
||||
|
||||
while (c != '\0')
|
||||
@@ -61,10 +63,12 @@ namespace Markdig.Parsers.Inlines
|
||||
// Transform '\n' into a single space
|
||||
if (c == '\n')
|
||||
{
|
||||
containsNewLine = true;
|
||||
c = ' ';
|
||||
}
|
||||
else if (c == '\r')
|
||||
{
|
||||
containsNewLine = true;
|
||||
slice.SkipChar();
|
||||
c = slice.CurrentChar;
|
||||
continue;
|
||||
@@ -98,33 +102,43 @@ namespace Markdig.Parsers.Inlines
|
||||
bool isMatching = false;
|
||||
if (closeSticks == openSticks)
|
||||
{
|
||||
string content;
|
||||
ReadOnlySpan<char> contentSpan = builder.AsSpan();
|
||||
|
||||
var content = containsNewLine
|
||||
? new LazySubstring(contentSpan.ToString())
|
||||
: new LazySubstring(slice.Text, contentStart, contentSpan.Length);
|
||||
|
||||
Debug.Assert(contentSpan.SequenceEqual(content.AsSpan()));
|
||||
|
||||
// Remove one space from front and back if the string is not all spaces
|
||||
if (!allSpace && builder.Length > 2 && builder[0] == ' ' && builder[builder.Length - 1] == ' ')
|
||||
if (!allSpace && contentSpan.Length > 2 && contentSpan[0] == ' ' && contentSpan[contentSpan.Length - 1] == ' ')
|
||||
{
|
||||
content = builder.ToString(1, builder.Length - 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
content = builder.ToString();
|
||||
content.Offset++;
|
||||
content.Length -= 2;
|
||||
}
|
||||
|
||||
int delimiterCount = Math.Min(openSticks, closeSticks);
|
||||
var spanStart = processor.GetSourcePosition(startPosition, out int line, out int column);
|
||||
var spanEnd = processor.GetSourcePosition(slice.Start - 1);
|
||||
processor.Inline = new CodeInline(content)
|
||||
var codeInline = new CodeInline(content)
|
||||
{
|
||||
Delimiter = match,
|
||||
ContentWithTrivia = new StringSlice(slice.Text, contentStart, contentEnd - 1),
|
||||
Span = new SourceSpan(spanStart, spanEnd),
|
||||
Line = line,
|
||||
Column = column,
|
||||
DelimiterCount = delimiterCount,
|
||||
};
|
||||
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
codeInline.ContentWithTrivia = new StringSlice(slice.Text, contentStart, contentEnd - 1);
|
||||
}
|
||||
|
||||
processor.Inline = codeInline;
|
||||
isMatching = true;
|
||||
}
|
||||
|
||||
builder.Dispose();
|
||||
return isMatching;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Markdig.Helpers;
|
||||
using Markdig.Renderers.Html;
|
||||
using Markdig.Syntax;
|
||||
@@ -91,11 +92,13 @@ namespace Markdig.Parsers.Inlines
|
||||
|
||||
public bool PostProcess(InlineProcessor state, Inline? root, Inline? lastChild, int postInlineProcessorIndex, bool isFinalProcessing)
|
||||
{
|
||||
if (!(root is ContainerInline container))
|
||||
if (root is null || !root.IsContainerInline)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
ContainerInline container = Unsafe.As<ContainerInline>(root);
|
||||
|
||||
List<EmphasisDelimiterInline>? delimiters = null;
|
||||
if (container is EmphasisDelimiterInline emphasisDelimiter)
|
||||
{
|
||||
@@ -113,16 +116,22 @@ namespace Markdig.Parsers.Inlines
|
||||
break;
|
||||
}
|
||||
|
||||
// If we have a delimiter, we search into it as we should have a tree of EmphasisDelimiterInline
|
||||
if (child is EmphasisDelimiterInline delimiter)
|
||||
if (child.IsContainer && child is DelimiterInline delimiterInline)
|
||||
{
|
||||
delimiters ??= inlinesCache.Get();
|
||||
delimiters.Add(delimiter);
|
||||
child = delimiter.FirstChild;
|
||||
continue;
|
||||
}
|
||||
// If we have a delimiter, we search into it as we should have a tree of EmphasisDelimiterInline
|
||||
if (delimiterInline is EmphasisDelimiterInline delimiter)
|
||||
{
|
||||
delimiters ??= inlinesCache.Get();
|
||||
delimiters.Add(delimiter);
|
||||
}
|
||||
|
||||
child = child.NextSibling;
|
||||
// Follow DelimiterInline (EmphasisDelimiter, TableDelimiter...)
|
||||
child = delimiterInline.FirstChild;
|
||||
}
|
||||
else
|
||||
{
|
||||
child = child.NextSibling;
|
||||
}
|
||||
}
|
||||
|
||||
if (delimiters != null)
|
||||
|
||||
@@ -41,45 +41,35 @@ namespace Markdig.Parsers.Inlines
|
||||
}
|
||||
|
||||
// A backslash at the end of the line is a [hard line break]:
|
||||
if (processor.TrackTrivia)
|
||||
if (c == '\n' || c == '\r')
|
||||
{
|
||||
if (c == '\n' || c == '\r')
|
||||
var newLine = c == '\n' ? NewLine.LineFeed : NewLine.CarriageReturn;
|
||||
if (c == '\r' && slice.PeekChar() == '\n')
|
||||
{
|
||||
var newLine = c == '\n' ? NewLine.LineFeed : NewLine.CarriageReturn;
|
||||
if (c == '\r' && slice.PeekChar() == '\n')
|
||||
{
|
||||
newLine = NewLine.CarriageReturnLineFeed;
|
||||
}
|
||||
processor.Inline = new LineBreakInline()
|
||||
{
|
||||
IsHard = true,
|
||||
IsBackslash = true,
|
||||
Span = { Start = processor.GetSourcePosition(startPosition, out line, out column) },
|
||||
Line = line,
|
||||
Column = column,
|
||||
NewLine = newLine
|
||||
};
|
||||
processor.Inline.Span.End = processor.Inline.Span.Start + 1;
|
||||
slice.SkipChar();
|
||||
return true;
|
||||
newLine = NewLine.CarriageReturnLineFeed;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (c == '\n' || c == '\r')
|
||||
var inline = new LineBreakInline()
|
||||
{
|
||||
processor.Inline = new LineBreakInline()
|
||||
{
|
||||
IsHard = true,
|
||||
IsBackslash = true,
|
||||
Span = { Start = processor.GetSourcePosition(startPosition, out line, out column) },
|
||||
Line = line,
|
||||
Column = column
|
||||
};
|
||||
processor.Inline.Span.End = processor.Inline.Span.Start + 1;
|
||||
slice.SkipChar();
|
||||
return true;
|
||||
IsHard = true,
|
||||
IsBackslash = true,
|
||||
Span = { Start = processor.GetSourcePosition(startPosition, out line, out column) },
|
||||
Line = line,
|
||||
Column = column,
|
||||
};
|
||||
processor.Inline = inline;
|
||||
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
inline.NewLine = newLine;
|
||||
}
|
||||
|
||||
inline.Span.End = inline.Span.Start + 1;
|
||||
slice.SkipChar(); // Skip \n or \r alone
|
||||
if (newLine == NewLine.CarriageReturnLineFeed)
|
||||
{
|
||||
slice.SkipChar(); // Skip \r\n
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace Markdig.Parsers.Inlines
|
||||
public override bool Match(InlineProcessor processor, ref StringSlice slice)
|
||||
{
|
||||
// Hard line breaks are for separating inline content within a block. Neither syntax for hard line breaks works at the end of a paragraph or other block element:
|
||||
if (!(processor.Block is ParagraphBlock))
|
||||
if (!processor.Block!.IsParagraphBlock)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
using Markdig.Helpers;
|
||||
using Markdig.Syntax;
|
||||
using Markdig.Syntax.Inlines;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Markdig.Parsers.Inlines
|
||||
{
|
||||
@@ -78,18 +79,23 @@ namespace Markdig.Parsers.Inlines
|
||||
|
||||
// Else we insert a LinkDelimiter
|
||||
slice.SkipChar();
|
||||
var labelWithTrivia = new StringSlice(slice.Text, labelWithTriviaSpan.Start, labelWithTriviaSpan.End);
|
||||
processor.Inline = new LinkDelimiterInline(this)
|
||||
var linkDelimiter = new LinkDelimiterInline(this)
|
||||
{
|
||||
Type = DelimiterType.Open,
|
||||
Label = label,
|
||||
LabelWithTrivia = labelWithTrivia,
|
||||
LabelSpan = processor.GetSourcePositionFromLocalSpan(labelSpan),
|
||||
IsImage = isImage,
|
||||
Span = new SourceSpan(startPosition, processor.GetSourcePosition(slice.Start - 1)),
|
||||
Line = line,
|
||||
Column = column
|
||||
};
|
||||
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
linkDelimiter.LabelWithTrivia = new StringSlice(slice.Text, labelWithTriviaSpan.Start, labelWithTriviaSpan.End);
|
||||
}
|
||||
|
||||
processor.Inline = linkDelimiter;
|
||||
return true;
|
||||
|
||||
case ']':
|
||||
@@ -137,18 +143,13 @@ namespace Markdig.Parsers.Inlines
|
||||
// Create a default link if the callback was not found
|
||||
if (link is null)
|
||||
{
|
||||
var labelWithTrivia = new StringSlice(text.Text, labelWithriviaSpan.Start, labelWithriviaSpan.End);
|
||||
// Inline Link
|
||||
link = new LinkInline()
|
||||
var linkInline = new LinkInline()
|
||||
{
|
||||
Url = HtmlHelper.Unescape(linkRef.Url),
|
||||
Title = HtmlHelper.Unescape(linkRef.Title),
|
||||
Label = label,
|
||||
LabelSpan = labelSpan,
|
||||
LabelWithTrivia = labelWithTrivia,
|
||||
LinkRefDefLabel = linkRef.Label,
|
||||
LinkRefDefLabelWithTrivia = linkRef.LabelWithTrivia,
|
||||
LocalLabel = localLabel,
|
||||
UrlSpan = linkRef.UrlSpan,
|
||||
IsImage = parent.IsImage,
|
||||
IsShortcut = isShortcut,
|
||||
@@ -157,6 +158,16 @@ namespace Markdig.Parsers.Inlines
|
||||
Line = parent.Line,
|
||||
Column = parent.Column,
|
||||
};
|
||||
|
||||
if (state.TrackTrivia)
|
||||
{
|
||||
linkInline.LabelWithTrivia = new StringSlice(text.Text, labelWithriviaSpan.Start, labelWithriviaSpan.End);
|
||||
linkInline.LinkRefDefLabel = linkRef.Label;
|
||||
linkInline.LinkRefDefLabelWithTrivia = linkRef.LabelWithTrivia;
|
||||
linkInline.LocalLabel = localLabel;
|
||||
}
|
||||
|
||||
link = linkInline;
|
||||
}
|
||||
|
||||
if (link is ContainerInline containerLink)
|
||||
@@ -233,74 +244,18 @@ namespace Markdig.Parsers.Inlines
|
||||
|
||||
if (text.CurrentChar == '(')
|
||||
{
|
||||
LinkInline? link = null;
|
||||
|
||||
if (inlineState.TrackTrivia)
|
||||
{
|
||||
if (LinkHelper.TryParseInlineLinkTrivia(
|
||||
ref text,
|
||||
out string? url,
|
||||
out SourceSpan unescapedUrlSpan,
|
||||
out string? title,
|
||||
out SourceSpan unescapedTitleSpan,
|
||||
out char titleEnclosingCharacter,
|
||||
out SourceSpan linkSpan,
|
||||
out SourceSpan titleSpan,
|
||||
out SourceSpan triviaBeforeLink,
|
||||
out SourceSpan triviaAfterLink,
|
||||
out SourceSpan triviaAfterTitle,
|
||||
out bool urlHasPointyBrackets))
|
||||
{
|
||||
var wsBeforeLink = new StringSlice(text.Text, triviaBeforeLink.Start, triviaBeforeLink.End);
|
||||
var wsAfterLink = new StringSlice(text.Text, triviaAfterLink.Start, triviaAfterLink.End);
|
||||
var wsAfterTitle = new StringSlice(text.Text, triviaAfterTitle.Start, triviaAfterTitle.End);
|
||||
var unescapedUrl = new StringSlice(text.Text, unescapedUrlSpan.Start, unescapedUrlSpan.End);
|
||||
var unescapedTitle = new StringSlice(text.Text, unescapedTitleSpan.Start, unescapedTitleSpan.End);
|
||||
// Inline Link
|
||||
var link = new LinkInline()
|
||||
{
|
||||
TriviaBeforeUrl = wsBeforeLink,
|
||||
Url = HtmlHelper.Unescape(url),
|
||||
UnescapedUrl = unescapedUrl,
|
||||
UrlHasPointyBrackets = urlHasPointyBrackets,
|
||||
TriviaAfterUrl = wsAfterLink,
|
||||
Title = HtmlHelper.Unescape(title),
|
||||
UnescapedTitle = unescapedTitle,
|
||||
TitleEnclosingCharacter = titleEnclosingCharacter,
|
||||
TriviaAfterTitle = wsAfterTitle,
|
||||
IsImage = openParent.IsImage,
|
||||
LabelSpan = openParent.LabelSpan,
|
||||
UrlSpan = inlineState.GetSourcePositionFromLocalSpan(linkSpan),
|
||||
TitleSpan = inlineState.GetSourcePositionFromLocalSpan(titleSpan),
|
||||
Span = new SourceSpan(openParent.Span.Start, inlineState.GetSourcePosition(text.Start - 1)),
|
||||
Line = openParent.Line,
|
||||
Column = openParent.Column,
|
||||
};
|
||||
|
||||
openParent.ReplaceBy(link);
|
||||
// Notifies processor as we are creating an inline locally
|
||||
inlineState.Inline = link;
|
||||
|
||||
// Process emphasis delimiters
|
||||
inlineState.PostProcessInlines(0, link, null, false);
|
||||
|
||||
// If we have a link (and not an image),
|
||||
// we also set all [ delimiters before the opening delimiter to inactive.
|
||||
// (This will prevent us from getting links within links.)
|
||||
if (!openParent.IsImage)
|
||||
{
|
||||
MarkParentAsInactive(parentDelimiter);
|
||||
}
|
||||
|
||||
link.IsClosed = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
link = TryParseInlineLinkTrivia(ref text, inlineState, openParent);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (LinkHelper.TryParseInlineLink(ref text, out string? url, out string? title, out SourceSpan linkSpan, out SourceSpan titleSpan))
|
||||
{
|
||||
// Inline Link
|
||||
var link = new LinkInline()
|
||||
link = new LinkInline()
|
||||
{
|
||||
Url = HtmlHelper.Unescape(url),
|
||||
Title = HtmlHelper.Unescape(title),
|
||||
@@ -312,34 +267,36 @@ namespace Markdig.Parsers.Inlines
|
||||
Line = openParent.Line,
|
||||
Column = openParent.Column,
|
||||
};
|
||||
|
||||
openParent.ReplaceBy(link);
|
||||
// Notifies processor as we are creating an inline locally
|
||||
inlineState.Inline = link;
|
||||
|
||||
// Process emphasis delimiters
|
||||
inlineState.PostProcessInlines(0, link, null, false);
|
||||
|
||||
// If we have a link (and not an image),
|
||||
// we also set all [ delimiters before the opening delimiter to inactive.
|
||||
// (This will prevent us from getting links within links.)
|
||||
if (!openParent.IsImage)
|
||||
{
|
||||
MarkParentAsInactive(parentDelimiter);
|
||||
}
|
||||
|
||||
link.IsClosed = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (link is not null)
|
||||
{
|
||||
openParent.ReplaceBy(link);
|
||||
// Notifies processor as we are creating an inline locally
|
||||
inlineState.Inline = link;
|
||||
|
||||
// Process emphasis delimiters
|
||||
inlineState.PostProcessInlines(0, link, null, false);
|
||||
|
||||
// If we have a link (and not an image),
|
||||
// we also set all [ delimiters before the opening delimiter to inactive.
|
||||
// (This will prevent us from getting links within links.)
|
||||
if (!openParent.IsImage)
|
||||
{
|
||||
MarkParentAsInactive(parentDelimiter);
|
||||
}
|
||||
|
||||
link.IsClosed = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
text = savedText;
|
||||
}
|
||||
|
||||
var labelSpan = SourceSpan.Empty;
|
||||
string? label = null;
|
||||
SourceSpan labelWithTrivia = SourceSpan.Empty;
|
||||
bool isLabelSpanLocal = true;
|
||||
|
||||
bool isShortcut = false;
|
||||
@@ -363,9 +320,10 @@ namespace Markdig.Parsers.Inlines
|
||||
label = openParent.Label;
|
||||
isShortcut = true;
|
||||
}
|
||||
|
||||
if (label != null || LinkHelper.TryParseLabelTrivia(ref text, true, out label, out labelSpan))
|
||||
{
|
||||
labelWithTrivia = new SourceSpan(labelSpan.Start, labelSpan.End);
|
||||
SourceSpan labelWithTrivia = new SourceSpan(labelSpan.Start, labelSpan.End);
|
||||
if (isLabelSpanLocal)
|
||||
{
|
||||
labelSpan = inlineState.GetSourcePositionFromLocalSpan(labelSpan);
|
||||
@@ -399,9 +357,55 @@ namespace Markdig.Parsers.Inlines
|
||||
|
||||
inlineState.Inline = openParent.ReplaceBy(literal);
|
||||
return false;
|
||||
|
||||
static LinkInline? TryParseInlineLinkTrivia(ref StringSlice text, InlineProcessor inlineState, LinkDelimiterInline openParent)
|
||||
{
|
||||
if (LinkHelper.TryParseInlineLinkTrivia(
|
||||
ref text,
|
||||
out string? url,
|
||||
out SourceSpan unescapedUrlSpan,
|
||||
out string? title,
|
||||
out SourceSpan unescapedTitleSpan,
|
||||
out char titleEnclosingCharacter,
|
||||
out SourceSpan linkSpan,
|
||||
out SourceSpan titleSpan,
|
||||
out SourceSpan triviaBeforeLink,
|
||||
out SourceSpan triviaAfterLink,
|
||||
out SourceSpan triviaAfterTitle,
|
||||
out bool urlHasPointyBrackets))
|
||||
{
|
||||
var wsBeforeLink = new StringSlice(text.Text, triviaBeforeLink.Start, triviaBeforeLink.End);
|
||||
var wsAfterLink = new StringSlice(text.Text, triviaAfterLink.Start, triviaAfterLink.End);
|
||||
var wsAfterTitle = new StringSlice(text.Text, triviaAfterTitle.Start, triviaAfterTitle.End);
|
||||
var unescapedUrl = new StringSlice(text.Text, unescapedUrlSpan.Start, unescapedUrlSpan.End);
|
||||
var unescapedTitle = new StringSlice(text.Text, unescapedTitleSpan.Start, unescapedTitleSpan.End);
|
||||
|
||||
return new LinkInline()
|
||||
{
|
||||
TriviaBeforeUrl = wsBeforeLink,
|
||||
Url = HtmlHelper.Unescape(url),
|
||||
UnescapedUrl = unescapedUrl,
|
||||
UrlHasPointyBrackets = urlHasPointyBrackets,
|
||||
TriviaAfterUrl = wsAfterLink,
|
||||
Title = HtmlHelper.Unescape(title),
|
||||
UnescapedTitle = unescapedTitle,
|
||||
TitleEnclosingCharacter = titleEnclosingCharacter,
|
||||
TriviaAfterTitle = wsAfterTitle,
|
||||
IsImage = openParent.IsImage,
|
||||
LabelSpan = openParent.LabelSpan,
|
||||
UrlSpan = inlineState.GetSourcePositionFromLocalSpan(linkSpan),
|
||||
TitleSpan = inlineState.GetSourcePositionFromLocalSpan(titleSpan),
|
||||
Span = new SourceSpan(openParent.Span.Start, inlineState.GetSourcePosition(text.Start - 1)),
|
||||
Line = openParent.Line,
|
||||
Column = openParent.Column,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void MarkParentAsInactive(Inline? inline)
|
||||
private static void MarkParentAsInactive(Inline? inline)
|
||||
{
|
||||
while (inline != null)
|
||||
{
|
||||
|
||||
@@ -68,12 +68,14 @@ namespace Markdig.Parsers.Inlines
|
||||
// The LiteralInlineParser is always matching (at least an empty string)
|
||||
var endPosition = slice.Start + length - 1;
|
||||
|
||||
if (processor.Inline is LiteralInline previousInline
|
||||
&& ReferenceEquals(previousInline.Content.Text, slice.Text)
|
||||
&& previousInline.Content.End + 1 == slice.Start)
|
||||
if (processor.Inline is { } previousInline
|
||||
&& !previousInline.IsContainer
|
||||
&& processor.Inline is LiteralInline previousLiteral
|
||||
&& ReferenceEquals(previousLiteral.Content.Text, slice.Text)
|
||||
&& previousLiteral.Content.End + 1 == slice.Start)
|
||||
{
|
||||
previousInline.Content.End = endPosition;
|
||||
previousInline.Span.End = processor.GetSourcePosition(endPosition);
|
||||
previousLiteral.Content.End = endPosition;
|
||||
previousLiteral.Span.End = processor.GetSourcePosition(endPosition);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -86,15 +86,19 @@ namespace Markdig.Parsers
|
||||
// interpretations of a line, the thematic break takes precedence
|
||||
BlockState result;
|
||||
var thematicParser = ThematicBreakParser.Default;
|
||||
if (!(processor.LastBlock is FencedCodeBlock) && thematicParser.HasOpeningCharacter(processor.CurrentChar))
|
||||
if (processor.LastBlock is not FencedCodeBlock && thematicParser.HasOpeningCharacter(processor.CurrentChar))
|
||||
{
|
||||
result = thematicParser.TryOpen(processor);
|
||||
if (result.IsBreak())
|
||||
{
|
||||
// TODO: We remove the thematic break, as it will be created later, but this is inefficient, try to find another way
|
||||
var thematicBreak = processor.NewBlocks.Pop();
|
||||
var linesBefore = thematicBreak.LinesBefore;
|
||||
processor.LinesBefore = linesBefore;
|
||||
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
processor.LinesBefore = thematicBreak.LinesBefore;
|
||||
}
|
||||
|
||||
return BlockState.None;
|
||||
}
|
||||
}
|
||||
@@ -259,10 +263,11 @@ namespace Markdig.Parsers
|
||||
// Starts/continue the list unless:
|
||||
// - an empty list item follows a paragraph
|
||||
// - an ordered list is not starting by '1'
|
||||
if ((block ?? state.LastBlock) is ParagraphBlock previousParagraph)
|
||||
block ??= state.LastBlock;
|
||||
if (block is not null && block.IsParagraphBlock)
|
||||
{
|
||||
if (state.IsBlankLine ||
|
||||
state.IsOpen(previousParagraph) && listInfo.BulletType == '1' && listInfo.OrderedStart is not "1")
|
||||
state.IsOpen(block) && listInfo.BulletType == '1' && listInfo.OrderedStart is not "1")
|
||||
{
|
||||
state.GoToColumn(initColumn);
|
||||
state.TriviaStart = savedTriviaStart; // restore changed TriviaStart state
|
||||
@@ -276,12 +281,17 @@ namespace Markdig.Parsers
|
||||
Column = initColumn,
|
||||
ColumnWidth = columnWidth,
|
||||
Order = order,
|
||||
SourceBullet = listInfo.SourceBullet,
|
||||
TriviaBefore = triviaBefore,
|
||||
Span = new SourceSpan(sourcePosition, sourceEndPosition),
|
||||
LinesBefore = state.UseLinesBefore(),
|
||||
NewLine = state.Line.NewLine,
|
||||
};
|
||||
|
||||
if (state.TrackTrivia)
|
||||
{
|
||||
newListItem.TriviaBefore = triviaBefore;
|
||||
newListItem.LinesBefore = state.UseLinesBefore();
|
||||
newListItem.NewLine = state.Line.NewLine;
|
||||
newListItem.SourceBullet = listInfo.SourceBullet;
|
||||
}
|
||||
|
||||
state.NewBlocks.Push(newListItem);
|
||||
|
||||
if (currentParent != null)
|
||||
@@ -313,8 +323,13 @@ namespace Markdig.Parsers
|
||||
OrderedDelimiter = listInfo.OrderedDelimiter,
|
||||
DefaultOrderedStart = listInfo.DefaultOrderedStart,
|
||||
OrderedStart = listInfo.OrderedStart,
|
||||
LinesBefore = state.UseLinesBefore(),
|
||||
};
|
||||
|
||||
if (state.TrackTrivia)
|
||||
{
|
||||
newList.LinesBefore = state.UseLinesBefore();
|
||||
}
|
||||
|
||||
state.NewBlocks.Push(newList);
|
||||
}
|
||||
return BlockState.Continue;
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Markdig.Helpers;
|
||||
using Markdig.Syntax;
|
||||
|
||||
@@ -43,8 +44,8 @@ namespace Markdig.Parsers
|
||||
|
||||
if (pipeline.PreciseSourceLocation)
|
||||
{
|
||||
int roughLineCountEstimate = text.Length / 40;
|
||||
roughLineCountEstimate = Math.Min(4, Math.Max(512, roughLineCountEstimate));
|
||||
int roughLineCountEstimate = text.Length / 32;
|
||||
roughLineCountEstimate = Math.Max(4, Math.Min(512, roughLineCountEstimate));
|
||||
document.LineStartIndexes = new List<int>(roughLineCountEstimate);
|
||||
}
|
||||
|
||||
@@ -149,8 +150,9 @@ namespace Markdig.Parsers
|
||||
for (; item.Index < container.Count; item.Index++)
|
||||
{
|
||||
var block = container[item.Index];
|
||||
if (block is LeafBlock leafBlock)
|
||||
if (block.IsLeafBlock)
|
||||
{
|
||||
LeafBlock leafBlock = Unsafe.As<LeafBlock>(block);
|
||||
leafBlock.OnProcessInlinesBegin(inlineProcessor);
|
||||
if (leafBlock.ProcessInlines)
|
||||
{
|
||||
@@ -167,10 +169,10 @@ namespace Markdig.Parsers
|
||||
}
|
||||
leafBlock.OnProcessInlinesEnd(inlineProcessor);
|
||||
}
|
||||
else if (block is ContainerBlock newContainer)
|
||||
else if (block.IsContainerBlock)
|
||||
{
|
||||
// If we need to remove it
|
||||
if (newContainer.RemoveAfterProcessInlines)
|
||||
if (block.RemoveAfterProcessInlines)
|
||||
{
|
||||
container.RemoveAt(item.Index);
|
||||
}
|
||||
@@ -185,8 +187,8 @@ namespace Markdig.Parsers
|
||||
Array.Resize(ref blocks, blockCount * 2);
|
||||
ThrowHelper.CheckDepthLimit(blocks.Length);
|
||||
}
|
||||
blocks[blockCount++] = new ContainerItem(newContainer);
|
||||
newContainer.OnProcessInlinesBegin(inlineProcessor);
|
||||
blocks[blockCount++] = new ContainerItem(Unsafe.As<ContainerBlock>(block));
|
||||
block.OnProcessInlinesBegin(inlineProcessor);
|
||||
goto process_new_block;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using Markdig.Helpers;
|
||||
using System;
|
||||
|
||||
namespace Markdig.Parsers
|
||||
{
|
||||
@@ -56,7 +57,16 @@ namespace Markdig.Parsers
|
||||
return false;
|
||||
}
|
||||
|
||||
result.OrderedStart = state.Line.Text.Substring(startChar, endChar - startChar + 1);
|
||||
if (startChar == endChar)
|
||||
{
|
||||
// Common case: a single digit character
|
||||
result.OrderedStart = CharHelper.SmallNumberToString(state.Line.Text[startChar] - '0');
|
||||
}
|
||||
else
|
||||
{
|
||||
result.OrderedStart = state.Line.Text.Substring(startChar, endChar - startChar + 1);
|
||||
}
|
||||
|
||||
result.OrderedDelimiter = orderedDelimiter;
|
||||
result.BulletType = '1';
|
||||
result.DefaultOrderedStart = "1";
|
||||
|
||||
@@ -23,13 +23,19 @@ namespace Markdig.Parsers
|
||||
}
|
||||
|
||||
// We continue trying to match by default
|
||||
processor.NewBlocks.Push(new ParagraphBlock(this)
|
||||
var paragraph = new ParagraphBlock(this)
|
||||
{
|
||||
Column = processor.Column,
|
||||
Span = new SourceSpan(processor.Line.Start, processor.Line.End),
|
||||
LinesBefore = processor.UseLinesBefore(),
|
||||
NewLine = processor.Line.NewLine,
|
||||
});
|
||||
};
|
||||
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
paragraph.LinesBefore = processor.UseLinesBefore();
|
||||
paragraph.NewLine = processor.Line.NewLine;
|
||||
}
|
||||
|
||||
processor.NewBlocks.Push(paragraph);
|
||||
return BlockState.Continue;
|
||||
}
|
||||
|
||||
@@ -128,15 +134,19 @@ namespace Markdig.Parsers
|
||||
Span = new SourceSpan(paragraph.Span.Start, line.Start),
|
||||
Level = level,
|
||||
Lines = paragraph.Lines,
|
||||
TriviaBefore = state.UseTrivia(sourcePosition - 1), // remove dashes
|
||||
TriviaAfter = new StringSlice(state.Line.Text, state.Start, line.End),
|
||||
LinesBefore = paragraph.LinesBefore,
|
||||
NewLine = state.Line.NewLine,
|
||||
IsSetext = true,
|
||||
HeaderCharCount = count,
|
||||
SetextNewline = paragraph.NewLine,
|
||||
};
|
||||
if (!state.TrackTrivia)
|
||||
|
||||
if (state.TrackTrivia)
|
||||
{
|
||||
heading.LinesBefore = paragraph.LinesBefore;
|
||||
heading.TriviaBefore = state.UseTrivia(sourcePosition - 1); // remove dashes
|
||||
heading.TriviaAfter = new StringSlice(state.Line.Text, state.Start, line.End);
|
||||
heading.NewLine = state.Line.NewLine;
|
||||
heading.SetextNewline = paragraph.NewLine;
|
||||
}
|
||||
else
|
||||
{
|
||||
heading.Lines.Trim();
|
||||
}
|
||||
|
||||
@@ -41,9 +41,13 @@ namespace Markdig.Parsers
|
||||
QuoteChar = quoteChar,
|
||||
Column = column,
|
||||
Span = new SourceSpan(sourcePosition, processor.Line.End),
|
||||
LinesBefore = processor.UseLinesBefore()
|
||||
};
|
||||
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
quoteBlock.LinesBefore = processor.UseLinesBefore();
|
||||
}
|
||||
|
||||
bool hasSpaceAfterQuoteChar = false;
|
||||
if (c == ' ')
|
||||
{
|
||||
@@ -56,28 +60,34 @@ namespace Markdig.Parsers
|
||||
processor.NextColumn();
|
||||
}
|
||||
|
||||
var triviaBefore = processor.UseTrivia(sourcePosition - 1);
|
||||
StringSlice triviaAfter = StringSlice.Empty;
|
||||
bool wasEmptyLine = false;
|
||||
if (processor.Line.IsEmptyOrWhitespace())
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
processor.TriviaStart = processor.Start;
|
||||
triviaAfter = processor.UseTrivia(processor.Line.End);
|
||||
wasEmptyLine = true;
|
||||
var triviaBefore = processor.UseTrivia(sourcePosition - 1);
|
||||
StringSlice triviaAfter = StringSlice.Empty;
|
||||
bool wasEmptyLine = false;
|
||||
if (processor.Line.IsEmptyOrWhitespace())
|
||||
{
|
||||
processor.TriviaStart = processor.Start;
|
||||
triviaAfter = processor.UseTrivia(processor.Line.End);
|
||||
wasEmptyLine = true;
|
||||
}
|
||||
|
||||
if (!wasEmptyLine)
|
||||
{
|
||||
processor.TriviaStart = processor.Start;
|
||||
}
|
||||
|
||||
quoteBlock.QuoteLines.Add(new QuoteBlockLine
|
||||
{
|
||||
TriviaBefore = triviaBefore,
|
||||
TriviaAfter = triviaAfter,
|
||||
QuoteChar = true,
|
||||
HasSpaceAfterQuoteChar = hasSpaceAfterQuoteChar,
|
||||
NewLine = processor.Line.NewLine,
|
||||
});
|
||||
}
|
||||
quoteBlock.QuoteLines.Add(new QuoteBlockLine
|
||||
{
|
||||
TriviaBefore = triviaBefore,
|
||||
TriviaAfter = triviaAfter,
|
||||
QuoteChar = true,
|
||||
HasSpaceAfterQuoteChar = hasSpaceAfterQuoteChar,
|
||||
NewLine = processor.Line.NewLine,
|
||||
});
|
||||
|
||||
processor.NewBlocks.Push(quoteBlock);
|
||||
if (!wasEmptyLine)
|
||||
{
|
||||
processor.TriviaStart = processor.Start;
|
||||
}
|
||||
return BlockState.Continue;
|
||||
}
|
||||
|
||||
@@ -94,7 +104,6 @@ namespace Markdig.Parsers
|
||||
// 5.1 Block quotes
|
||||
// A block quote marker consists of 0-3 spaces of initial indent, plus (a) the character > together with a following space, or (b) a single character > not followed by a space.
|
||||
var c = processor.CurrentChar;
|
||||
bool hasSpaceAfterQuoteChar = false;
|
||||
if (c != quote.QuoteChar)
|
||||
{
|
||||
if (processor.IsBlankLine)
|
||||
@@ -103,14 +112,19 @@ namespace Markdig.Parsers
|
||||
}
|
||||
else
|
||||
{
|
||||
quote.QuoteLines.Add(new QuoteBlockLine
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
QuoteChar = false,
|
||||
NewLine = processor.Line.NewLine,
|
||||
});
|
||||
quote.QuoteLines.Add(new QuoteBlockLine
|
||||
{
|
||||
QuoteChar = false,
|
||||
NewLine = processor.Line.NewLine,
|
||||
});
|
||||
}
|
||||
return BlockState.None;
|
||||
}
|
||||
}
|
||||
|
||||
bool hasSpaceAfterQuoteChar = false;
|
||||
c = processor.NextChar(); // Skip quote marker char
|
||||
if (c == ' ')
|
||||
{
|
||||
@@ -122,28 +136,33 @@ namespace Markdig.Parsers
|
||||
{
|
||||
processor.NextColumn();
|
||||
}
|
||||
var TriviaSpaceBefore = processor.UseTrivia(sourcePosition - 1);
|
||||
StringSlice triviaAfter = StringSlice.Empty;
|
||||
bool wasEmptyLine = false;
|
||||
if (processor.Line.IsEmptyOrWhitespace())
|
||||
{
|
||||
processor.TriviaStart = processor.Start;
|
||||
triviaAfter = processor.UseTrivia(processor.Line.End);
|
||||
wasEmptyLine = true;
|
||||
}
|
||||
quote.QuoteLines.Add(new QuoteBlockLine
|
||||
{
|
||||
QuoteChar = true,
|
||||
HasSpaceAfterQuoteChar = hasSpaceAfterQuoteChar,
|
||||
TriviaBefore = TriviaSpaceBefore,
|
||||
TriviaAfter = triviaAfter,
|
||||
NewLine = processor.Line.NewLine,
|
||||
});
|
||||
|
||||
if (!wasEmptyLine)
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
processor.TriviaStart = processor.Start;
|
||||
var triviaSpaceBefore = processor.UseTrivia(sourcePosition - 1);
|
||||
StringSlice triviaAfter = StringSlice.Empty;
|
||||
bool wasEmptyLine = false;
|
||||
if (processor.Line.IsEmptyOrWhitespace())
|
||||
{
|
||||
processor.TriviaStart = processor.Start;
|
||||
triviaAfter = processor.UseTrivia(processor.Line.End);
|
||||
wasEmptyLine = true;
|
||||
}
|
||||
quote.QuoteLines.Add(new QuoteBlockLine
|
||||
{
|
||||
QuoteChar = true,
|
||||
HasSpaceAfterQuoteChar = hasSpaceAfterQuoteChar,
|
||||
TriviaBefore = triviaSpaceBefore,
|
||||
TriviaAfter = triviaAfter,
|
||||
NewLine = processor.Line.NewLine,
|
||||
});
|
||||
|
||||
if (!wasEmptyLine)
|
||||
{
|
||||
processor.TriviaStart = processor.Start;
|
||||
}
|
||||
}
|
||||
|
||||
block.UpdateSpanEnd(processor.Line.End);
|
||||
return BlockState.Continue;
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ namespace Markdig.Parsers
|
||||
}
|
||||
|
||||
// Push a new block
|
||||
processor.NewBlocks.Push(new ThematicBreakBlock(this)
|
||||
var thematicBreak = new ThematicBreakBlock(this)
|
||||
{
|
||||
Column = processor.Column,
|
||||
Span = new SourceSpan(startPosition, line.End),
|
||||
@@ -94,10 +94,16 @@ namespace Markdig.Parsers
|
||||
// TODO: should we separate whitespace before/after?
|
||||
//BeforeWhitespace = beforeWhitespace,
|
||||
//AfterWhitespace = processor.PopBeforeWhitespace(processor.CurrentLineStartPosition),
|
||||
LinesBefore = processor.UseLinesBefore(),
|
||||
Content = new StringSlice(line.Text, processor.TriviaStart, line.End, line.NewLine), //include whitespace for now
|
||||
NewLine = processor.Line.NewLine,
|
||||
});
|
||||
};
|
||||
|
||||
if (processor.TrackTrivia)
|
||||
{
|
||||
thematicBreak.LinesBefore = processor.UseLinesBefore();
|
||||
thematicBreak.NewLine = processor.Line.NewLine;
|
||||
}
|
||||
|
||||
processor.NewBlocks.Push(thematicBreak);
|
||||
return BlockState.BreakDiscard;
|
||||
}
|
||||
}
|
||||
|
||||
16
src/Markdig/Polyfills/ConcurrentQueue.cs
Normal file
16
src/Markdig/Polyfills/ConcurrentQueue.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
#if NET452 || NETSTANDARD2_0
|
||||
namespace System.Collections.Concurrent
|
||||
{
|
||||
internal static class ConcurrentQueueExtensions
|
||||
{
|
||||
public static void Clear<T>(this ConcurrentQueue<T> queue)
|
||||
{
|
||||
while (queue.TryDequeue(out _)) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -15,6 +15,9 @@ namespace System.Diagnostics.CodeAnalysis
|
||||
|
||||
public bool ReturnValue { get; }
|
||||
}
|
||||
|
||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, Inherited = false)]
|
||||
public sealed class AllowNullAttribute : Attribute { }
|
||||
#endif
|
||||
|
||||
#if !NET5_0_OR_GREATER
|
||||
|
||||
17
src/Markdig/Polyfills/Unsafe.cs
Normal file
17
src/Markdig/Polyfills/Unsafe.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright (c) Alexandre Mutel. All rights reserved.
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
namespace System.Runtime.CompilerServices
|
||||
{
|
||||
#if NETSTANDARD2_1
|
||||
internal static class Unsafe
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static T As<T>(object o) where T : class
|
||||
{
|
||||
return (T)o;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -15,27 +15,25 @@ namespace Markdig.Renderers.Html
|
||||
/// <seealso cref="HtmlObjectRenderer{CodeBlock}" />
|
||||
public class CodeBlockRenderer : HtmlObjectRenderer<CodeBlock>
|
||||
{
|
||||
private HashSet<string>? _blocksAsDiv;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CodeBlockRenderer"/> class.
|
||||
/// </summary>
|
||||
public CodeBlockRenderer()
|
||||
{
|
||||
BlocksAsDiv = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
public CodeBlockRenderer() { }
|
||||
|
||||
public bool OutputAttributesOnPre { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a map of fenced code block infos that should be rendered as div blocks instead of pre/code blocks.
|
||||
/// </summary>
|
||||
public HashSet<string> BlocksAsDiv { get; }
|
||||
public HashSet<string> BlocksAsDiv => _blocksAsDiv ??= new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
protected override void Write(HtmlRenderer renderer, CodeBlock obj)
|
||||
{
|
||||
renderer.EnsureLine();
|
||||
|
||||
var fencedCodeBlock = obj as FencedCodeBlock;
|
||||
if (fencedCodeBlock?.Info != null && BlocksAsDiv.Contains(fencedCodeBlock.Info))
|
||||
if (_blocksAsDiv is not null && (obj as FencedCodeBlock)?.Info is string info && _blocksAsDiv.Contains(info))
|
||||
{
|
||||
var infoPrefix = (obj.Parser as FencedCodeBlockParser)?.InfoPrefix ??
|
||||
FencedCodeBlockParser.DefaultInfoPrefix;
|
||||
@@ -48,7 +46,7 @@ namespace Markdig.Renderers.Html
|
||||
renderer.Write("<div")
|
||||
.WriteAttributes(obj.TryGetAttributes(),
|
||||
cls => cls.StartsWith(infoPrefix, StringComparison.Ordinal) ? cls.Substring(infoPrefix.Length) : cls)
|
||||
.Write('>');
|
||||
.WriteRaw('>');
|
||||
}
|
||||
|
||||
renderer.WriteLeafRawLines(obj, true, true, true);
|
||||
@@ -57,7 +55,6 @@ namespace Markdig.Renderers.Html
|
||||
{
|
||||
renderer.WriteLine("</div>");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -70,14 +67,14 @@ namespace Markdig.Renderers.Html
|
||||
renderer.WriteAttributes(obj);
|
||||
}
|
||||
|
||||
renderer.Write("><code");
|
||||
renderer.WriteRaw("><code");
|
||||
|
||||
if (!OutputAttributesOnPre)
|
||||
{
|
||||
renderer.WriteAttributes(obj);
|
||||
}
|
||||
|
||||
renderer.Write('>');
|
||||
renderer.WriteRaw('>');
|
||||
}
|
||||
|
||||
renderer.WriteLeafRawLines(obj, true, true);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// This file is licensed under the BSD-Clause 2 license.
|
||||
// See the license.txt file in the project root for more information.
|
||||
|
||||
using System.Globalization;
|
||||
using Markdig.Syntax;
|
||||
|
||||
namespace Markdig.Renderers.Html
|
||||
@@ -25,20 +24,26 @@ namespace Markdig.Renderers.Html
|
||||
protected override void Write(HtmlRenderer renderer, HeadingBlock obj)
|
||||
{
|
||||
int index = obj.Level - 1;
|
||||
string headingText = ((uint)index < (uint)HeadingTexts.Length)
|
||||
? HeadingTexts[index]
|
||||
: "h" + obj.Level.ToString(CultureInfo.InvariantCulture);
|
||||
string[] headings = HeadingTexts;
|
||||
string headingText = ((uint)index < (uint)headings.Length)
|
||||
? headings[index]
|
||||
: $"h{obj.Level}";
|
||||
|
||||
if (renderer.EnableHtmlForBlock)
|
||||
{
|
||||
renderer.Write("<").Write(headingText).WriteAttributes(obj).Write('>');
|
||||
renderer.Write('<');
|
||||
renderer.WriteRaw(headingText);
|
||||
renderer.WriteAttributes(obj);
|
||||
renderer.WriteRaw('>');
|
||||
}
|
||||
|
||||
renderer.WriteLeafInline(obj);
|
||||
|
||||
if (renderer.EnableHtmlForBlock)
|
||||
{
|
||||
renderer.Write("</").Write(headingText).WriteLine(">");
|
||||
renderer.Write("</");
|
||||
renderer.WriteRaw(headingText);
|
||||
renderer.WriteLine('>');
|
||||
}
|
||||
|
||||
renderer.EnsureLine();
|
||||
|
||||
@@ -54,28 +54,26 @@ namespace Markdig.Renderers.Html.Inlines
|
||||
{
|
||||
if (renderer.EnableHtmlForInline)
|
||||
{
|
||||
renderer.Write("<a href=\"");
|
||||
if (obj.IsEmail)
|
||||
{
|
||||
renderer.Write("mailto:");
|
||||
}
|
||||
renderer.Write(obj.IsEmail ? "<a href=\"mailto:" : "<a href=\"");
|
||||
renderer.WriteEscapeUrl(obj.Url);
|
||||
renderer.Write('"');
|
||||
renderer.WriteRaw('"');
|
||||
renderer.WriteAttributes(obj);
|
||||
|
||||
if (!obj.IsEmail && !string.IsNullOrWhiteSpace(Rel))
|
||||
{
|
||||
renderer.Write($" rel=\"{Rel}\"");
|
||||
renderer.WriteRaw(" rel=\"");
|
||||
renderer.WriteRaw(Rel);
|
||||
renderer.WriteRaw('"');
|
||||
}
|
||||
|
||||
renderer.Write('>');
|
||||
renderer.WriteRaw('>');
|
||||
}
|
||||
|
||||
renderer.WriteEscape(obj.Url);
|
||||
|
||||
if (renderer.EnableHtmlForInline)
|
||||
{
|
||||
renderer.Write("</a>");
|
||||
renderer.WriteRaw("</a>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,19 +16,21 @@ namespace Markdig.Renderers.Html.Inlines
|
||||
{
|
||||
if (renderer.EnableHtmlForInline)
|
||||
{
|
||||
renderer.Write("<code").WriteAttributes(obj).Write('>');
|
||||
renderer.Write("<code");
|
||||
renderer.WriteAttributes(obj);
|
||||
renderer.WriteRaw('>');
|
||||
}
|
||||
if (renderer.EnableHtmlEscape)
|
||||
{
|
||||
renderer.WriteEscape(obj.Content);
|
||||
renderer.WriteEscape(obj.ContentSpan);
|
||||
}
|
||||
else
|
||||
{
|
||||
renderer.Write(obj.Content);
|
||||
renderer.Write(obj.ContentSpan);
|
||||
}
|
||||
if (renderer.EnableHtmlForInline)
|
||||
{
|
||||
renderer.Write("</code>");
|
||||
renderer.WriteRaw("</code>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user