Make sure that an emphasis is not added twice (issue #4)

This commit is contained in:
Alexandre Mutel
2016-05-30 22:33:50 +09:00
parent fb559af72b
commit 09beb2f867
5 changed files with 59 additions and 29 deletions

View File

@@ -27,34 +27,47 @@ Later in a text we are using HTML and it becomes an abbr tag HTML
Console.WriteLine(result);
}
// Test for emoji and smileys
// var text = @" This is a test with a :) and a :angry: smiley";
[Test]
public void TestSamePipelineAllExtensions()
{
var pipeline = new MarkdownPipeline().UseAllExtensions();
// Reuse the same pipeline
var result1 = Markdown.ToHtml("This is a \"\"citation\"\"", pipeline);
var result2 = Markdown.ToHtml("This is a \"\"citation\"\"", pipeline);
Assert.AreEqual("<p>This is a <cite>citation</cite></p>", result1.Trim());
Assert.AreEqual(result1, result2);
}
// Test for emoji and smileys
// var text = @" This is a test with a :) and a :angry: smiley";
// Test for definition lists:
//
// var text = @"
//Term 1
//: This is a definition item
// With a paragraph
// > This is a block quote
// Test for definition lists:
//
// var text = @"
//Term 1
//: This is a definition item
// With a paragraph
// > This is a block quote
// - This is a list
// - item2
// - This is a list
// - item2
// ```java
// Test
// ```java
// Test
// ```
// ```
// And a lazy line
//: This ia another definition item
// And a lazy line
//: This ia another definition item
//Term2
//Term3 *with some inline*
//: This is another definition for term2
//";
//Term2
//Term3 *with some inline*
//: This is another definition for term2
//";
// Test for grid table

View File

@@ -18,15 +18,8 @@ namespace Markdig.Extensions.Cites
public void Setup(MarkdownPipeline pipeline)
{
var parser = pipeline.InlineParsers.FindExact<EmphasisInlineParser>();
if (parser != null)
if (parser != null && !parser.HasEmphasisChar('"'))
{
foreach (var emphasis in parser.EmphasisDescriptors)
{
if (emphasis.Character == '"')
{
return;
}
}
parser.EmphasisDescriptors.Add(new EmphasisDescriptor('"', 2, 2, false));
}
@@ -37,6 +30,7 @@ namespace Markdig.Extensions.Cites
var emphasisRenderer = htmlRenderer.ObjectRenderers.FindExact<EmphasisInlineRenderer>();
if (emphasisRenderer != null)
{
// TODO: Use an ordered list instead as we don't know if this specific GetTag has been already added
var previousTag = emphasisRenderer.GetTag;
emphasisRenderer.GetTag = inline => GetTag(inline) ?? previousTag(inline);
}

View File

@@ -23,7 +23,7 @@ namespace Markdig.Extensions.CustomContainers
// Plug the inline parser for CustomContainerInline
var inlineParser = pipeline.InlineParsers.Find<EmphasisInlineParser>();
if (inlineParser != null)
if (inlineParser != null && !inlineParser.HasEmphasisChar(':'))
{
inlineParser.EmphasisDescriptors.Add(new EmphasisDescriptor(':', 2, 2, true));
var previousCreateEmphasisInline = inlineParser.CreateEmphasisInline;

View File

@@ -13,6 +13,7 @@ namespace Markdig
/// <summary>
/// This class allows to modify the pipeline to parse and render a Markdown document.
/// </summary>
/// <remarks>NOTE: A pipeline is not thread-safe.</remarks>
public class MarkdownPipeline
{
/// <summary>
@@ -95,6 +96,11 @@ namespace Markdig
/// <exception cref="System.InvalidOperationException">An extension cannot be null</exception>
public void Initialize()
{
// TODO: Review the whole initialization process for extensions
// - It does not prevent a user to modify the pipeline after it has been used
// - a pipeline is not thread safe.
// We should find a proper way to make the pipeline safely modifiable/freezable (PipelineBuilder -> Pipeline)
// Allow extensions to modify existing BlockParsers, InlineParsers and Renderer
foreach (var extension in Extensions)
{

View File

@@ -41,6 +41,23 @@ namespace Markdig.Parsers.Inlines
/// </summary>
public List<EmphasisDescriptor> EmphasisDescriptors { get; }
/// <summary>
/// Determines whether this parser is using the specified character as an emphasis delimiter.
/// </summary>
/// <param name="c">The character to look for.</param>
/// <returns><c>true</c> if this parser is using the specified character as an emphasis delimiter; otherwise <c>false</c></returns>
public bool HasEmphasisChar(char c)
{
foreach (var emphasis in EmphasisDescriptors)
{
if (emphasis.Character == c)
{
return true;
}
}
return false;
}
/// <summary>
/// Gets or sets the create emphasis inline delegate (allowing to create a different emphasis inline class)
/// </summary>