diff --git a/changelog_unreleased/markdown/13155.md b/changelog_unreleased/markdown/13155.md new file mode 100644 index 000000000000..ca3b25123442 --- /dev/null +++ b/changelog_unreleased/markdown/13155.md @@ -0,0 +1,20 @@ +#### Don't lowercase link references (#13155 by @DerekNonGeneric & @fisker) + + +```markdown + +We now don't strictly follow the release notes format suggested by [Keep a Changelog]. + +[Keep a Changelog]: https://example.com/ + + +We now don't strictly follow the release notes format suggested by [Keep a Changelog]. + +[keep a changelog]: https://example.com/ + + + + +``` diff --git a/package.json b/package.json index d0b8c8ccb44c..2129d77fc3fa 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "chalk": "5.0.1", "ci-info": "3.3.0", "cjk-regex": "2.0.1", + "collapse-white-space": "1.0.6", "cosmiconfig": "7.0.1", "css-units-list": "1.1.0", "dashify": "2.0.0", diff --git a/src/language-markdown/clean.js b/src/language-markdown/clean.js index 60c3c7caac3a..29eac8a2ba6a 100644 --- a/src/language-markdown/clean.js +++ b/src/language-markdown/clean.js @@ -1,5 +1,6 @@ "use strict"; +const collapseWhiteSpace = require("collapse-white-space"); const { isFrontMatterNode } = require("../common/util.js"); const { startWithPragma } = require("./pragma.js"); @@ -42,11 +43,12 @@ function clean(ast, newObj, parent) { newObj.value = ast.value.trim().replace(/[\t\n]+/g, " "); } - if (ast.type === "definition" || ast.type === "linkReference") { - newObj.label = ast.label - .trim() - .replace(/[\t\n ]+/g, " ") - .toLowerCase(); + if ( + ast.type === "definition" || + ast.type === "linkReference" || + ast.type === "imageReference" + ) { + newObj.label = collapseWhiteSpace(ast.label); } if ( diff --git a/src/language-markdown/printer-markdown.js b/src/language-markdown/printer-markdown.js index f13f116bdd0f..ee02e2abbcc1 100644 --- a/src/language-markdown/printer-markdown.js +++ b/src/language-markdown/printer-markdown.js @@ -1,5 +1,6 @@ "use strict"; +const collapseWhiteSpace = require("collapse-white-space"); const { getLast, getMinNotPresentContinuousCount, @@ -352,7 +353,7 @@ function genericPrint(path, options, print) { printChildren(path, options, print), "]", node.referenceType === "full" - ? ["[", node.identifier, "]"] + ? printLinkReference(node) : node.referenceType === "collapsed" ? "[]" : "", @@ -360,7 +361,7 @@ function genericPrint(path, options, print) { case "imageReference": switch (node.referenceType) { case "full": - return ["![", node.alt || "", "][", node.identifier, "]"]; + return ["![", node.alt || "", "]", printLinkReference(node)]; default: return [ "![", @@ -372,9 +373,8 @@ function genericPrint(path, options, print) { case "definition": { const lineOrSpace = options.proseWrap === "always" ? line : " "; return group([ - "[", - node.identifier, - "]:", + printLinkReference(node), + ":", indent([ lineOrSpace, printUrl(node.url), @@ -390,7 +390,7 @@ function genericPrint(path, options, print) { case "footnote": return ["[^", printChildren(path, options, print), "]"]; case "footnoteReference": - return ["[^", node.identifier, "]"]; + return printFootnoteReference(node); case "footnoteDefinition": { const nextNode = path.getParentNode().children[path.getName() + 1]; const shouldInlineFootnote = @@ -401,9 +401,8 @@ function genericPrint(path, options, print) { node.children[0].position.start.line === node.children[0].position.end.line)); return [ - "[^", - node.identifier, - "]: ", + printFootnoteReference(node), + ": ", shouldInlineFootnote ? printChildren(path, options, print) : group([ @@ -913,6 +912,16 @@ function hasPrettierIgnore(path) { return isPrettierIgnore(prevNode) === "next"; } +// `remark-parse` lowercase the `label` as `identifier`, we don't want do that +// https://github.com/remarkjs/remark/blob/daddcb463af2d5b2115496c395d0571c0ff87d15/packages/remark-parse/lib/tokenize/reference.js +function printLinkReference(node) { + return `[${collapseWhiteSpace(node.label)}]`; +} + +function printFootnoteReference(node) { + return `[^${node.label}]`; +} + module.exports = { preprocess, print: genericPrint, diff --git a/tests/format/markdown/linkReference/case-and-space/__snapshots__/jsfmt.spec.js.snap b/tests/format/markdown/linkReference/case-and-space/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000000..2a9d689f2704 --- /dev/null +++ b/tests/format/markdown/linkReference/case-and-space/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,114 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`case-and-space.md format 1`] = ` +====================================options===================================== +parsers: ["markdown"] +printWidth: 80 + | printWidth +=====================================input====================================== +# \`linkReference\` + +[ See \`AsyncGeneratorFunction\` ][ See \`AsyncGeneratorFunction\` ] + +# \`imageReference\` + +![ See \`AsyncGeneratorFunction\` ][ See \`AsyncGeneratorFunction\` ] + +# \`definition\` + +[ See \`AsyncGeneratorFunction\` ]: ./index.html + +# \`footnoteReference\` + +[^See\`AsyncGeneratorFunction\`] + +# \`footnoteDefinition\` + +[^See\`AsyncGeneratorFunction\`]: ./index.html + +=====================================output===================================== +# \`linkReference\` + +[ See \`AsyncGeneratorFunction\` ][ See \`AsyncGeneratorFunction\` ] + +# \`imageReference\` + +![ See \`AsyncGeneratorFunction\` ][ See \`AsyncGeneratorFunction\` ] + +# \`definition\` + +[ See \`AsyncGeneratorFunction\` ]: ./index.html + +# \`footnoteReference\` + +[^See\`AsyncGeneratorFunction\`] + +# \`footnoteDefinition\` + +[^See\`AsyncGeneratorFunction\`]: ./index.html + +================================================================================ +`; + +exports[`issue-3835.md format 1`] = ` +====================================options===================================== +parsers: ["markdown"] +printWidth: 80 + | printWidth +=====================================input====================================== +[1][Test Text](http://example.com) + +=====================================output===================================== +[1][Test Text](http://example.com) + +================================================================================ +`; + +exports[`issue-7118.md format 1`] = ` +====================================options===================================== +parsers: ["markdown"] +printWidth: 80 + | printWidth +=====================================input====================================== +- see[Link to Foo][master-LinkToFoo] + +[master-LinkToFoo]: http://foo.com + +Bla bla [PascalCase][] bla. + +[PascalCase]: ./PascalCase.md + +## [Unreleased] +… +[Unreleased]: https://github.com/username/project/compare/v1.0.0...HEAD + +[darktable]: https://www.darktable.org/ +[js;dr]: https://indieweb.org/js;dr +[LinuxFest Northwest]: https://www.linuxfestnorthwest.org/ +[OpenStreetMap]: https://www.openstreetmap.org/about +[SeaGL]: https://seagl.org/ +[uBlock Origin]: https://github.com/gorhill/uBlock + +=====================================output===================================== +- see[Link to Foo][master-LinkToFoo] + +[master-LinkToFoo]: http://foo.com + +Bla bla [PascalCase][] bla. + +[PascalCase]: ./PascalCase.md + +## [Unreleased] + +… +[Unreleased]: https://github.com/username/project/compare/v1.0.0...HEAD + +[darktable]: https://www.darktable.org/ +[js;dr]: https://indieweb.org/js;dr +[LinuxFest Northwest]: https://www.linuxfestnorthwest.org/ +[OpenStreetMap]: https://www.openstreetmap.org/about +[SeaGL]: https://seagl.org/ +[uBlock Origin]: https://github.com/gorhill/uBlock + +================================================================================ +`; diff --git a/tests/format/markdown/linkReference/case-and-space/case-and-space.md b/tests/format/markdown/linkReference/case-and-space/case-and-space.md new file mode 100644 index 000000000000..f849494fd66c --- /dev/null +++ b/tests/format/markdown/linkReference/case-and-space/case-and-space.md @@ -0,0 +1,19 @@ +# `linkReference` + +[ See `AsyncGeneratorFunction` ][ See `AsyncGeneratorFunction` ] + +# `imageReference` + +![ See `AsyncGeneratorFunction` ][ See `AsyncGeneratorFunction` ] + +# `definition` + +[ See `AsyncGeneratorFunction` ]: ./index.html + +# `footnoteReference` + +[^See`AsyncGeneratorFunction`] + +# `footnoteDefinition` + +[^See`AsyncGeneratorFunction`]: ./index.html diff --git a/tests/format/markdown/linkReference/case-and-space/issue-3835.md b/tests/format/markdown/linkReference/case-and-space/issue-3835.md new file mode 100644 index 000000000000..b8d5aa7e4c01 --- /dev/null +++ b/tests/format/markdown/linkReference/case-and-space/issue-3835.md @@ -0,0 +1 @@ +[1][Test Text](http://example.com) diff --git a/tests/format/markdown/linkReference/case-and-space/issue-7118.md b/tests/format/markdown/linkReference/case-and-space/issue-7118.md new file mode 100644 index 000000000000..9507886e22ba --- /dev/null +++ b/tests/format/markdown/linkReference/case-and-space/issue-7118.md @@ -0,0 +1,18 @@ +- see[Link to Foo][master-LinkToFoo] + +[master-LinkToFoo]: http://foo.com + +Bla bla [PascalCase][] bla. + +[PascalCase]: ./PascalCase.md + +## [Unreleased] +… +[Unreleased]: https://github.com/username/project/compare/v1.0.0...HEAD + +[darktable]: https://www.darktable.org/ +[js;dr]: https://indieweb.org/js;dr +[LinuxFest Northwest]: https://www.linuxfestnorthwest.org/ +[OpenStreetMap]: https://www.openstreetmap.org/about +[SeaGL]: https://seagl.org/ +[uBlock Origin]: https://github.com/gorhill/uBlock diff --git a/tests/format/markdown/linkReference/case-and-space/jsfmt.spec.js b/tests/format/markdown/linkReference/case-and-space/jsfmt.spec.js new file mode 100644 index 000000000000..ff3b58b03c88 --- /dev/null +++ b/tests/format/markdown/linkReference/case-and-space/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, ["markdown"]); diff --git a/tests/format/markdown/spec/__snapshots__/jsfmt.spec.js.snap b/tests/format/markdown/spec/__snapshots__/jsfmt.spec.js.snap index 4be0bcab9465..cb8e1af7eb71 100644 --- a/tests/format/markdown/spec/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/markdown/spec/__snapshots__/jsfmt.spec.js.snap @@ -3074,7 +3074,7 @@ proseWrap: "always" [Foo*bar\\]] =====================================output===================================== -[foo*bar\\]]: my_(url) "title (with parens)" +[Foo*bar\\]]: my_(url) "title (with parens)" [Foo*bar\\]] @@ -3095,7 +3095,7 @@ proseWrap: "always" [Foo bar] =====================================output===================================== -[foo bar]: my%20url "title" +[Foo bar]: my%20url "title" [Foo bar] @@ -3235,7 +3235,7 @@ proseWrap: "always" [Foo] =====================================output===================================== -[foo]: /url +[FOO]: /url [Foo] @@ -3254,7 +3254,7 @@ proseWrap: "always" [αγω] =====================================output===================================== -[αγω]: /φου +[ΑΓΩ]: /φου [αγω] @@ -8889,7 +8889,7 @@ proseWrap: "always" [bar]: /url "title" =====================================output===================================== -[foo][bar] +[foo][BaR] [bar]: /url "title" @@ -8908,9 +8908,9 @@ proseWrap: "always" [ТОЛПОЙ]: /url =====================================output===================================== -[Толпой][толпой] is a Russian word. +[Толпой][Толпой] is a Russian word. -[толпой]: /url +[ТОЛПОЙ]: /url ================================================================================ `; @@ -8928,9 +8928,9 @@ proseWrap: "always" [Baz][Foo bar] =====================================output===================================== -[foo bar]: /url +[Foo bar]: /url -[Baz][foo bar] +[Baz][Foo bar] ================================================================================ `; @@ -9558,7 +9558,7 @@ proseWrap: "always" =====================================output===================================== ![foo *bar*][foobar] -[foobar]: train.jpg "train & tracks" +[FOOBAR]: train.jpg "train & tracks" ================================================================================ `; @@ -9656,7 +9656,7 @@ proseWrap: "always" =====================================output===================================== ![foo][bar] -[bar]: /url +[BAR]: /url ================================================================================ `; diff --git a/yarn.lock b/yarn.lock index e3c427e3ddf9..904f5095c18f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2344,7 +2344,7 @@ co@^4.6.0: resolved "https://registry.yarnpkg.com/co/-/co-4.6.0.tgz#6ea6bdf3d853ae54ccb8e47bfa0bf3f9031fb184" integrity sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ= -collapse-white-space@^1.0.2: +collapse-white-space@1.0.6, collapse-white-space@^1.0.2: version "1.0.6" resolved "https://registry.yarnpkg.com/collapse-white-space/-/collapse-white-space-1.0.6.tgz#e63629c0016665792060dbbeb79c42239d2c5287" integrity sha512-jEovNnrhMuqyCcjfEJA56v0Xq8SkIoPKDyaHahwo3POf4qcSXqMYuwNcOTzp74vTsR9Tn08z4MxWqAhcekogkQ==