scope to image

pull/3346/head
Divyansh Singh 2 years ago
parent 0fe161fa65
commit 2ac85b2815

@ -87,7 +87,9 @@ export default defineConfig({
title: 'Example',
description: 'An example app using VitePress.',
markdown: {
lazyLoading: true
image: {
lazyLoading: true
}
},
themeConfig: {
sidebar,

@ -849,13 +849,15 @@ $$ x = {-b \pm \sqrt{b^2-4ac} \over 2a} $$
## Image Lazy Loading
You can enable lazy loading for each image in the content via config:
You can enable lazy loading for each image added via markdown by setting `lazyLoading` to `true` in your config file:
```js
// image lazy loading is disabled by default
export default {
markdown: {
lazyLoading: true
image: {
// image lazy loading is disabled by default
lazyLoading: true
}
}
}
```

@ -23,7 +23,7 @@ import type { Logger } from 'vite'
import { containerPlugin, type ContainerOptions } from './plugins/containers'
import { highlight } from './plugins/highlight'
import { highlightLinePlugin } from './plugins/highlightLines'
import { imagePlugin } from './plugins/image'
import { imagePlugin, type Options as ImageOptions } from './plugins/image'
import { lineNumberPlugin } from './plugins/lineNumbers'
import { linkPlugin } from './plugins/link'
import { preWrapperPlugin } from './plugins/preWrapper'
@ -166,11 +166,7 @@ export interface MarkdownOptions extends MarkdownIt.Options {
* @see https://vitepress.dev/guide/markdown#math-equations
*/
math?: boolean | any
/**
* Support native lazy loading for the <img> tag.
* @default false
*/
lazyLoading?: boolean
image?: ImageOptions
}
export type MarkdownRenderer = MarkdownIt
@ -203,7 +199,7 @@ export const createMarkdownRenderer = async (
.use(preWrapperPlugin, { hasSingleTheme })
.use(snippetPlugin, srcDir)
.use(containerPlugin, { hasSingleTheme }, options.container)
.use(imagePlugin, options.lazyLoading)
.use(imagePlugin, options.image)
.use(
linkPlugin,
{ target: '_blank', rel: 'noreferrer', ...options.externalLinks },

@ -3,7 +3,15 @@
import type MarkdownIt from 'markdown-it'
import { EXTERNAL_URL_RE } from '../../shared'
export const imagePlugin = (md: MarkdownIt, lazyLoading: boolean) => {
export interface Options {
/**
* Support native lazy loading for the `<img>` tag.
* @default false
*/
lazyLoading?: boolean
}
export const imagePlugin = (md: MarkdownIt, { lazyLoading }: Options = {}) => {
const imageRule = md.renderer.rules.image!
md.renderer.rules.image = (tokens, idx, options, env, self) => {
const token = tokens[idx]

Loading…
Cancel
Save