Git Product home page Git Product logo

Comments (2)

yugasun avatar yugasun commented on August 21, 2024

@xuexb 还是别编译动态获取了,直接要求用户在使用此模板是,如果需要使用img标签,必须定义宽高属性就行,hexojs 编译时只需读取 <img> 宽高属性值。

from hexo-theme-mip.

xuexb avatar xuexb commented on August 21, 2024

思路是在页面渲染后 after_render:html ,使用勾子把内容里的 <img> 替换为 <mip-img> ,代码是:

hexo.extend.filter.register('after_render:html',function (html) {
    return html.replace(/<img([\S\s]+?)\/?>/ig,function (match,tag) {
        return '<mip-img ${tag}></mip-img>'.replace('${tag}',tag.trim());
    });
});
这里只是"暴力"的替换了,并没有处理规范里的宽高,所以在主题里使用图片时,还是需要明确的定义图片的宽高。

当然也可以做成自动抓取

  1. 如果是本地 - 直接获取图片信息,并设置宽高
  2. 如果是远程 - 根据链接生成 md5 小缀缀,远程下载图片到本地,获取宽高后使用小缀缀设置缓存,最后设置图片宽高

from hexo-theme-mip.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.