mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-05-26 20:49:55 +00:00
Add image support
Works around Vite imports vite-imagetools is borked for this
This commit is contained in:
@@ -185,7 +185,7 @@ function add_toc_rehype(self, opts) {
|
|||||||
vFile.data.headings = [];
|
vFile.data.headings = [];
|
||||||
|
|
||||||
visit(tree, isHeadingNode, (node) => {
|
visit(tree, isHeadingNode, (node) => {
|
||||||
console.log(node)
|
// console.log(node)
|
||||||
vFile.data.headings.push({
|
vFile.data.headings.push({
|
||||||
level: node.depth,
|
level: node.depth,
|
||||||
title: hast_tree_to_string(node),
|
title: hast_tree_to_string(node),
|
||||||
@@ -196,6 +196,96 @@ function add_toc_rehype(self, opts) {
|
|||||||
vFile.data.fm.headings = vFile.data.headings;
|
vFile.data.fm.headings = vFile.data.headings;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
import toCamel from "just-camel-case";
|
||||||
|
const RE_SCRIPT_START =
|
||||||
|
/<script(?:\s+?[a-zA-z]+(=(?:["']){0,1}[a-zA-Z0-9]+(?<!module)(?:["']){0,1}){0,1})*\s*?>/;
|
||||||
|
function vite_images_rehype(opts) {
|
||||||
|
return async function transformer(tree, vFile) {
|
||||||
|
const urls = new Map();
|
||||||
|
const url_count = new Map();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} url
|
||||||
|
*/
|
||||||
|
function transformUrl(url) {
|
||||||
|
// url = decodeURIComponent(url)
|
||||||
|
// console.log("decoded", url)
|
||||||
|
|
||||||
|
// filenames can start with digits,
|
||||||
|
// prepend underscore to guarantee valid module name
|
||||||
|
let camel = `_${toCamel(url)}`;
|
||||||
|
const count = url_count.get(camel);
|
||||||
|
const dupe = urls.get(url);
|
||||||
|
|
||||||
|
if (count && !dupe) {
|
||||||
|
url_count.set(camel, count + 1);
|
||||||
|
camel = `${camel}_${count}`;
|
||||||
|
} else if (!dupe) {
|
||||||
|
url_count.set(camel, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
urls.set(url, {
|
||||||
|
path: url,
|
||||||
|
id: camel
|
||||||
|
});
|
||||||
|
|
||||||
|
return camel;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
// console.log(tree)
|
||||||
|
// vFile.data.headings = [];
|
||||||
|
|
||||||
|
// console.log(tree)
|
||||||
|
visit(tree, { tagName: "img" }, (node) => {
|
||||||
|
node.properties.src = `{${transformUrl(node.properties.src)}}`
|
||||||
|
// new URL('./img.png', import.meta.url).href
|
||||||
|
// vFile.data.headings.push({
|
||||||
|
// level: node.depth,
|
||||||
|
// title: hast_tree_to_string(node),
|
||||||
|
// });
|
||||||
|
});
|
||||||
|
visit(tree, { tagName: "Components.img" }, (node) => {
|
||||||
|
// let url = node.properties.src;
|
||||||
|
// url.includes("?") ? url = url + "&svex-enhanced" : url + "?svex-enhanced";
|
||||||
|
|
||||||
|
node.properties.src = `{${transformUrl(node.properties.src)}}`
|
||||||
|
// node.properties.src = `{new URL('${url}', import.meta.url)}`
|
||||||
|
// new URL('./img.png', import.meta.url).href
|
||||||
|
// vFile.data.headings.push({
|
||||||
|
// level: node.depth,
|
||||||
|
// title: hast_tree_to_string(node),
|
||||||
|
// });
|
||||||
|
});
|
||||||
|
|
||||||
|
let scripts = "";
|
||||||
|
// urls.forEach((x) => (scripts += `import ${x.id} from "${(x.path.includes("?") ? x.path + "&" : x.path + "?") + "url"}";\n`));
|
||||||
|
urls.forEach((x) => (scripts += `const ${x.id} = new URL("${x.path}", import.meta.url);\n`));
|
||||||
|
// console.log(scripts)
|
||||||
|
// urls.forEach((x) => {
|
||||||
|
// if (x.meta) {
|
||||||
|
// let a = ["src", "width", "height"]
|
||||||
|
// scripts += `import {${a.map((a) => a + " as " + x.id + "_" + a).join(",")}} from "${x.path.includes("?") ? x.path + "&as=metadata" : x.path + "?as=metadata:src;width;height"}";\n`
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
|
||||||
|
let is_script = false;
|
||||||
|
|
||||||
|
visit(tree, { type: "raw" }, (node) => {
|
||||||
|
// console.log(node)
|
||||||
|
if (RE_SCRIPT_START.test(node.value)) {
|
||||||
|
// console.log("inserting")
|
||||||
|
is_script = true;
|
||||||
|
node.value = node.value.replace(RE_SCRIPT_START, (script) => {
|
||||||
|
return `${script}\n${scripts}`;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @type {import("mdsvex").MdsvexOptions}
|
* @type {import("mdsvex").MdsvexOptions}
|
||||||
*/
|
*/
|
||||||
@@ -209,7 +299,7 @@ const config = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// layout: {
|
// layout: {
|
||||||
// _: "./src/layout.svelte"
|
// _: "./src/lib/mdlayouts/default.svelte"
|
||||||
// },
|
// },
|
||||||
|
|
||||||
highlight: {
|
highlight: {
|
||||||
@@ -264,6 +354,7 @@ const config = {
|
|||||||
rehypeKatexSvelte,
|
rehypeKatexSvelte,
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
[rehypeSlug, { prefix: "h-" }],
|
[rehypeSlug, { prefix: "h-" }],
|
||||||
|
vite_images_rehype
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
"github-slugger": "^2.0.0",
|
"github-slugger": "^2.0.0",
|
||||||
"glob": "^10.4.1",
|
"glob": "^10.4.1",
|
||||||
"hast-util-to-string": "^3.0.0",
|
"hast-util-to-string": "^3.0.0",
|
||||||
|
"just-camel-case": "^6.2.0",
|
||||||
"mdast-util-to-string": "^4.0.0",
|
"mdast-util-to-string": "^4.0.0",
|
||||||
"mdsvex": "^0.11.2",
|
"mdsvex": "^0.11.2",
|
||||||
"rehype-katex-svelte": "1.2",
|
"rehype-katex-svelte": "1.2",
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
<script>
|
||||||
|
export let src;
|
||||||
|
export let alt;
|
||||||
|
// console.log(src)
|
||||||
|
// import _PastedImage20240716123726Png from "./Pasted%20image%2020240716123726.png?meta";
|
||||||
|
|
||||||
|
</script>
|
||||||
|
<img src={src} alt={alt}/>
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
<script context="module">
|
||||||
|
import img from "$lib/htmlComponents/img.svelte";
|
||||||
|
export { img };
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<slot />
|
||||||
@@ -9,6 +9,8 @@ import { mdsvex } from 'mdsvex';
|
|||||||
import mdsvexConfig from "./mdsvex.config.js";
|
import mdsvexConfig from "./mdsvex.config.js";
|
||||||
import { extname } from 'node:path';
|
import { extname } from 'node:path';
|
||||||
|
|
||||||
|
// import { imagetools } from 'vite-imagetools'
|
||||||
|
|
||||||
function mdsvex_transform() {
|
function mdsvex_transform() {
|
||||||
return {
|
return {
|
||||||
name: "Mdsvex transformer",
|
name: "Mdsvex transformer",
|
||||||
@@ -23,6 +25,17 @@ function mdsvex_transform() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
const fallback: {[key: string]: string} = {
|
||||||
|
'.avif': 'png',
|
||||||
|
'.gif': 'gif',
|
||||||
|
'.heif': 'jpg',
|
||||||
|
'.jpeg': 'jpg',
|
||||||
|
'.jpg': 'jpg',
|
||||||
|
'.png': 'png',
|
||||||
|
'.tiff': 'jpg',
|
||||||
|
'.webp': 'png'
|
||||||
|
};
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
resolve: {
|
resolve: {
|
||||||
alias: {
|
alias: {
|
||||||
@@ -34,6 +47,25 @@ export default defineConfig({
|
|||||||
ViteImageOptimizer({
|
ViteImageOptimizer({
|
||||||
/* pass your config */
|
/* pass your config */
|
||||||
}),
|
}),
|
||||||
|
// imagetools({
|
||||||
|
// namedExports: false,
|
||||||
|
// defaultDirectives: async (url, metadata) => {
|
||||||
|
// console.log("vite", url)
|
||||||
|
// // if (!url.searchParams.has('svex-enhanced')) return new URLSearchParams();
|
||||||
|
|
||||||
|
// // const img_width = url.searchParams.get('imgWidth');
|
||||||
|
// // const width = img_width ? parseInt(img_width) : (await metadata()).width;
|
||||||
|
// // if (!width) {
|
||||||
|
// // console.warn(`Could not determine width of image ${url.href}`);
|
||||||
|
// return new URLSearchParams();
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// // return new URLSearchParams({
|
||||||
|
// // 'metadata': '',
|
||||||
|
// // // format: `avif;webp;${fallback[path.extname(url.href)] ?? 'png'}`
|
||||||
|
// // });
|
||||||
|
// },
|
||||||
|
// }),
|
||||||
// mdsvex_transform(),
|
// mdsvex_transform(),
|
||||||
sveltekit(),
|
sveltekit(),
|
||||||
dynamicImport({
|
dynamicImport({
|
||||||
|
|||||||
Generated
+9
-1
@@ -122,6 +122,9 @@ importers:
|
|||||||
hast-util-to-string:
|
hast-util-to-string:
|
||||||
specifier: ^3.0.0
|
specifier: ^3.0.0
|
||||||
version: 3.0.0
|
version: 3.0.0
|
||||||
|
just-camel-case:
|
||||||
|
specifier: ^6.2.0
|
||||||
|
version: 6.2.0
|
||||||
mdast-util-to-string:
|
mdast-util-to-string:
|
||||||
specifier: ^4.0.0
|
specifier: ^4.0.0
|
||||||
version: 4.0.0
|
version: 4.0.0
|
||||||
@@ -946,7 +949,7 @@ packages:
|
|||||||
'@codemirror/view': '>=6.0.0'
|
'@codemirror/view': '>=6.0.0'
|
||||||
|
|
||||||
Notes@file:packages/website/Notes-1.0.0.tgz:
|
Notes@file:packages/website/Notes-1.0.0.tgz:
|
||||||
resolution: {integrity: sha512-5ly2YBmx1v2j7S5QtOTzZbTY5WJv1/1JNKGXOle8VtgRwg1WvOohRqDSaySiypHDWOGebqLq9KbxIr/Y6VCUnw==, tarball: file:packages/website/Notes-1.0.0.tgz}
|
resolution: {integrity: sha512-/+M3hG2iGHPS6t1mSgVniEdhrVG0jZ41BikQqqvGt3AgOXCY7FnyqziKALc/g3oVaulnwduqg0ZjaxwhQlh3GA==, tarball: file:packages/website/Notes-1.0.0.tgz}
|
||||||
version: 1.0.0
|
version: 1.0.0
|
||||||
|
|
||||||
acorn@8.12.0:
|
acorn@8.12.0:
|
||||||
@@ -1521,6 +1524,9 @@ packages:
|
|||||||
resolution: {integrity: sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==}
|
resolution: {integrity: sha512-PRp66vJ865SSqOlgqS8hujT5U4AOgMfhrwYIuIhfKaoSCZcirrmASQr8CX7cUg+RMih+hgznrjp99o+W4pJLHQ==}
|
||||||
engines: {node: '>=12', npm: '>=6'}
|
engines: {node: '>=12', npm: '>=6'}
|
||||||
|
|
||||||
|
just-camel-case@6.2.0:
|
||||||
|
resolution: {integrity: sha512-ICenRLXwkQYLk3UyvLQZ+uKuwFVJ3JHFYFn7F2782G2Mv2hW8WPePqgdhpnjGaqkYtSVWnyCESZhGXUmY3/bEg==}
|
||||||
|
|
||||||
jwa@1.4.1:
|
jwa@1.4.1:
|
||||||
resolution: {integrity: sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==}
|
resolution: {integrity: sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==}
|
||||||
|
|
||||||
@@ -3661,6 +3667,8 @@ snapshots:
|
|||||||
ms: 2.1.3
|
ms: 2.1.3
|
||||||
semver: 7.6.2
|
semver: 7.6.2
|
||||||
|
|
||||||
|
just-camel-case@6.2.0: {}
|
||||||
|
|
||||||
jwa@1.4.1:
|
jwa@1.4.1:
|
||||||
dependencies:
|
dependencies:
|
||||||
buffer-equal-constant-time: 1.0.1
|
buffer-equal-constant-time: 1.0.1
|
||||||
|
|||||||
Reference in New Issue
Block a user