@@ -26,33 +26,38 @@ <h1>Articles</h1>
26
26
'articles/article1.js' ,
27
27
'articles/article2.js' ,
28
28
'articles/article3.js' ,
29
- // Add more files here
30
29
] ;
31
30
32
31
const mainPage = document . getElementById ( "main-page" ) ;
33
32
const articlePage = document . getElementById ( "article-page" ) ;
34
33
const articleContainer = document . getElementById ( "article-container" ) ;
35
34
const articleGrid = document . getElementById ( "article-grid" ) ;
35
+ const backBtn = document . querySelector ( ".back-btn" ) ;
36
36
37
37
function goBack ( ) {
38
+ location . hash = "" ;
38
39
articlePage . style . display = "none" ;
39
40
mainPage . style . display = "block" ;
40
41
articleContainer . innerHTML = "" ;
41
42
}
42
43
43
- document . querySelector ( ".back-btn" ) . onclick = goBack ;
44
+ backBtn . onclick = goBack ;
44
45
45
46
async function loadArticle ( file ) {
46
47
mainPage . style . display = "none" ;
47
48
articlePage . style . display = "block" ;
48
49
49
- const module = await import ( `./${ file } ` ) ;
50
- const { layout, content, runDemo } = module ;
50
+ try {
51
+ const module = await import ( `./${ file } ?v=${ Date . now ( ) } ` ) ;
52
+ const { layout, content, runDemo } = module ;
51
53
52
- if ( layout === "simple" ) {
53
- renderSimple ( content ) ;
54
- } else if ( layout === "split" ) {
55
- renderSplit ( content , runDemo ) ;
54
+ if ( layout === "simple" ) {
55
+ renderSimple ( content ) ;
56
+ } else if ( layout === "split" ) {
57
+ renderSplit ( content , runDemo ) ;
58
+ }
59
+ } catch ( err ) {
60
+ articleContainer . innerHTML = `<p>Error loading article.</p>` ;
56
61
}
57
62
}
58
63
@@ -96,18 +101,37 @@ <h1>Articles</h1>
96
101
} ;
97
102
}
98
103
99
- for ( const file of articleFiles ) {
100
- const meta = await loadMetadata ( file ) ;
101
- const card = document . createElement ( "div" ) ;
102
- card . className = "card" ;
103
- card . dataset . file = meta . file ;
104
- card . innerHTML = `
105
- <img src="${ meta . image } " alt="${ meta . title } " />
106
- <h2>${ meta . title } </h2>
107
- ` ;
108
- card . onclick = ( ) => loadArticle ( meta . file ) ;
109
- articleGrid . appendChild ( card ) ;
104
+ async function buildGrid ( ) {
105
+ for ( const file of articleFiles ) {
106
+ const meta = await loadMetadata ( file ) ;
107
+ const card = document . createElement ( "div" ) ;
108
+ card . className = "card" ;
109
+ card . dataset . file = meta . file ;
110
+ card . innerHTML = `
111
+ <img src="${ meta . image } " alt="${ meta . title } " />
112
+ <h2>${ meta . title } </h2>
113
+ ` ;
114
+ card . onclick = ( ) => {
115
+ location . hash = meta . file ;
116
+ } ;
117
+ articleGrid . appendChild ( card ) ;
118
+ }
119
+ }
120
+
121
+ async function checkHash ( ) {
122
+ const file = decodeURIComponent ( location . hash . slice ( 1 ) ) ;
123
+ if ( articleFiles . includes ( file ) ) {
124
+ await loadArticle ( file ) ;
125
+ } else {
126
+ goBack ( ) ;
127
+ }
110
128
}
129
+
130
+ window . addEventListener ( "hashchange" , checkHash ) ;
131
+ window . addEventListener ( "DOMContentLoaded" , async ( ) => {
132
+ await buildGrid ( ) ;
133
+ checkHash ( ) ;
134
+ } ) ;
111
135
</ script >
112
136
</ body >
113
137
</ html >
0 commit comments