Initial commit

This commit is contained in:
Autonomic Cooperative
2024-05-17 22:32:32 +00:00
commit d07472434f
43 changed files with 12506 additions and 0 deletions

View File

@ -0,0 +1,41 @@
import { payloadSlateToDomConfig, slateToHtml } from "slate-serializers";
import { Element } from "domhandler";
export const getContentArray = (content: any) => {
const html = slateToHtml(content, {
...payloadSlateToDomConfig,
elementTransforms: {
...payloadSlateToDomConfig.elementTransforms,
upload: ({ node }) =>
// @ts-ignore
new Element("img", {
src: node.value.filename,
width: `${node.value.width}`,
height: `${node.value.height}`,
}),
},
}).replaceAll("<p></p>", "<p>&nbsp;</p>");
const htmlImageArray: (
| string
| { src: string; width: number; height: number }
)[] = [];
let lastIndex = 0;
while (true) {
const imgStartIndex = html.indexOf("<img", lastIndex);
if (imgStartIndex === -1) {
htmlImageArray.push(html.substring(lastIndex));
break;
}
const imgEndIndex = html.indexOf(">", imgStartIndex) + 1;
const imgTag = html.substring(imgStartIndex, imgEndIndex);
const remainingHtml = html.substring(lastIndex, imgStartIndex);
const imgObject = {
src: imgTag.match(/src="(.*?)"/)![1],
width: +imgTag.match(/width="(.*?)"/)![1],
height: +imgTag.match(/height="(.*?)"/)![1],
};
htmlImageArray.push(remainingHtml, imgObject);
lastIndex = imgEndIndex;
}
return htmlImageArray;
};

View File

@ -0,0 +1,13 @@
import type { Post } from "@/types";
const url = import.meta.env.DEV
? "http://payload:3001"
: `${import.meta.env.PAYLOAD_URL}`;
export const getPosts = async () =>
(await (await fetch(`${url}/api/posts`)).json()).docs as Post[];
export const getPost = async (id: string) =>
(await (await fetch(`${url}/api/posts/${id}`)).json()) as Post;
export const getImageSrc = (src: string) => `${url}/media/${src}`;