Skip to content

markdown 图片自定义渲染,token.attrGet('alt') 获取不到值 #4607

Closed
@Huangbinghui

Description

@Huangbinghui

Describe the bug

markdown 图片自定义渲染,token.attrGet('alt') 获取不到值。
md.renderer.rules.image = (tokens, idx, options, env, self) => { const token = tokens[idx]; let alt = token.attrGet('alt'); console.log("alt :"+ alt); let result = self.renderToken(tokens, idx, options); if (alt) { result +=\n } return result; }
这段代码alt的值获取不到。

Reproduction

config.js 的markdown配置中配置如下:
md.renderer.rules.image = (tokens, idx, options, env, self) => { const token = tokens[idx]; let alt = token.attrGet('alt'); console.log("alt :"+ alt); let result = self.renderToken(tokens, idx, options); if (alt) { result +=\n } return result; }

Expected behavior

希望可以取到alt属性值。

System Info

System:
    OS: macOS 15.3.1
    CPU: (8) arm64 Apple M1
    Memory: 77.58 MB / 8.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 23.6.0 - ~/.nvm/versions/node/v23.6.0/bin/node
    npm: 10.9.2 - ~/.nvm/versions/node/v23.6.0/bin/npm
    pnpm: 8.15.0 - ~/Library/pnpm/pnpm
  Browsers:
    Chrome: 133.0.6943.142
    Safari: 18.3
  npmPackages:
    vitepress: ^1.6.3 => 1.6.3

Additional context

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions