Skip to content

Commit

Permalink
feat: Create project
Browse files Browse the repository at this point in the history
  • Loading branch information
3w36zj6 committed Mar 31, 2024
1 parent 8663389 commit 767028f
Show file tree
Hide file tree
Showing 13 changed files with 373 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*.{js,ts,json}]
indent_style = tab
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
lib/
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bun 1.0.36
nodejs 20.12.0
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2024 3w36zj6
Copyright (c) 2015 azu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# textlint-plugin-mdx

[textlint](https://github.com/textlint/textlint) plugin to lint [MDX](https://mdxjs.com/)

## Installation

```sh
# npm
npm install textlint-plugin-mdx

# Yarn
yarn add textlint-plugin-mdx

# pnpm
pnpm add textlint-plugin-mdx

# Bun
bun add textlint-plugin-mdx
```

## Usage

```json
{
"plugins": {
"mdx": true
}
}
```

## Options

- `extensions`: `string[]`
- Additional file extensions for MDX

## Contributing

1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D

## License

[MIT License](LICENSE)
17 changes: 17 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "https://biomejs.dev/schemas/1.6.3/schema.json",
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
}
}
Binary file added bun.lockb
Binary file not shown.
52 changes: 52 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "textlint-plugin-mdx",
"version": "1.0.0",
"description": "textlint plugin to lint MDX",
"keywords": ["textlint", "textlintplugin", "plugin", "lint", "mdx"],
"repository": "https://github.com/3w36zj6/textlint-plugin-mdx",
"funding": {
"type": "github",
"url": "https://github.com/sponsors/3w36zj6"
},
"license": "MIT",
"author": "3w36zj6",
"main": "lib/index.js",
"files": ["lib"],
"scripts": {
"build": "esbuild --bundle src/*.ts --outdir=lib --platform=node",
"clean": "rm -frv lib",
"format": "biome format --write .",
"format:check": "biome format .",
"lint": "biome lint .",
"lint:fix": "biome lint --apply .",
"check": "biome check .",
"test": "vitest"
},
"dependencies": {
"remark-footnotes": "^4.0.1",
"remark-frontmatter": "^5.0.0",
"remark-gfm": "^4.0.0",
"remark-mdx": "^3.0.1",
"remark-parse": "^11.0.0",
"traverse": "^0.6.8",
"unified": "^11.0.4"
},
"devDependencies": {
"@biomejs/biome": "^1.6.3",
"@textlint/ast-node-types": "^14.0.4",
"@textlint/ast-tester": "^14.0.4",
"@textlint/types": "^14.0.4",
"@types/node": "^20.12.2",
"@types/traverse": "^0.6.36",
"bun-types": "latest",
"esbuild": "^0.20.2",
"textlint-scripts": "^14.0.4",
"textlint-tester": "^14.0.4",
"tsc": "^2.0.4",
"typescript": "^5.4.3",
"vitest": "^1.4.0"
},
"peerDependencies": {
"typescript": "^5.0.0"
}
}
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { MdxProcessor } from "./mdxProcessor";

export default {
Processor: MdxProcessor,
};
39 changes: 39 additions & 0 deletions src/mapping/markdown-syntax-map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// LICENSE : MIT
import { ASTNodeTypes } from "@textlint/ast-node-types";

export const SyntaxMap = {
root: ASTNodeTypes.Document,
paragraph: ASTNodeTypes.Paragraph,
blockquote: ASTNodeTypes.BlockQuote,
listItem: ASTNodeTypes.ListItem,
list: ASTNodeTypes.List,
Bullet: "Bullet", // no need?
heading: ASTNodeTypes.Header,
code: ASTNodeTypes.CodeBlock,
HtmlBlock: ASTNodeTypes.HtmlBlock,
thematicBreak: ASTNodeTypes.HorizontalRule,
// inline block
text: ASTNodeTypes.Str,
break: ASTNodeTypes.Break,
emphasis: ASTNodeTypes.Emphasis,
strong: ASTNodeTypes.Strong,
html: ASTNodeTypes.Html,
link: ASTNodeTypes.Link,
image: ASTNodeTypes.Image,
inlineCode: ASTNodeTypes.Code,
delete: ASTNodeTypes.Delete,
// remark(markdown) extension
// Following type is not in @textlint/ast-node-types
yaml: "Yaml",
table: "Table",
tableRow: "TableRow",
tableCell: "TableCell",
linkReference: "LinkReference",
imageReference: "ImageReference",
footnoteReference: "FootnoteReference", // textlint@12+
definition: "Definition",
/**
* @deprecated
*/
ReferenceDef: ASTNodeTypes.ReferenceDef,
} as const;
31 changes: 31 additions & 0 deletions src/mdxProcessor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { TxtNode } from "@textlint/ast-node-types";
import type { TextlintPluginOptions } from "@textlint/types";
import { parse } from "./parse";

