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
31 changes: 26 additions & 5 deletions rules/list-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,19 @@ 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;
}
Expand All @@ -117,6 +124,13 @@ function validateList(list, file) {
}

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') {
// TODO: need to test link children against listItemLinkNodeAllowList?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be resolved.

return true;
}

if (link.type !== 'link') {
file.message('Invalid list item link', link);
return false;
Expand Down Expand Up @@ -168,11 +182,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
17 changes: 13 additions & 4 deletions test/fixtures/list-item/0.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ All list-items in this document should be linted as **valid**.
- [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-bar](https://foo-bar.com) - Valid description.
- 👍 [foo](https://foo-bar.com) - Valid description.

## Tutorials

Expand Down Expand Up @@ -133,9 +135,16 @@ 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`).
- [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

1 change: 1 addition & 0 deletions test/fixtures/list-item/1.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ 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
2 changes: 1 addition & 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
Binary file modified test/rules/snapshots/list-item.js.snap
Binary file not shown.