-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33ac3d1
commit d75cd72
Showing
14 changed files
with
1,140 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This is an MDX example: | ||
|
||
![img](./cat.jpg?myrotate=145&crop=o-300,o-300,o300,o300&width=200&tractor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { MDXComponents } from "mdx/types"; | ||
import Image from "next/image"; | ||
|
||
export function useMDXComponents(components: MDXComponents): MDXComponents { | ||
return { | ||
TractorLoaderImage: (props) => { | ||
return ( | ||
<Image | ||
{...props} | ||
alt={props.alt || ""} | ||
className="my-0" | ||
sizes="(max-width: 56rem) 50vw, 28rem" | ||
/> | ||
); | ||
}, | ||
...components, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
dist/ | ||
coverage/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2023 Jason Thorsness | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# 🚜 Tractor Loader MDX | ||
|
||
Tractor Loader is a remark plugin that enables Tractor Loader for images in MDX files loaded through | ||
[@next/mdx](https://nextjs.org/docs/app/building-your-application/configuring/mdx). | ||
|
||
```markdown | ||
![img](./cat.jpg?width=200&tractor) | ||
``` | ||
|
||
Refer to the | ||
[installation instructions](https://tractor-loader.vercel.app/#section-overview-installation). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "tractor-loader-mdx", | ||
"type": "module", | ||
"version": "0.1.0", | ||
"description": "A remark plugin that enables Tractor Loader for images in MDX files loaded through @next/mdx", | ||
"repository": "https://github.com/jasonthorsness/tractor-loader-mdx", | ||
"license": "MIT", | ||
"author": "Jason Thorsness", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist", | ||
"README.md" | ||
], | ||
"scripts": { | ||
"build": "tsc", | ||
"dev": "tsc --watch", | ||
"preinstall": "npx only-allow pnpm", | ||
"lint": "prettier --write --parser typescript \"{src,test}/**/*.{j,t}s\"" | ||
}, | ||
"devDependencies": { | ||
"prettier": "^3.0.0", | ||
"typescript": "^5.1.6", | ||
"@types/mdast": "^3.0.0" | ||
}, | ||
"dependencies": { | ||
"remark": "^14.0.0", | ||
"remark-mdx": "^2.0.0", | ||
"unified": "^10.0.0", | ||
"unist-util-visit": "^4.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { visit } from "unist-util-visit"; | ||
import { remark } from "remark"; | ||
import remarkMDX from "remark-mdx"; | ||
|
||
import * as mdast from "mdast"; | ||
import type * as unified from "unified"; | ||
|
||
export const plugin: unified.Plugin<[], mdast.Root> = () => { | ||
return (tree) => { | ||
const imports: mdast.Content[] = []; | ||
let counter = 0; | ||
visit(tree, "image", (node, index, parent) => { | ||
if (parent == null || index == null || !/\?.+&tractor$/.test(node.url)) { | ||
return; | ||
} | ||
|
||
counter++; | ||
|
||
const sanitizedURL = JSON.stringify(node.url); | ||
|
||
const parsedRoot = remark() | ||
.use(remarkMDX) | ||
.parse( | ||
`import img${counter} from ${sanitizedURL};\n\n<TractorLoaderImage src={img${counter}} />`, | ||
); | ||
|
||
if (parsedRoot.children.length !== 2) { | ||
throw new Error("Internal error"); | ||
} | ||
|
||
const importNode = parsedRoot.children[0]; | ||
const imageNode = parsedRoot.children[1]; | ||
|
||
parent.children[index] = imageNode; | ||
imports.push(importNode); | ||
}); | ||
|
||
tree.children.unshift(...imports); | ||
}; | ||
}; | ||
|
||
export default plugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"outDir": "dist", | ||
"sourceMap": true, | ||
"target": "es5", | ||
"module": "ES2015", | ||
"moduleResolution": "node", | ||
"esModuleInterop": true, | ||
"declaration": true, | ||
"allowJs": false, | ||
"noUnusedLocals": true, | ||
"strictNullChecks": true, | ||
"noImplicitAny": true, | ||
"removeComments": false, | ||
"skipLibCheck": true, | ||
"lib": ["esnext", "DOM"], | ||
"strict": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"noImplicitThis": true, | ||
"alwaysStrict": true, | ||
"allowUnusedLabels": false, | ||
"allowUnreachableCode": false, | ||
"exactOptionalPropertyTypes": true, | ||
"noImplicitOverride": true, | ||
"noPropertyAccessFromIndexSignature": true, | ||
"noUnusedParameters": true | ||
}, | ||
"include": ["src"], | ||
"exclude": ["node_modules"] | ||
} |
Oops, something went wrong.