File tree 1 file changed +18
-1
lines changed
.vitepress/theme/composables
1 file changed +18
-1
lines changed Original file line number Diff line number Diff line change 1
1
import { createContentLoader } from 'vitepress'
2
2
import { EXCERPT_LENGTH , EXCERPT_MARK , POST_FILES } from '../../constants'
3
+ import { fileURLToPath } from 'url'
4
+ import { createHash } from 'crypto'
5
+ import { readFileSync } from 'fs'
6
+ import { parse } from 'path'
3
7
4
8
export interface Post {
5
9
title : string
@@ -51,14 +55,27 @@ export default createContentLoader(POST_FILES, {
51
55
*/
52
56
function updateImageSrc ( excerpt : string | undefined , urlPrefix : string ) {
53
57
if ( excerpt == undefined ) return
58
+
54
59
return excerpt . replace (
55
60
/ < i m g \s + ( [ ^ > ] * ?) s r c = [ " ' ] \. \/ ( [ ^ " ' ] * ?) [ " ' ] ( [ ^ > ] * ?) > / gi,
56
61
( match , p1 , p2 , p3 ) => {
57
- return `<img ${ p1 } src="${ urlPrefix } ${ p2 } "${ p3 } >`
62
+ const imageUrl = fileToUrl ( urlPrefix + p2 )
63
+ return `<img ${ p1 } src="${ imageUrl } "${ p3 } >`
58
64
}
59
65
)
60
66
}
61
67
68
+ function fileToUrl ( file : string ) {
69
+ if ( process . env . NODE_ENV !== 'production' ) return file
70
+ const path = fileURLToPath ( new URL ( '../../../' + file , import . meta. url ) )
71
+ const parsed = parse ( path )
72
+ const hash = createHash ( 'sha256' )
73
+ . update ( readFileSync ( path ) )
74
+ . digest ( 'hex' )
75
+ . slice ( 0 , 8 )
76
+ return `/assets/${ parsed . name } .${ hash } ${ parsed . ext } `
77
+ }
78
+
62
79
function generateTitle (
63
80
frontmatter : Record < string , any > ,
64
81
src : string | undefined
You can’t perform that action at this time.
0 commit comments