11// --- GitHub Config ---
22const GITHUB_USER = 'codebysushil' ;
3- const GITHUB_REPO = " codebysushil.github.io" ;
3+ const GITHUB_REPO = ' codebysushil.github.io' ;
44const ARTICLES_PATH = 'articles' ;
55const BRANCH = 'main' ;
66
@@ -21,13 +21,6 @@ function parseFrontMatter(md){
2121}
2222function formatDate ( iso ) { try { const d = new Date ( iso ) ; return isNaN ( d ) ?iso :d . toLocaleDateString ( undefined , { year :'numeric' , month :'short' , day :'numeric' } ) ; } catch ( e ) { return iso } }
2323function estimateReadTime ( text ) { const words = text . trim ( ) . split ( / \s + / ) . length ; return Math . max ( 1 , Math . round ( words / 200 ) ) + ' min read' }
24- async function incAndGetViews ( key ) {
25- try {
26- const res = await fetch ( `https://api.countapi.xyz/hit/${ GITHUB_USER } -${ GITHUB_REPO } /${ key } ` ) ;
27- const json = await res . json ( ) ;
28- return json . value || 0 ;
29- } catch ( e ) { return '—' }
30- }
3124
3225// --- DOM Elements ---
3326const grid = document . getElementById ( 'articles-grid' ) ;
@@ -85,7 +78,6 @@ let currentIndex=0; // current article in modal
8578 <div class="meta">By ${ article . author } | ${ article . date } </div>
8679 <div class="tags">${ article . tags } </div>
8780 ` ;
88-
8981 card . addEventListener ( 'click' , ( ) => openModal ( articlesData . indexOf ( article ) ) ) ;
9082 grid . appendChild ( card ) ;
9183
@@ -96,18 +88,49 @@ let currentIndex=0; // current article in modal
9688
9789// --- Modal functions ---
9890async function openModal ( index ) {
99- currentIndex = index ;
100- const article = articlesData [ index ] ;
101- modalTitle . textContent = article . title ;
102- modalAuthor . textContent = 'By ' + article . author ;
103- modalDate . textContent = article . date ;
104- modalReadtime . textContent = estimateReadTime ( article . content ) ;
105- modalTags . textContent = article . tags ;
106- if ( article . cover ) { modalCover . src = article . cover ; modalCover . style . display = 'block' ; } else modalCover . style . display = 'none' ;
107- articleContent . innerHTML = marked . parse ( article . content ) ;
91+ currentIndex = index ;
92+ const article = articlesData [ index ] ;
93+
94+ // --- Update modal content ---
95+ modalTitle . textContent = article . title ;
96+ modalAuthor . textContent = 'By ' + article . author ;
97+ modalDate . textContent = article . date ;
98+ modalReadtime . textContent = estimateReadTime ( article . content ) ;
99+ modalTags . textContent = article . tags ;
100+
101+ if ( article . cover ) {
102+ modalCover . src = article . cover ;
103+ modalCover . style . display = 'block' ;
104+ } else {
105+ modalCover . style . display = 'none' ;
106+ }
107+
108+ articleContent . innerHTML = marked . parse ( article . content ) ;
108109 Prism . highlightAll ( ) ;
109- modalViews . textContent = 'Views: ' + await incAndGetViews ( article . fileName . replace ( / \. [ ^ / . ] + $ / , "" ) ) ;
110- modal . style . display = 'flex' ;
110+
111+ // --- GA4 Event Tracking ---
112+ if ( typeof gtag === 'function' ) {
113+ gtag ( 'event' , 'view_article' , {
114+ article_name : article . title ,
115+ article_file : article . fileName
116+ } ) ;
117+ }
118+
119+ // --- Local view counter ---
120+ const storageKey = 'views_' + article . fileName ;
121+ let views = parseInt ( localStorage . getItem ( storageKey ) ) || 0 ;
122+ views ++ ;
123+ localStorage . setItem ( storageKey , views ) ;
124+ modalViews . textContent = 'Views (you): ' + views ;
125+
126+ // --- Update page title & meta description ---
127+ document . title = article . title + " | Code By Sushil" ;
128+ const metaDesc = document . querySelector ( 'meta[name="description"]' ) ;
129+ if ( metaDesc ) {
130+ metaDesc . setAttribute ( 'content' , article . description || article . title ) ;
131+ }
132+
133+ modal . style . display = 'flex' ;
111134}
112135
113136// Next / Previous
0 commit comments