Skip to content

Commit

Permalink
Fix missing use case (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
katydecorah authored Dec 7, 2023
1 parent 17f1adc commit ea08eeb
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 220 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Add the plugin to your remark configuration:

- [Link text is not descriptive (`not-descriptive`)](#link-text-is-not-descriptive)
- [Link text is not unique (`unique`)](#link-text-is-not-unique)
- [Link text is a URL (`url`)](#link-text-is-a-url)
- [Link text is a URL (`not-url`)](#link-text-is-a-url)
- [Link text is missing (`empty`)](#link-text-is-missing)
- [Linked image is missing alt text (`empty-alt-text`)](#linked-image-is-missing-alt-text)
- [Link to email does not contain email address in link text (`email`)](#link-to-email-does-not-contain-email-address-in-link-text)
Expand Down Expand Up @@ -119,7 +119,7 @@ Configuration:
<!-- prettier-ignore-start -->
```js
// disable the rule:
["@double-great/remark-lint-link-text", [1, {"url":false}]]
["@double-great/remark-lint-link-text", [1, {"not-url":false}]]
```
<!-- prettier-ignore-end -->

Expand Down
14 changes: 14 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/** @type {import('jest').Config} */
const config = {
clearMocks: true,
transformIgnorePatterns: ["!node_modules/"],
prettierPath: require.resolve("prettier-2"),
moduleNameMapper: {
"^..?/rule.js$": "<rootDir>/src/rule.ts",
"^..?/docs.js$": "<rootDir>/src/docs.ts",
"^..?/utils.js$": "<rootDir>/src/rules/utils.ts",
"^..?/rules/(.*).js$": "<rootDir>/src/rules/$1.ts",
},
};

module.exports = config;
216 changes: 17 additions & 199 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 2 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"github-slugger": "^2.0.0",
"jest": "^29.7.0",
"prettier": "^3.1.0",
"prettier-2": "npm:prettier@^2",
"remark": "^15.0.1",
"typescript": "^5.3.2"
},
Expand All @@ -52,17 +53,5 @@
"bugs": {
"url": "https://github.com/double-great/remark-lint-link-text/issues"
},
"homepage": "https://github.com/double-great/remark-lint-link-text#readme",
"jest": {
"clearMocks": true,
"transformIgnorePatterns": [
"!node_modules/"
],
"moduleNameMapper": {
"^..?/rule.js$": "<rootDir>/src/rule.ts",
"^..?/docs.js$": "<rootDir>/src/docs.ts",
"^..?/utils.js$": "<rootDir>/src/rules/utils.ts",
"^..?/rules/(.*).js$": "<rootDir>/src/rules/$1.ts"
}
}
"homepage": "https://github.com/double-great/remark-lint-link-text#readme"
}
11 changes: 11 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ describe("remark-lint-link-text", () => {
expect(lint.messages.length).toEqual(0);
});

test("works, random test cases", async () => {
const lint = await processMarkdown(dedent`
1. [Bullet item](https://example.com)
A [\`code\`](https://example.com) and [\`another code\`](https://example.com)
- [**clues.js**](https://github.com/double-great/alt-text/blob/main/clues.js)
`);
expect(lint.messages.length).toEqual(0);
});

test("warns against banned link text", async () => {
const lint = await processMarkdown(
dedent`
Expand Down
15 changes: 11 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,17 @@ const checkLinkText = lintRule(
const hasImage =
node.children.filter(({ type }) => type === "image").length > 0;

const text = node.children
.filter(({ type }) => type === "text")
.map(({ value }) => value)
.join(" ");
// recursively return all values from node.children
const getText = (node: TextNode): string => {
if (node.value) {
return node.value;
} else if (node.children) {
return node.children.map(getText).join(" ");
}
return "";
};

const text = getText(node);

const altText = node.children
.filter(({ type }) => type === "image")
Expand Down
Loading

0 comments on commit ea08eeb

Please sign in to comment.