From 0b1cd0afd078825a8674a53917c823fd2ffc02fd Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sat, 3 Oct 2020 11:08:29 +0200 Subject: [PATCH] Change to comply to CommonMark MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Change to comply to CommonMark — prefer the first when duplicate definitions are found * Replace `xtend` w/ `Object.assign` * Refactor old stuff in tests --- index.js | 14 +- package.json | 12 +- readme.md | 11 +- test/fixtures/blockquote/output.html | 2 +- test/fixtures/code/output.html | 8 +- test/fixtures/entities-named/output.html | 2 +- test/fixtures/entities-numerical/output.html | 2 +- test/fixtures/escape/output.html | 30 +-- test/fixtures/footnotes/config.json | 3 - test/fixtures/footnotes/input.md | 83 ------- test/fixtures/footnotes/output.html | 84 ------- test/fixtures/images/output.html | 8 +- test/fixtures/list/output.html | 34 +-- test/fixtures/references/output.html | 6 +- test/fixtures/tables/config.json | 3 - test/fixtures/tables/input.md | 7 - test/fixtures/tables/output.html | 42 ---- test/fixtures/yaml/config.json | 3 - test/fixtures/yaml/input.md | 5 - test/fixtures/yaml/output.html | 1 - test/index.js | 224 ++++++------------- 21 files changed, 141 insertions(+), 443 deletions(-) delete mode 100644 test/fixtures/footnotes/config.json delete mode 100644 test/fixtures/footnotes/input.md delete mode 100644 test/fixtures/footnotes/output.html delete mode 100644 test/fixtures/tables/config.json delete mode 100644 test/fixtures/tables/input.md delete mode 100644 test/fixtures/tables/output.html delete mode 100644 test/fixtures/yaml/config.json delete mode 100644 test/fixtures/yaml/input.md delete mode 100644 test/fixtures/yaml/output.html diff --git a/index.js b/index.js index fde0912..886aa05 100644 --- a/index.js +++ b/index.js @@ -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') @@ -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) { @@ -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' } diff --git a/package.json b/package.json index ccffb64..98f6b39 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" diff --git a/readme.md b/readme.md index c956f9d..a21b2c7 100644 --- a/readme.md +++ b/readme.md @@ -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][]: @@ -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` @@ -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 diff --git a/test/fixtures/blockquote/output.html b/test/fixtures/blockquote/output.html index 2b7ec57..4a95075 100644 --- a/test/fixtures/blockquote/output.html +++ b/test/fixtures/blockquote/output.html @@ -10,7 +10,7 @@

