Skip to content

Commit

Permalink
Change to comply to CommonMark
Browse files Browse the repository at this point in the history
* Change to comply to CommonMark — prefer the first when duplicate
  definitions are found
* Replace `xtend` w/ `Object.assign`
* Refactor old stuff in tests
  • Loading branch information
wooorm committed Oct 3, 2020
1 parent 5fec432 commit 0b1cd0a
Show file tree
Hide file tree
Showing 21 changed files with 141 additions and 443 deletions.
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

var xtend = require('xtend')
var toHast = require('mdast-util-to-hast')
var toHtml = require('hast-util-to-html')
var sanitize = require('hast-util-sanitize')
Expand All @@ -17,7 +16,11 @@ function plugin(options) {

function compiler(node, file) {
var root = node && node.type && node.type === 'root'
var hast = toHast(node, {allowDangerousHtml: !clean, handlers: handlers})
var hast = toHast(node, {
allowDangerousHtml: !clean,
handlers: handlers,
commonmark: true
})
var result

if (file.extname) {
Expand All @@ -28,10 +31,13 @@ function plugin(options) {
hast = sanitize(hast, schema)
}

result = toHtml(hast, xtend(settings, {allowDangerousHtml: !clean}))
result = toHtml(
hast,
Object.assign({}, settings, {allowDangerousHtml: !clean})
)

// Add an eof eol.
if (root && result.charAt(result.length - 1) !== '\n') {
if (root && result && /[^\r\n]/.test(result.charAt(result.length - 1))) {
result += '\n'
}

Expand Down
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,23 @@
"dependencies": {
"hast-util-sanitize": "^3.0.0",
"hast-util-to-html": "^7.0.0",
"mdast-util-to-hast": "^9.0.0",
"xtend": "^4.0.1"
"mdast-util-to-hast": "^9.0.0"
},
"devDependencies": {
"browserify": "^16.0.0",
"commonmark.json": "^0.29.0",
"dtslint": "^4.0.0",
"is-hidden": "^1.0.0",
"not": "^0.1.0",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"remark": "^12.0.0",
"rehype-parse": "^7.0.0",
"rehype-stringify": "^8.0.0",
"remark": "^13.0.0-alpha.0",
"remark-cli": "^8.0.0",
"remark-github": "^9.0.0",
"remark-preset-wooorm": "^7.0.0",
"remark-slug": "^6.0.0",
"remark-toc": "^7.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
Expand Down Expand Up @@ -86,7 +89,8 @@
"esnext": false,
"rules": {
"unicorn/no-fn-reference-in-iterator": "off",
"unicorn/prefer-includes": "off"
"unicorn/prefer-includes": "off",
"unicorn/prefer-optional-catch-binding": "off"
},
"ignores": [
"remark-html.js"
Expand Down
11 changes: 9 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
> It’s probably smarter to use `remark-rehype` directly and benefit from the
> [**rehype**][rehype] ecosystem.
## Note!

This plugin is ready for the new parser in remark
([`remarkjs/remark#536`](https://github.com/remarkjs/remark/pull/536)).
The current and previous version of the plugin works with the current and
previous version of remark.

## Install

[npm][]:
Expand Down Expand Up @@ -79,7 +86,7 @@ All options except for `sanitize` and `handlers` are passed to
###### `options.handlers`

Object mapping [mdast][] [nodes][mdast-node] to functions handling them.
This option is passed to [`mdast-util-to-html`][to-mdast-handlers].
This option is passed to [`mdast-util-to-hast`][to-hast-handlers].

###### `options.sanitize`

Expand Down Expand Up @@ -264,7 +271,7 @@ abide by its terms.

[to-html]: https://github.com/syntax-tree/hast-util-to-html

[to-mdast-handlers]: https://github.com/syntax-tree/mdast-util-to-hast#optionshandlers
[to-hast-handlers]: https://github.com/syntax-tree/mdast-util-to-hast#optionshandlers

[sanitize]: https://github.com/syntax-tree/hast-util-sanitize

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/blockquote/output.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h1>Block Quote</h1>
</li>
<li>
<p>Normal list</p>
<p>Paragraph.</p>
</li>
</ul>
<p>Paragraph.</p>
</blockquote>
8 changes: 4 additions & 4 deletions test/fixtures/code/output.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ <h1>Code</h1>
</code></pre>
<pre><code class="language-empty"></code></pre>
<pre><code class="language-tabs"> two spaces
one
two
one
mixed.
one
two
one
mixed.
</code></pre>
2 changes: 1 addition & 1 deletion test/fixtures/entities-named/output.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h1>Entities</h1>
<pre><code class="language-AT&amp;T">Something in the AT&amp;T language
</code></pre>
<p>Automatic links:</p>
<p><a href="http://at&amp;t.com">http://at&amp;t.com</a>, <a href="http://at&amp;t.com">http://at&amp;t.com</a>, and <a href="http://at&amp;t.com">http://at&amp;t.com</a>.</p>
<p><a href="http://at&amp;amp;t.com">http://at&amp;amp;t.com</a>, <a href="http://at&amp;#x26;t.com">http://at&amp;#x26;t.com</a>, and <a href="http://at&amp;t.com">http://at&amp;t.com</a>.</p>
<p>Link <code>href</code>:</p>
<p><a href="http://at&amp;t.com">With entity</a>, <a href="http://at&amp;t.com">numeric entity</a>, <a href="http://at&amp;t.com">without entity</a>.</p>
<p>Link <code>title</code>:</p>
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/entities-numerical/output.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h1>Entities</h1>
<pre><code class="language-AT&#x26;T">Something in the AT&#x26;T language
</code></pre>
<p>Automatic links:</p>
<p><a href="http://at&#x26;t.com">http://at&#x26;t.com</a>, <a href="http://at&#x26;t.com">http://at&#x26;t.com</a>, and <a href="http://at&#x26;t.com">http://at&#x26;t.com</a>.</p>
<p><a href="http://at&#x26;amp;t.com">http://at&#x26;amp;t.com</a>, <a href="http://at&#x26;#x26;t.com">http://at&#x26;#x26;t.com</a>, and <a href="http://at&#x26;t.com">http://at&#x26;t.com</a>.</p>
<p>Link <code>href</code>:</p>
<p><a href="http://at&#x26;t.com">With entity</a>, <a href="http://at&#x26;t.com">numeric entity</a>, <a href="http://at&#x26;t.com">without entity</a>.</p>
<p>Link <code>title</code>:</p>
Expand Down
30 changes: 15 additions & 15 deletions test/fixtures/escape/output.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@
<p>Pipe: |</p>
<p>Tilde: ~</p>
<p><strong>Commonmark:</strong></p>
<p>Quote: \"</p>
<p>Dollar: \$</p>
<p>Percentage: \%</p>
<p>Ampersand: \&#x26;</p>
<p>Single quote: \'</p>
<p>Comma: \,</p>
<p>Forward slash: \/</p>
<p>Colon: \:</p>
<p>Semicolon: \;</p>
<p>Less-than: \&#x3C;</p>
<p>Equals: \=</p>
<p>Question mark: \?</p>
<p>At-sign: \@</p>
<p>Caret: \^</p>
<p>New line: \
<p>Quote: "</p>
<p>Dollar: $</p>
<p>Percentage: %</p>
<p>Ampersand: &#x26;</p>
<p>Single quote: '</p>
<p>Comma: ,</p>
<p>Forward slash: /</p>
<p>Colon: :</p>
<p>Semicolon: ;</p>
<p>Less-than: &#x3C;</p>
<p>Equals: =</p>
<p>Question mark: ?</p>
<p>At-sign: @</p>
<p>Caret: ^</p>
<p>New line: <br>
only works in paragraphs.</p>
<p>Two spaces:<br>
only works in paragraphs.</p>
3 changes: 0 additions & 3 deletions test/fixtures/footnotes/config.json

This file was deleted.

83 changes: 0 additions & 83 deletions test/fixtures/footnotes/input.md

This file was deleted.

84 changes: 0 additions & 84 deletions test/fixtures/footnotes/output.html

This file was deleted.

8 changes: 4 additions & 4 deletions test/fixtures/images/output.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<p><img src="http://example.com/favicon.ico" alt="Example" title="Example Image"></p>
<p><img src="http://example.com/favicon.ico" alt="Example"></p>
<p><img src="http://example.com/favicon.ico"></p>
<p><img src="" title="Example Image"></p>
<p><img src=""></p>
<p><img src=""></p>
<p><img src="http://example.com/favicon.ico" alt=""></p>
<p><img src="" alt="" title="Example Image"></p>
<p><img src="" alt=""></p>
<p><img src="" alt=""></p>
Loading

0 comments on commit 0b1cd0a

Please sign in to comment.