@@ -10,6 +10,9 @@ const state = {
1010
1111const treeRoot = document . getElementById ( "treeRoot" ) ;
1212const codeBlock = document . getElementById ( "codeBlock" ) ;
13+ const codeContainer = document . getElementById ( "codeContainer" ) ;
14+ const markdownContent = document . getElementById ( "markdownContent" ) ;
15+ const imageContent = document . getElementById ( "imageContent" ) ;
1316const contentTitle = document . getElementById ( "contentTitle" ) ;
1417const contentFooter = document . getElementById ( "contentFooter" ) ;
1518const copyLink = document . getElementById ( "copyLink" ) ;
@@ -79,6 +82,30 @@ function languageForPath(path) {
7982 return map [ ext ] || "" ;
8083}
8184
85+ function isImageFile ( path ) {
86+ const ext = fileExtension ( path ) ;
87+ return [ "png" , "jpg" , "jpeg" , "gif" , "svg" , "webp" ] . includes ( ext ) ;
88+ }
89+
90+ function renderCode ( text , languageClass ) {
91+ const highlighted = window . hljs
92+ ? ( languageClass
93+ ? window . hljs . highlight ( text , { language : languageClass } ) . value
94+ : window . hljs . highlightAuto ( text ) . value )
95+ : text . replace ( / [ & < > ] / g, ( char ) => ( { "&" : "&" , "<" : "<" , ">" : ">" } [ char ] ) ) ;
96+
97+ const lines = highlighted . split ( "\n" ) ;
98+ const wrapped = lines
99+ . map ( ( line , index ) => {
100+ const lineNumber = index + 1 ;
101+ const content = line . length ? line : " " ;
102+ return `<span class="code-line"><span class="line-number">${ lineNumber } </span><span class="line-content">${ content } </span></span>` ;
103+ } )
104+ . join ( "\n" ) ;
105+
106+ codeBlock . innerHTML = `<span class="code-lines">${ wrapped } </span>` ;
107+ }
108+
82109function setActiveItem ( element ) {
83110 if ( state . activeItem ) {
84111 state . activeItem . classList . remove ( "active" ) ;
@@ -117,12 +144,44 @@ async function loadFile(path, meta = {}) {
117144 if ( ! response . ok ) {
118145 throw new Error ( `Failed to fetch ${ path } ` ) ;
119146 }
147+ if ( isImageFile ( path ) ) {
148+ const blob = await response . blob ( ) ;
149+ const imageUrl = URL . createObjectURL ( blob ) ;
150+ imageContent . innerHTML = `<img src="${ imageUrl } " alt="${ path } " />` ;
151+ imageContent . classList . remove ( "hidden" ) ;
152+ markdownContent . classList . add ( "hidden" ) ;
153+ codeContainer . classList . add ( "hidden" ) ;
154+ return ;
155+ }
156+
120157 const text = await response . text ( ) ;
121- codeBlock . textContent = text ;
122- if ( window . hljs ) {
123- window . hljs . highlightElement ( codeBlock ) ;
158+ if ( languageClass === "markdown" && window . marked ) {
159+ const html = window . marked . parse ( text ) ;
160+ markdownContent . innerHTML = html ;
161+ markdownContent . classList . remove ( "hidden" ) ;
162+ imageContent . classList . add ( "hidden" ) ;
163+ codeContainer . classList . add ( "hidden" ) ;
164+ } else if ( languageClass === "json" ) {
165+ let formatted = text ;
166+ try {
167+ formatted = JSON . stringify ( JSON . parse ( text ) , null , 2 ) ;
168+ } catch ( error ) {
169+ formatted = text ;
170+ }
171+ markdownContent . classList . add ( "hidden" ) ;
172+ imageContent . classList . add ( "hidden" ) ;
173+ codeContainer . classList . remove ( "hidden" ) ;
174+ renderCode ( formatted , languageClass ) ;
175+ } else {
176+ markdownContent . classList . add ( "hidden" ) ;
177+ imageContent . classList . add ( "hidden" ) ;
178+ codeContainer . classList . remove ( "hidden" ) ;
179+ renderCode ( text , languageClass ) ;
124180 }
125181 } catch ( error ) {
182+ markdownContent . classList . add ( "hidden" ) ;
183+ imageContent . classList . add ( "hidden" ) ;
184+ codeContainer . classList . remove ( "hidden" ) ;
126185 codeBlock . textContent = `Unable to load ${ path } . ${ error . message } ` ;
127186 }
128187}
0 commit comments