export class MdxProcessor {
config: TextlintPluginOptions;
extensions: Array<string>;
constructor(config = {}) {
this.config = config;
this.extensions = this.config.extensions ? this.config.extensions : [];
}

availableExtensions() {
return [".mdx"].concat(this.extensions);
}

processor(_ext: string) {
return {
preProcess(text: string, _filePath?: string): TxtNode {
return parse(text);
},
// biome-ignore lint/suspicious/noExplicitAny: Allowing 'any' type for messages as the exact structure is not known.
postProcess(messages: any[], filePath?: string) {
return {
messages,
filePath: filePath ? filePath : "<mdx>",
};
},
};
}
}
126 changes: 126 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import type { TxtDocumentNode } from "@textlint/ast-node-types";
import { ASTNodeTypes } from "@textlint/ast-node-types";
import debug0 from "debug";
import traverse from "traverse";
import { SyntaxMap } from "./mapping/markdown-syntax-map";

import { unified } from "unified";

//import footnotes from "remark-footnotes";
import frontmatter from "remark-frontmatter";
import remarkGfm from "remark-gfm";
import remarkMdx from "remark-mdx";
import remarkParse from "remark-parse";
import type { Node } from "unist";

export const parseMarkdown = (text: string): Node => {
const remark = unified()
.use(remarkParse)
.use(frontmatter, ["yaml"])
.use(remarkGfm)
.use(remarkMdx);
/*.use(footnotes, {
inlineNotes: true
});*/
return remark.parse(text);
};

const debug = debug0("@textlint/markdown-to-ast");

export { ASTNodeTypes as Syntax };

/**
* parse Markdown text and return ast mapped location info.
* @param {string} text
*/
export function parse(text: string): TxtDocumentNode {
// remark-parse's AST does not consider BOM
// AST's position does not +1 by BOM
// So, just trim BOM and parse it for `raw` property
// textlint's SourceCode also take same approach - trim BOM and check the position
// This means that the loading side need to consider BOM position - for example fs.readFile and text slice script.
// https://github.com/micromark/micromark/blob/0f19c1ac25964872a160d8b536878b125ddfe393/lib/preprocess.mjs#L29-L31
const hasBOM = text.charCodeAt(0) === 0xfeff;
const textWithoutBOM = hasBOM ? text.slice(1) : text;
const ast = parseMarkdown(textWithoutBOM);
// biome-ignore lint/complexity/noForEach: traverse cannot use for...of syntax.
traverse(ast).forEach(function (node) {
const isMdx =
node &&
Object.prototype.hasOwnProperty.call(node, "type") &&
/^(mdxJsxFlowElement|mdxJsxTextElement|mdxjsEsm|mdxFlowExpression|mdxTextExpression)$/.test(
node.type,
);
if (this.notLeaf) {
// MDX support
if (isMdx) {
if (node.type === "mdxJsxFlowElement") {
node.type = ASTNodeTypes.Html;
} else if (node.type === "mdxJsxTextElement") {
node.type = ASTNodeTypes.Paragraph;
} else if (
node.type === "mdxjsEsm" ||
node.type === "mdxFlowExpression" ||
node.type === "mdxTextExpression"
) {
if (
Object.prototype.hasOwnProperty.call(node, "value") &&
/^(\/\*(.|\s)*\*\/)$/.test(node.value)
) {
node.type = ASTNodeTypes.Comment;
node.value = node.value.replace(/^(\/\*)/, "").replace(/\*\/$/, "");
} else {
node.type =
node.type === "mdxTextExpression"
? ASTNodeTypes.Code
: ASTNodeTypes.CodeBlock;
}
}
if (Object.prototype.hasOwnProperty.call(node, "attributes")) {
node.attributes = undefined;
}
if (Object.prototype.hasOwnProperty.call(node, "name")) {
node.name = undefined;
}
if (Object.prototype.hasOwnProperty.call(node, "data")) {
node.data = undefined;
}
} else if (node.type) {
const replacedType = SyntaxMap[node.type as keyof typeof SyntaxMap];
if (!replacedType) {
debug(`replacedType : ${replacedType} , node.type: ${node.type}`);
} else {
node.type = replacedType;
}
}
// map `range`, `loc` and `raw` to node
if (node.position) {
const position = node.position;
// line start with 1
// column start with 0
const positionCompensated = {
start: {
line: position.start.line,
column: Math.max(position.start.column - 1, 0),
},
end: {
line: position.end.line,
column: Math.max(position.end.column - 1, 0),
},
};
const range = [position.start.offset, position.end.offset] as const;
node.loc = positionCompensated;
node.range = range;
node.raw = textWithoutBOM.slice(range[0], range[1]);
// Compatible for https://github.com/syntax-tree/unist, but it is hidden
Object.defineProperty(node, "position", {
enumerable: false,
configurable: false,
writable: false,
value: position,
});
}
}
});
return ast as TxtDocumentNode;
}
22 changes: 22 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"lib": ["ESNext"],
"module": "esnext",
"target": "esnext",
"moduleResolution": "bundler",
"moduleDetection": "force",
"allowImportingTsExtensions": true,
"noEmit": true,
"composite": true,
"strict": true,
"downlevelIteration": true,
"skipLibCheck": true,
"jsx": "react-jsx",
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"allowJs": true,
"types": [
"bun-types" // add Bun global
]
}
}

0 comments on commit 767028f

Please sign in to comment.