Skip to content

Commit

Permalink
[Breaking] Support underscores for emphasis (#42)
Browse files Browse the repository at this point in the history
Since GFM supports underscores for emphasis, we can do that here as well. Factored out of #31.

Note that this is breaking since previously if you have ambiguous underscores in open text that it may create a syntax error or italicize text. To correct this replace `_` with `\_`.
  • Loading branch information
leebyron committed Jan 8, 2021
1 parent 0ec7e59 commit b8c3e52
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 17 deletions.
12 changes: 10 additions & 2 deletions spec/Markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ Todo: Links do not yet support a title attribute.

### Emphasis

Wrapping asterisks *(\*)* indicate emphasis. Like Github-flavored
Markdown, Spec Markdown does not treat underscore *(_)* as emphasis.
Wrapping asterisks *(\*)* indicate emphasis.

```
Example of **bold** and *italic* and ***bold italic***.
Expand All @@ -97,6 +96,15 @@ Produces the following:

Example of **bold** and *italic* and ***bold italic***.

Alternatively, use underscore *(\_)* for italic emphasis.

```
Example of _italic_ and **_bold italic_** or _**bold italic**_.
```

Produces the following:

Example of _italic_ and **_bold italic_** or _**bold italic**_.


### Inline Code
Expand Down
13 changes: 11 additions & 2 deletions src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ inlineEntity = inlineEdit / inlineCode / reference / bold / italic / link / imag
content = inlineEntity / text

textChar = escaped
/ [^\n\r+\-{`*[!<]
/ [^\n\r+\-{`*_[!<]
/ '++' !'}'
/ '+' !'+}'
/ '--' !'}'
Expand Down Expand Up @@ -246,7 +246,16 @@ bold = '**' contents:(inlineCode / link / italic / text)+ '**' {
};
}

italic = '*' contents:(inlineCode / link / text)+ '*' {
italic = asteriskItalic / underscoreItalic

asteriskItalic = '*' contents:(inlineCode / link / text)+ '*' {
return {
type: 'Italic',
contents: contents
};
}

underscoreItalic = '_' contents:(inlineCode / link / bold / text)+ '_' {
return {
type: 'Italic',
contents: contents
Expand Down
108 changes: 96 additions & 12 deletions test/readme/ast.json
Original file line number Diff line number Diff line change
Expand Up @@ -578,20 +578,99 @@
},
{
"type": "Text",
"value": " indicate emphasis. Like Github-flavored\nMarkdown, Spec Markdown does not treat underscore "
"value": " indicate emphasis."
}
]
},
{
"type": "Code",
"raw": false,
"lang": null,
"example": false,
"counter": false,
"code": "Example of **bold** and *italic* and ***bold italic***.\n"
},
{
"type": "Paragraph",
"contents": [
{
"type": "Text",
"value": "Produces the following:"
}
]
},
{
"type": "Paragraph",
"contents": [
{
"type": "Text",
"value": "Example of "
},
{
"type": "Bold",
"contents": [
{
"type": "Text",
"value": "bold"
}
]
},
{
"type": "Text",
"value": " and "
},
{
"type": "Italic",
"contents": [
{
"type": "Text",
"value": "(_)"
"value": "italic"
}
]
},
{
"type": "Text",
"value": " as emphasis."
"value": " and "
},
{
"type": "Bold",
"contents": [
{
"type": "Italic",
"contents": [
{
"type": "Text",
"value": "bold italic"
}
]
}
]
},
{
"type": "Text",
"value": "."
}
]
},
{
"type": "Paragraph",
"contents": [
{
"type": "Text",
"value": "Alternatively, use underscore "
},
{
"type": "Italic",
"contents": [
{
"type": "Text",
"value": "(\\_)"
}
]
},
{
"type": "Text",
"value": " for italic emphasis."
}
]
},
Expand All @@ -601,7 +680,7 @@
"lang": null,
"example": false,
"counter": false,
"code": "Example of **bold** and *italic* and ***bold italic***.\n"
"code": "Example of _italic_ and **_bold italic_** or _**bold italic**_.\n"
},
{
"type": "Paragraph",
Expand All @@ -620,11 +699,11 @@
"value": "Example of "
},
{
"type": "Bold",
"type": "Italic",
"contents": [
{
"type": "Text",
"value": "bold"
"value": "italic"
}
]
},
Expand All @@ -633,23 +712,28 @@
"value": " and "
},
{
"type": "Italic",
"type": "Bold",
"contents": [
{
"type": "Text",
"value": "italic"
"type": "Italic",
"contents": [
{
"type": "Text",
"value": "bold italic"
}
]
}
]
},
{
"type": "Text",
"value": " and "
"value": " or "
},
{
"type": "Bold",
"type": "Italic",
"contents": [
{
"type": "Italic",
"type": "Bold",
"contents": [
{
"type": "Text",
Expand Down
7 changes: 6 additions & 1 deletion test/readme/output.html
Original file line number Diff line number Diff line change
Expand Up @@ -1080,11 +1080,16 @@ <h4><span class="spec-secid" title="link to this section"><a href="#sec-Links">2
</section>
<section id="sec-Emphasis" secid="2.2.3">
<h4><span class="spec-secid" title="link to this section"><a href="#sec-Emphasis">2.2.3</a></span>Emphasis</h4>
<p>Wrapping asterisks <em>(*)</em> indicate emphasis. Like Github&#8208;flavored Markdown, Spec Markdown does not treat underscore <em>(_)</em> as emphasis.</p>
<p>Wrapping asterisks <em>(*)</em> indicate emphasis.</p>
<pre><code>Example of **bold** and *italic* and ***bold italic***.
</code></pre>
<p>Produces the following:</p>
<p>Example of <strong>bold</strong> and <em>italic</em> and <strong><em>bold italic</em></strong>.</p>
<p>Alternatively, use underscore <em>(_)</em> for italic emphasis.</p>
<pre><code>Example of _italic_ and **_bold italic_** or _**bold italic**_.
</code></pre>
<p>Produces the following:</p>
<p>Example of <em>italic</em> and <strong><em>bold italic</em></strong> or <em><strong>bold italic</strong></em>.</p>
</section>
<section id="sec-Inline-Code" secid="2.2.4">
<h4><span class="spec-secid" title="link to this section"><a href="#sec-Inline-Code">2.2.4</a></span>Inline Code</h4>
Expand Down

0 comments on commit b8c3e52

Please sign in to comment.