From 8c67e6d0f47853159c12ba6769ce9de0f77530d7 Mon Sep 17 00:00:00 2001 From: Mimi <1119186082@qq.com> Date: Wed, 1 Jul 2020 11:29:19 +0800 Subject: [PATCH] feat: lazyload (#156) --- README.md | 2 ++ index.js | 1 + lib/renderer.js | 1 + test/index.js | 18 ++++++++++++++++++ 4 files changed, 22 insertions(+) diff --git a/README.md b/README.md index ed921e3..7b824b7 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ marked: autolink: true sanitizeUrl: false headerIds: true + lazyload: false prependRoot: false external_link: enable: false @@ -47,6 +48,7 @@ marked: - **autolink** - Enable autolink for URLs. E.g. `https://hexo.io` will become `https://hexo.io`. - **sanitizeUrl** - Remove URLs that start with `javascript:`, `vbscript:` and `data:`. - **headerIds** - Insert header id, e.g. `

text

`. Useful for inserting anchor link to each paragraph with a heading. +- **lazyload** - Lazy loading images via `loading="lazy"` attribute. - **prependRoot** - Prepend root value to (internal) image path. * Example `_config.yml`: ``` yml diff --git a/index.js b/index.js index 698f6a7..4a82c9b 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,7 @@ hexo.config.marked = Object.assign({ autolink: true, sanitizeUrl: false, headerIds: true, + lazyload: false, // TODO: enable prependRoot by default in v3 prependRoot: false, external_link: { diff --git a/lib/renderer.js b/lib/renderer.js index ed8900f..0ca933d 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -93,6 +93,7 @@ class Renderer extends MarkedRenderer { let out = `${text} { ].join('\n')); }); + it('lazyload image', () => { + const body = [ + '![](/bar/baz.jpg)', + '![foo](/aaa/bbb.jpg)' + ].join('\n'); + + ctx.config.marked.lazyload = true; + + const r = require('../lib/renderer').bind(ctx); + + const result = r({ text: body }); + + result.should.eql([ + '

', + 'foo

\n' + ].join('\n')); + }); + describe('exec filter to extend', () => { it('should execute filter registered to marked:renderer', () => { const hexo = new Hexo(__dirname, {silent: true});