diff --git a/site/docs/extensions/definition-lists.md b/site/docs/extensions/definition-lists.md
index db0c3a6a..22635918 100644
--- a/site/docs/extensions/definition-lists.md
+++ b/site/docs/extensions/definition-lists.md
@@ -10,23 +10,25 @@ Definition lists render as `
` / `- ` / `
- ` HTML elements. Inspired by [
## Syntax
-A definition list consists of terms followed by their definitions. Definitions are prefixed with `:` (colon followed by a space):
+A definition list consists of terms followed by their definitions. Definitions are
+prefixed with `:` or `~`, followed by at least one space or tab. A single space is
+enough:
```markdown
Term 1
-: Definition of term 1.
+: Definition of term 1.
Term 2
-: Definition of term 2.
-: Another definition of term 2.
+: Definition of term 2.
+~ Another definition of term 2.
```
Term 1
-: Definition of term 1.
+: Definition of term 1.
Term 2
-: Definition of term 2.
-: Another definition of term 2.
+: Definition of term 2.
+~ Another definition of term 2.
## Multi-line definitions
@@ -79,7 +81,7 @@ Term B
## Rules
- There must be a blank line before the first term (or it will be treated as a paragraph).
-- The `:` marker must be followed by at least one space.
+- The `:` or `~` marker must be followed by at least one space or tab.
- Continuation lines must be indented.
- A line beginning with `:` or `~` that is not a valid definition marker keeps
that character, whether it continues the current definition or starts a new term.
diff --git a/src/Markdig.Tests/Specs/DefinitionListSpecs.generated.cs b/src/Markdig.Tests/Specs/DefinitionListSpecs.generated.cs
index 3f7fee27..c63bcf05 100644
--- a/src/Markdig.Tests/Specs/DefinitionListSpecs.generated.cs
+++ b/src/Markdig.Tests/Specs/DefinitionListSpecs.generated.cs
@@ -101,7 +101,7 @@ namespace Markdig.Tests.Specs.DefinitionLists
TestParser.TestSpec("Term 1\n\n: Definition\nwith lazy continuation.\n\n Second paragraph of the definition.", "
\n- Term 1
\nDefinition\nwith lazy continuation.
\nSecond paragraph of the definition.
\n \n
", "definitionlists+attributes|advanced", context: "Example 2\nSection Extensions / Definition lists\n");
}
- // The definition must be indented to 4 characters including the `:`.
+ // The `:` or `~` must be followed by at least one space or tab. A single space is enough:
[Test]
public void ExtensionsDefinitionLists_Example003()
{
@@ -109,17 +109,26 @@ namespace Markdig.Tests.Specs.DefinitionLists
// Section: Extensions / Definition lists
//
// The following Markdown:
- // Term 1
- // : Invalid with less than 3 characters
+ // First Term
+ // : This is the definition of the first term.
+ //
+ // Second Term
+ // : This is one definition of the second term.
+ // ~ This is another definition of the second term.
//
// Should be rendered as:
- // Term 1
- // : Invalid with less than 3 characters
+ //
+ // - First Term
+ // - This is the definition of the first term.
+ // - Second Term
+ // - This is one definition of the second term.
+ // - This is another definition of the second term.
+ //
- TestParser.TestSpec("Term 1\n: Invalid with less than 3 characters", "Term 1\n: Invalid with less than 3 characters
", "definitionlists+attributes|advanced", context: "Example 3\nSection Extensions / Definition lists\n");
+ TestParser.TestSpec("First Term\n: This is the definition of the first term.\n\nSecond Term\n: This is one definition of the second term.\n~ This is another definition of the second term.", "\n- First Term
\n- This is the definition of the first term.
\n- Second Term
\n- This is one definition of the second term.
\n- This is another definition of the second term.
\n
", "definitionlists+attributes|advanced", context: "Example 3\nSection Extensions / Definition lists\n");
}
- // The `:` can be indented up to 3 spaces:
+ // Without a space or tab after the `:`, the line is not a definition:
[Test]
public void ExtensionsDefinitionLists_Example004()
{
@@ -128,6 +137,24 @@ namespace Markdig.Tests.Specs.DefinitionLists
//
// The following Markdown:
// Term 1
+ // :Invalid without a space after the colon
+ //
+ // Should be rendered as:
+ // Term 1
+ // :Invalid without a space after the colon
+
+ TestParser.TestSpec("Term 1\n:Invalid without a space after the colon", "Term 1\n:Invalid without a space after the colon
", "definitionlists+attributes|advanced", context: "Example 4\nSection Extensions / Definition lists\n");
+ }
+
+ // The `:` can be indented up to 3 spaces:
+ [Test]
+ public void ExtensionsDefinitionLists_Example005()
+ {
+ // Example 5
+ // Section: Extensions / Definition lists
+ //
+ // The following Markdown:
+ // Term 1
// : Valid even if `:` starts at most 3 spaces
//
// Should be rendered as:
@@ -136,14 +163,14 @@ namespace Markdig.Tests.Specs.DefinitionLists
// - Valid even if
: starts at most 3 spaces
//
- TestParser.TestSpec("Term 1\n : Valid even if `:` starts at most 3 spaces", "\n- Term 1
\n- Valid even if
: starts at most 3 spaces \n
", "definitionlists+attributes|advanced", context: "Example 4\nSection Extensions / Definition lists\n");
+ TestParser.TestSpec("Term 1\n : Valid even if `:` starts at most 3 spaces", "\n- Term 1
\n- Valid even if
: starts at most 3 spaces \n
", "definitionlists+attributes|advanced", context: "Example 5\nSection Extensions / Definition lists\n");
}
// But more than 3 spaces before `:` will trigger an indented code block:
[Test]
- public void ExtensionsDefinitionLists_Example005()
+ public void ExtensionsDefinitionLists_Example006()
{
- // Example 5
+ // Example 6
// Section: Extensions / Definition lists
//
// The following Markdown:
@@ -156,14 +183,14 @@ namespace Markdig.Tests.Specs.DefinitionLists
// : Not valid
//
- TestParser.TestSpec("Term 1\n\n : Not valid", "Term 1
\n: Not valid\n
", "definitionlists+attributes|advanced", context: "Example 5\nSection Extensions / Definition lists\n");
+ TestParser.TestSpec("Term 1\n\n : Not valid", "Term 1
\n: Not valid\n
", "definitionlists+attributes|advanced", context: "Example 6\nSection Extensions / Definition lists\n");
}
// Definition lists can be nested inside list items
[Test]
- public void ExtensionsDefinitionLists_Example006()
+ public void ExtensionsDefinitionLists_Example007()
{
- // Example 6
+ // Example 7
// Section: Extensions / Definition lists
//
// The following Markdown:
@@ -189,14 +216,14 @@ namespace Markdig.Tests.Specs.DefinitionLists
//
//
- TestParser.TestSpec("1. First\n \n2. Second\n \n Term 1\n : Definition\n \n Term 2\n : Second Definition", "\nFirst
\nSecond
\n\n- Term 1
\n- Definition
\n- Term 2
\n- Second Definition
\n
\n
", "definitionlists+attributes|advanced", context: "Example 6\nSection Extensions / Definition lists\n");
+ TestParser.TestSpec("1. First\n \n2. Second\n \n Term 1\n : Definition\n \n Term 2\n : Second Definition", "\nFirst
\nSecond
\n\n- Term 1
\n- Definition
\n- Term 2
\n- Second Definition
\n
\n
", "definitionlists+attributes|advanced", context: "Example 7\nSection Extensions / Definition lists\n");
}
// An invalid continuation must preserve its leading marker when it starts the next term:
[Test]
- public void ExtensionsDefinitionLists_Example007()
+ public void ExtensionsDefinitionLists_Example008()
{
- // Example 7
+ // Example 8
// Section: Extensions / Definition lists
//
// The following Markdown:
@@ -214,14 +241,14 @@ namespace Markdig.Tests.Specs.DefinitionLists
// Second definition
//
- TestParser.TestSpec("Term\n: First definition\n\n:foo: Foo\n: Second definition", "\n- Term
\n- First definition
\n- :foo: Foo
\n- Second definition
\n
", "definitionlists+attributes|advanced", context: "Example 7\nSection Extensions / Definition lists\n");
+ TestParser.TestSpec("Term\n: First definition\n\n:foo: Foo\n: Second definition", "\n- Term
\n- First definition
\n- :foo: Foo
\n- Second definition
\n
", "definitionlists+attributes|advanced", context: "Example 8\nSection Extensions / Definition lists\n");
}
// The same applies to the tilde marker:
[Test]
- public void ExtensionsDefinitionLists_Example008()
+ public void ExtensionsDefinitionLists_Example009()
{
- // Example 8
+ // Example 9
// Section: Extensions / Definition lists
//
// The following Markdown:
@@ -239,14 +266,14 @@ namespace Markdig.Tests.Specs.DefinitionLists
// Second definition
//
- TestParser.TestSpec("Term\n: First definition\n\n~foo Foo\n: Second definition", "\n- Term
\n- First definition
\n- ~foo Foo
\n- Second definition
\n
", "definitionlists+attributes|advanced", context: "Example 8\nSection Extensions / Definition lists\n");
+ TestParser.TestSpec("Term\n: First definition\n\n~foo Foo\n: Second definition", "\n- Term
\n- First definition
\n- ~foo Foo
\n- Second definition
\n
", "definitionlists+attributes|advanced", context: "Example 9\nSection Extensions / Definition lists\n");
}
// Without a blank line, the next line remains part of the current definition but keeps its marker:
[Test]
- public void ExtensionsDefinitionLists_Example009()
+ public void ExtensionsDefinitionLists_Example010()
{
- // Example 9
+ // Example 10
// Section: Extensions / Definition lists
//
// The following Markdown:
@@ -261,7 +288,7 @@ namespace Markdig.Tests.Specs.DefinitionLists
// :foo: Foo
//
- TestParser.TestSpec("Term\n: First definition\n:foo: Foo", "\n- Term
\n- First definition\n:foo: Foo
\n
", "definitionlists+attributes|advanced", context: "Example 9\nSection Extensions / Definition lists\n");
+ TestParser.TestSpec("Term\n: First definition\n:foo: Foo", "\n- Term
\n- First definition\n:foo: Foo
\n
", "definitionlists+attributes|advanced", context: "Example 10\nSection Extensions / Definition lists\n");
}
}
}
diff --git a/src/Markdig.Tests/Specs/DefinitionListSpecs.md b/src/Markdig.Tests/Specs/DefinitionListSpecs.md
index 6cd3dfa0..836236f7 100644
--- a/src/Markdig.Tests/Specs/DefinitionListSpecs.md
+++ b/src/Markdig.Tests/Specs/DefinitionListSpecs.md
@@ -72,14 +72,33 @@ with lazy continuation.
````````````````````````````````
-The definition must be indented to 4 characters including the `:`.
+The `:` or `~` must be followed by at least one space or tab. A single space is enough:
+
+```````````````````````````````` example
+First Term
+: This is the definition of the first term.
+
+Second Term
+: This is one definition of the second term.
+~ This is another definition of the second term.
+.
+
+- First Term
+- This is the definition of the first term.
+- Second Term
+- This is one definition of the second term.
+- This is another definition of the second term.
+
+````````````````````````````````
+
+Without a space or tab after the `:`, the line is not a definition:
```````````````````````````````` example
Term 1
-: Invalid with less than 3 characters
+:Invalid without a space after the colon
.
Term 1
-: Invalid with less than 3 characters
+:Invalid without a space after the colon
````````````````````````````````
The `:` can be indented up to 3 spaces:
diff --git a/src/Markdig.Tests/TestDefinitionLists.cs b/src/Markdig.Tests/TestDefinitionLists.cs
index 5b62c8df..306e939c 100644
--- a/src/Markdig.Tests/TestDefinitionLists.cs
+++ b/src/Markdig.Tests/TestDefinitionLists.cs
@@ -7,8 +7,10 @@ public class TestDefinitionLists
{
[TestCase(":foo: Foo", 1)]
[TestCase(":foo: Foo", 2)]
+ [TestCase(":foo: Foo", 3)]
[TestCase("~foo Foo", 1)]
[TestCase("~foo Foo", 2)]
+ [TestCase("~foo Foo", 3)]
public void IndentedInvalidContinuationPreservesTerm(string term, int indent)
{
var markdown = $"Term\n: First definition\n\n{new string(' ', indent)}{term}\n: Second definition";
@@ -21,7 +23,7 @@ public class TestDefinitionLists
public void InvalidDefinitionMarkerPreservesSourceLocation(
[Values("Term\n", "Term\n: First definition\n")] string prefix,
[Values(":foo: Foo", "~foo Foo")] string marker,
- [Values(1, 2)] int indent)
+ [Values(1, 2, 3)] int indent)
{
var markdown = $"{prefix}{new string(' ', indent)}{marker}";
var pipeline = new MarkdownPipelineBuilder().UseDefinitionLists().UsePreciseSourceLocation().Build();
@@ -32,4 +34,15 @@ public class TestDefinitionLists
Assert.AreEqual(prefix.Length + indent, literal.Span.Start);
Assert.AreEqual(markdown.Length - 1, literal.Span.End);
}
+
+ [Test]
+ public void DefinitionMarkersAcceptSpacesOrTabs(
+ [Values(':', '~')] char marker,
+ [Values(" ", " ", "\t")] string whitespace)
+ {
+ var markdown = $"Term\n{marker}{whitespace}First definition\n{marker}{whitespace}Second definition";
+ const string expected = "\n- Term
\n- First definition
\n- Second definition
\n
\n";
+
+ TestParser.TestSpec(markdown, expected, "definitionlists+attributes|advanced");
+ }
}
diff --git a/src/Markdig/Extensions/DefinitionLists/DefinitionListParser.cs b/src/Markdig/Extensions/DefinitionLists/DefinitionListParser.cs
index 339a86d5..470beb27 100644
--- a/src/Markdig/Extensions/DefinitionLists/DefinitionListParser.cs
+++ b/src/Markdig/Extensions/DefinitionLists/DefinitionListParser.cs
@@ -2,6 +2,7 @@
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.
+using Markdig.Helpers;
using Markdig.Parsers;
using Markdig.Syntax;
@@ -36,12 +37,12 @@ public class DefinitionListParser : BlockParser
var startColumn = processor.Column;
var column = processor.ColumnBeforeIndent;
- processor.NextChar();
+ var c = processor.NextChar();
processor.ParseIndent();
var delta = processor.Column - column;
- // We expect to have a least
- if (delta < 4)
+ // The marker must be followed by at least one space or tab
+ if (!c.IsSpaceOrTab())
{
// Return back to original position
processor.GoToColumn(startColumn);
@@ -146,12 +147,12 @@ public class DefinitionListParser : BlockParser
var startPosition = processor.Start;
var startColumn = processor.Column;
var column = processor.ColumnBeforeIndent;
- processor.NextChar();
+ var c = processor.NextChar();
processor.ParseIndent();
var delta = processor.Column - column;
- // We expect to have a least
- if (delta < 4)
+ // The marker must be followed by at least one space or tab
+ if (!c.IsSpaceOrTab())
{
processor.GoToColumn(startColumn);