Skip to content

Commit

Permalink
Handle the language context
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinyaigeek committed May 3, 2024
1 parent 63dfb3e commit 64e1ac5
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 8 deletions.
6 changes: 5 additions & 1 deletion packages/applications/turbo-blog/src/build/build.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Router } from "ssg-router";
import type { Context } from "./context/context";
import { generateIndexPage } from "./handlers/index/generate";
import { outputIndexPage } from "./handlers/index/output";
import { registerLanguagePlugin } from "./plugin/language";

const router = new Router();
const router = new Router<Context>();

router.register(registerLanguagePlugin);

router.on("/", {
generate: generateIndexPage,
Expand Down
3 changes: 3 additions & 0 deletions packages/applications/turbo-blog/src/build/context/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type Context = {
language: "ja" | "en";
};
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import fs from "node:fs/promises";
import nodePath from "node:path";
import { renderToStaticMarkup } from "react-dom/server";
import type { GenerateHandler } from "ssg-router";
import { Layout } from "../../../ui/components/Layout/Layout";
import { Home } from "../../../ui/pages/Home/Home";
import { GetBlogPostsUsecase } from "../../application/getBlogPosts/getBlogposts.usecase";
import type { Context } from "../../context/context";
import { BlogRepository } from "../../model/blog/blog.repository";
import { Shell } from "../../util/helmet";

export const generateIndexPage: () => Promise<string> = async () => {
export const generateIndexPage: GenerateHandler<Context> = async ({
context,
}) => {
const blogRepository = new BlogRepository(fs, nodePath);
const getblogPostsUsecase = new GetBlogPostsUsecase(blogRepository);
const blogPosts = await getblogPostsUsecase.getBlogPosts();
const language = context.language;

return renderToStaticMarkup(
<Shell language="ja" which="TODO" title="shinyaigeek.dev" slug="/">
<Layout language="ja" page="1" currentPath="/">
<Shell language={language} which="TODO" title="shinyaigeek.dev" slug="/">
<Layout language={language} page="1" currentPath="/">
<Home />
</Layout>
</Shell>,
Expand Down
8 changes: 8 additions & 0 deletions packages/applications/turbo-blog/src/build/plugin/language.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Plugin } from "ssg-router";
import type { Context } from "../context/context";

export const registerLanguagePlugin: Plugin<Context> = {
async onRouted(_, context) {
context.language = "ja";
},
};
6 changes: 5 additions & 1 deletion packages/modules/ssg-router/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export { Router } from "./router/router";
export {
Router,
type GenerateHandler,
type OutputHandler,
} from "./router/router";
export type { Plugin } from "./plugin/basic";
6 changes: 3 additions & 3 deletions packages/modules/ssg-router/src/plugin/basic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type Plugin<RoutingContext> = {
onRouted: (path: string, context: RoutingContext) => Promise<void>;
onGenerated: (path: string, content: string) => Promise<void>;
onOutput: (path: string) => Promise<void>;
onRouted?: (path: string, context: RoutingContext) => Promise<void>;
onGenerated?: (path: string, content: string) => Promise<void>;
onOutput?: (path: string) => Promise<void>;
};
2 changes: 2 additions & 0 deletions packages/modules/ssg-router/src/router/router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { Plugin } from "../plugin/basic";
import type { BasicRouter, GenerateHandler, OutputHandler } from "./basic";

export type { GenerateHandler, OutputHandler } from "./basic";

export class Router<RoutingContext> implements BasicRouter<RoutingContext> {
private routing: Map<
string,
Expand Down

0 comments on commit 64e1ac5

Please sign in to comment.