在降价中添加后备图像

如何解决在降价中添加后备图像

我一直在用 .webp 替换我现有的网站图像。在 HTML 中,添加回退支持相对容易,例如不支持 .webp。但是,我正在努力寻找 Markdown 的等价物?

HTML

<img src="img.webp" />

支持后备的 HTML

<picture>
  <source srcset="img.webp" type="image/webp">
  <source srcset="img.jpg" type="image/jpeg"> 
  <source srcset="img.png" type="image/png"> 
</picture> 

例如

降价

![](img.webp)

提供后备支持的 Markdown

![](img.webp,img.png)

解决方法

我认为 Markdown 中没有像这样的后备图像规范,但是如果您使用 markdown-it(这是 Eleventy 中的默认 Markdown 解析器),您可以添加自定义插件来扩展语法。您也可以只将 HTML 放在 Markdown 中,或者如果您使用 Eleventy,甚至可以在 Markdown 中使用短代码。

使用短代码

您可以在 Eleventy 中定义 custom shortcodes,然后定义 use them in your markdown。这可能比设置 markdown-it 插件更简单、更灵活,但缺点是将非 Markdown 内容放入 Markdown。

使用 markdown-it 插件

如果您已经有要使用的预生成图像,您可能需要使用类似 markdown-it-picture 的内容并修改它以设置 type 属性,方法是解析文件扩展名或更改接受 type 的媒体查询/标题点。您需要将代码复制到项目中的新文件中,进行修改,然后像这样注册:

// .eleventy.js
module.exports = function(eleventyConfig) {
  const mdPicture = require('/path/to/plugin.js')
  const md = require('markdown-it')()
  md.use(mdPicture)
  eleventyConfig.setLibrary('md',md)
  // ...
};

eleventy-image

或者,如果您还没有生成图像并希望在 11ty 构建步骤中生成它们,您可以使用 eleventy-image (Docs)。此 Twitter thread 讨论了创建自定义 markdown-it 渲染器,我已将其修改为与下面的 eleventy-image 一起使用。

// you might want to put this in another file
// such as ./utils/markdown.js

// responsive images with 11ty image
// this overrides the default image renderer
// titles are also used for size setting (optional)
//
// ![alt text](/path/to/img.png '(max-width: 768px) 100vw,768px')

const md = require('markdown-it')();
const Image = require('@11ty/eleventy-img')

md.renderer.rules.image = function (tokens,idx,options,env,self) {
  const token = tokens[idx]
  let imgSrc = token.attrGet('src')
  const imgAlt = token.content

  // you can modify the default sizes,or omit
  const imgSize = token.attrGet('title') || '(max-width: 768px) 100vw,768px'

  const widths = [250,426,580,768] // choose your own widths,or [null] to disable resize
  const imgOpts = {
    widths: widths
      .concat(widths.map((w) => w * 2)) // generate 2x sizes for retina displays
      .filter((v,i,s) => s.indexOf(v) === i),// dedupe widths
    formats: ['webp','jpeg'],// choose your own formats (see docs)
    urlPath: '/assets/img/',// src path in HTML output
    outputDir: './_site/assets/img/' // where the generated images will go
  }

  // generate the images
  // see https://www.11ty.dev/docs/plugins/image/#synchronous-usage
  Image(imgSrc,imgOpts)

  const metadata = Image.statsSync(imgSrc,imgOpts)

  return Image.generateHTML(metadata,{
    alt: imgAlt,sizes: imgSize,loading: 'lazy',decoding: 'async'
  })
}

// in your .eleventy.js
module.exports = function(eleventyConfig) {
  // you may need to `require` the file with
  // your markdown config with the custom renderer
  // const md = require('./utils/markdown.js')
  eleventyConfig.setLibrary('md',md)
  // ...
};

这个渲染器会自动生成不同尺寸的图片,以及不同的格式,然后在你的 HTML 中输出 <picture> 标签。由于 markdown-it 不能很好地与 async 函数配合使用,因此我们使用 eleventy-imagesyncronous pattern。有关配置的更多信息可以在 docs 中找到。

您可能需要注意的一件事是,markdown 中的图像路径应该是图像在网站源中的位置,不是最终发布的 URL。例如,我的图像可能位于 src/images/img.png,但发布在 https://example.com/images/img.png。降价应为 ![alt text](src/images/img.png)NOT ![alt text](/images/img.png)。您还可以在渲染器函数中进行额外的解析。

,

不确定这是否可行,但您可以尝试一下:

[![my image](img.webp)](image_url)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-