@@ -5,55 +5,84 @@ export function parseMarkdown(markdownContent) {
5
5
processYAML ( markdownContent ) ;
6
6
const indexFirstH1title = markdownContent . indexOf ( "# " ) ;
7
7
const mainContent = markdownContent . substring ( indexFirstH1title ) ;
8
- const lines = mainContent . split ( "\n" ) ;
9
8
let calendarData = [ ] ;
10
9
let calendarTitle = "Mon calendrier de l'Avent" ;
11
- let initialMessageComputed = false ;
12
10
let initialMessageContent = [ ] ;
13
11
let calendarMarkdown = [ ] ;
14
12
15
- const totalLines = lines . length ;
16
- let initialImageComputed = false ;
13
+ // Extraction du Titre
14
+ const titleStartIndex = mainContent . indexOf ( "# " ) ;
15
+ if ( titleStartIndex !== - 1 ) {
16
+ const titleEndIndex = mainContent . indexOf ( "\n" , titleStartIndex ) ;
17
+ calendarTitle = mainContent
18
+ . substring ( titleStartIndex + 2 , titleEndIndex )
19
+ . trim ( ) ;
20
+ }
21
+
22
+ // Extraction du Message Initial
23
+ const messageStartIndex = mainContent . indexOf ( "> " ) ;
24
+ if ( messageStartIndex !== - 1 ) {
25
+ const daysStartIndex = mainContent . indexOf ( "\n## " , messageStartIndex ) ;
26
+ const messageBlock = mainContent . substring (
27
+ messageStartIndex ,
28
+ daysStartIndex === - 1 ? undefined : daysStartIndex ,
29
+ ) ;
30
+ initialMessageContent = messageBlock
31
+ . split ( "\n" )
32
+ . filter ( ( line ) => line . startsWith ( ">" ) )
33
+ . map ( ( line ) => line . replace ( / ^ > \s ? / , "" ) . trim ( ) ) ;
34
+ }
35
+
36
+ // Extraction des Éléments du Calendrier
37
+ const days = mainContent . split ( "\n## " ) . slice ( 1 ) ;
17
38
let day = 1 ;
18
- for ( let i = 0 ; i < totalLines ; i ++ ) {
19
- let line = lines [ i ] ;
20
- const isLastLine = i === totalLines - 1 ;
21
- // On parcourt le contenu du fichier ligne par ligne
22
- if ( line . startsWith ( "# " ) ) {
23
- // Récupération du titre
24
- calendarTitle = line . replace ( "# " , "" ) . trim ( ) ;
25
- } else if ( line . startsWith ( ">" ) && ! initialMessageComputed ) {
26
- // Récupération du message initial, défini par un bloc citation
27
- line = line . replace ( / ^ > \s ? / , "" ) . trim ( ) ;
28
- initialMessageContent . push ( line ) ;
29
- } else {
30
- if ( line . startsWith ( "## " ) ) {
31
- let titleH2 = line . replace ( "## " , "" ) . trim ( ) ;
32
- titleH2 = titleH2 == day ? "" : titleH2 ;
33
- const hasTitle = titleH2 ? "hasTitle" : "" ;
34
- line = initialMessageComputed
35
- ? `</section></section></section>\n\n<section markdown class="day ${ hasTitle } " id="day-${ day } "><h2>${ day } <span>${ titleH2 } </span></h2>`
36
- : `\n<section markdown class="day ${ hasTitle } " id="day-${ day } "><h2>${ day } <span>${ titleH2 } </span></h2>` ;
37
- initialMessageComputed = true ;
38
- initialImageComputed = false ;
39
- day ++ ;
40
- }
41
- if ( line . startsWith ( "![" ) && ! initialImageComputed ) {
42
- line =
43
- line +
44
- '\n\n<section markdown class="dayContent"><button class="closeButton">X</button><section markdown class="content">' ;
45
- initialImageComputed = true ;
46
- }
47
- // Récupération du contenu du calendrier
48
- line = isLastLine ? line + "</section></section></section>" : line ;
49
- calendarMarkdown . push ( line ) ;
39
+
40
+ for ( let dayBlock of days ) {
41
+ // Extraction des informations du jour
42
+ const titleEndIndex = dayBlock . indexOf ( "\n" ) ;
43
+ let titleH2 = dayBlock . substring ( 0 , titleEndIndex ) . trim ( ) ;
44
+ titleH2 = titleH2 == day ? "" : titleH2 ;
45
+
46
+ // Génération de la structure HTML pour le jour
47
+ const hasTitle = titleH2 ? "hasTitle" : "" ;
48
+ const dayHtmlStart = `<section markdown class="day ${ hasTitle } " id="day-${ day } "><h2>${ day } <span>${ markdownToHTML ( titleH2 ) } </span></h2>` ;
49
+
50
+ // Extraction du contenu
51
+ let dayContent = dayBlock . substring ( titleEndIndex + 1 ) . trim ( ) ;
52
+ const firstImageIndex = dayContent . indexOf ( "![" ) ;
53
+ if ( firstImageIndex !== - 1 ) {
54
+ // Ligne contenant l'image et le reste du contenu après
55
+ const imageAndAfter = dayContent . substring ( firstImageIndex ) ;
56
+ const nextLineBreak = imageAndAfter . indexOf ( "\n" ) ;
57
+ const imageLine =
58
+ nextLineBreak !== - 1
59
+ ? imageAndAfter . substring ( 0 , nextLineBreak ) . trim ( )
60
+ : imageAndAfter . trim ( ) ;
61
+ const afterImage =
62
+ nextLineBreak !== - 1
63
+ ? imageAndAfter . substring ( nextLineBreak + 1 ) . trim ( )
64
+ : "" ;
65
+ // Ajout de la structure HTML autour de la première image
66
+ dayContent =
67
+ `${ markdownToHTML ( imageLine ) } <section markdown class="dayContent"><p><button class="closeButton">X</button></p><section markdown class="content">` +
68
+ markdownToHTML ( afterImage ) ;
50
69
}
70
+ const dayHtmlEnd = "</section></section></section>" ;
71
+ const fullDayHtml = `${ dayHtmlStart } ${ dayContent } ${ dayHtmlEnd } ` ;
72
+ calendarMarkdown . push ( fullDayHtml ) ;
73
+ day ++ ;
74
+ }
75
+
76
+ // Ajout de la dernière fermeture, si nécessaire
77
+ if ( calendarMarkdown . length > 0 ) {
78
+ calendarMarkdown [ calendarMarkdown . length - 1 ] +=
79
+ "</section></section></section>" ;
51
80
}
52
81
53
82
calendarData = [
54
83
calendarTitle ,
55
84
markdownToHTML ( initialMessageContent . join ( "\n" ) ) ,
56
- markdownToHTML ( calendarMarkdown . join ( "\n" ) ) ,
85
+ calendarMarkdown . join ( "\n" ) ,
57
86
] ;
58
87
return calendarData ;
59
88
}
0 commit comments