Block Quote

  • Normal list

    -

    Paragraph.

  • +

    Paragraph.

    diff --git a/test/fixtures/code/output.html b/test/fixtures/code/output.html index e88dc38..f14ae1a 100644 --- a/test/fixtures/code/output.html +++ b/test/fixtures/code/output.html @@ -7,8 +7,8 @@

    Code

      two spaces
    -    one
    -        two
    -    one
    -      mixed.
    +	one
    +		two
    +	one
    +	  mixed.
     
    diff --git a/test/fixtures/entities-named/output.html b/test/fixtures/entities-named/output.html index f8fd278..071e009 100644 --- a/test/fixtures/entities-named/output.html +++ b/test/fixtures/entities-named/output.html @@ -9,7 +9,7 @@

    Entities

    Something in the AT&T language
     

    Automatic links:

    -

    http://at&t.com, http://at&t.com, and http://at&t.com.

    +

    http://at&t.com, http://at&t.com, and http://at&t.com.

    Link href:

    With entity, numeric entity, without entity.

    Link title:

    diff --git a/test/fixtures/entities-numerical/output.html b/test/fixtures/entities-numerical/output.html index 130270a..c4fa09e 100644 --- a/test/fixtures/entities-numerical/output.html +++ b/test/fixtures/entities-numerical/output.html @@ -9,7 +9,7 @@

    Entities

    Something in the AT&T language
     

    Automatic links:

    -

    http://at&t.com, http://at&t.com, and http://at&t.com.

    +

    http://at&t.com, http://at&t.com, and http://at&t.com.

    Link href:

    With entity, numeric entity, without entity.

    Link title:

    diff --git a/test/fixtures/escape/output.html b/test/fixtures/escape/output.html index 6dfcd64..934d342 100644 --- a/test/fixtures/escape/output.html +++ b/test/fixtures/escape/output.html @@ -19,21 +19,21 @@

    Pipe: |

    Tilde: ~

    Commonmark:

    -

    Quote: \"

    -

    Dollar: \$

    -

    Percentage: \%

    -

    Ampersand: \&

    -

    Single quote: \'

    -

    Comma: \,

    -

    Forward slash: \/

    -

    Colon: \:

    -

    Semicolon: \;

    -

    Less-than: \<

    -

    Equals: \=

    -

    Question mark: \?

    -

    At-sign: \@

    -

    Caret: \^

    -

    New line: \ +

    Quote: "

    +

    Dollar: $

    +

    Percentage: %

    +

    Ampersand: &

    +

    Single quote: '

    +

    Comma: ,

    +

    Forward slash: /

    +

    Colon: :

    +

    Semicolon: ;

    +

    Less-than: <

    +

    Equals: =

    +

    Question mark: ?

    +

    At-sign: @

    +

    Caret: ^

    +

    New line:
    only works in paragraphs.

    Two spaces:
    only works in paragraphs.

    diff --git a/test/fixtures/footnotes/config.json b/test/fixtures/footnotes/config.json deleted file mode 100644 index 7f76e14..0000000 --- a/test/fixtures/footnotes/config.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "footnotes": true -} diff --git a/test/fixtures/footnotes/input.md b/test/fixtures/footnotes/input.md deleted file mode 100644 index b347841..0000000 --- a/test/fixtures/footnotes/input.md +++ /dev/null @@ -1,83 +0,0 @@ -# Footnotes - -Alpha bravo[^charlie india]. - -Delta Echo[^1]. - -Foxtrot Golf Hotel[^2]. - -Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum. - -Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum. - -Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum. - -Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum. - -Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum. - -Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum. - -Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum. - -Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum. - -Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum. - -Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum. - -[^1]: This reference style footnote can contains paragraphs. - - - and lists - -[^2]: Normal footnote. diff --git a/test/fixtures/footnotes/output.html b/test/fixtures/footnotes/output.html deleted file mode 100644 index 2d801e1..0000000 --- a/test/fixtures/footnotes/output.html +++ /dev/null @@ -1,84 +0,0 @@ -

    Footnotes

    -

    Alpha bravo3.

    -

    Delta Echo1.

    -

    Foxtrot Golf Hotel2.

    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    -

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod -tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, -quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo -consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse -cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non -proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

    -
    -
    -
      -
    1. -

      This reference style footnote can contains paragraphs.

      -
        -
      • and lists
      • -
      - -
    2. -
    3. -

      Normal footnote.

      - -
    4. -
    5. -charlie india - -
    6. -
    -
    diff --git a/test/fixtures/images/output.html b/test/fixtures/images/output.html index 6d50996..11dab25 100644 --- a/test/fixtures/images/output.html +++ b/test/fixtures/images/output.html @@ -1,6 +1,6 @@

    Example

    Example

    -

    -

    -

    -

    +

    +

    +

    +

    diff --git a/test/fixtures/list/output.html b/test/fixtures/list/output.html index 506b488..bd5dfdb 100644 --- a/test/fixtures/list/output.html +++ b/test/fixtures/list/output.html @@ -1,29 +1,19 @@

    List

    +
      +
    1. One;
    2. +
    3. Two;
    4. +
      -
    1. -

      Four.

      -
    2. -
    3. -

      Five.

      -
    4. +
    5. Four.
    6. +
    7. Five.
    8. +
    +
    And a rule.
     
    diff --git a/test/fixtures/references/output.html b/test/fixtures/references/output.html index f4925d6..f074a30 100644 --- a/test/fixtures/references/output.html +++ b/test/fixtures/references/output.html @@ -1,6 +1,6 @@

    References

    Entities contains some serious entity tests relating to titles and links in definitions.

    -

    However, the [missing], missing, and missing are omitted.

    -

    However, the ![missing], missing, and missing are omitted.

    -

    Same goes for and .

    +

    However, the [missing], [missing][], and [missing][missing] are omitted.

    +

    However, the ![missing], ![missing][], and ![missing][missing] are omitted.

    +

    Same goes for [][empty] and ![][empty].

    diff --git a/test/fixtures/tables/config.json b/test/fixtures/tables/config.json deleted file mode 100644 index fb3bbe6..0000000 --- a/test/fixtures/tables/config.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "sanitize": false -} diff --git a/test/fixtures/tables/input.md b/test/fixtures/tables/input.md deleted file mode 100644 index f2b948e..0000000 --- a/test/fixtures/tables/input.md +++ /dev/null @@ -1,7 +0,0 @@ -| Alpha | Bravo | Charlie | Delta | -| ------- | :------- | -------: | :------: | -| Echo | Foxtrot | **Golf** | -| India | Juliett | Kilo | Lima | -| Mike | November | Oscar | ~~Papa~~ | -| Quebec | _Romeo_ | Sierra | Tango | -| Uniform | Victor | Whiskey | diff --git a/test/fixtures/tables/output.html b/test/fixtures/tables/output.html deleted file mode 100644 index 7199145..0000000 --- a/test/fixtures/tables/output.html +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    AlphaBravoCharlieDelta
    EchoFoxtrotGolf
    IndiaJuliettKiloLima
    MikeNovemberOscarPapa
    QuebecRomeoSierraTango
    UniformVictorWhiskey
    diff --git a/test/fixtures/yaml/config.json b/test/fixtures/yaml/config.json deleted file mode 100644 index d573bf8..0000000 --- a/test/fixtures/yaml/config.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "yaml": true -} \ No newline at end of file diff --git a/test/fixtures/yaml/input.md b/test/fixtures/yaml/input.md deleted file mode 100644 index 8bd77ae..0000000 --- a/test/fixtures/yaml/input.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -YAML: cool ---- - -# Hello world diff --git a/test/fixtures/yaml/output.html b/test/fixtures/yaml/output.html deleted file mode 100644 index 159202e..0000000 --- a/test/fixtures/yaml/output.html +++ /dev/null @@ -1 +0,0 @@ -

    Hello world

    diff --git a/test/index.js b/test/index.js index e4fefe8..0c0b2e9 100644 --- a/test/index.js +++ b/test/index.js @@ -2,118 +2,29 @@ var path = require('path') var fs = require('fs') -var assert = require('assert') var test = require('tape') var remark = require('remark') +var slug = require('remark-slug') var toc = require('remark-toc') var github = require('remark-github') var commonmark = require('commonmark.json') var vfile = require('to-vfile') var hidden = require('is-hidden') +var not = require('not') +var unified = require('unified') +var parse = require('remark-parse') +var rehypeParse = require('rehype-parse') +var rehypeStringify = require('rehype-stringify') var all = require('mdast-util-to-hast/lib/all') var html = require('..') -var read = fs.readFileSync -var exists = fs.existsSync -var join = path.join - -// By default, CommonMark failures are accepted. -// To fail on CommonMark exceptions, set the `CMARK` environment variable. -var ignoreCommonMarkException = !('CMARK' in global.process.env) - -var integrationMap = {github: github, toc: toc} -var integrationRoot = join(__dirname, 'integrations') -var fixtureRoot = join(__dirname, 'fixtures') - -var commonmarkOptions = { - entities: {escapeOnly: true, useNamedReferences: true}, - commonmark: true, - yaml: false, - closeSelfClosing: true, - sanitize: false -} - -// List of CommonMark tests I dissagree with. -// For reasoning, see `doc/commonmark.md`. -// Note that these differences have to do with not puting more time into -// features which IMHO produce less quality HTML. -// So if you’d like to write the features, I’ll gladly merge! -var commonmarkIgnore = [ - // Exception 1. - 247, - 248, - - // Exception 2. - 3, - 50, - 76, - 77, - 80, - 86, - 89, - 98, - 118, - 176, - 230, - 231, - 233, - 236, - 257, - 258, - 261, - 262, - 263, - 264, - 265, - 266, - 267, - 268, - 269, - 270, - 395, - 396, - 433, - 445, - 520, - 522, - 551, - - // Exception 3. - 428, - 477, - 478, - 479, - 480, - 481, - 489, - 493 -] - -var fixtures = fs.readdirSync(fixtureRoot) -var integrations = fs.readdirSync(integrationRoot) - -fixtures = fixtures.filter(hidden) -integrations = integrations.filter(hidden) - -var section -var start - -commonmark.forEach(function (test, position) { - if (section !== test.section) { - section = test.section - start = position - } - - test.relative = position - start + 1 -}) - test('remark-html()', function (t) { var processor t.equal(typeof html, 'function', 'should be a function') t.doesNotThrow(function () { - html.call(remark()) + remark().use(html).freeze() }, 'should not throw if not passed options') t.throws( @@ -315,93 +226,104 @@ test('remark-html()', function (t) { // Assert fixtures. test('Fixtures', function (t) { - fixtures.forEach(function (fixture) { - var filepath = join(fixtureRoot, fixture) - var output = read(join(filepath, 'output.html'), 'utf-8') - var input = read(join(filepath, 'input.md'), 'utf-8') - var config = join(filepath, 'config.json') - var file = vfile(fixture + '.md') + var base = path.join(__dirname, 'fixtures') + + fs.readdirSync(base).filter(not(hidden)).forEach(each) + + t.end() + + function each(name) { + var output = String(fs.readFileSync(path.join(base, name, 'output.html'))) + var input = String(fs.readFileSync(path.join(base, name, 'input.md'))) + var config = {} + var file = vfile(name + '.md') var result file.contents = input - config = exists(config) ? JSON.parse(read(config, 'utf-8')) : {} - result = processSync(file, config) + try { + config = JSON.parse(fs.readFileSync(path.join(base, name, 'config.json'))) + } catch (_) {} - t.equal(result, output, 'should work on `' + fixture + '`') - }) + result = processSync(file, config) - t.end() + t.equal(result, output, 'should work on `' + name + '`') + } }) -// Assert CommonMark. test('CommonMark', function (t) { - commonmark.forEach(function (test, n) { - var name = test.section + ' ' + test.relative - var file = vfile(name + '.md') - var result - var message - var exception + var start = 0 + var section - file.contents = test.markdown - result = processSync(file, commonmarkOptions) + commonmark.forEach(each) - n += 1 + t.end() - try { - assert.strictEqual(result, test.html) - } catch (error) { - exception = error + function each(example, index) { + if (section !== example.section) { + section = example.section + start = index } - message = '(' + n + ') should work on ' + name - - if ( - commonmarkIgnore.indexOf(n) !== -1 || - (ignoreCommonMarkException && exception) - ) { - t.skip(message) - } else { - t.equal(result, test.html, message) - } - }) + var actual = unified() + .use(parse) + .use(html) + .processSync(example.markdown) + .toString() - t.end() + var reformat = unified() + .use(rehypeParse, {fragment: true}) + .use(rehypeStringify) + + // Normalize meaningless stuff, like character references, `
    ` is `
    `, + // etc. + t.equal( + String(reformat.processSync(actual)), + String(reformat.processSync(example.html)), + index + ': ' + example.section + ' (' + (index - start + 1) + ')' + ) + } }) -// Assert integrations. test('Integrations', function (t) { - integrations.forEach(function (integration) { - var filepath = join(integrationRoot, integration) - var output = read(join(filepath, 'output.html'), 'utf-8') - var input = read(join(filepath, 'input.md'), 'utf-8') - var config = join(filepath, 'config.json') - var file = vfile(integration + '.md') + var integrationMap = {github: github, toc: [slug, toc]} + var base = path.join(__dirname, 'integrations') + + fs.readdirSync(base) + .filter(not(hidden)) + // Ignore `github` for now, which needs to be updated for remark@next. + // To do: add gfm, footnotes, frontmatter integrations. + .filter((d) => d !== 'github') + .forEach(each) + + t.end() + + function each(name) { + var output = String(fs.readFileSync(path.join(base, name, 'output.html'))) + var input = String(fs.readFileSync(path.join(base, name, 'input.md'))) + var file = vfile(name + '.md') + var config = {} var result file.contents = input - config = exists(config) ? JSON.parse(read(config, 'utf-8')) : {} + try { + config = JSON.parse(fs.readFileSync(path.join(base, name, 'config.json'))) + } catch (_) {} config.sanitize = false result = remark() .data('settings', config) .use(html, config) - .use(integrationMap[integration], config) + .use(integrationMap[name], config) .processSync(file) .toString() - t.equal(result, output, 'should work on `' + integration + '`') - }) - - t.end() + t.equal(result, output, 'should work on `' + name + '`') + } }) function processSync(file, config) { - return remark() - .data('settings', config) - .use(html, config) - .processSync(file) - .toString() + return remark().use(html, config).processSync(file).toString() }