1
- import React from 'react' ;
1
+ import React , { useState , useEffect } from 'react' ;
2
2
import { useParams } from 'react-router-dom' ;
3
- import courses from './courseData' ; // Ensure the path is correct
3
+ import courseData from './courseData' ; // Ensure correct path
4
4
import YouTubeEmbed from './YouTubeEmbed' ;
5
- import { marked } from 'marked' ; // Import the marked library
6
5
7
6
function CourseDetail ( ) {
8
7
const { courseName } = useParams ( ) ;
9
8
const courseKey = courseName . toLowerCase ( ) ; // Ensure proper formatting
10
- const course = courses [ courseKey ] ; // Lookup using the sanitized course key
9
+ const [ course , setCourse ] = useState ( null ) ; // Store course data
10
+ const [ loading , setLoading ] = useState ( true ) ; // Loading state
11
+
12
+ useEffect ( ( ) => {
13
+ const loadCourseData = async ( ) => {
14
+ try {
15
+ const courses = await courseData ( ) ; // Fetch courses from Firestore
16
+ setCourse ( courses [ courseKey ] ) ; // Set the specific course
17
+ } catch ( error ) {
18
+ console . error ( "Error fetching course:" , error ) ;
19
+ }
20
+ setLoading ( false ) ; // Stop loading once data is fetched
21
+ } ;
22
+
23
+ loadCourseData ( ) ;
24
+ } , [ courseKey ] ) ;
25
+
26
+ if ( loading ) {
27
+ return < h2 className = "text-center" > Loading...</ h2 > ;
28
+ }
11
29
12
30
if ( ! course ) {
31
+ console . log ( course ) ;
13
32
return < h2 className = "text-center" > Course not found</ h2 > ;
14
33
}
15
34
16
- // Function to convert markdown to HTML using 'marked'
17
- const renderMarkdown = ( markdownText ) => {
18
- return { __html : marked ( markdownText ) } ;
35
+ // Function to convert newlines to <br />
36
+ const formatContent = ( content ) => {
37
+ return content ? content . split ( '\n' ) . map ( ( line , index ) => (
38
+ < span key = { index } >
39
+ { line }
40
+ < br />
41
+ </ span >
42
+ ) ) : '' ;
19
43
} ;
20
44
21
45
return (
@@ -25,10 +49,7 @@ function CourseDetail() {
25
49
< p > { course . description } </ p >
26
50
< h4 > Course Content:</ h4 >
27
51
< YouTubeEmbed videoId = { course . ytb_vid } />
28
-
29
- { /* Use dangerouslySetInnerHTML to inject the HTML from the markdown */ }
30
- < div dangerouslySetInnerHTML = { renderMarkdown ( course . content ) } />
31
-
52
+ < p > { formatContent ( course . content ) } </ p >
32
53
< h6 > < span className = "badge bg-primary" > < i className = "fa-solid fa-pen-nib" > </ i > { course . author } </ span > </ h6 >
33
54
</ div >
34
55
) ;
0 commit comments