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

로컬 링크 lint 규칙 개선 #451

Merged
merged 20 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .remarkrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { readFileSync } from "node:fs";

import { remarkLintLocalLinksValid } from "@portone-io/lint-local-links-valid";
import { load } from "js-yaml";
import stringWidth from "string-width";

Expand Down Expand Up @@ -69,7 +70,7 @@ export default {
"remark-lint-table-pipes",
["remark-lint-unordered-list-marker-style", "-"],
[
"remark-lint-local-links-valid",
remarkLintLocalLinksValid,
{
baseDir: "./src/content",
excludePaths: ["/api"],
Expand Down
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"astro",
"typescript",
"typescriptreact",
"mdx"
"mdx",
"yaml"
],
"eslint.experimental.useFlatConfig": true,
"iconify.inplace": false,
Expand All @@ -31,5 +32,7 @@
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"editor.rulers": [100]
},
"eslint.runtime": "./ts-node-eslint"
"eslint.runtime": "./ts-node-eslint",
"mdx.validate.validateFileLinks": "ignore",
"mdx.validate.validateReferences": "ignore"
}
41 changes: 40 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import eslint from "@eslint/js";
import {
navLintLocalLinksValid,
redirLintLocalLinksValid,
} from "@portone-io/lint-local-links-valid";
import tsEslintPlugin from "@typescript-eslint/eslint-plugin";
import tsEslintParser from "@typescript-eslint/parser";
import unocss from "@unocss/eslint-plugin";
Expand All @@ -8,6 +12,14 @@ import * as mdx from "eslint-plugin-mdx";
import prettierRecommended from "eslint-plugin-prettier/recommended";
import react from "eslint-plugin-react";
import sortImports from "eslint-plugin-simple-import-sort";
import { readFileSync } from "fs";
import { load } from "js-yaml";
import YAMLParser from "yaml-eslint-parser";

const redirects = load(readFileSync("src/content/docs/_redir.yaml", "utf8"));
if (!Array.isArray(redirects)) {
throw new Error("Expected an array of redirects");
}

/** @type {import("eslint").Linter.RulesRecord} */
const tsRules = {
Expand All @@ -34,7 +46,12 @@ export default [
},
{
files: ["**/*.{ts,tsx}"],
ignores: ["scripts/**/*.ts", "**/*.astro/*.ts", "**/*.mdx/*"],
ignores: [
"scripts/**/*.ts",
"**/*.astro/*.ts",
"**/*.mdx/*",
"**/__fixtures__/**/*",
],
languageOptions: {
parser: tsEslintParser,
parserOptions: {
Expand Down Expand Up @@ -137,4 +154,26 @@ export default [
"prettier/prettier": "off",
},
},
{
files: ["**/_redir.yaml", "**/_nav.yaml"],
ignores: [],
plugins: {
redir: redirLintLocalLinksValid,
nav: navLintLocalLinksValid,
},
languageOptions: {
parser: YAMLParser,
},
rules: {
"redir/local-links-valid": "error",
"nav/local-links-valid": [
"error",
{
redirects: Object.fromEntries(
redirects.map(({ old: from, new: to }) => [from, to]),
),
},
],
},
},
];
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"httpsnippet-lite": "^3.0.5",
"js-yaml": "^4.1.0",
"json5": "^2.2.3",
"@portone-io/lint-local-links-valid": "workspace:^",
"lodash-es": "^4.17.21",
"mdast-util-from-markdown": "^2.0.0",
"mdast-util-mdx": "^3.0.0",
Expand Down Expand Up @@ -107,7 +108,6 @@
"remark-lint-list-item-content-indent": "^3.1.2",
"remark-lint-list-item-indent": "^3.1.2",
"remark-lint-list-item-spacing": "^4.1.2",
"remark-lint-local-links-valid": "workspace:^",
"remark-lint-maximum-line-length": "^3.1.3",
"remark-lint-no-blockquote-without-marker": "^5.1.2",
"remark-lint-no-consecutive-blank-lines": "^4.1.3",
Expand Down Expand Up @@ -149,7 +149,8 @@
"unified": "^11.0.4",
"unist-util-visit": "^5.0.0",
"universal-cookie": "^7.1.4",
"unocss": "^0.59.4"
"unocss": "^0.59.4",
"yaml-eslint-parser": "^1.2.2"
},
"packageManager": "[email protected]",
"pnpm": {
Expand Down
1 change: 1 addition & 0 deletions packages/lint-local-links-valid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## @portone-io/lint-local-links-valid
29 changes: 29 additions & 0 deletions packages/lint-local-links-valid/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@portone-io/lint-local-links-valid",
"version": "0.1.1",
"repository": "https://github.com/portone-io/developers.portone.io",
"type": "module",
"private": true,
"main": "src/index.ts",
"scripts": {
"test": "vitest"
},
"dependencies": {
"eslint-plugin-yml": "^1.14.0",
"ts-pattern": "^5.1.1",
"unified-lint-rule": "^3.0.0",
"unist-util-visit": "^5.0.0",
"yaml": "^2.4.2",
"yaml-eslint-parser": "^1.2.2"
},
"devDependencies": {
"@types/node": "^20.12.7",
"@vitest/ui": "^1.5.3",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^1.5.3"
},
"peerDependencies": {
"eslint": ">=9.0.0"
},
"packageManager": "[email protected]"
}
2 changes: 2 additions & 0 deletions packages/lint-local-links-valid/src/eslint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as navLintLocalLinksValid } from "./nav.js";
export { default as redirLintLocalLinksValid } from "./redir.js";
95 changes: 95 additions & 0 deletions packages/lint-local-links-valid/src/eslint/nav.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import path from "path";
import type { AST } from "yaml-eslint-parser";

