-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
137 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# test 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,42 @@ | ||
import { useEffect, useState } from 'react'; | ||
import Markdown from 'react-markdown'; | ||
|
||
import { UsecaseContext } from '@/app/contexts/UsecaseContext'; | ||
import { PageComponent } from '@/app/page'; | ||
import { useGlobalContext } from '@/app/utils/useGlobalContext'; | ||
import { Page } from '@/entities/page'; | ||
import { LectureUsecase } from '@/usecases/LectureUsecase'; | ||
|
||
export const Lecture: PageComponent<Page.LECTURE> = ({ params }) => { | ||
const { markdown } = useGlobalContext( | ||
UsecaseContext, | ||
).lectureUsecase.showLecture(params.lectureIndex); | ||
const [sections, setSections] = | ||
useState<Awaited<ReturnType<LectureUsecase['showLecture']>>['sections']>(); | ||
|
||
const { lectureUsecase } = useGlobalContext(UsecaseContext); | ||
|
||
useEffect(() => { | ||
let ignore = false; | ||
|
||
lectureUsecase | ||
.showLecture(params.lectureIndex) | ||
.then((result) => { | ||
if (ignore) return; | ||
setSections(result.sections); | ||
}) | ||
.catch(() => { | ||
console.error('Failed to load lecture'); | ||
}); | ||
|
||
return () => { | ||
ignore = true; | ||
}; | ||
}, [lectureUsecase, params.lectureIndex]); | ||
|
||
return ( | ||
<div> | ||
<h1>Lecture {params.lectureIndex}</h1> | ||
<Markdown>{markdown}</Markdown> | ||
{sections?.map((section, index) => ( | ||
<Markdown key={index}>{section.markdown}</Markdown> | ||
))} | ||
</div> | ||
); | ||
}; |
19 changes: 19 additions & 0 deletions
19
react/lecture/src/infrastructures/implViteVirtualModuleLectureContentRepository.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { contents } from 'virtual:lecture-contents'; | ||
|
||
import { implLectureUsecase } from '@/usecases/LectureUsecase'; | ||
|
||
export const implViteVirtualModuleLectureContentRepository = (): Parameters< | ||
typeof implLectureUsecase | ||
>[0]['lectureContentRepository'] => { | ||
return { | ||
loadLectureMarkdowns: ({ lectureIndex }) => { | ||
const found = contents.find( | ||
(c) => c.folder === `lecture${lectureIndex}`, | ||
)?.markdowns; | ||
|
||
if (found === undefined) throw new Error('Lecture not found'); | ||
|
||
return Promise.resolve(found); | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
/// <reference types="vite/client" /> | ||
|
||
declare module 'virtual:lecture-contents' { | ||
export const contents: { folder: string; markdowns: string[] }[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,51 @@ | ||
import react from '@vitejs/plugin-react-swc'; | ||
import { defineConfig } from 'vite'; | ||
import fs from 'fs/promises'; | ||
import { defineConfig, Plugin } from 'vite'; | ||
|
||
const lecturePlugin = (): Plugin => { | ||
const virtualModuleId = 'virtual:lecture-contents'; | ||
|
||
return { | ||
name: 'lecture-plugin', | ||
resolveId(id) { | ||
if (id === virtualModuleId) return virtualModuleId; | ||
}, | ||
async load(id) { | ||
if (id === virtualModuleId) { | ||
const folders = await fs.readdir('./contents').catch(() => []); | ||
|
||
const foldersWithFiles = await Promise.all( | ||
folders.map(async (folder) => { | ||
const files = ( | ||
await fs.readdir(`./contents/${folder}`).catch(() => []) | ||
) | ||
.filter((file) => file.endsWith('.md')) | ||
.sort(); | ||
|
||
const markdowns = await Promise.all( | ||
files.map(async (file) => { | ||
const markdown = await fs.readFile( | ||
`./contents/${folder}/${file}`, | ||
'utf-8', | ||
); | ||
return markdown; | ||
}), | ||
); | ||
|
||
return { folder, markdowns }; | ||
}), | ||
); | ||
|
||
return `export const contents = ${JSON.stringify(foldersWithFiles)};`; | ||
} | ||
}, | ||
}; | ||
}; | ||
|
||
// https://vitejs.dev/config/ | ||
export default defineConfig({ | ||
plugins: [react()], | ||
plugins: [react(), lecturePlugin()], | ||
server: { port: 3025 }, | ||
resolve: { alias: { '@': '/src' } }, | ||
assetsInclude: ['**/*.md'], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters