Generate json
Tim Izzo tim@octree.ch
Fri, 25 Nov 2022 16:45:21 +0100
11 files changed,
85 insertions(+),
43 deletions(-)
M
generator/collection.ts
→
generator/collection.ts
@@ -1,6 +1,7 @@
// @deno-types="./types.d.ts" -import { getJsonLdCollection, postToJsonLd } from "./json-ld.ts"; import { getFileContent } from "./utils.ts"; +import { postToJson } from "./json-ld/post.ts"; +import { formatJsonCollection } from "./json-ld/collection.ts"; export const getCollection = async ( postsPath: string,@@ -10,9 +11,9 @@ let posts = [];
for await (const post of Deno.readDir(postsPath)) { const mdContent = await getFileContent(`${postsPath}/${post.name}`); - const jsonLdPost = await postToJsonLd(mdContent, mediaType); + const jsonLdPost = await postToJson(mdContent, mediaType); posts.push(jsonLdPost); } - return getJsonLdCollection(posts); + return formatJsonCollection(posts); };
M
generator/feeds/lib.ts
→
generator/feeds/lib.ts
@@ -2,18 +2,6 @@ // @deno-types="../types.d.ts"
import { getRssFeed } from "./rss.ts"; import { getAtomFeed } from "./atom.ts"; -export const generateJsonLd = async ( - collection: Collection, - outputPath: string -) => { - const jsonLdFilename = `${outputPath}/ld.json`; - await Deno.writeTextFile( - jsonLdFilename, - JSON.stringify(collection, undefined, 2) - ); - console.log(`JSON-LD written to ${jsonLdFilename}`); -}; - export const generateFeeds = async ( mediaType: MediaType, collection: Collection,
M
generator/json-ld.ts
→
generator/json-ld/post.ts
@@ -1,5 +1,5 @@
-// @deno-types="./types.d.ts" -import { slugify, getExtension } from "./utils.ts"; +// @deno-types="../types.d.ts" +import { slugify, getExtension } from "../utils.ts"; import { datetime, remarkParse,@@ -8,28 +8,10 @@ rehypeStringify,
remarkRehype, rehypeRaw, rehypeSanitize, -} from "./deps.ts"; - -const author = { - type: "Person", - name: "Tim Izzo", - email: "tim@5ika.ch", - url: "https://5ika.ch", -}; - -export const getJsonLdCollection = (items: Post[]): Collection => ({ - "@context": "https://www.w3.org/ns/activitystreams", - name: "Le blog de 5ika", - url: "https://5ika.ch/posts", - type: "Collection", - summary: "Retrouvez les postes de 5ika.ch dans votre feed", - totalItems: items.length, - items: items, - attributedTo: author, - published: datetime("").toISO(), -}); +} from "../deps.ts"; +import author from "../../templates/author.json" assert { type: "json" }; -export const postToJsonLd = async ( +export const postToJson = async ( mdContent: string, mediaType: MediaType ): Promise<Post> => {
A
generator/json-ld/collection.ts
@@ -0,0 +1,13 @@
+// @deno-types="../types.d.ts" +import { datetime } from "../deps.ts"; +import author from "../../templates/author.json" assert { type: "json" }; +import collection from "../../templates/collection.json" assert { type: "json" }; + +export const formatJsonCollection = (items: Post[]): Collection => + ({ + ...collection, + totalItems: items.length, + items: items, + attributedTo: author, + published: datetime("").toISO(), + } as Collection);
A
generator/json-ld/lib.ts
@@ -0,0 +1,14 @@
+export const copyStatics = async () => { + const srcPath = "src"; + const dstPath = "build/json"; + + for await (const pageInfo of Deno.readDir(srcPath)) { + if (!pageInfo.name.endsWith(".json")) continue; + + await Deno.copyFile( + `${srcPath}/${pageInfo.name}`, + `${dstPath}/${pageInfo.name}` + ); + console.log(`${pageInfo.name} copied to ${dstPath}`); + } +};
M
generator/mod.ts
→
generator/mod.ts
@@ -1,8 +1,9 @@
import { parse } from "https://deno.land/std/flags/mod.ts"; -import { generateFeeds, generateJsonLd } from "./feeds/lib.ts"; +import { generateFeeds } from "./feeds/lib.ts"; import { generatePages, generatePosts } from "./pages/lib.ts"; import { getCollection } from "./collection.ts"; import { getExtension } from "./utils.ts"; +import { copyStatics } from "./json-ld/lib.ts"; const args = parse(Deno.args);@@ -12,9 +13,9 @@
if (!args.output) throw new Error("No --output provided"); const OUTPUT_PATH = args.output; -const collectionHtml = await getCollection(POSTS_PATH, "text/html"); -await generateJsonLd(collectionHtml, OUTPUT_PATH); - +/** + * Generate content for mime types + */ const generateContent = async (mediaType: MediaType) => { const collection = await getCollection(POSTS_PATH, mediaType); const ext = getExtension(mediaType);@@ -27,3 +28,16 @@ };
await generateContent("text/html"); await generateContent("text/markdown"); + +/** + * Generate JSON-LD content + */ +const outputPath = `${OUTPUT_PATH}/json`; +await Deno.mkdir(outputPath, { recursive: true }); +await copyStatics(); +const jsonCollection = await getCollection(POSTS_PATH, "text/html"); +await Deno.writeTextFile( + `${outputPath}/collection.json`, + JSON.stringify(jsonCollection, null, 4) +); +console.log(`JSON+LD collection written to ${outputPath}/collection.json`);
M
generator/pages/lib.ts
→
generator/pages/lib.ts
@@ -39,7 +39,7 @@ const pagePath = `${outputPath}/${formatFilename(pageInfo.name, ext)}`;
const postLinks = collectionToLinks(collection); const rawMdPage = Deno.readTextFileSync(`${srcPath}/${pageInfo.name}`); const mdPage = rawMdPage.replace(/\\?\$POSTS_LIST/, postLinks); - const page = (await converter[mediaType]?.(mdPage)) || "NOT SUPPORTED YET"; + const page = (await converter[mediaType]?.(mdPage)) || mdPage; const pageContent = template .replace("$CONTENT", page) .replace("$TITLE", "Tim Izzo @5ika.ch");
M
generator/utils.ts
→
generator/utils.ts
@@ -5,6 +5,7 @@ ({
"text/html": "html", "text/markdown": "md", "text/gemini": "gmi", + "application/json": "json", }[mediaType] || "md"); export const getFileContent = async (filePath: string): Promise<string> => {
A
src/index.json
@@ -0,0 +1,16 @@
+{ + "@context": "http://schema.org/", + "@type": "Person", + "@id": "https://5ika.ch", + "type": "Person", + "name": "Tim Izzo", + "email": "tim@5ika.ch", + "giventName": "Timothy", + "familyName": "Izzo", + "alternateName": "5ika", + "jobTitle": "IT Engineer", + "url": "https://5ika.ch", + "worksFor": ["https://octree.ch"], + "image": "https://tooting.ch/system/accounts/avatars/107/355/320/386/938/203/original/7e6c00f72ed8b6db.jpeg", + "sameAs": "https://tooting.ch/users/5ika" +}
A
templates/collection.json
@@ -0,0 +1,7 @@
+{ + "@context": "https://www.w3.org/ns/activitystreams", + "name": "Le blog de 5ika", + "url": "https://5ika.ch/posts", + "type": "Collection", + "summary": "Retrouvez les postes de 5ika.ch dans votre feed" +}