Skip to content

Commit

Permalink
Progress converting from CommonJS -> ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
JoyceZhu committed Jan 10, 2025
1 parent cce98aa commit 62e3669
Show file tree
Hide file tree
Showing 21 changed files with 132 additions and 167 deletions.
18 changes: 0 additions & 18 deletions .eslintrc.js

This file was deleted.

18 changes: 18 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"root": true,
"parserOptions": {
"ecmaVersion": 2020
},
"env": {
"es6": true,
"node": true,
"jest": true
},
"plugins": ["github"],
"extends": ["plugin:github/recommended"],
"rules": {
"import/no-commonjs": "off",
"filenames/match-regex": "off",
"i18n-text/no-en": "off"
}
}
12 changes: 0 additions & 12 deletions .markdownlint-cli2.cjs

This file was deleted.

12 changes: 12 additions & 0 deletions .markdownlint-cli2.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// import init from "./index.js";

const options = {
default: false,
"heading-increment": true,
"no-alt-text": true,
"single-h1": true,
"no-emphasis-as-heading": true,
"first-line-heading": true,
};
export const config = options;
export const customRules = ["./index.js"];
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
14
18
23 changes: 11 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
const _ = require("lodash");
import _ from "lodash-es";

const accessibilityRules = require("./style/accessibility.json");
const base = require("./style/base.json");
const gitHubCustomRules = require("./src/rules/index").rules;

module.exports = [...gitHubCustomRules];
import * as accessibilityRules from "./style/accessibility.json";
import * as base from "./style/base.json";
import { githubMarkdownLint } from "./src/rules/index.js";

const offByDefault = ["no-empty-alt-text"];
const foo = base;

for (const rule of gitHubCustomRules) {
const ruleName = rule.names[1];
base[ruleName] = offByDefault.includes(ruleName) ? false : true;
}
// for (const rule of githubMarkdownLint) {
// const ruleName = rule.names[1];
// foo[ruleName] = offByDefault.includes(ruleName) ? false : true;
// }

module.exports.init = function init(consumerConfig) {
export function init(consumerConfig) {
// left overwrites right
return _.defaultsDeep(consumerConfig, accessibilityRules, base);
};
}
4 changes: 4 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"transform": {
}
}
87 changes: 24 additions & 63 deletions package-lock.json

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

14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
{
"name": "@github/markdownlint-github",
"version": "0.1.0",
"version": "0.2.0",
"description": "An opinionated collection of markdownlint rules used by GitHub.",
"main": "index.js",
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=18"
},
"directories": {
"test": "test"
},
"scripts": {
"publish": "npm publish --access public --@github:registry=https://registry.npmjs.org",
"test": "npm run lint && jest",
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"lint": "markdownlint-cli2 \"**/*.{md,mdx}\" \"!node_modules\" \"!docs/rules\" \"!test/example.md\" && eslint .",
"lint:fix": "npm run lint -- --fix"
},
"dependencies": {
"lodash": "^4.17.15"
"lodash-es": "^4.17.15"
},
"devDependencies": {
"eslint": "^8.22.0",
"eslint-plugin-github": "^5.0.1",
"jest": "^29.5.0",
"markdownlint": "^0.37.3",
"markdownlint-cli2": "^0.16.0"
"markdownlint-cli2": "^0.17.0"
},
"repository": {
"type": "git",
Expand Down
4 changes: 1 addition & 3 deletions src/helpers/strip-and-downcase-text.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/* Downcase and strip extra whitespaces and punctuation */
function stripAndDowncaseText(text) {
export function stripAndDowncaseText(text) {
return text
.toLowerCase()
.replace(/[.,/#!$%^&*;:{}=\-_`~()]/g, "")
.replace(/\s+/g, " ")
.trim();
}

module.exports = { stripAndDowncaseText };
16 changes: 9 additions & 7 deletions src/rules/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
module.exports = {
rules: [
require("./no-default-alt-text"),
require("./no-generic-link-text"),
require("./no-empty-alt-text"),
],
};
import { noEmptyStringAltRule } from "./no-empty-alt-text.js";
import { noGenericLinkTextRule } from "./no-generic-link-text.js";
import { altTextRule } from "./no-default-alt-text.js";

export const githubMarkdownLint = [
altTextRule,
noGenericLinkTextRule,
noEmptyStringAltRule,
];
8 changes: 4 additions & 4 deletions src/rules/no-default-alt-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ const combinedRegex = `(${[defaultScreenshotRegex, imageRegex].join("|")})`;
const markdownAltRegex = new RegExp(`!\\[${combinedRegex}\\]\\(.*\\)`, "gid");
const htmlAltRegex = new RegExp(`alt=["']${combinedRegex}["']`, "gid");

module.exports = {
export const altTextRule = {
names: ["GH001", "no-default-alt-text"],
description: "Images should have meaningful alternative text (alt text)",
information: new URL(
"https://github.com/github/markdownlint-github/blob/main/docs/rules/GH001-no-default-alt-text.md",
"https://github.com/github/markdownlint-github/blob/main/docs/rules/GH001-no-default-alt-text.md"
),
tags: ["accessibility", "images"],
function: function GH001(params, onError) {
Expand All @@ -30,12 +30,12 @@ module.exports = {
token.content.includes("<img") &&
token.children.some((child) => child.type === "html_inline"))
);
},
}
);
const inlineImages = params.parsers.markdownit.tokens.filter(
(token) =>
token.type === "inline" &&
token.children.some((child) => child.type === "image"),
token.children.some((child) => child.type === "image")
);

for (const token of [...htmlTagsWithImages, ...inlineImages]) {
Expand Down
6 changes: 3 additions & 3 deletions src/rules/no-empty-alt-text.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
export const noEmptyStringAltRule = {
names: ["GH003", "no-empty-alt-text"],
description: "Please provide an alternative text for the image.",
information: new URL(
"https://github.com/github/markdownlint-github/blob/main/docs/rules/GH003-no-empty-alt-text.md",
"https://github.com/github/markdownlint-github/blob/main/docs/rules/GH003-no-empty-alt-text.md"
),
tags: ["accessibility", "images"],
function: function GH003(params, onError) {
Expand All @@ -14,7 +14,7 @@ module.exports = {
token.content.includes("<img") &&
token.children.some((child) => child.type === "html_inline"))
);
},
}
);

const ImageRegex = new RegExp(/<img(.*?)>/, "gid");
Expand Down
Loading

0 comments on commit 62e3669

Please sign in to comment.