diff --git a/docs/USING_ADVANCED.md b/docs/USING_ADVANCED.md index 6490caa782..216b0b05be 100644 --- a/docs/USING_ADVANCED.md +++ b/docs/USING_ADVANCED.md @@ -39,25 +39,26 @@ console.log(myMarked('I am using __markdown__.'));

Options

-|Member |Type |Default |Since |Notes | -|:-----------|:---------|:--------|:--------|:-------------| -|baseUrl |`string` |`null` |0.3.9 |A prefix url for any relative link. | -|breaks |`boolean` |`false` |v0.2.7 |If true, add `
` on a single line break (copies GitHub). Requires `gfm` be `true`.| -|gfm |`boolean` |`true` |v0.2.1 |If true, use approved [GitHub Flavored Markdown (GFM) specification](https://github.github.com/gfm/).| -|headerIds |`boolean` |`true` |v0.4.0 |If true, include an `id` attribute when emitting headings (h1, h2, h3, etc).| -|headerPrefix|`string` |`''` |v0.3.0 |A string to prefix the `id` attribute when emitting headings (h1, h2, h3, etc).| -|highlight |`function`|`null` |v0.3.0 |A function to highlight code blocks, see Asynchronous highlighting.| -|langPrefix |`string` |`'language-'`|v0.3.0|A string to prefix the className in a `` block. Useful for syntax highlighting.| -|mangle |`boolean` |`true` |v0.3.4 |If true, autolinked email address is escaped with HTML character references.| -|pedantic |`boolean` |`false` |v0.2.1 |If true, conform to the original `markdown.pl` as much as possible. Don't fix original markdown bugs or behavior. Turns off and overrides `gfm`.| -|renderer |`object` |`new Renderer()`|v0.3.0|An object containing functions to render tokens to HTML. See [extensibility](USING_PRO.md) for more details.| -|sanitize |`boolean` |`false` |v0.2.1 |If true, sanitize the HTML passed into `markdownString` with the `sanitizer` function.| -|sanitizer |`function`|`null` |v0.3.4 |A function to sanitize the HTML passed into `markdownString`.| -|silent |`boolean` |`false` |v0.2.7 |If true, the parser does not throw any exception.| -|smartLists |`boolean` |`false` |v0.2.8 |If true, use smarter list behavior than those found in `markdown.pl`.| -|smartypants |`boolean` |`false` |v0.2.9 |If true, use "smart" typographic punctuation for things like quotes and dashes.| -|tables |`boolean` |`true` |v0.2.7 |If true and `gfm` is true, use [GFM Tables extension](https://github.github.com/gfm/#tables-extension-).| -|xhtml |`boolean` |`false` |v0.3.2 |If true, emit self-closing HTML tags for void elements (<br/>, <img/>, etc.) with a "/" as required by XHTML.| +|Member |Type |Default |Since |Removed |Notes | +|:-----------|:---------|:--------|:--------|:-------|:-------------| +|baseUrl |`string` |`null` |0.3.9 |nope |A prefix url for any relative link. | +|breaks |`boolean` |`false` |v0.2.7 |nope |If true, add `
` on a single line break (copies GitHub). Requires `gfm` be `true`.| +|gfm |`boolean` |`true` |v0.2.1 |nope |If true, use approved [GitHub Flavored Markdown (GFM) specification](https://github.github.com/gfm/).| +|~~headerIds~~|`boolean` |`true` |v0.4.0 |v0.6.0 |If true, include an `id` attribute when emitting headings (h1, h2, h3, etc).| +|~~headerPrefix~~|`string` |`''` |v0.3.0 |v0.6.0 |A string to prefix the `id` attribute when emitting headings (h1, h2, h3, etc).| +|highlight |`function`|`null` |v0.3.0 |nope |A function to highlight code blocks, see Asynchronous highlighting.| +|langPrefix |`string` |`'language-'`|v0.3.0|nope |A string to prefix the className in a `` block. Useful for syntax highlighting.| +|mangle |`boolean` |`true` |v0.3.4 |nope |If true, autolinked email address is escaped with HTML character references.| +|pedantic |`boolean` |`false` |v0.2.1 |nope |If true, conform to the original `markdown.pl` as much as possible. Don't fix original markdown bugs or behavior. Turns off and overrides `gfm`.| +|renderer |`object` |`new Renderer()`|v0.3.0|nope |An object containing functions to render tokens to HTML. See [extensibility](USING_PRO.md) for more details.| +|sanitize |`boolean` |`false` |v0.2.1 |nope |If true, sanitize the HTML passed into `markdownString` with the `sanitizer` function.| +|sanitizer |`function`|`null` |v0.3.4 |nope |A function to sanitize the HTML passed into `markdownString`.| +|silent |`boolean` |`false` |v0.2.7 |nope |If true, the parser does not throw any exception.| +|slugger |`function`|`null` |v0.6.0 |nope |A function used when generating header ids.| +|smartLists |`boolean` |`false` |v0.2.8 |nope |If true, use smarter list behavior than those found in `markdown.pl`.| +|smartypants |`boolean` |`false` |v0.2.9 |nope |If true, use "smart" typographic punctuation for things like quotes and dashes.| +|tables |`boolean` |`true` |v0.2.7 |nope |If true and `gfm` is true, use [GFM Tables extension](https://github.github.com/gfm/#tables-extension-).| +|xhtml |`boolean` |`false` |v0.3.2 |nope |If true, emit self-closing HTML tags for void elements (<br/>, <img/>, etc.) with a "/" as required by XHTML.|

Asynchronous highlighting

@@ -76,3 +77,53 @@ console.log(myMarked(markdownString)); ``` In both examples, `code` is a `string` representing the section of code to pass to the highlighter. In this example, `lang` is a `string` informing the highlighter what programming lnaguage to use for the `code` and `callback` is the `function` the asynchronous highlighter will call once complete. + +

Slugger

+ +The default behavior of marked is to follow the CommonMark specification. However, in some cases, it may be desirable to generate IDs for [ATX Headers](https://spec.commonmark.org/0.28/#atx-heading) for the purpose of permalinks or maybe a Table of Contents. + +In this case, marked offers a `slugger` option that can take an implementation of [github-slugger](https://www.npmjs.com/package/github-slugger) or your own class with a `slug` method. + +Below is a slugger implementation that generates a lowercase header ID (formerly the `headerIds` option). It also avoids duplicate header IDs by incrementing a count of seen slugs. + +```js +function Slugger () { + this.seen = {}; +} + +Slugger.prototype.slug = function (raw) { + var slug = raw.toLowerCase().replace(/[^\w]+/g, '-'); + var count = this.seen.hasOwnProperty(slug) ? this.seen[slug] + 1 : 0; + this.seen[slug] = count; + + if (count > 0) { + slug = slug + '-' + count; + } + + return slug; +}; + +var options = { slugger: new Slugger() }; +var markdownString = `# My Heading +This is a paragraph.`; +var html = marked(markdownString, options); +``` + +Sometimes, it is desirable to mix static HTML with dynamic markdown (which of course renders to HTML). Generating dynamic IDs with a slugger might cause collisions with other ID properties on the page. In this case, you can add a prefix to the slugger. + +Below is an example that extends the `github-slugger` implementation to add a prefix to each ID (formerly the `headerPrefix` option). + +```js +var marked = require('marked'); +var GithubSlugger = require('github-slugger'); +var ghslugger = new GithubSlugger(); + +function PrefixSlugger (prefix) { + this.headerPrefix = prefix; +} +PrefixSlugger.prototype.slug = function (value) { + return this.headerPrefix + ghslugger.slug(value); +}; + +var options = { slugger: new PrefixSlugger('comment-') }; +``` \ No newline at end of file diff --git a/docs/index.html b/docs/index.html index 07a4d5ee4c..24e7fd91fc 100644 --- a/docs/index.html +++ b/docs/index.html @@ -148,6 +148,7 @@

Marked.js Documentation

  • diff --git a/lib/marked.js b/lib/marked.js index 9e99346462..0ed6e9d868 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -947,12 +947,11 @@ Renderer.prototype.html = function(html) { }; Renderer.prototype.heading = function(text, level, raw) { - if (this.options.headerIds) { + if (this.options.slugger) { return '' + text + '([^<]+)<\/\1>/g, function(s, h, text) { - var id = text - .replace(/'/g, '\'') - .replace(/"/g, '"') - .replace(/>/g, '>') - .replace(/</g, '<') - .replace(/&/g, '&'); - - id = id.toLowerCase().replace(/[^\w]+/g, '-'); - - return '<' + h + ' id="' + id + '">' + text + ''; - }); - fs.writeFileSync(file, html); }); diff --git a/test/new/cm_autolinks.html b/test/new/cm_autolinks.html index e7ae0ee416..7a8ecdd787 100644 --- a/test/new/cm_autolinks.html +++ b/test/new/cm_autolinks.html @@ -1,18 +1,18 @@

    Here are some valid autolinks:

    -

    Example 565

    +

    Example 565

    http://foo.bar.baz

    -

    Example 566

    +

    Example 566

    http://foo.bar.baz/test?q=hello&id=22&boolean

    -

    Example 567

    +

    Example 567

    irc://foo.bar:2233/baz

    -

    Example 568

    +

    Example 568

    Uppercase is also fine:

    @@ -20,29 +20,29 @@

    Example 568

    Note that many strings that count as absolute URIs for purposes of this spec are not valid URIs, because their schemes are not registered or because of other problems with their syntax:

    -

    Example 569

    +

    Example 569

    a+b+c:d

    -

    Example 570

    +

    Example 570

    made-up-scheme://foo,bar

    -

    Example 571

    +

    Example 571

    http://../

    -

    Example 572

    +

    Example 572

    localhost:5001/foo

    -

    Example 573

    +

    Example 573

    Spaces are not allowed in autolinks:

    <http://foo.bar/baz bim>

    -

    Example 574

    +

    Example 574

    Backslash-escapes do not work inside autolinks:

    @@ -50,15 +50,15 @@

    Example 574

    Examples of email autolinks:

    -

    Example 575

    +

    Example 575

    foo@bar.example.com

    -

    Example 576

    +

    Example 576

    foo+special@Bar.baz-bar0.com

    -

    Example 577

    +

    Example 577

    Backslash-escapes do not work inside email autolinks:

    @@ -66,26 +66,26 @@

    Example 577

    These are not autolinks:

    -

    Example 578

    +

    Example 578

    <>

    -

    Example 579

    +

    Example 579

    < http://foo.bar >

    -

    Example 580

    +

    Example 580

    <m:abc>

    -

    Example 581

    +

    Example 581

    <foo.bar.baz>

    -

    Example 582

    +

    Example 582

    http://example.com

    -

    Example 583

    +

    Example 583

    foo@bar.example.com

    \ No newline at end of file diff --git a/test/new/cm_blockquotes.html b/test/new/cm_blockquotes.html index f63e5e0067..3ea0e8c58c 100644 --- a/test/new/cm_blockquotes.html +++ b/test/new/cm_blockquotes.html @@ -1,32 +1,32 @@ -

    Example 191

    +

    Example 191

    -

    Foo

    +

    Foo

    bar baz

    -

    Example 192

    +

    Example 192

    The spaces after the > characters can be omitted:

    -

    Foo

    +

    Foo

    bar baz

    -

    Example 193

    +

    Example 193

    The > characters can be indented 1-3 spaces:

    -

    Foo

    +

    Foo

    bar baz

    -

    Example 194

    +

    Example 194

    Four spaces gives us a code block:

    @@ -34,17 +34,17 @@

    Example 194

    > bar > baz
    -

    Example 195

    +

    Example 195

    The Laziness clause allows us to omit the > before paragraph continuation text:

    -

    Foo

    +

    Foo

    bar baz

    -

    Example 196

    +

    Example 196

    A block quote can contain some lazy and some non-lazy continuation lines:

    @@ -54,7 +54,7 @@

    Example 196

    foo

    -

    Example 197

    +

    Example 197

    Laziness only applies to lines that would have been continuations of paragraphs had they been prepended with block quote markers. For example, the > cannot be omitted in the second line of

    @@ -65,14 +65,14 @@

    Example 197

    without changing the meaning.

    -

    Example 198

    +

    Example 198

    Similarly, if we omit the `>` in the second line then the block quote ends after the first line:
     
     > - foo
     - bar
    -

    Example 199

    +

    Example 199

    For the same reason, we can’t omit the > in front of subsequent lines of an indented or fenced code block:

    @@ -81,7 +81,7 @@

    Example 199

    bar
    -

    Example 200

    +

    Example 200

    > ```
     foo
    @@ -93,7 +93,7 @@ 

    Example 200

    <p>foo</p> <pre><code></code></pre>
    -

    Example 201

    +

    Example 201

    > foo
         - bar
     
    @@ -102,19 +102,19 @@ 

    Example 201

    - bar</p> </blockquote>
    -

    Example 202

    +

    Example 202

    A block quote can be empty:

    -

    Example 203

    +

    Example 203

    -

    Example 204

    +

    Example 204

    A block quote can have initial or final blank lines:

    @@ -123,7 +123,7 @@

    Example 204

    -

    Example 205

    +

    Example 205

    A blank line always separates block quotes:

    @@ -134,7 +134,7 @@

    Example 205

    bar

    -

    Example 206

    +

    Example 206

    Consecutiveness means that if we put these block quotes together, we get a single block quote:

    @@ -143,7 +143,7 @@

    Example 206

    bar

    -

    Example 207

    +

    Example 207

    To get a block quote with two paragraphs, use:

    @@ -152,7 +152,7 @@

    Example 207

    bar

    -

    Example 208

    +

    Example 208

    Block quotes can interrupt paragraphs:

    @@ -161,7 +161,7 @@

    Example 208

    bar

    -

    Example 209

    +

    Example 209

    In general, blank lines are not needed before or after block quotes:

    @@ -173,7 +173,7 @@

    Example 209

    bbb

    -

    Example 210

    +

    Example 210

    However, because of laziness, a blank line is needed between a block quote and a following paragraph:

    @@ -182,21 +182,21 @@

    Example 210

    baz

    -

    Example 211

    +

    Example 211

    bar

    baz

    -

    Example 212

    +

    Example 212

    bar

    baz

    -

    Example 213

    +

    Example 213

    It is a consequence of the Laziness rule that any number of initial >s may be omitted on a continuation line of a nested block quote:

    @@ -209,7 +209,7 @@

    Example 213

    -

    Example 214

    +

    Example 214

    @@ -221,7 +221,7 @@

    Example 214

    -

    Example 215

    +

    Example 215

    When including an indented code block in a block quote, remember that the block quote marker includes both the > and a following space. So five spaces are needed after the >:

    diff --git a/test/new/cm_html_blocks.html b/test/new/cm_html_blocks.html index 80fdff579f..b7eda8af9c 100644 --- a/test/new/cm_html_blocks.html +++ b/test/new/cm_html_blocks.html @@ -1,6 +1,6 @@ -

    HTML blocks

    +

    HTML blocks

    -

    Example 116

    +

    Example 116

    @@ -9,7 +9,7 @@ 

    Example 116

    -

    Example 117

    +

    Example 117

    @@ -20,113 +20,113 @@

    Example 117

    okay.

    -

    Example 118

    +

    Example 118

    *hello* -

    Example 119

    +

    Example 119

    *foo* -

    Example 120

    +

    Example 120

    Markdown

    -

    Example 121

    +

    Example 121

    -

    Example 122

    +

    Example 122

    -

    Example 123

    +

    Example 123

    *foo*

    bar

    -

    Example 124

    +

    Example 124

    Example 125 +

    Example 125

    Example 126 +

    Example 126

    Example 127 +

    Example 127

    -

    Example 128

    +

    Example 128

    foo
    -

    Example 129

    +

    Example 129

    ``` c int x = 33; ``` -

    Example 130

    +

    Example 130

    *bar* -

    Example 131

    +

    Example 131

    *bar* -

    Example 132

    +

    Example 132

    *bar* -

    Example 133

    +

    Example 133

    *bar* -

    Example 134

    +

    Example 134

    *foo* -

    Example 135

    +

    Example 135

    foo

    -

    Example 136

    +

    Example 136

    foo

    -

    Example 137

    +

    Example 137

    
     import Text.HTML.TagSoup
    @@ -136,7 +136,7 @@ 

    Example 137

    okay

    -

    Example 138

    +

    Example 138

    okay

    -

    Example 139

    +

    Example 139

    okay

    -

    Example 141

    +

    Example 141

    @@ -163,7 +163,7 @@

    Example 141

    bar

    -

    Example 142

    +

    Example 142

    • @@ -172,23 +172,23 @@

      Example 142

    • foo
    -

    Example 143

    +

    Example 143

    foo

    -

    Example 144

    +

    Example 144

    *bar*

    baz

    -

    Example 145

    +

    Example 145

    1. *bar* -

    Example 146

    +

    Example 146

    okay

    -

    Example 147

    +

    Example 147

    Example 147 ?>

    okay

    -

    Example 148

    +

    Example 148

    -

    Example 149

    +

    Example 149

    Example 149 ]]>

    okay

    -

    Example 150

    +

    Example 150

    <!-- foo -->
     
    -

    Example 151

    +

    Example 151

    <div>
     
    -

    Example 152

    +

    Example 152

    Foo

    bar
    -

    Example 153

    +

    Example 153

    bar
    *foo* -

    Example 154

    +

    Example 154

    Foo baz

    -

    Example 155

    +

    Example 155

    Emphasized text.

    -

    Example 156

    +

    Example 156

    *Emphasized* text.
    -

    Example 157

    +

    Example 157

    @@ -279,7 +279,7 @@

    Example 157

    -

    Example 158

    +

    Example 158

    @@ -290,7 +290,7 @@

    Example 158

    -

    Example 140

    +

    Example 140

    If there is no matching end tag, the block will end at the end of the document (or the enclosing block quote or list item):

    diff --git a/test/new/cm_link_defs.html b/test/new/cm_link_defs.html index 8763ca155c..64bd0d0a04 100644 --- a/test/new/cm_link_defs.html +++ b/test/new/cm_link_defs.html @@ -1,20 +1,20 @@ -

    Example 159

    +

    Example 159

    foo159

    -

    Example 160

    +

    Example 160

    foo160

    -

    Example 161

    +

    Example 161

    Foo161*bar]

    -

    Example 162

    +

    Example 162

    Foo162 bar

    -

    Example 163

    +

    Example 163

    Example 163 line2 ">foo163

    -

    Example 164

    +

    Example 164

    [foo164]: /url 'title

    with blank line'

    [foo164]

    -

    Example 165

    +

    Example 165

    foo165

    -

    Example 166

    +

    Example 166

    [foo166]:

    [foo166]

    -

    Example 167

    +

    Example 167

    [foo167]: /url\bar\*baz "foo\"bar\baz"
     
    @@ -47,68 +47,68 @@ 

    Example 167

    <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo167</a></p>
    -

    Example 168

    +

    Example 168

    foo168

    -

    Example 169

    +

    Example 169

    foo169

    -

    Example 170

    +

    Example 170

    Foo170

    -

    Example 171

    +

    Example 171

    αγω

    -

    Example 172

    +

    Example 172

    -

    Example 173

    +

    Example 173

    bar

    -

    Example 174

    +

    Example 174

    [foo174]: /url "title" ok

    -

    Example 175

    +

    Example 175

    "title" ok

    -

    Example 176

    +

    Example 176

    [foo176]: /url "title"
     

    [foo176]

    -

    Example 177

    +

    Example 177

    [foo177]: /url
     

    [foo177]

    -

    Example 178

    +

    Example 178

    Foo [bar178]: /baz

    [bar178]

    -

    Example 179

    +

    Example 179

    -

    Foo179

    +

    Foo179

    bar

    -

    Example 180

    +

    Example 180

    foo180, bar180, baz180

    -

    Example 181

    +

    Example 181

    foo181

    diff --git a/test/new/cm_links.html b/test/new/cm_links.html index 08c65e7cdd..80d4ebf7d2 100644 --- a/test/new/cm_links.html +++ b/test/new/cm_links.html @@ -1,397 +1,397 @@ -

    Links

    +

    Links

    -

    Example 459

    +

    Example 459

    link

    -

    Example 460

    +

    Example 460

    link

    -

    Example 461

    +

    Example 461

    link

    -

    Example 462

    +

    Example 462

    link

    -

    Example 463

    +

    Example 463

    [link](/my uri)

    -

    Example 464

    +

    Example 464

    [link](</my uri>)

    -

    Example 465

    +

    Example 465

    [link](foo bar)

    -

    Example 466

    +

    Example 466

    [link]()

    -

    Example 467

    +

    Example 467

    link

    -

    Example 4680

    +

    Example 4680

    ONE LEVEL of parentheses are allowed without escaping, as long as they are balanced:

    link

    -

    Example 469

    +

    Example 469

    link

    -

    Example 470

    +

    Example 470

    However, if you have ANY unbalanced parentheses, you need to escape or use the <...> form:

    link

    -

    Example 471

    +

    Example 471

    link

    -

    Example 472

    +

    Example 472

    link

    link

    link

    -

    Example 473

    +

    Example 473

    link

    -

    Example 4740

    +

    Example 4740

    link

    -

    Example 475

    +

    Example 475

    link

    -

    Example 476

    +

    Example 476

    link link link

    -

    Example 477

    +

    Example 477

    link

    -

    Example 479

    +

    Example 479

    [link](/url "title "and" title")

    -

    Example 480

    +

    Example 480

    link

    -

    Example 481

    +

    Example 481

    link

    -

    Example 482

    +

    Example 482

    [link] (/uri)

    -

    Example 4830

    +

    Example 4830

    The link text may contain ONE LEVEL of balanced brackets, but not unbalanced ones, unless they are escaped:

    link [foo4830]

    -

    Example 484

    +

    Example 484

    [link] bar](/uri)

    -

    Example 485

    +

    Example 485

    [link bar

    -

    Example 486

    +

    Example 486

    link [bar

    -

    Example 487

    +

    Example 487

    link foo bar #

    -

    Example 488

    +

    Example 488

    moon

    -

    Example 493

    +

    Example 493

    foo *bar

    -

    Example 494

    +

    Example 494

    foo [bar baz]

    -

    Example 498

    +

    Example 498

    foo

    -

    Example 4990

    +

    Example 4990

    link [foo499]

    -

    Example 500

    +

    Example 500

    link [bar

    -

    Example 501

    +

    Example 501

    link foo bar #

    -

    Example 502

    +

    Example 502

    moon

    -

    Example 5030

    +

    Example 5030

    foo bar

    -

    Example 504

    +

    Example 504

    foo bar baz

    -

    Example 506

    +

    Example 506

    foo *bar

    -

    Example 510

    +

    Example 510

    foo

    -

    Example 511

    +

    Example 511

    Толпой is a Russian word.

    -

    Example 512

    +

    Example 512

    Baz

    -

    Example 513

    +

    Example 513

    [foo513] bar513

    -

    Example 514

    +

    Example 514

    [foo514] bar514

    -

    Example 515

    +

    Example 515

    bar

    -

    Example 516

    +

    Example 516

    [bar][foo!516]

    -

    Example 517

    +

    Example 517

    [foo517][ref[517]

    [ref[517]: /uri

    -

    Example 518

    +

    Example 518

    [foo518][ref[bar518]518]

    [ref[bar518]518]: /uri

    -

    Example 519

    +

    Example 519

    [[[foo519]]]

    [[[foo519]]]: /url

    -

    Example 520

    +

    Example 520

    foo

    -

    Example 521

    +

    Example 521

    bar\

    -

    Example 522

    +

    Example 522

    []

    []: /uri

    -

    Example 523

    +

    Example 523

    [ ]

    [ ]: /uri

    -

    Example 524

    +

    Example 524

    foo

    -

    Example 525

    +

    Example 525

    foo bar

    -

    Example 526

    +

    Example 526

    Foo

    -

    Example 527

    +

    Example 527

    foo []

    -

    Example 528

    +

    Example 528

    foo

    -

    Example 529

    +

    Example 529

    foo bar

    -

    Example 530

    +

    Example 530

    [foo bar]

    -

    Example 531

    +

    Example 531

    [[bar foo531

    -

    Example 532

    +

    Example 532

    Foo

    -

    Example 533

    +

    Example 533

    foo533 bar

    -

    Example 534

    +

    Example 534

    [foo]

    -

    Example 536

    +

    Example 536

    foo536

    -

    Example 537

    +

    Example 537

    foo537

    -

    Example 538

    +

    Example 538

    foo538

    -

    Example 539

    +

    Example 539

    foo539(not a link)

    -

    Example 540

    +

    Example 540

    [foo540]bar540

    -

    Example 541

    +

    Example 541

    foo541baz541

    -

    Example 542

    +

    Example 542

    [foo542]bar542

    -

    Example 543

    +

    Example 543

    foo543

    -

    Example 5440

    +

    Example 5440

    foo *bar*544

    -

    Example 5450

    +

    Example 5450

    foo ![bar](/url)

    -

    Example 5460

    +

    Example 5460

    foo [bar](/url)

    -

    Example 5470

    +

    Example 5470

    foo *bar*547

    -

    Example 5480

    +

    Example 5480

    foo *bar*

    -

    Example 549

    +

    Example 549

    foo

    -

    Example 550

    +

    Example 550

    My foo bar

    -

    Example 551

    +

    Example 551

    foo

    -

    Example 552

    +

    Example 552

    -

    Example 553

    +

    Example 553

    foo

    -

    Example 554

    +

    Example 554

    foo

    -

    Example 555

    +

    Example 555

    foo

    -

    Example 5560

    +

    Example 5560

    *foo* bar

    -

    Example 557

    +

    Example 557

    Foo

    -

    Example 558

    +

    Example 558

    foo []

    -

    Example 559

    +

    Example 559

    foo

    -

    Example 5600

    +

    Example 5600

    *foo* bar

    -

    Example 561

    +

    Example 561

    ![[foo561]]

    [[foo561]]: /url "title"

    -

    Example 562

    +

    Example 562

    Foo

    -

    Example 563

    +

    Example 563

    ![foo]

    -

    Example 564

    +

    Example 564

    !foo

    diff --git a/test/new/cm_raw_html.html b/test/new/cm_raw_html.html index f3da050ef5..2eb6187736 100644 --- a/test/new/cm_raw_html.html +++ b/test/new/cm_raw_html.html @@ -1,77 +1,77 @@ -

    Raw HTML

    +

    Raw HTML

    -

    Example 584

    +

    Example 584

    -

    Example 585

    +

    Example 585

    -

    Example 586

    +

    Example 586

    -

    Example 587

    +

    Example 587

    -

    Example 588

    +

    Example 588

    Foo

    -

    Example 589

    +

    Example 589

    <33> <__>

    -

    Example 590

    +

    Example 590

    <a h*#ref="hi">

    -

    Example 591

    +

    Example 591

    <a href="hi'> <a href=hi'>

    -

    Example 592

    +

    Example 592

    < a>< foo><bar/ >

    -

    Example 593

    +

    Example 593

    <a href='bar'title=title>

    -

    Example 594

    +

    Example 594

    -

    Example 595

    +

    Example 595

    </a href="foo">

    -

    Example 596

    +

    Example 596

    foo

    -

    Example 599

    +

    Example 599

    foo

    -

    Example 600

    +

    Example 600

    foo

    -

    Example 601

    +

    Example 601

    foo &<]]>

    -

    Example 602

    +

    Example 602

    foo

    -

    Example 603

    +

    Example 603

    foo

    diff --git a/test/new/cm_thematic_breaks.html b/test/new/cm_thematic_breaks.html index ec3f9f7462..b88de9ccaf 100644 --- a/test/new/cm_thematic_breaks.html +++ b/test/new/cm_thematic_breaks.html @@ -1,73 +1,73 @@ -

    Thematic breaks

    +

    Thematic breaks

    -

    Example 13

    +

    Example 13




    -

    Example 14

    +

    Example 14

    +++

    -

    Example 15

    +

    Example 15

    ===

    -

    Example 16

    +

    Example 16

    -- ** __

    -

    Example 17

    +

    Example 17




    -

    Example 18

    +

    Example 18

    ***
     
    -

    Example 19

    +

    Example 19

    Foo ***

    -

    Example 20

    +

    Example 20


    -

    Example 21

    +

    Example 21


    -

    Example 22

    +

    Example 22


    -

    Example 23

    +

    Example 23


    -

    Example 24

    +

    Example 24


    -

    Example 25

    +

    Example 25

    _ _ _ _ a

    a------

    ---a---

    -

    Example 26

    +

    Example 26

    -

    -

    Example 27

    +

    Example 27

    • foo
    • @@ -77,18 +77,18 @@

      Example 27

    • bar
    -

    Example 28

    +

    Example 28

    Foo


    bar

    -

    Example 29

    +

    Example 29

    -

    Foo

    +

    Foo

    bar

    -

    Example 30

    +

    Example 30

    • Foo
    • @@ -98,7 +98,7 @@

      Example 30

    • Bar
    -

    Example 31

    +

    Example 31

    • Foo
    • diff --git a/test/new/gfm_autolinks.html b/test/new/gfm_autolinks.html index 9058f2f201..9545671503 100644 --- a/test/new/gfm_autolinks.html +++ b/test/new/gfm_autolinks.html @@ -12,18 +12,18 @@

      link with nothing http://example.com/hello-world

      -

      Example 597

      +

      Example 597

      The scheme http will be inserted automatically:

      www.commonmark.org

      -

      Example 598

      +

      Example 598

      After a valid domain, zero or more non-space non-< characters may follow:

      Visit www.commonmark.org/help for more information.

      -

      Example 599

      +

      Example 599

      Trailing punctuation (specifically, ?, !, ., ,, :, *, _, and ~) will not be considered part of the autolink, though they may be included in the interior of the link:

      @@ -31,29 +31,29 @@

      Example 599

      Visit www.commonmark.org/a.b.

      -

      Example 600

      +

      Example 600

      www.google.com/search?q=Markup+(business)

      (www.google.com/search?q=Markup+(business))

      -

      Example 601

      +

      Example 601

      www.google.com/search?q=(business))+ok

      -

      Example 602

      +

      Example 602

      www.google.com/search?q=commonmark&hl=en

      www.google.com/search?q=commonmark&

      -

      Example 603

      +

      Example 603

      < immediately ends an autolink.

      www.commonmark.org/he<lp

      -

      Example 604

      +

      Example 604

      http://commonmark.org

      @@ -63,15 +63,15 @@

      Example 604

      Extended email autolinks:

      -

      Example 605

      +

      Example 605

      foo@bar.baz

      -

      Example 606

      +

      Example 606

      hello@mail+xyz.example isn't valid, but hello+xyz@mail.example is.

      -

      Example 607

      +

      Example 607

      a.b-c_d@a.b

      diff --git a/test/new/gfm_code_hr_list.html b/test/new/gfm_code_hr_list.html index d15b52fe02..4ec88f465d 100644 --- a/test/new/gfm_code_hr_list.html +++ b/test/new/gfm_code_hr_list.html @@ -1,4 +1,4 @@ -

      foo

      +

      foo

      1. bar:

        diff --git a/test/new/gfm_hashtag.html b/test/new/gfm_hashtag.html index 6f9e43f52d..7a4ca2bd62 100644 --- a/test/new/gfm_hashtag.html +++ b/test/new/gfm_hashtag.html @@ -1,5 +1,5 @@

        #header

        -

        header1

        +

        header1

        -

        header2

        +

        header2

        diff --git a/test/new/headings_id.html b/test/new/headings_id.html index 6f0ae49e32..3c013e9e0f 100644 --- a/test/new/headings_id.html +++ b/test/new/headings_id.html @@ -1,13 +1,13 @@ - +

        Heading with a link

        -

        Heading with some italic text

        +

        Heading with some italic text

        -

        Or some strong

        +

        Or some strong

        (which doesn't really make any difference, here)

        -

        Or even code

        +

        Or even code

        -

        What about strikethrough

        +

        What about strikethrough

        - \ No newline at end of file +

        And a ref link

        \ No newline at end of file diff --git a/test/new/html_comments.html b/test/new/html_comments.html index 872b45f6ae..35db0b31d5 100644 --- a/test/new/html_comments.html +++ b/test/new/html_comments.html @@ -1,54 +1,54 @@ -

        Example 1

        +

        Example 1

        -

        Example 2

        +

        Example 2

        -

        Example 3

        +

        Example 3

        -

        Example 4

        +

        Example 4

        -

        Example 5

        +

        Example 5

        -

        Example 6

        +

        Example 6

        -

        Example 7

        +

        Example 7

        -

        Example 8

        +

        Example 8

        -

        Example 9

        +

        Example 9

        -

        Example 10

        +

        Example 10

        -

        Example 11

        +

        Example 11

        <!-- too much indentation -->
         
        -

        Example 12

        +

        Example 12

        <!--> not a comment -->

        diff --git a/test/new/main.html b/test/new/main.html index 8f78c4d30e..a1e20f6a29 100644 --- a/test/new/main.html +++ b/test/new/main.html @@ -1,4 +1,4 @@ -

        A heading

        Just a note, I've found that I can't test my markdown parser vs others. For example, both markdown.js and showdown code blocks in lists wrong. They're also completely inconsistent with regards to paragraphs in list items.

        A link. Not anymore.

      2. New List Item 3 The last item
  • List Item 3 The final item.

  • List Item 4 The real final item.

  • Paragraph.


    Another blockquote! I really need to get more creative with mockup text.. markdown.js breaks here again

    Another Heading

    Hello world. Here is a link. And an image alt.

    Code goes here.
     Lots of it...
    diff --git a/test/new/nogfm_hashtag.html b/test/new/nogfm_hashtag.html index ecd95d692e..9b53c8e230 100644 --- a/test/new/nogfm_hashtag.html +++ b/test/new/nogfm_hashtag.html @@ -1,5 +1,5 @@ -

    header

    +

    header

    -

    header1

    +

    header1

    -

    header2

    +

    header2

    diff --git a/test/new/relative_urls.html b/test/new/relative_urls.html index ee7f3bff0e..eb67c1f99d 100644 --- a/test/new/relative_urls.html +++ b/test/new/relative_urls.html @@ -1,35 +1,35 @@ -

    Absolutization of RFC 3986 URIs

    +

    Absolutization of RFC 3986 URIs

    -

    Absolute URI

    +

    Absolute URI

    section 4.3

    -

    Network-path reference

    +

    Network-path reference

    section 4.2

    -

    Absolute path

    +

    Absolute path

    section 4.2

    -

    Relative path

    +

    Relative path

    section 4.2

    -

    Dot-relative path

    +

    Dot-relative path

    section 3.3

    section 3.3

    -

    Same-document query

    +

    Same-document query

    section 4.4

    -

    Same-document fragment

    +

    Same-document fragment

    section 4.4

    -

    Empty

    +

    Empty

    section 4.2

    diff --git a/test/new/toplevel_paragraphs.html b/test/new/toplevel_paragraphs.html index d15bfccef0..ca82fbba2c 100644 --- a/test/new/toplevel_paragraphs.html +++ b/test/new/toplevel_paragraphs.html @@ -9,10 +9,10 @@

    paragraph before head with hash

    -

    how are you

    +

    how are you

    paragraph before head with equals

    -

    how are you

    +

    how are you

    paragraph before blockquote

    text for blockquote

    diff --git a/test/slugger.js b/test/slugger.js new file mode 100644 index 0000000000..be492545be --- /dev/null +++ b/test/slugger.js @@ -0,0 +1,23 @@ +/** + * Slugger + */ +function Slugger () { + this.seen = {}; +} + +/** + * Generate a unique slug. + */ +Slugger.prototype.slug = function (raw) { + var slug = raw.toLowerCase().replace(/[^\w]+/g, '-'); + var count = this.seen.hasOwnProperty(slug) ? this.seen[slug] + 1 : 0; + this.seen[slug] = count; + + if (count > 0) { + slug = slug + '-' + count; + } + + return slug; +}; + +module.exports = Slugger; diff --git a/test/specs/commonmark/commonmark-spec.js b/test/specs/commonmark/commonmark-spec.js index 7cca2d830b..9052923a62 100644 --- a/test/specs/commonmark/commonmark-spec.js +++ b/test/specs/commonmark/commonmark-spec.js @@ -15,7 +15,7 @@ Messenger.prototype.test = function(spec, section, ignore) { var shouldFail = ~ignore.indexOf(spec.example); it('should ' + (shouldFail ? 'fail' : 'pass') + ' example ' + spec.example, function() { var expected = spec.html; - var actual = marked(spec.markdown, { headerIds: false, xhtml: true }); + var actual = marked(spec.markdown, { slugger: null, xhtml: true }); since(messenger.message(spec, expected, actual)).expect( htmlDiffer.isEqual(expected, actual) ).toEqual(!shouldFail); diff --git a/test/specs/gfm/gfm-spec.js b/test/specs/gfm/gfm-spec.js index 83340db0fa..bf2aed922c 100644 --- a/test/specs/gfm/gfm-spec.js +++ b/test/specs/gfm/gfm-spec.js @@ -15,7 +15,7 @@ Messenger.prototype.test = function(spec, section, ignore) { var shouldFail = ~ignore.indexOf(spec.example); it('should ' + (shouldFail ? 'fail' : 'pass') + ' example ' + spec.example, function() { var expected = spec.html; - var actual = marked(spec.markdown, { headerIds: false, xhtml: false }); + var actual = marked(spec.markdown, { slugger: null, xhtml: false }); since(messenger.message(spec, expected, actual)).expect( htmlDiffer.isEqual(expected, actual) ).toEqual(!shouldFail); diff --git a/test/specs/marked/marked-spec.js b/test/specs/marked/marked-spec.js index acc4495c7f..40f54b54e9 100644 --- a/test/specs/marked/marked-spec.js +++ b/test/specs/marked/marked-spec.js @@ -22,7 +22,7 @@ Messenger.prototype.test = function(spec, section, ignore) { var shouldFail = ~ignore.indexOf(spec.example); it('should ' + (shouldFail ? 'fail' : 'pass') + ' example ' + spec.example, function() { var expected = spec.html; - var actual = marked(spec.markdown, { headerIds: false, xhtml: true }); + var actual = marked(spec.markdown, { slugger: null, xhtml: true }); since(messenger.message(spec, expected, actual)).expect( htmlDiffer.isEqual(expected, actual) ).toEqual(!shouldFail); diff --git a/test/unit/marked-spec.js b/test/unit/marked-spec.js index 97789afe09..c7ae36c789 100644 --- a/test/unit/marked-spec.js +++ b/test/unit/marked-spec.js @@ -1,14 +1,21 @@ var marked = require('../../lib/marked.js'); +var Slugger = require('../../test/slugger.js'); -describe('Test heading ID functionality', function() { - it('should add id attribute by default', function() { - var renderer = new marked.Renderer(marked.defaults); - var header = renderer.heading('test', 1, 'test'); - expect(header).toBe('

    test

    \n'); +describe('Test slugger ID functionality', function() { + it('should add id attribute when slugger option defined', function() { + var slugger = new Slugger(); + var html = marked('# One\n\n# Two', { slugger: slugger }); + expect(html).toBe('

    One

    \n

    Two

    \n'); }); - it('should NOT add id attribute when options set false', function() { - var renderer = new marked.Renderer({ headerIds: false }); + it('should add id attribute w/count when slugger option defined', function() { + var slugger = new Slugger(); + var html = marked('# head\n\n# head\n\n# head', { slugger: slugger }); + expect(html).toBe('

    head

    \n

    head

    \n

    head

    \n'); + }); + + it('should NOT add ID attribute with defaults', function() { + var renderer = new marked.Renderer(marked.defaults); var header = renderer.heading('test', 1, 'test'); expect(header).toBe('

    test

    \n'); });