Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
CirnoV committed Dec 16, 2024
1 parent 3ac1e2a commit 4318e0a
Show file tree
Hide file tree
Showing 13 changed files with 332 additions and 48 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@parcel/watcher": "^2.4.1",
"@portone-io/lint-local-links-valid": "workspace:^",
"@portone-io/lint-no-jamo": "workspace:^",
"@portone-io/remark-param-tree": "workspace:^",
"@portone/browser-sdk": "^0.0.10",
"@rollup/plugin-yaml": "^4.1.2",
"@shikijs/rehype": "^1.11.0",
Expand Down
1 change: 1 addition & 0 deletions packages/remark-param-tree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## @portone-io/remark-param-tree
31 changes: 31 additions & 0 deletions packages/remark-param-tree/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@portone-io/remark-param-tree",
"version": "0.0.1",
"repository": "https://github.com/portone-io/developers.portone.io",
"type": "module",
"private": true,
"main": "src/index.ts",
"scripts": {
"test": "vitest"
},
"dependencies": {
"mdast-util-from-markdown": "^2.0.1",
"mdast-util-to-string": "^4.0.0",
"remark": "^15.0.1",
"remark-stringify": "^11.0.0",
"ts-pattern": "^5.1.1",
"unified": "^11.0.5",
"unist-util-visit": "^5.0.0",
"unist-util-visit-parents": "^6.0.1"
},
"devDependencies": {
"@types/mdast": "^4.0.4",
"@types/node": "^20.12.7",
"@types/unist": "^3.0.3",
"@vitest/ui": "^1.5.3",
"mdast-util-mdx-jsx": "^3.1.3",
"vfile": "^6.0.3",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^1.5.3"
}
}
115 changes: 115 additions & 0 deletions packages/remark-param-tree/scripts/migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import "mdast-util-mdx-jsx";

import path from "node:path";

import type { BlockContent, DefinitionContent, Paragraph, Root } from "mdast";
import { fromMarkdown } from "mdast-util-from-markdown";
import { toString } from "mdast-util-to-string";
import { match, P } from "ts-pattern";
import { SKIP, visit } from "unist-util-visit";
import type { VFile } from "vfile";

const isParameter = (node: BlockContent | DefinitionContent) => {};

Check failure on line 12 in packages/remark-param-tree/scripts/migration.ts

View workflow job for this annotation

GitHub Actions / Lint code and MDX files

'isParameter' is assigned a value but never used. Allowed unused vars must match /^_/u

Check failure on line 12 in packages/remark-param-tree/scripts/migration.ts

View workflow job for this annotation

GitHub Actions / Lint code and MDX files

'node' is defined but never used. Allowed unused args must match /^_/u

type TypeDefinition = {
name: string;
type: string;
optional: boolean;
};

export default function remarkParamTreePlugin() {
return function (tree: Root, file: VFile) {
const workingFile = path.resolve(
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument
file.cwd,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
file.history[0] ?? "",
);
visit(tree, "mdxJsxFlowElement", (node) => {
if (node.name === "ParamTree") {
visit(node, "listItem", (listItem) => {
if (listItem.children[0]?.type !== "paragraph") {
return;
}
listItem.children[0].children = listItem.children[0].children.filter(
(child) =>
match(child)
.with(
{
type: "text",
value: P.when((value) => value.trim() === ""),
},
() => false,
)
.otherwise(() => true),
);
const typeDefinition: TypeDefinition | null = match(
listItem.children[0],
)
.with(
{
children: [
{ type: P.not(P.union("inlineCode", "strong")) },
...P.array(),
],
},
() => null,
)
.with(
{
children: [
{ type: P.union("inlineCode", "strong") },
{
type: "mdxJsxTextElement",
name: "mark",
},
],
},
(typeDefinition) => {
return {
name: toString(typeDefinition.children[0]),
type: toString(typeDefinition.children[1]),
optional: true,
} satisfies TypeDefinition;
},
)
.with(
{
children: [
{ type: P.union("inlineCode", "strong") },
{
type: "mdxJsxTextElement",
name: "mark",
},
{
type: "mdxJsxTextElement",
name: "mark",
},
],
},
(typeDefinition) => {
return {
name: toString(typeDefinition.children[0]),
type: toString(typeDefinition.children[2]),
optional: false,
} satisfies TypeDefinition;
},
)
.otherwise(() => {
// console.log(workingFile, toString(listItem.children[0]));
return null;
});

if (typeDefinition) {
listItem.children[0] = fromMarkdown(
`${typeDefinition.name}${typeDefinition.optional ? "" : "*"}: ${typeDefinition.type}`,
"utf-8",
).children[0] as Paragraph;
console.log(workingFile, listItem.children[0]);
}
});
}
return SKIP;
});
};
}
22 changes: 22 additions & 0 deletions packages/remark-param-tree/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import "mdast-util-mdx-jsx";

import path from "node:path";

Check failure on line 3 in packages/remark-param-tree/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint code and MDX files

'path' is defined but never used. Allowed unused vars must match /^_/u

import type { BlockContent, DefinitionContent, Paragraph, Root } from "mdast";

Check failure on line 5 in packages/remark-param-tree/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint code and MDX files

'Paragraph' is defined but never used. Allowed unused vars must match /^_/u
import { fromMarkdown } from "mdast-util-from-markdown";

Check failure on line 6 in packages/remark-param-tree/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint code and MDX files

'fromMarkdown' is defined but never used. Allowed unused vars must match /^_/u
import { toString } from "mdast-util-to-string";

Check failure on line 7 in packages/remark-param-tree/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint code and MDX files

'toString' is defined but never used. Allowed unused vars must match /^_/u
import { match, P } from "ts-pattern";

Check failure on line 8 in packages/remark-param-tree/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint code and MDX files

'match' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 8 in packages/remark-param-tree/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint code and MDX files

'P' is defined but never used. Allowed unused vars must match /^_/u
import { SKIP, visit } from "unist-util-visit";

Check failure on line 9 in packages/remark-param-tree/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint code and MDX files

'SKIP' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 9 in packages/remark-param-tree/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint code and MDX files

'visit' is defined but never used. Allowed unused vars must match /^_/u
import type { VFile } from "vfile";

const isParameter = (node: BlockContent | DefinitionContent) => {};

type TypeDefinition = {
name: string;
type: string;
optional: boolean;
};

export default function remarkParamTreePlugin() {
return function (tree: Root, file: VFile) {};
}
14 changes: 14 additions & 0 deletions packages/remark-param-tree/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"types": ["vitest/globals"],
"baseUrl": "./"
}
}
Loading

0 comments on commit 4318e0a

Please sign in to comment.