GitHub Flavored Markdown中summary跨区块锚点链接无法识别的问题求助
跨
区块的Markdown锚点链接问题解决方法
问题描述
在GitHub Flavored Markdown中,尝试从Stage 3的
区块内链接到Stage 1的方法1:给
<summary id="stage-one">Stage 1</summary>,使用[Link to Stage 1](#stage-one)语法时,VS Code提示No definition for the id stage-one;改用<a href="#stage-one">**Stage 1**</a>锚标签也遇到同样的编辑器提示问题,代码示例如下:<details> <summary id="stage-one">Stage 1</summary> * Vegetables * Fruits * Fish </details> <details> <summary id="stage-two">Stage 2</summary> * Vegetables * Fruits * Fish </details> <details> <summary id="stage-three">Stage 3</summary> * Vegetables * Fruits * Fish * [Link to Stage 1](#stage-one) </details>
解决方案
方法1:给标签添加id(推荐)
将id直接绑定到外层的
标签上,而非仅绑定到summary标签,这样GitHub渲染时能稳定识别锚点,同时编辑器的提示问题也会解决:
<details id="stage-one"> <summary>Stage 1</summary> * Vegetables * Fruits * Fish </details> <details id="stage-two"> <summary>Stage 2</summary> * Vegetables * Fruits * Fish </details> <details id="stage-three"> <summary>Stage 3</summary> * Vegetables * Fruits * Fish * [Link to Stage 1](#stage-one) </details>
方法2:添加隐藏的独立锚点元素
如果需要保留summary的id,可以在对应
区块的最顶部添加一个隐藏的锚点元素,确保锚点能被全局识别:
<details> <span id="stage-one" style="display: none;"></span> <summary>Stage 1</summary> * Vegetables * Fruits * Fish </details> <details> <summary id="stage-two">Stage 2</summary> * Vegetables * Fruits * Fish </details> <details> <summary id="stage-three">Stage 3</summary> * Vegetables * Fruits * Fish * [Link to Stage 1](#stage-one) </details>
说明
VS Code的提示属于编辑器对HTML元素嵌套场景下id识别的局限,但上述两种方法在GitHub上渲染后,点击链接都能准确定位到目标
区块,完全满足跨区块锚点链接的需求。
内容的提问来源于stack exchange,提问作者plant eater
相关产品推荐
相关产品推荐

