Skip to content

Commit 69c0fc6

Browse files
committed
Add plugin for images height and width
1 parent 8093a9f commit 69c0fc6

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/.vitepress/config.mts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,41 @@ export default defineConfig({
1313
cleanUrls: true,
1414
outDir: "../dist/docs",
1515
markdown: {
16-
config: (md) => {
17-
markdownItImageSize(md, { publicDir: path.resolve(import.meta.dirname, '../public'), });
16+
config(md) {
17+
// Fix for image width and height handling for proper scrolling
18+
const maxHeight = 500 /* px */
19+
20+
const img = md.renderer.rules.image!
21+
md.renderer.rules.image = function (tokens, idx, options, env, self) {
22+
const token = tokens[idx]
23+
24+
const widthAttr = token.attrGet('width')
25+
const heightAttr = token.attrGet('height')
26+
const existingStyle = token.attrGet('style') || ''
27+
28+
const style: string[] = []
29+
30+
let w = widthAttr ? Number.parseInt(widthAttr, 10) : null
31+
let h = heightAttr ? Number.parseInt(heightAttr, 10) : null
32+
33+
if (w) {
34+
if (h && h > maxHeight) {
35+
const scale = maxHeight / h
36+
w = Math.round(w * scale)
37+
h = maxHeight
38+
}
39+
style.push(`width: ${w}px;`)
40+
}
41+
42+
if (style.length > 0) {
43+
const sep = existingStyle && !existingStyle.trim().endsWith(';') ? ';' : ''
44+
token.attrSet('style', existingStyle + sep + style.join(' '))
45+
}
46+
47+
return img(tokens, idx, options, env, self)
48+
}
49+
50+
markdownItImageSize(md, { publicDir: path.resolve(import.meta.dirname, '../public') })
1851
},
1952
},
2053
themeConfig: {

src/.vitepress/theme/style.css

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,7 @@ a img,svg {
146146

147147
img {
148148
max-width: 90%;
149-
max-height: 500px;
150149
cursor: pointer;
151-
display: block;
152-
width: auto;
153150
}
154151

155152
/** Custom mm theme styles */

0 commit comments

Comments
 (0)