Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't lowercase Markdown link definition labels #13155

Merged
merged 12 commits into from Dec 15, 2022
20 changes: 20 additions & 0 deletions changelog_unreleased/markdown/13155.md
@@ -0,0 +1,20 @@
#### Don't lowercase link references (#13155 by @DerekNonGeneric & @fisker)

<!-- prettier-ignore -->
```markdown
<!-- Input -->
We now don't strictly follow the release notes format suggested by [Keep a Changelog].

[Keep a Changelog]: https://example.com/

<!-- Prettier stable -->
We now don't strictly follow the release notes format suggested by [Keep a Changelog].

[keep a changelog]: https://example.com/
<!--
^^^^^^^^^^^^^^^^^^ lowercased
-->

<!-- Prettier main -->
<Same as input>
```
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -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",
Expand Down
12 changes: 7 additions & 5 deletions 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");

Expand Down Expand Up @@ -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 (
Expand Down
27 changes: 18 additions & 9 deletions src/language-markdown/printer-markdown.js
@@ -1,5 +1,6 @@
"use strict";

const collapseWhiteSpace = require("collapse-white-space");
const {
getLast,
getMinNotPresentContinuousCount,
Expand Down Expand Up @@ -352,15 +353,15 @@ function genericPrint(path, options, print) {
printChildren(path, options, print),
"]",
node.referenceType === "full"
? ["[", node.identifier, "]"]
? printLinkReference(node)
: node.referenceType === "collapsed"
? "[]"
: "",
];
case "imageReference":
switch (node.referenceType) {
case "full":
return ["![", node.alt || "", "][", node.identifier, "]"];
return ["![", node.alt || "", "]", printLinkReference(node)];
default:
return [
"![",
Expand All @@ -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),
Expand All @@ -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 =
Expand All @@ -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([
Expand Down Expand Up @@ -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,
Expand Down
@@ -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

================================================================================
`;
@@ -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
@@ -0,0 +1 @@
[1][Test Text](http://example.com)
18 changes: 18 additions & 0 deletions 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
@@ -0,0 +1 @@
run_spec(__dirname, ["markdown"]);
22 changes: 11 additions & 11 deletions tests/format/markdown/spec/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -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\\]]

Expand All @@ -3095,7 +3095,7 @@ proseWrap: "always"
[Foo bar]

=====================================output=====================================
[foo bar]: my%20url "title"
[Foo bar]: my%20url "title"

[Foo bar]

Expand Down Expand Up @@ -3235,7 +3235,7 @@ proseWrap: "always"
[Foo]

=====================================output=====================================
[foo]: /url
[FOO]: /url

[Foo]

Expand All @@ -3254,7 +3254,7 @@ proseWrap: "always"
[αγω]

=====================================output=====================================
[αγω]: /φου
[ΑΓΩ]: /φου

[αγω]

Expand Down Expand Up @@ -8889,7 +8889,7 @@ proseWrap: "always"
[bar]: /url "title"

=====================================output=====================================
[foo][bar]
[foo][BaR]

[bar]: /url "title"

Expand All @@ -8908,9 +8908,9 @@ proseWrap: "always"
[ТОЛПОЙ]: /url

=====================================output=====================================
[Толпой][толпой] is a Russian word.
[Толпой][Толпой] is a Russian word.

[толпой]: /url
[ТОЛПОЙ]: /url

================================================================================
`;
Expand All @@ -8928,9 +8928,9 @@ proseWrap: "always"
[Baz][Foo bar]

=====================================output=====================================
[foo bar]: /url
[Foo bar]: /url

[Baz][foo bar]
[Baz][Foo bar]

================================================================================
`;
Expand Down Expand Up @@ -9558,7 +9558,7 @@ proseWrap: "always"
=====================================output=====================================
![foo *bar*][foobar]

[foobar]: train.jpg "train & tracks"
[FOOBAR]: train.jpg "train & tracks"

================================================================================
`;
Expand Down Expand Up @@ -9656,7 +9656,7 @@ proseWrap: "always"
=====================================output=====================================
![foo][bar]

[bar]: /url
[BAR]: /url

================================================================================
`;
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Expand Up @@ -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==
Expand Down