next-blurhash-previews 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/bin/markdown-sync.js +25 -0
- package/components/ImageWithPreview.tsx +99 -0
- package/components/NextWrapper.tsx +19 -0
- package/components/imagePreviewBootstrap.tsx +7 -0
- package/example.md +9 -0
- package/img1.png +0 -0
- package/img5.png +0 -0
- package/index.js +5 -0
- package/junk.js +1 -0
- package/next-runner/.eslintrc.json +3 -0
- package/next-runner/README.md +34 -0
- package/next-runner/next.config.js +6 -0
- package/next-runner/package-lock.json +5064 -0
- package/next-runner/package.json +23 -0
- package/next-runner/pages/_app.js +11 -0
- package/next-runner/pages/_document.js +19 -0
- package/next-runner/pages/index.js +26 -0
- package/next-runner/public/dynamo-introduction/img1.png +0 -0
- package/next-runner/public/dynamo-introduction/img10.png +0 -0
- package/next-runner/public/dynamo-introduction/img11.png +0 -0
- package/next-runner/public/dynamo-introduction/img12.png +0 -0
- package/next-runner/public/dynamo-introduction/img13.png +0 -0
- package/next-runner/public/dynamo-introduction/img14.png +0 -0
- package/next-runner/public/dynamo-introduction/img15.png +0 -0
- package/next-runner/public/dynamo-introduction/img2.png +0 -0
- package/next-runner/public/dynamo-introduction/img3.png +0 -0
- package/next-runner/public/dynamo-introduction/img4.png +0 -0
- package/next-runner/public/dynamo-introduction/img5.png +0 -0
- package/next-runner/public/dynamo-introduction/img5a.png +0 -0
- package/next-runner/public/dynamo-introduction/img6.png +0 -0
- package/next-runner/public/dynamo-introduction/img7.png +0 -0
- package/next-runner/public/dynamo-introduction/img8.png +0 -0
- package/next-runner/public/dynamo-introduction/img9.png +0 -0
- package/next-runner/public/favicon.ico +0 -0
- package/next-runner/public/vercel.svg +4 -0
- package/next-runner/styles/Home.module.css +0 -0
- package/next-runner/styles/globals.css +22 -0
- package/next-runner/yalc.lock +9 -0
- package/next-runner/yarn.lock +1645 -0
- package/output.md +15 -0
- package/package.json +42 -0
- package/plugin.js +78 -0
- package/tsconfig.json +21 -0
- package/tsconfig.node.json +8 -0
- package/util/setBootstrap.js +13 -0
- package/vite.config.ts +21 -0
- package/yalc.lock +9 -0
package/output.md
ADDED
package/package.json
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
{
|
2
|
+
"name": "next-blurhash-previews",
|
3
|
+
"version": "0.0.1",
|
4
|
+
"description": "",
|
5
|
+
"main": "index.js",
|
6
|
+
"bin": {
|
7
|
+
"next-static-markdown-previews": "./bin/markdown-sync.js"
|
8
|
+
},
|
9
|
+
"scripts": {
|
10
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
11
|
+
"build": "vite build",
|
12
|
+
"build-watch": "vite build -w"
|
13
|
+
},
|
14
|
+
"type": "module",
|
15
|
+
"repository": {
|
16
|
+
"type": "git",
|
17
|
+
"url": "git+https://github.com/arackaf/next-static-image-previews.git"
|
18
|
+
},
|
19
|
+
"author": "",
|
20
|
+
"license": "ISC",
|
21
|
+
"bugs": {
|
22
|
+
"url": "https://github.com/arackaf/next-static-image-previews/issues"
|
23
|
+
},
|
24
|
+
"homepage": "https://github.com/arackaf/next-static-image-previews#readme",
|
25
|
+
"dependencies": {
|
26
|
+
"@types/react": "^18.0.15",
|
27
|
+
"@types/react-dom": "^18.0.6",
|
28
|
+
"@vitejs/plugin-react": "^1.3.2",
|
29
|
+
"blurhash": "^1.1.5",
|
30
|
+
"next": "^12.2.0",
|
31
|
+
"node-html-parser": "^5.3.3",
|
32
|
+
"prettier": "^2.7.1",
|
33
|
+
"remark": "^14.0.2",
|
34
|
+
"retext": "^8.1.0",
|
35
|
+
"sharp": "^0.30.7",
|
36
|
+
"to-vfile": "^7.2.3",
|
37
|
+
"unist-util-visit": "^4.1.0",
|
38
|
+
"unist-util-visit-parents": "^5.1.0",
|
39
|
+
"vfile-reporter": "^7.0.4",
|
40
|
+
"vite": "^2.9.13"
|
41
|
+
}
|
42
|
+
}
|
package/plugin.js
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
import fs from "fs";
|
2
|
+
import path, { dirname } from "path";
|
3
|
+
|
4
|
+
import sharp from "sharp";
|
5
|
+
import { encode, isBlurhashValid } from "blurhash";
|
6
|
+
|
7
|
+
import { visit } from "unist-util-visit";
|
8
|
+
import { visitParents } from "unist-util-visit-parents";
|
9
|
+
|
10
|
+
import HTMLParser from "node-html-parser";
|
11
|
+
import prettier from "prettier";
|
12
|
+
|
13
|
+
import { fileURLToPath } from "url";
|
14
|
+
const __filename = fileURLToPath(import.meta.url);
|
15
|
+
const __dirname = dirname(__filename);
|
16
|
+
|
17
|
+
export default function retextSentenceSpacing() {
|
18
|
+
return (tree, file, done) => {
|
19
|
+
visitParents(tree, "image", async (node, ancestors) => {
|
20
|
+
const { url } = node;
|
21
|
+
|
22
|
+
//const resolvedPath = path.join(process.cwd(), url);
|
23
|
+
const resolvedPath = path.join(__dirname, url);
|
24
|
+
console.log(resolvedPath);
|
25
|
+
|
26
|
+
const image = sharp(resolvedPath);
|
27
|
+
const dimensions = await image.metadata();
|
28
|
+
|
29
|
+
const { width, height } = dimensions;
|
30
|
+
console.log(height, width);
|
31
|
+
|
32
|
+
image
|
33
|
+
.raw()
|
34
|
+
.ensureAlpha()
|
35
|
+
.toBuffer((err, buffer) => {
|
36
|
+
console.log("got buffer");
|
37
|
+
try {
|
38
|
+
if (err) {
|
39
|
+
console.log("Error getting buffer", err);
|
40
|
+
} else {
|
41
|
+
const blurhash = encode(new Uint8ClampedArray(buffer), width, height, 4, 4);
|
42
|
+
if (isBlurhashValid(blurhash)) {
|
43
|
+
console.log({ blurhash, w: width, h: height });
|
44
|
+
} else {
|
45
|
+
return console.log("FAIL");
|
46
|
+
}
|
47
|
+
}
|
48
|
+
} catch (err) {
|
49
|
+
return console.log("FAIL", err);
|
50
|
+
}
|
51
|
+
});
|
52
|
+
|
53
|
+
const parent = ancestors[ancestors.length - 1];
|
54
|
+
const index = parent.children.indexOf(node);
|
55
|
+
|
56
|
+
const newNode = HTMLParser.parse(`<div>
|
57
|
+
<img />
|
58
|
+
<span>Hey there 2</span>
|
59
|
+
</div>`);
|
60
|
+
|
61
|
+
parent.children[index] = {
|
62
|
+
type: "html",
|
63
|
+
value: prettier.format(newNode.outerHTML, { parser: "html" }).trimEnd(),
|
64
|
+
};
|
65
|
+
|
66
|
+
setTimeout(() => {
|
67
|
+
done();
|
68
|
+
}, 3000);
|
69
|
+
//parent.children[index] = replacement;
|
70
|
+
|
71
|
+
// parent.children.push({
|
72
|
+
// type: "html",
|
73
|
+
// value: `<div><img /><span>Hey there</span></div>`,
|
74
|
+
// position: node.position,
|
75
|
+
// });
|
76
|
+
});
|
77
|
+
};
|
78
|
+
}
|
package/tsconfig.json
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"target": "ES2020",
|
4
|
+
"useDefineForClassFields": true,
|
5
|
+
"lib": ["DOM", "DOM.Iterable", "ESNext"],
|
6
|
+
"allowJs": false,
|
7
|
+
"skipLibCheck": true,
|
8
|
+
"esModuleInterop": false,
|
9
|
+
"allowSyntheticDefaultImports": true,
|
10
|
+
"strict": true,
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
12
|
+
"module": "ESNext",
|
13
|
+
"moduleResolution": "Node",
|
14
|
+
"resolveJsonModule": true,
|
15
|
+
"isolatedModules": true,
|
16
|
+
"noEmit": true,
|
17
|
+
"jsx": "react-jsx"
|
18
|
+
},
|
19
|
+
"include": ["src"],
|
20
|
+
"references": [{ "path": "./tsconfig.node.json" }]
|
21
|
+
}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
import fs from "fs";
|
2
|
+
|
3
|
+
export default function writeBootstrapScript() {
|
4
|
+
return {
|
5
|
+
name: "write-bootstrap-script",
|
6
|
+
closeBundle() {
|
7
|
+
let BootstrapModule = fs.readFileSync("./components/imagePreviewBootstrap.tsx", "utf8");
|
8
|
+
const bootstrapScript = fs.readFileSync("./build/ImageWithPreview.js", "utf8");
|
9
|
+
BootstrapModule = BootstrapModule.replace("/*HERE*/", bootstrapScript.replace(/[\r\n]\s*$/, "").replace(/`/g, "\\`")).replace(/\${/g, "\\${");
|
10
|
+
fs.writeFileSync("./imagePreviewBootstrap.js", BootstrapModule);
|
11
|
+
},
|
12
|
+
};
|
13
|
+
}
|
package/vite.config.ts
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
import { defineConfig } from "vite";
|
2
|
+
import react from "@vitejs/plugin-react";
|
3
|
+
import writeBootstrapPlugin from "./util/setBootstrap";
|
4
|
+
|
5
|
+
export default defineConfig({
|
6
|
+
build: {
|
7
|
+
target: "es2022",
|
8
|
+
outDir: "./build",
|
9
|
+
lib: {
|
10
|
+
entry: "components/ImageWithPreview.tsx",
|
11
|
+
formats: ["cjs"],
|
12
|
+
fileName: () => "ImageWithPreview.js",
|
13
|
+
name: "imageWithPreview",
|
14
|
+
},
|
15
|
+
//minify: false,
|
16
|
+
rollupOptions: {
|
17
|
+
external: ["react", "react-dom", "next", "next/script"],
|
18
|
+
},
|
19
|
+
},
|
20
|
+
plugins: [react(), writeBootstrapPlugin()],
|
21
|
+
});
|