import {
isLocalLink,
isMarkdownExistsSync,
isYAMLPair,
isYAMLScalar,
isYAMLSequence,
resolveRedirect,
type RuleModule,
} from "../utils.js";

export const rule: RuleModule = {
meta: {
type: "problem",
schema: [
{
type: "object",
properties: {
redirects: {
type: "object",
patternProperties: {
".*": {
type: "string",
},
},
additionalProperties: false,
},
},
additionalProperties: false,
},
],
docs: {
description:
"`_nav.yaml` 파일에 정의되어 있는 markdown 파일 링크의 유효성을 검사합니다.",
},
fixable: "code",
},
create(context) {
const { name: filename, dir } = path.parse(context.physicalFilename);
const baseDir = path.resolve(dir, "..");
const options = context.options[0] as unknown;
const redirects: Map<string, string> =
options &&
typeof options === "object" &&
"redirects" in options &&
options.redirects &&
typeof options.redirects === "object"
? new Map<string, string>(Object.entries(options.redirects))
: new Map<string, string>();
if (filename !== "_nav") return {};
return {
YAMLScalar(node: AST.YAMLScalar) {
let url: string | null = null;
if (
isYAMLSequence(node.parent) &&
isYAMLPair(node.parent.parent) &&
isYAMLScalar(node.parent.parent.key) &&
node.parent.parent.key.value === "items" &&
typeof node.value === "string"
) {
url = node.value;
}
if (
isYAMLPair(node.parent) &&
node.parent.key !== node &&
isYAMLScalar(node.parent.key) &&
node.parent.key.value === "slug" &&
typeof node.value === "string"
) {
url = node.value;
}
if (url && isLocalLink(url)) {
url = url.split(/[#?]/)[0] ?? "";
const resolvedUrl = resolveRedirect(redirects, url);
if (!isLocalLink(resolvedUrl)) return;
const absPath = path.join(baseDir, resolvedUrl);
isMarkdownExistsSync(absPath, [], (reason) => {
context.report({
loc: node.loc,
message: reason,
});
});
}
},
};
},
};

const plugin = {
rules: { "local-links-valid": rule },
};

export default plugin;
Loading