File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 232232 // 兼容 MkDocs Material 的 SPA 导航(instant loading)
233233 document . addEventListener ( "DOMContentSwitch" , initCodeCollapse ) ;
234234 } ) ( ) ;
235+ // 自动对图片 src 中的中文字符进行 URL 编码,修复 GitHub Pages 中文路径问题
236+ ( function fixChineseImagePaths ( ) {
237+ function encodeChinesePaths ( ) {
238+ document . querySelectorAll ( 'img' ) . forEach ( function ( img ) {
239+ var src = img . getAttribute ( 'src' ) ;
240+ if ( ! src ) return ;
241+ // 只处理含中文的路径
242+ if ( / [ \u4e00 - \u9fff \u3000 - \u303f \uff00 - \uffef ] / . test ( src ) ) {
243+ var encoded = src . split ( '/' ) . map ( function ( segment ) {
244+ return segment . split ( '.' ) . map ( function ( part ) {
245+ return encodeURIComponent ( part ) ;
246+ } ) . join ( '.' ) ;
247+ } ) . join ( '/' ) ;
248+ img . setAttribute ( 'src' , encoded ) ;
249+ }
250+ } ) ;
251+ }
252+ if ( document . readyState === 'loading' ) {
253+ document . addEventListener ( 'DOMContentLoaded' , encodeChinesePaths ) ;
254+ } else {
255+ encodeChinesePaths ( ) ;
256+ }
257+ document . addEventListener ( 'DOMContentSwitch' , encodeChinesePaths ) ;
258+ } ) ( ) ;
235259 </ script >
236260{% endblock %}
You can’t perform that action at this time.
0 commit comments