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

add more flexibility to hyphen separated check; explicit em-dash check #185

Merged
merged 10 commits into from
Jan 2, 2024
52 changes: 40 additions & 12 deletions rules/list-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,49 @@ function validateList(list, file) {
continue;
}

const [link, ...description] = paragraph.children;

if (link.type === 'text') {
if (paragraph.children[0].type === 'text') {
continue;
}

let [link, ...description] = paragraph.children;

// Might have children like: '{image} {text} {link} { - description}'
// Keep discarding prefix elements until we find something link-like.
while (link.type !== 'linkReference' && link.type !== 'link' && description.length > 1) {
link = description[0];
description = description.slice(1);
}

if (!validateListItemLink(link, file)) {
continue;
}

if (!validateListItemLinkChildren(link, file)) {
continue;
}

validateListItemDescription(description, file);
}
}

function validateListItemLinkChildren(link, file) {
for (const node of link.children) {
if (!listItemLinkNodeAllowList.has(node.type)) {
file.message('Invalid list item link', node);
return false;
}
}

return true;
}

function validateListItemLink(link, file) {
// NB. We need remark-lint-no-undefined-references separately
// to catch if this is a valid reference. Here we only care that it exists.
if (link.type === 'linkReference') {
return true;
}

if (link.type !== 'link') {
file.message('Invalid list item link', link);
return false;
Expand All @@ -133,13 +161,6 @@ function validateListItemLink(link, file) {
return false;
}

for (const node of link.children) {
if (!listItemLinkNodeAllowList.has(node.type)) {
file.message('Invalid list item link', node);
return false;
}
}

return true;
}

Expand Down Expand Up @@ -168,11 +189,18 @@ function validateListItemDescription(description, file) {
return false;
}

if (/^\s*—/.test(prefixText)) {
file.message('List item link and description separated by invalid en-dash', prefix);
// Some editors auto-correct ' - ' to – (en-dash). Also avoid — (em-dash).
if (/^\s*[/\u{02013}\u{02014}]/u.test(prefixText)) {
file.message('List item link and description separated by invalid en-dash or em-dash', prefix);
return false;
}

// Might have image and link on left side before description.
// Assume a hyphen with spaces in the description is good enough.
if (/ - [A-Z]/.test(descriptionText)) {
return true;
}

file.message('List item link and description must be separated with a dash', prefix);
return false;
}
Expand Down
20 changes: 15 additions & 5 deletions test/fixtures/list-item/0.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ All list-items in this document should be linted as **valid**.
- [foo](https://foo.com) - Valid sub-item.
- [foo](https://foo.com) - Valid sub-item.
- [foo](https://foo.com) - Valid sub-sub-item!!!!!
- [foo](https://foo.com) - GitHub's descriptions can also be valid.
- [foo](https://foo.com) - GitHub's descriptions can also be valid.
- [foo-bar](https://foo-bar.com) - Valid description.
- 👍 [foo](https://foo-bar.com) - Valid description.

## Tutorials

Expand Down Expand Up @@ -133,9 +135,17 @@ These sub-lists use mixed indentation (spaces and tabs).

- [why-is-node-running](https://github.com/mafintosh/why-is-node-running) - Node.js is running but you don't know why?

<!--
TODO: these list-items should possibly lint as valid, but they currently don't.

- ![v3](img/vapor-3.png) [API Error Middleware](https://github.com/skelpo/APIErrorMiddleware) – Vapor middleware for converting thrown errors to JSON responses.
- ![v2](img/vapor-2.png) ![v3](img/vapor-3.png) [Bugsnag](https://github.com/nodes-vapor/bugsnag) – Report errors with Bugsnag.
- [gtkada] - Ada graphical toolkit based on Gtk3 components (issue #161 link reference below but checked by `remark-lint-no-undefined-references` not `rules/list-item.js`).
- [*example* `code`][exampleref] - Another linkref but with children.
- [foo](https://foo.com) ![stars](https://img.shields.io/github/stars/foo/foo.svg) - Valid description.
- ![v3](img/vapor-3.png) [API Error Middleware](https://github.com/skelpo/APIErrorMiddleware) - Vapor middleware for converting thrown errors to JSON responses.
- ![v2](img/vapor-2.png) ![v3](img/vapor-3.png) [Bugsnag](https://github.com/nodes-vapor/bugsnag) - Report errors with Bugsnag.
- [ArcGIS location services - Postman Workspace](https://www.postman.com/arcgis-developer/workspace/arcgis-location-services) - Official Postman collections to work with the Geocoding & Search API, Routing & Directions API, Demographics & GeoEnrichment API, Data hosting and more (issue #146).
- [rmw](https://github.com/ros2/rmw/tree/master/rmw) - Contains the ROS middleware API ![rmw](https://img.shields.io/github/stars/ros2/rmw.svg) (issue #49).
<!-- TODO
- [__Compiling machine learning programs via high-level tracing__. Roy Frostig, Matthew James Johnson, Chris Leary. _MLSys 2018_.](https://mlsys.org/Conferences/doc/2018/146.pdf) - This white paper describes an early version of JAX, detailing how computation is traced and compiled (issue #136)
-->

[gtkada]: https://github.com/AdaCore/gtkada
[exampleref]: https://example.com
4 changes: 4 additions & 0 deletions test/fixtures/list-item/1.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ Test description's ending punctuation.

- [foo](https://foo.com) - Quote-inducing double punctuation “end…”??!
- [foo](https://foo.com) - Quote-inducing double punctuation “end?…...”…...?
- [gtkada] - Reference link with no reference.
- [*example* ![img](https://image.img)][exampleref] - Another linkref but with a bad child.

[exampleref]: https://ref.com
7 changes: 6 additions & 1 deletion test/rules/snapshots/list-item.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Generated by [AVA](https://avajs.dev).
},
{
line: 13,
message: 'List item link and description separated by invalid en-dash',
message: 'List item link and description separated by invalid en-dash or em-dash',
ruleId: 'awesome-list-item',
},
{
Expand Down Expand Up @@ -279,6 +279,11 @@ Generated by [AVA](https://avajs.dev).
message: 'List item description must end with proper punctuation',
ruleId: 'awesome-list-item',
},
{
line: 74,
message: 'Invalid list item link',
ruleId: 'awesome-list-item',
},
]

## list-item - invalid sublist punctuation
Expand Down
Binary file modified test/rules/snapshots/list-item.js.snap
Binary file not shown.