Skip to content

feat: Support browser-level image lazy-loading #1432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ Only when you both set the `routerMode: 'history'` and `externalLinkTarget: '_se
![logo](https://docsify.js.org/_media/icon.svg ':id=someCssId')
```

### Lazy Loading

```md
![logo](https://docsify.js.org/_media/icon.svg ':loading=lazy')
```

## Customise ID for headings

```md
Expand Down
4 changes: 4 additions & 0 deletions src/core/render/compiler/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export const imageCompiler = ({ renderer, contentBase, router }) =>
attrs.push(`id="${config.id}"`);
}

if (config.loading) {
attrs.push(`loading="${config.loading}"`);
}

if (!isAbsolutePath(href)) {
url = getPath(contentBase, getParentPath(router.getCurrentPath()), href);
}
Expand Down
10 changes: 10 additions & 0 deletions test/integration/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ describe('render', function() {
);
});

test('loading', async function() {
const output = window.marked(
"![alt text](http://imageUrl ':loading=lazy')"
);

expect(output).toMatchInlineSnapshot(
`"<p><img src=\\"http://imageUrl\\" data-origin=\\"http://imageUrl\\" alt=\\"alt text\\" loading=\\"lazy\\" /></p>"`
);
});

test('id', async function() {
const output = window.marked(
"![alt text](http://imageUrl ':id=someCssID')"
Expand Down