Skip to content

Commit

Permalink
Add extension tests, Fix bugs in extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed May 16, 2019
1 parent 1963434 commit 2ddc99b
Show file tree
Hide file tree
Showing 29 changed files with 912 additions and 144 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ Parser and Renderer options
| `parser.WithParagraphTransformers` | A `util.PrioritizedSlice` whose elements are `parser.ParagraphTransformer` | Transformers for transforming paragraph nodes. |
| `parser.WithAutoHeadingID` | `-` | Enables auto heading ids. |
| `parser.WithAttribute` | `-` | Enables custom attributes. Currently only headings supports attributes. |
| `parser.WithFilterTags` | `...string` | HTML tag names forbidden in HTML blocks and Raw HTMLs. |

### HTML Renderer options

Expand All @@ -116,7 +115,8 @@ Parser and Renderer options
- [Gitmark Flavored Markdown: Task list items](https://github.github.com/gfm/#task-list-items-extension-)
- `extension.GFM`
- This extension enables Table, Strikethrough, Linkify and TaskList.
In addition, this extension sets some tags to `parser.FilterTags` .
- This extension does not filter tags defined in [6.11Disallowed Raw HTML (extension)](https://github.github.com/gfm/#disallowed-raw-html-extension-).
If you need to filter HTML tags, see [Security](#security)
- `extension.DefinitionList`
- [PHP Markdown Extra: Definition lists](https://michelf.ca/projects/php-markdown/extra/#def-list)
- `extension.Footnote`
Expand Down
27 changes: 27 additions & 0 deletions _test/options.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
1
//- - - - - - - - -//
## Title 0

## Title1 # {#id_1 .class-1}

## Title2 {#id_2}

## Title3 ## {#id_3 .class-3}

## Title4 ## {attr3=value3}

## Title5 ## {#id_5 attr5=value5}

## Title6 ## {#id_6 .class6 attr6=value6}

## Title7 ## {#id_7 attr7="value \"7"}
//- - - - - - - - -//
<h2 id="title-0">Title 0</h2>
<h2 id="id_1" class="class-1">Title1</h2>
<h2 id="id_2">Title2</h2>
<h2 id="id_3" class="class-3">Title3</h2>
<h2 attr3="value3" id="title4">Title4</h2>
<h2 id="id_5" attr5="value5">Title5</h2>
<h2 id="id_6" class="class6" attr6="value6">Title6</h2>
<h2 id="id_7" attr7="value &quot;7">Title7</h2>
//= = = = = = = = = = = = = = = = = = = = = = = =//
29 changes: 25 additions & 4 deletions ast/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,18 +362,21 @@ const (
// An AutoLink struct represents an autolink of the Markdown text.
type AutoLink struct {
BaseInline
// Value is a link text of this node.
Value *Text
// Type is a type of this autolink.
AutoLinkType AutoLinkType

// Protocol specified a protocol of the link.
Protocol []byte

value *Text
}

// Inline implements Inline.Inline.
func (n *AutoLink) Inline() {}

// Dump implenets Node.Dump
func (n *AutoLink) Dump(source []byte, level int) {
segment := n.Value.Segment
segment := n.value.Segment
m := map[string]string{
"Value": string(segment.Value(source)),
}
Expand All @@ -388,11 +391,29 @@ func (n *AutoLink) Kind() NodeKind {
return KindAutoLink
}

// URL returns an url of this node.
func (n *AutoLink) URL(source []byte) []byte {
if n.Protocol != nil {
s := n.value.Segment
ret := make([]byte, 0, len(n.Protocol)+s.Len()+3)
ret = append(ret, n.Protocol...)
ret = append(ret, ':', '/', '/')
ret = append(ret, n.value.Text(source)...)
return ret
}
return n.value.Text(source)
}

// Label returns a label of this node.
func (n *AutoLink) Label(source []byte) []byte {
return n.value.Text(source)
}

// NewAutoLink returns a new AutoLink node.
func NewAutoLink(typ AutoLinkType, value *Text) *AutoLink {
return &AutoLink{
BaseInline: BaseInline{},
Value: value,
value: value,
AutoLinkType: typ,
}
}
Expand Down
32 changes: 9 additions & 23 deletions commonmark_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package goldmark

import (
"bytes"
"encoding/json"
"io/ioutil"
"testing"
Expand All @@ -27,30 +26,17 @@ func TestSpec(t *testing.T) {
if err := json.Unmarshal(bs, &testCases); err != nil {
panic(err)
}
cases := []MarkdownTestCase{}
for _, c := range testCases {
cases = append(cases, MarkdownTestCase{
No: c.Example,
Markdown: c.Markdown,
Expected: c.HTML,
})
}
markdown := New(WithRendererOptions(
html.WithXHTML(),
html.WithUnsafe(),
))
for _, testCase := range testCases {
var out bytes.Buffer
if err := markdown.Convert([]byte(testCase.Markdown), &out); err != nil {
panic(err)
}
if !bytes.Equal(bytes.TrimSpace(out.Bytes()), bytes.TrimSpace([]byte(testCase.HTML))) {
format := `============= case %d ================
Markdown:
-----------
%s
Expected:
----------
%s
Actual
---------
%s
`
t.Errorf(format, testCase.Example, testCase.Markdown, testCase.HTML, out.Bytes())
}
}
DoTestCases(markdown, cases, t)
}
143 changes: 143 additions & 0 deletions extension/_test/definition_list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
1
//- - - - - - - - -//
Apple
: Pomaceous fruit of plants of the genus Malus in
the family Rosaceae.

Orange
: The fruit of an evergreen tree of the genus Citrus.
//- - - - - - - - -//
<dl>
<dt>Apple</dt>
<dd>Pomaceous fruit of plants of the genus Malus in
the family Rosaceae.</dd>
<dt>Orange</dt>
<dd>The fruit of an evergreen tree of the genus Citrus.</dd>
</dl>
//= = = = = = = = = = = = = = = = = = = = = = = =//



2
//- - - - - - - - -//
Apple
: Pomaceous fruit of plants of the genus Malus in
the family Rosaceae.
: An American computer company.

Orange
: The fruit of an evergreen tree of the genus Citrus.
//- - - - - - - - -//
<dl>
<dt>Apple</dt>
<dd>Pomaceous fruit of plants of the genus Malus in
the family Rosaceae.</dd>
<dd>An American computer company.</dd>
<dt>Orange</dt>
<dd>The fruit of an evergreen tree of the genus Citrus.</dd>
</dl>
//= = = = = = = = = = = = = = = = = = = = = = = =//



3
//- - - - - - - - -//
Term 1
Term 2
: Definition a

Term 3
: Definition b
//- - - - - - - - -//
<dl>
<dt>Term 1</dt>
<dt>Term 2</dt>
<dd>Definition a</dd>
<dt>Term 3</dt>
<dd>Definition b</dd>
</dl>
//= = = = = = = = = = = = = = = = = = = = = = = =//



4
//- - - - - - - - -//
Apple

: Pomaceous fruit of plants of the genus Malus in
the family Rosaceae.

Orange

: The fruit of an evergreen tree of the genus Citrus.
//- - - - - - - - -//
<dl>
<dt>Apple</dt>
<dd>
<p>Pomaceous fruit of plants of the genus Malus in
the family Rosaceae.</p>
</dd>
<dt>Orange</dt>
<dd>
<p>The fruit of an evergreen tree of the genus Citrus.</p>
</dd>
</dl>
//= = = = = = = = = = = = = = = = = = = = = = = =//


5
//- - - - - - - - -//
Term 1

: This is a definition with two paragraphs. Lorem ipsum
dolor sit amet, consectetuer adipiscing elit. Aliquam
hendrerit mi posuere lectus.

Vestibulum enim wisi, viverra nec, fringilla in, laoreet
vitae, risus.

: Second definition for term 1, also wrapped in a paragraph
because of the blank line preceding it.

Term 2

: This definition has a code block, a blockquote and a list.

code block.

> block quote
> on two lines.

1. first list item
2. second list item
//- - - - - - - - -//
<dl>
<dt>Term 1</dt>
<dd>
<p>This is a definition with two paragraphs. Lorem ipsum
dolor sit amet, consectetuer adipiscing elit. Aliquam
hendrerit mi posuere lectus.</p>
<p>Vestibulum enim wisi, viverra nec, fringilla in, laoreet
vitae, risus.</p>
</dd>
<dd>
<p>Second definition for term 1, also wrapped in a paragraph
because of the blank line preceding it.</p>
</dd>
<dt>Term 2</dt>
<dd>
<p>This definition has a code block, a blockquote and a list.</p>
<pre><code>code block.
</code></pre>
<blockquote>
<p>block quote
on two lines.</p>
</blockquote>
<ol>
<li>first list item</li>
<li>second list item</li>
</ol>
</dd>
</dl>
//= = = = = = = = = = = = = = = = = = = = = = = =//

22 changes: 22 additions & 0 deletions extension/_test/footnote.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
1
//- - - - - - - - -//
That's some text with a footnote.[^1]

[^1]: And that's the footnote.

That's the second paragraph.
//- - - - - - - - -//
<p>That's some text with a footnote.<sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup></p>
<section class="footnotes" role="doc-endnotes">
<hr>
<ol>
<li id="fn:1" role="doc-endnote">
<p>And that's the footnote.</p>
<p>That's the second paragraph.</p>
</li>
</ol>
<section>
//= = = = = = = = = = = = = = = = = = = = = = = =//



Loading

0 comments on commit 2ddc99b

Please sign in to comment.