init
This commit is contained in:
commit
b6ec1f19d5
8 changed files with 1555 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
node_modules/
|
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2023 sup39
|
||||||
|
|
||||||
|
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.
|
52
README.md
Normal file
52
README.md
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
# rehype-mdx-auto-import
|
||||||
|
A rehype plugin to implicitly import variables in MDX file
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
```bash
|
||||||
|
# If you are using yarn
|
||||||
|
yarn add @sup39/rehype-mdx-auto-import
|
||||||
|
|
||||||
|
# If you are using npm
|
||||||
|
npm install @sup39/rehype-mdx-auto-import
|
||||||
|
```
|
||||||
|
|
||||||
|
## Example
|
||||||
|
In next.config.mjs:
|
||||||
|
```javascript
|
||||||
|
import mdx from '@next/mdx';
|
||||||
|
import AutoImport from '@sup39/rehype-mdx-auto-import';
|
||||||
|
|
||||||
|
const withMDX = mdx({
|
||||||
|
extension: /\.mdx?$/,
|
||||||
|
options: {
|
||||||
|
rehypePlugins: [
|
||||||
|
[AutoImport,
|
||||||
|
// import {T, S, C as Code} from '@sup39/mdx-components';
|
||||||
|
{
|
||||||
|
import: ['T', 'S', {import: 'C', as: 'Code'}],
|
||||||
|
from: '@sup39/mdx-components',
|
||||||
|
},
|
||||||
|
// import * as React from 'react';
|
||||||
|
{
|
||||||
|
namespace: 'React',
|
||||||
|
from: 'react',
|
||||||
|
},
|
||||||
|
// import React2 from 'react';
|
||||||
|
{
|
||||||
|
default: 'React2',
|
||||||
|
from: 'react',
|
||||||
|
},
|
||||||
|
// import React3, {useState} from 'react';
|
||||||
|
{
|
||||||
|
default: 'React3',
|
||||||
|
import: ['useState'],
|
||||||
|
from: 'react',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
export default withMDX({
|
||||||
|
pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
|
||||||
|
});
|
||||||
|
```
|
25
index.d.ts
vendored
Normal file
25
index.d.ts
vendored
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
export = AutoImport;
|
||||||
|
/**
|
||||||
|
* @typedef {{
|
||||||
|
* from: string
|
||||||
|
* import?: (
|
||||||
|
* string | {import: string, as: string}
|
||||||
|
* )[]
|
||||||
|
* default?: string
|
||||||
|
* namespace?: string
|
||||||
|
* }} ImportEntry
|
||||||
|
*/
|
||||||
|
/** @type {import('unified').Plugin<ImportEntry[], import('hast').Root>} */
|
||||||
|
declare const AutoImport: import('unified').Plugin<ImportEntry[], import('hast').Root>;
|
||||||
|
declare namespace AutoImport {
|
||||||
|
export { ImportEntry };
|
||||||
|
}
|
||||||
|
type ImportEntry = {
|
||||||
|
from: string;
|
||||||
|
import?: (string | {
|
||||||
|
import: string;
|
||||||
|
as: string;
|
||||||
|
})[];
|
||||||
|
default?: string;
|
||||||
|
namespace?: string;
|
||||||
|
};
|
53
index.js
Normal file
53
index.js
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
/**
|
||||||
|
* @typedef {{
|
||||||
|
* from: string
|
||||||
|
* import?: (
|
||||||
|
* string | {import: string, as: string}
|
||||||
|
* )[]
|
||||||
|
* default?: string
|
||||||
|
* namespace?: string
|
||||||
|
* }} ImportEntry
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** @type {import('unified').Plugin<ImportEntry[], import('hast').Root>} */
|
||||||
|
const AutoImport = (...entries) => ({children}) => {
|
||||||
|
children.unshift(/**@type{import('mdast-util-mdx').MdxjsEsm}*/({
|
||||||
|
type: 'mdxjsEsm',
|
||||||
|
data: {
|
||||||
|
estree: {
|
||||||
|
type: 'Program',
|
||||||
|
sourceType: 'module',
|
||||||
|
body: entries.map(entry => ({
|
||||||
|
type: 'ImportDeclaration',
|
||||||
|
specifiers: [
|
||||||
|
...(entry.default ? [{
|
||||||
|
type: 'ImportDefaultSpecifier',
|
||||||
|
local: {type: 'Identifier', name: entry.default},
|
||||||
|
}] : []),
|
||||||
|
...(entry.namespace ? [{
|
||||||
|
type: 'ImportNamespaceSpecifier',
|
||||||
|
local: {type: 'Identifier', name: entry.namespace},
|
||||||
|
}] : []),
|
||||||
|
...(entry.import ?? []).map(e => ({
|
||||||
|
type: 'ImportSpecifier',
|
||||||
|
imported: {
|
||||||
|
type: 'Identifier',
|
||||||
|
name: typeof e === 'string' ? e : e.import,
|
||||||
|
},
|
||||||
|
local: {
|
||||||
|
type: 'Identifier',
|
||||||
|
name: typeof e === 'string' ? e : e.as,
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
],
|
||||||
|
source: {
|
||||||
|
type: 'Literal',
|
||||||
|
value: entry.from,
|
||||||
|
},
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = AutoImport;
|
36
package.json
Normal file
36
package.json
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
{
|
||||||
|
"name": "@sup39/rehype-mdx-auto-import",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"author": "sup39",
|
||||||
|
"repository": "https://github.com/sup39/rehype-mdx-auto-import",
|
||||||
|
"description": "A rehype plugin to implicitly import variables in MDX file",
|
||||||
|
"license": "MIT",
|
||||||
|
"main": "index.js",
|
||||||
|
"types": "index.d.ts",
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"lint": "eslint index.js",
|
||||||
|
"pre-commit:add": "git add -u"
|
||||||
|
},
|
||||||
|
"eslintConfig": {
|
||||||
|
"env": {
|
||||||
|
"node": true,
|
||||||
|
"es2020": true
|
||||||
|
},
|
||||||
|
"extends": ["@sup39/basic"]
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@sup39/eslint-config-basic": "^0.1.5",
|
||||||
|
"@types/hast": "^2.3.4",
|
||||||
|
"eslint": "^8.34.0",
|
||||||
|
"mdast-util-mdx": "^2.0.1",
|
||||||
|
"pre-commit": "^1.2.2",
|
||||||
|
"typescript": "^4.9.5",
|
||||||
|
"unified": "^10.1.2"
|
||||||
|
},
|
||||||
|
"pre-commit": [
|
||||||
|
"lint",
|
||||||
|
"build",
|
||||||
|
"pre-commit:add"
|
||||||
|
]
|
||||||
|
}
|
11
tsconfig.json
Normal file
11
tsconfig.json
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"include": ["index.js"],
|
||||||
|
"compilerOptions": {
|
||||||
|
"allowJs": true,
|
||||||
|
"checkJs": true,
|
||||||
|
"lib": ["es2020"],
|
||||||
|
"strict": true,
|
||||||
|
"declaration": true,
|
||||||
|
"emitDeclarationOnly": true
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue